diff --git a/docs/src/tutorials/renewal-negbin.md b/docs/src/tutorials/renewal-negbin.md index d94c9dd3..a413f6e1 100644 --- a/docs/src/tutorials/renewal-negbin.md +++ b/docs/src/tutorials/renewal-negbin.md @@ -186,33 +186,20 @@ const CI_QS = [0.025, 0.25, 0.5, 0.75, 0.975] # time × 5 credible bands from a time × draws matrix function credible_bands(mat; qs = CI_QS) - reduce(hcat, (map(eachrow(mat)) do row - vals = collect(skipmissing(row)) - isempty(vals) ? missing : quantile(vals, q) - end for q in qs)) + reduce(vcat, (permutedims(quantile(row, qs)) for row in eachrow(mat))) end # median line with 50% and 95% ribbons function ci_ribbon!(ax, ts, bands; color, label) - keep = findall(!ismissing, view(bands, :, 3)) - x, b = ts[keep], Float64.(bands[keep, :]) - band!(ax, x, b[:, 1], b[:, 5]; color = (color, 0.15)) - band!(ax, x, b[:, 2], b[:, 4]; color = (color, 0.3)) - lines!(ax, x, b[:, 3]; color = color, linewidth = 2, label = label) + band!(ax, ts, bands[:, 1], bands[:, 5]; color = (color, 0.15)) + band!(ax, ts, bands[:, 2], bands[:, 4]; color = (color, 0.3)) + lines!(ax, ts, bands[:, 3]; color = color, linewidth = 2, label = label) end -# posterior-predictive y_t bands from a `predict` chain; any leading -# indices a reporting delay leaves unscored are filled with `missing` +# posterior-predictive y_t bands from a `predict` chain function predictive_bands(pred, n) - ndraws = length(vec(pred[@varname(y_t[n])])) - rows = map(1:n) do i - try - permutedims(vec(pred[@varname(y_t[i])])) - catch - fill(missing, 1, ndraws) - end - end - credible_bands(reduce(vcat, rows)) + credible_bands( + reduce(vcat, (permutedims(vec(pred[@varname(y_t[i])])) for i in 1:n))) end ``` @@ -262,16 +249,14 @@ FC_CI_QS = [0.05, 0.2, 0.35, 0.5, 0.65, 0.8, 0.95] # Draw three nested CI ribbons with the median line function multi_ci_ribbon!(ax, ts, bands; color, label) - keep = findall(!ismissing, view(bands, :, 4)) - x, b = ts[keep], Float64.(bands[keep, :]) # 90% CI (cols 1, 7) - band!(ax, x, b[:, 1], b[:, 7]; color = (color, 0.1)) + band!(ax, ts, bands[:, 1], bands[:, 7]; color = (color, 0.1)) # 60% CI (cols 2, 6) - band!(ax, x, b[:, 2], b[:, 6]; color = (color, 0.25)) + band!(ax, ts, bands[:, 2], bands[:, 6]; color = (color, 0.25)) # 30% CI (cols 3, 5) - band!(ax, x, b[:, 3], b[:, 5]; color = (color, 0.5)) + band!(ax, ts, bands[:, 3], bands[:, 5]; color = (color, 0.5)) # median (col 4) - lines!(ax, x, b[:, 4]; color = color, linewidth = 2, label = label) + lines!(ax, ts, bands[:, 4]; color = color, linewidth = 2, label = label) end # Multi-level credible bands for the forecast diff --git a/docs/src/tutorials/sir-ode.md b/docs/src/tutorials/sir-ode.md index 5890109a..119af9d1 100644 --- a/docs/src/tutorials/sir-ode.md +++ b/docs/src/tutorials/sir-ode.md @@ -98,7 +98,7 @@ model = IDModel(sir_process, observation) Fitting recovers the SIR parameters from the observed "in bed" counts. This page differentiates with **ForwardDiff**, not the package's recommended [Mooncake](https://chalk-lab.github.io/Mooncake.jl/) default. -Reverse-mode NUTS through the ODE solver is not available yet, a pre-existing Turing + Mooncake + `SciMLSensitivity` integration gap that affects every ODE infection model (tracked in [issue #46](https://github.com/EpiAware/ComposableTuringIDModels.jl/issues/46)). +Reverse-mode NUTS through the ODE solver is not available yet for the ODE infection models. Forward-mode autodiff suits a system this small anyway. We draw two chains in parallel with `MCMCThreads()` so a cross-chain ``\hat R`` is available. diff --git a/docs/src/tutorials/time-varying-damping.md b/docs/src/tutorials/time-varying-damping.md index 550b5a4b..d127c368 100644 --- a/docs/src/tutorials/time-varying-damping.md +++ b/docs/src/tutorials/time-varying-damping.md @@ -120,7 +120,7 @@ fig The band covers the true trajectory across the series, so the time-varying damping is recovered from data. Only the order-1 case is built so far. -Time-varying coefficients for higher-order AR(`p`) are tracked in [#113](https://github.com/EpiAware/ComposableTuringIDModels.jl/issues/113). +Time-varying coefficients for a higher-order AR(`p`) are not built yet. Nothing about this is specific to `AR`. [`arma`](@ref) takes the same `damp` slot, and [`DiffLatentModel`](@ref) differences whatever it wraps, so a time-varying-damping ARIMA is the two of them composed. diff --git a/src/ComposableTuringIDModels.jl b/src/ComposableTuringIDModels.jl index 2405f7c5..386fd64b 100644 --- a/src/ComposableTuringIDModels.jl +++ b/src/ComposableTuringIDModels.jl @@ -143,7 +143,7 @@ public ModelShape, across_shape, infection_strata, AbstractAccumulationStep, AbstractConstantRenewalStep, ConstantRenewalStep, AbstractRenewalModifier, modifier_init_state, apply_modifier, renewal_foi, renewal_init_state, renewal_init_window, - MissingObservations, + MissingObservations, AbstractBroadcastRule, at, path_prior, prior_order, assert_prior_length, wrapped_models, observation_components, rewrap, swap diff --git a/src/base/base.jl b/src/base/base.jl index 98d0c624..af58025c 100644 --- a/src/base/base.jl +++ b/src/base/base.jl @@ -89,7 +89,7 @@ a presence mask, so it carries no `Missing` in its type. `value[i]` is the observed entry at `i` when `present[i]` is `true`, and an unused placeholder otherwise. Defined here (rather than in `compose.jl`, where -[`concrete_observations`](@ref) builds one) so that it loads before the +`concrete_observations` builds one) so that it loads before the observation-error models that score one directly, further down the include order. diff --git a/src/observation_models/ObservationErrorModels/methods.jl b/src/observation_models/ObservationErrorModels/methods.jl index be67caec..0cad2471 100644 --- a/src/observation_models/ObservationErrorModels/methods.jl +++ b/src/observation_models/ObservationErrorModels/methods.jl @@ -166,7 +166,7 @@ _restore_missing(y::MissingObservations) = map((v, p) -> p ? v : missing, y.valu # A series reached through a `NamedTuple` field is not a model argument, so # DynamicPPL neither copies nor promotes it and the `y_t[i] ~ …` sugar would # write a blank's draw straight back into the caller's array. -# Narrowing it with [`concrete_observations`](@ref) turns any blank into a +# Narrowing it with `concrete_observations` turns any blank into a # [`MissingObservations`](@ref) carrier that is scored by reading only. # A series passed as the model's own `y_t` argument already has DynamicPPL's # copy, so it passes through untouched. diff --git a/src/steps/ImportedCases.jl b/src/steps/ImportedCases.jl index 55deff90..b0ad5ffe 100644 --- a/src/steps/ImportedCases.jl +++ b/src/steps/ImportedCases.jl @@ -39,7 +39,7 @@ time-varying ``\iota_t`` stays positive even as the underlying path crosses zero. The rate is drawn before the scan through the modifier seam (see -[`AbstractRenewalModifier`](@ref)), giving an [`ImportedRate`](@ref) that adds +[`AbstractRenewalModifier`](@ref)), giving an `ImportedRate` that adds ``\iota_t`` at step ``t``. Where it sits in the modifier tuple therefore decides how it composes: placed *after* a [`SusceptibleDepletion`](@ref) the imports are added to the depleted incidence, so they are not scaled by the susceptible @@ -163,7 +163,7 @@ Sample the importation rate ahead of the scan. Draws the unconstrained rate slot through [`as_turing_submodel`](@ref) — a bare `Distribution` giving one constant rate, a process giving a length-`n` path — maps it onto the positive scale with the modifier's `transformation`, and -returns the [`ImportedRate`](@ref) the scan uses. The map is broadcast, so a +returns the `ImportedRate` the scan uses. The map is broadcast, so a constant stays a scalar (no length-`n` allocation) and the scan reads either shape with [`at`](@ref). diff --git a/src/steps/RenewalStep.jl b/src/steps/RenewalStep.jl index 36504773..c3babc53 100644 --- a/src/steps/RenewalStep.jl +++ b/src/steps/RenewalStep.jl @@ -31,7 +31,7 @@ interface: extra. A prior-carrying modifier implements this method, draws its slots through [`as_turing_submodel`](@ref), and returns a *resolved* scan modifier holding the drawn values — e.g. [`ImportedCases`](@ref) resolves - to an [`ImportedRate`](@ref) and implements no scan interface of its own. + to an `ImportedRate` and implements no scan interface of its own. [`RenewalStep`](@ref) resolves its whole modifier tuple through this one seam (see its `as_turing_model` method), so a sampling modifier needs no special diff --git a/test/Project.toml b/test/Project.toml index 0b18d22b..7885f71d 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -14,6 +14,7 @@ DynamicPPL = "366bfd00-2699-11ea-058f-f148b4cae6d8" EpiAwarePackageTools = "7aaea248-0d11-4a0d-a7dc-86da30abb951" ExplicitImports = "7d51a73a-1435-4ff3-83d9-f097790105c7" ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" +InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240" KernelFunctions = "ec8451be-7e33-11e9-00cf-bbf324bd1392" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" LogDensityProblems = "6fdf6af0-433a-55f7-b3ed-c6c6e0b8df7c"