Skip to content

Continuous-time latent states ​

The data are 40 synthetic measurements at irregular times over a period of several weeks. A latent Ornstein–Uhlenbeck process fluctuates around a stationary mean; independent Gaussian measurement error separates the observations from that process. The simulation uses seed 144 and retains the generating states for comparison. Time is measured in days, and the response is in arbitrary units. The fit sees the observation times and measurements; the generating states are used only to assess and display the result.

Continuous-time model ​

The process returns towards its mean at a positive rate. Its stochastic differential equation can be written in terms of the stationary standard deviation, rather than the diffusion coefficient:

dXt=λ(μ−Xt)dt+2λsdWt,Yj∼Normal(Xtj,σ).

The exact transition over a gap of length dt has persistence exp(-rate * dt) and innovation standard deviation stationary_sd * sqrt(1 - exp(-2rate * dt)). Small gaps retain most of the previous state; long gaps approach the stationary distribution. The first state is drawn from that stationary distribution. This is a substantive initial-condition assumption, not an estimate of a pre-observation path.

The same transition is used to simulate the data and to construct the fitted path. No numerical integration grid is introduced, and there is no Euler discretization error for this model. expm1 evaluates the innovation variance accurately when time gaps are small.

julia
StanBlocks.@deffun @juliacompat begin
    ou_path(dt::vector[n], z::vector[n], mu::real, rate::real, stationary_sd::real)::vector[n] = begin
        x::vector[n]
        x[1] = mu + stationary_sd * z[1]
        for j in 2:n
            persistence = exp(-rate * dt[j])
            innovation_sd = stationary_sd * sqrt(-expm1(-2.0 * rate * dt[j]))
            x[j] = mu + persistence * (x[j - 1] - mu) + innovation_sd * z[j]
        end
        x
    end
end

Sampled states ​

The declaration samples independent standard-normal innovations z and constructs the latent state as a deterministic function of those innovations and the population parameters. This is a non-centred representation of the Gaussian path. BRM exposes state as a named derived output, so posterior summaries use its public descriptor address without identifying generated Stan coordinates by hand.

brm-comparison
Irregularly observed Ornstein–Uhlenbeck process
panes: brm|stanblocks|rkppl|stan|jax:2
julia
function state_space_model(data=state_space_data())
    @brm data begin
        mu ~ Normal(0.0, 1.0)
        rate ~ LogNormal(log(0.8), 0.5)
        stationary_sd ~ LogNormal(log(0.6), 0.5)
        sigma ~ LogNormal(log(0.15), 0.4)
        z[1:n] .~ Normal.(0.0, 1.0)
        state = ou_path(dt, z, mu, rate, stationary_sd)
        y ~ Normal(state, sigma)
    end
end
julia
SBBRMI with data keys = [:dt, :n, :y, :z_n]
emitted @slic body:
begin
    mu ~ normal(0.0, 1.0)
    rate ~ lognormal(-0.2231435513142097, 0.5)
    stationary_sd ~ lognormal(-0.5108256237659907, 0.5)
    sigma ~ lognormal(-1.8971199848858813, 0.4)
    z::vector[z_n] ~ normal(0.0, 1.0)
    state = (Main.state_space.ou_path)(dt, z, mu, rate, stationary_sd)
    y ~ normal(state, sigma)
end
julia
begin
    mu ~ Normal(0.0, 1.0)
    rate ~ LogNormal(-0.2231435513142097, 0.5)
    stationary_sd ~ LogNormal(-0.5108256237659907, 0.5)
    sigma ~ LogNormal(-1.8971199848858813, 0.4)
    z_prior_mean = 0.0
    z_prior_scale = 1.0
    z[1:z_length] .~ Normal.(z_prior_mean, z_prior_scale)
    state = ou_path(dt, z, mu, rate, stationary_sd)
    y .~ Normal.(state, sigma)
end
stan
functions {
vector ou_path(
    vector dt,
    vector z,
    real mu,
    real rate,
    real stationary_sd
) {
    int n = dims(dt)[1];
    if (dims(z)[1] != n) reject("ou_path: dim mismatch — `z` dim 1 (= ", dims(z)[1], ") does not match `n` (= ", n, "), inferred from `dt` dim 1. `n` sizes: `dt` dim 1 (= ", dims(dt)[1], "), `z` dim 1 (= ", dims(z)[1], ").");
    vector[n] x;
    x[1] = (mu + (stationary_sd * z[1]));
    for(j in 2:n) {
        real persistence = exp(((-rate) * dt[j]));
        real innovation_sd = (stationary_sd * sqrt((-expm1((-2.0 * rate * dt[j])))));
        x[j] = (mu + (persistence * (x[(j - 1)] - mu)) + (innovation_sd * z[j]));
    }
    return x;
}
vector normal_lpdfs(
    vector obs,
    vector loc,
    real scale
) {
    return jbroadcasted_normal_lpdfs(obs, loc, scale);
}
vector jbroadcasted_normal_lpdfs(
    vector x1,
    vector x2,
    real x3
) {
    int n = dims(x1)[1];
    vector[n] rv;
    for(i in 1:n) {
        rv[i] = normal_lpdfs(broadcasted_getindex(x1, i), broadcasted_getindex(x2, i), x3);
    }
    return rv;
}
real normal_lpdfs(
    real args1,
    real args2,
    real args3
) {
    return normal_lpdf(args1 | args2, args3);
}
real broadcasted_getindex(vector x, int i) {
    return x[i];
}
vector normal_vector_rng(
    int anontok__1,
    vector a,
    real b
) {
    int n = anontok__1;
    if((n == 0)) {
        vector[n] rv;
        return rv;
    } else {
        return to_vector(normal_rng(a, b));
    }
}
}
data {
    int z_n;
    int dt_n;
    vector[dt_n] dt;
    int y_n;
    vector[y_n] y;
}
transformed data {
}
parameters {
    real mu;
    real<lower=0.0> rate;
    real<lower=0.0> stationary_sd;
    real<lower=0.0> sigma;
    vector[z_n] z;
}
transformed parameters {
    vector[z_n] state = ou_path(dt, z, mu, rate, stationary_sd);
}
model {
    mu ~ normal(0.0, 1.0);
    rate ~ lognormal(-0.2231435513142097, 0.5);
    stationary_sd ~ lognormal(-0.5108256237659907, 0.5);
    sigma ~ lognormal(-1.8971199848858813, 0.4);
    z ~ normal(0.0, 1.0);
    y ~ normal(state, sigma);
}
generated quantities {
    vector[y_n] y_likelihood = normal_lpdfs(y, state, sigma);
    vector[y_n] y_gen = normal_vector_rng(y_n, state, sigma);
}
julia
using ReactiveKernels, ReactiveKernelsPPL
rk = RKBRMI(model)
source = jax_source(rk.model, rk_bound_plan(rk))
write("model.py", source)
inputs = rk_bound_data(rk, source)   # exactly the inputs prepare_data reads
text
This model has no JAX module yet.

JAXEmissionError: the function `ou_path` has loops or branches, so its lowered code is not translated in recipe `state = (dt, z, mu, rate, stationary_sd) -> Main.var"##RKEmittedModels#21296".ou_path(dt, z, mu, rate, stationary_sd)`. Define it with `@traceable` (its authored body is translated), author it as a kernel, or give it a `ReactiveKernels._jax_function_call` method; for a method a package generates, the package supplies its source with `ReactiveKernels.register_source_provider!`

The mean has a normal prior. The reversion rate, stationary process SD and measurement SD have lognormal priors on positive values. The prior on the rate is centred around a response to changes over a few days; the prior on measurement noise reflects the intended scale of the synthetic example. These values are visible in the declaration and are part of this model, not defaults supplied by BRM.

Stationary process variation and measurement error affect the data differently. Process variation is correlated across nearby observations, whereas measurement errors are independent. Irregular gaps help express that difference, although a short series may still leave the two scales weakly separated. The fitted state between observations is not part of this output: only states at the supplied times are sampled. Adding a plotting grid requires conditional path recovery or additional latent times, rather than interpreting straight line segments as posterior draws of a continuous path.

Fit and diagnostics ​

Four independent WarmupHMC chains start from different random seeds, each requesting 1,000 retained draws. The completion sampler requires three chains to finish and then lets the remaining chain finish its current warmup round. The table records how many chains contributed, which were omitted, the total retained draws, divergences and sampling time. These are results of the cached run; rebuilding this page reads its tables without sampling again.

startedretainedomitteddrawsdivergences%seconds
434300000.042.7

Population summaries use the retained chains on the quantities' natural scales. The intervals contain the central 95 % of their pooled draws. Split R-hat compares chains; bulk and tail effective sample sizes describe the information in the centre and tails. A small divergence count alone is insufficient evidence that the chains agree. The completion rule can also select chains by speed, so the omitted-chain record belongs with the result.

quantityunitmedian2.5 %97.5 %R-hatbulk ESStail ESS
meanresponse0.4088-0.065710.88931.008385527
rate1/day0.54740.27071.0541.004586968
stationary SDresponse0.69670.53260.96191.005556930
observation SDresponse0.13730.066610.2691.011218375

Latent states and observations ​

The retained run has 3,000 draws across three chains and no divergences. R-hat is 1.004–1.011 for the reported population quantities; the smallest bulk ESS is 218 for the observation SD. All four generating values fall inside their reported 95 % intervals. The uncertainty in the measurement scale remains visible: its interval is 0.067–0.269 response units around a generating value of 0.15.

brm-figure
{"spec":"{\"$schema\":\"https://vega.github.io/schema/vega-lite/v5.json\",\"_aovSummary\":{\"bands\":[[\"q025\",\"q975\",\"q025/q975\"],[\"q25\",\"q75\",\"q25/q75\"]],\"point_col\":\"q50\",\"value_label\":\"q50\"},\"config\":{\"axis\":{\"labelLimit\":0},\"header\":{\"labelLimit\":0},\"legend\":{\"labelLimit\":0}},\"height\":300,\"layer\":[{\"_lr_layer\":true,\"data\":{\"values\":[{\"q025\":1.388,\"q25\":1.682,\"q50\":1.781,\"q75\":1.866,\"q975\":2.025,\"time\":0.0},{\"q025\":0.8426,\"q25\":1.008,\"q50\":1.089,\"q75\":1.17,\"q975\":1.413,\"time\":0.39450343399489923},{\"q025\":0.72,\"q25\":0.9172,\"q50\":0.9961,\"q75\":1.078,\"q975\":1.247,\"time\":0.8516894971322544},{\"q025\":0.1552,\"q25\":0.3293,\"q50\":0.41,\"q75\":0.5024,\"q975\":0.7427,\"time\":1.4887134342930364},{\"q025\":0.6295,\"q25\":0.8613,\"q50\":0.9536,\"q75\":1.041,\"q975\":1.218,\"time\":2.3368336865916963},{\"q025\":0.446,\"q25\":0.6977,\"q50\":0.7973,\"q75\":0.8822,\"q975\":1.055,\"time\":3.364624891599069},{\"q025\":-1.116,\"q25\":-0.9637,\"q50\":-0.8721,\"q75\":-0.7555,\"q975\":-0.4384,\"time\":4.1181010478659195},{\"q025\":-0.4886,\"q25\":-0.2398,\"q50\":-0.1499,\"q75\":-0.06475,\"q975\":0.1161,\"time\":5.273349658108186},{\"q025\":-1.158,\"q25\":-1.002,\"q50\":-0.9234,\"q75\":-0.8236,\"q975\":-0.5507,\"time\":5.960312150337159},{\"q025\":-0.8833,\"q25\":-0.6733,\"q50\":-0.5863,\"q75\":-0.4992,\"q975\":-0.2996,\"time\":6.829470315036054},{\"q025\":-1.025,\"q25\":-0.8359,\"q50\":-0.7559,\"q75\":-0.6678,\"q975\":-0.4373,\"time\":7.4028061301588455},{\"q025\":-0.593,\"q25\":-0.3963,\"q50\":-0.3103,\"q75\":-0.2253,\"q975\":-0.02007,\"time\":8.341920105583718},{\"q025\":-0.4901,\"q25\":-0.2869,\"q50\":-0.2033,\"q75\":-0.1168,\"q975\":0.07871,\"time\":8.953414195713771},{\"q025\":-0.4237,\"q25\":-0.2271,\"q50\":-0.1401,\"q75\":-0.04301,\"q975\":0.1693,\"time\":10.094768643415078},{\"q025\":0.4819,\"q25\":0.7145,\"q50\":0.8008,\"q75\":0.8796,\"q975\":1.066,\"time\":11.228414023594505},{\"q025\":0.7593,\"q25\":0.9569,\"q50\":1.039,\"q75\":1.122,\"q975\":1.339,\"time\":12.126322628734565},{\"q025\":1.141,\"q25\":1.38,\"q50\":1.477,\"q75\":1.561,\"q975\":1.723,\"time\":12.752962789660291},{\"q025\":0.8381,\"q25\":1.059,\"q50\":1.145,\"q75\":1.229,\"q975\":1.428,\"time\":13.12295840889448},{\"q025\":0.1082,\"q25\":0.2908,\"q50\":0.3763,\"q75\":0.4695,\"q975\":0.7161,\"time\":14.092808839404974},{\"q025\":0.1274,\"q25\":0.3304,\"q50\":0.4173,\"q75\":0.5098,\"q975\":0.719,\"time\":15.301254182266259},{\"q025\":0.1922,\"q25\":0.4302,\"q50\":0.5138,\"q75\":0.6004,\"q975\":0.7836,\"time\":16.054291534574237},{\"q025\":-0.1452,\"q25\":0.03018,\"q50\":0.1152,\"q75\":0.2059,\"q975\":0.4203,\"time\":16.896604106621318},{\"q025\":0.07758,\"q25\":0.2838,\"q50\":0.37,\"q75\":0.4538,\"q975\":0.6619,\"time\":17.607045459608926},{\"q025\":0.3916,\"q25\":0.5977,\"q50\":0.6819,\"q75\":0.7647,\"q975\":0.951,\"time\":18.45947585587963},{\"q025\":0.3701,\"q25\":0.587,\"q50\":0.6695,\"q75\":0.75,\"q975\":0.934,\"time\":18.993334864393116},{\"q025\":0.03005,\"q25\":0.2264,\"q50\":0.3145,\"q75\":0.4023,\"q975\":0.6077,\"time\":20.26111644471066},{\"q025\":-0.5493,\"q25\":-0.3399,\"q50\":-0.2554,\"q75\":-0.17,\"q975\":0.03687,\"time\":21.420653897980436},{\"q025\":-0.6418,\"q25\":-0.4609,\"q50\":-0.3783,\"q75\":-0.2953,\"q975\":-0.08095,\"time\":21.797096202118695},{\"q025\":-0.5113,\"q25\":-0.3277,\"q50\":-0.2457,\"q75\":-0.1608,\"q975\":0.06071,\"time\":22.986017417586066},{\"q025\":-0.5388,\"q25\":-0.3616,\"q50\":-0.2812,\"q75\":-0.188,\"q975\":0.05177,\"time\":23.439047461748697},{\"q025\":0.08684,\"q25\":0.317,\"q50\":0.4069,\"q75\":0.4864,\"q975\":0.6557,\"time\":23.842587283673858},{\"q025\":0.0968,\"q25\":0.2914,\"q50\":0.3749,\"q75\":0.4607,\"q975\":0.6466,\"time\":24.26596780466209},{\"q025\":-0.2166,\"q25\":-0.03955,\"q50\":0.04768,\"q75\":0.1321,\"q975\":0.3395,\"time\":25.49830285930549},{\"q025\":-0.2066,\"q25\":-0.01781,\"q50\":0.06755,\"q75\":0.1487,\"q975\":0.3425,\"time\":25.866428922676587},{\"q025\":-0.38,\"q25\":-0.1852,\"q50\":-0.1035,\"q75\":-0.01627,\"q975\":0.2091,\"time\":26.435448164024088},{\"q025\":-0.344,\"q25\":-0.1745,\"q50\":-0.09095,\"q75\":0.001185,\"q975\":0.2648,\"time\":27.7331607720007},{\"q025\":0.968,\"q25\":1.215,\"q50\":1.308,\"q75\":1.389,\"q975\":1.567,\"time\":28.781208692814612},{\"q025\":1.052,\"q25\":1.256,\"q50\":1.34,\"q75\":1.42,\"q975\":1.598,\"time\":29.38213684239173},{\"q025\":1.033,\"q25\":1.221,\"q50\":1.303,\"q75\":1.389,\"q975\":1.575,\"time\":29.990541931670133},{\"q025\":1.132,\"q25\":1.358,\"q50\":1.451,\"q75\":1.532,\"q975\":1.703,\"time\":30.72583150856143}]},\"encoding\":{\"tooltip\":[{\"field\":\"time\",\"title\":\"Time (days)\",\"type\":\"quantitative\"},{\"field\":\"q50\",\"title\":\"median\",\"type\":\"quantitative\"},{\"field\":\"q025\",\"title\":\"q025\",\"type\":\"quantitative\"},{\"field\":\"q975\",\"title\":\"q975\",\"type\":\"quantitative\"}],\"x\":{\"field\":\"time\",\"scale\":{\"zero\":false},\"title\":\"Time (days)\",\"type\":\"quantitative\"},\"y\":{\"field\":\"q025\",\"scale\":{\"zero\":false},\"title\":\"Latent response\",\"type\":\"quantitative\"},\"y2\":{\"field\":\"q975\"}},\"mark\":{\"color\":\"#2a78d6\",\"line\":false,\"opacity\":0.2,\"type\":\"area\"},\"params\":[{\"bind\":\"scales\",\"name\":\"grid\",\"select\":{\"encodings\":[\"x\",\"y\"],\"type\":\"interval\"}}]},{\"_lr_layer\":true,\"data\":{\"values\":[{\"q025\":1.388,\"q25\":1.682,\"q50\":1.781,\"q75\":1.866,\"q975\":2.025,\"time\":0.0},{\"q025\":0.8426,\"q25\":1.008,\"q50\":1.089,\"q75\":1.17,\"q975\":1.413,\"time\":0.39450343399489923},{\"q025\":0.72,\"q25\":0.9172,\"q50\":0.9961,\"q75\":1.078,\"q975\":1.247,\"time\":0.8516894971322544},{\"q025\":0.1552,\"q25\":0.3293,\"q50\":0.41,\"q75\":0.5024,\"q975\":0.7427,\"time\":1.4887134342930364},{\"q025\":0.6295,\"q25\":0.8613,\"q50\":0.9536,\"q75\":1.041,\"q975\":1.218,\"time\":2.3368336865916963},{\"q025\":0.446,\"q25\":0.6977,\"q50\":0.7973,\"q75\":0.8822,\"q975\":1.055,\"time\":3.364624891599069},{\"q025\":-1.116,\"q25\":-0.9637,\"q50\":-0.8721,\"q75\":-0.7555,\"q975\":-0.4384,\"time\":4.1181010478659195},{\"q025\":-0.4886,\"q25\":-0.2398,\"q50\":-0.1499,\"q75\":-0.06475,\"q975\":0.1161,\"time\":5.273349658108186},{\"q025\":-1.158,\"q25\":-1.002,\"q50\":-0.9234,\"q75\":-0.8236,\"q975\":-0.5507,\"time\":5.960312150337159},{\"q025\":-0.8833,\"q25\":-0.6733,\"q50\":-0.5863,\"q75\":-0.4992,\"q975\":-0.2996,\"time\":6.829470315036054},{\"q025\":-1.025,\"q25\":-0.8359,\"q50\":-0.7559,\"q75\":-0.6678,\"q975\":-0.4373,\"time\":7.4028061301588455},{\"q025\":-0.593,\"q25\":-0.3963,\"q50\":-0.3103,\"q75\":-0.2253,\"q975\":-0.02007,\"time\":8.341920105583718},{\"q025\":-0.4901,\"q25\":-0.2869,\"q50\":-0.2033,\"q75\":-0.1168,\"q975\":0.07871,\"time\":8.953414195713771},{\"q025\":-0.4237,\"q25\":-0.2271,\"q50\":-0.1401,\"q75\":-0.04301,\"q975\":0.1693,\"time\":10.094768643415078},{\"q025\":0.4819,\"q25\":0.7145,\"q50\":0.8008,\"q75\":0.8796,\"q975\":1.066,\"time\":11.228414023594505},{\"q025\":0.7593,\"q25\":0.9569,\"q50\":1.039,\"q75\":1.122,\"q975\":1.339,\"time\":12.126322628734565},{\"q025\":1.141,\"q25\":1.38,\"q50\":1.477,\"q75\":1.561,\"q975\":1.723,\"time\":12.752962789660291},{\"q025\":0.8381,\"q25\":1.059,\"q50\":1.145,\"q75\":1.229,\"q975\":1.428,\"time\":13.12295840889448},{\"q025\":0.1082,\"q25\":0.2908,\"q50\":0.3763,\"q75\":0.4695,\"q975\":0.7161,\"time\":14.092808839404974},{\"q025\":0.1274,\"q25\":0.3304,\"q50\":0.4173,\"q75\":0.5098,\"q975\":0.719,\"time\":15.301254182266259},{\"q025\":0.1922,\"q25\":0.4302,\"q50\":0.5138,\"q75\":0.6004,\"q975\":0.7836,\"time\":16.054291534574237},{\"q025\":-0.1452,\"q25\":0.03018,\"q50\":0.1152,\"q75\":0.2059,\"q975\":0.4203,\"time\":16.896604106621318},{\"q025\":0.07758,\"q25\":0.2838,\"q50\":0.37,\"q75\":0.4538,\"q975\":0.6619,\"time\":17.607045459608926},{\"q025\":0.3916,\"q25\":0.5977,\"q50\":0.6819,\"q75\":0.7647,\"q975\":0.951,\"time\":18.45947585587963},{\"q025\":0.3701,\"q25\":0.587,\"q50\":0.6695,\"q75\":0.75,\"q975\":0.934,\"time\":18.993334864393116},{\"q025\":0.03005,\"q25\":0.2264,\"q50\":0.3145,\"q75\":0.4023,\"q975\":0.6077,\"time\":20.26111644471066},{\"q025\":-0.5493,\"q25\":-0.3399,\"q50\":-0.2554,\"q75\":-0.17,\"q975\":0.03687,\"time\":21.420653897980436},{\"q025\":-0.6418,\"q25\":-0.4609,\"q50\":-0.3783,\"q75\":-0.2953,\"q975\":-0.08095,\"time\":21.797096202118695},{\"q025\":-0.5113,\"q25\":-0.3277,\"q50\":-0.2457,\"q75\":-0.1608,\"q975\":0.06071,\"time\":22.986017417586066},{\"q025\":-0.5388,\"q25\":-0.3616,\"q50\":-0.2812,\"q75\":-0.188,\"q975\":0.05177,\"time\":23.439047461748697},{\"q025\":0.08684,\"q25\":0.317,\"q50\":0.4069,\"q75\":0.4864,\"q975\":0.6557,\"time\":23.842587283673858},{\"q025\":0.0968,\"q25\":0.2914,\"q50\":0.3749,\"q75\":0.4607,\"q975\":0.6466,\"time\":24.26596780466209},{\"q025\":-0.2166,\"q25\":-0.03955,\"q50\":0.04768,\"q75\":0.1321,\"q975\":0.3395,\"time\":25.49830285930549},{\"q025\":-0.2066,\"q25\":-0.01781,\"q50\":0.06755,\"q75\":0.1487,\"q975\":0.3425,\"time\":25.866428922676587},{\"q025\":-0.38,\"q25\":-0.1852,\"q50\":-0.1035,\"q75\":-0.01627,\"q975\":0.2091,\"time\":26.435448164024088},{\"q025\":-0.344,\"q25\":-0.1745,\"q50\":-0.09095,\"q75\":0.001185,\"q975\":0.2648,\"time\":27.7331607720007},{\"q025\":0.968,\"q25\":1.215,\"q50\":1.308,\"q75\":1.389,\"q975\":1.567,\"time\":28.781208692814612},{\"q025\":1.052,\"q25\":1.256,\"q50\":1.34,\"q75\":1.42,\"q975\":1.598,\"time\":29.38213684239173},{\"q025\":1.033,\"q25\":1.221,\"q50\":1.303,\"q75\":1.389,\"q975\":1.575,\"time\":29.990541931670133},{\"q025\":1.132,\"q25\":1.358,\"q50\":1.451,\"q75\":1.532,\"q975\":1.703,\"time\":30.72583150856143}]},\"encoding\":{\"tooltip\":[{\"field\":\"time\",\"title\":\"Time (days)\",\"type\":\"quantitative\"},{\"field\":\"q50\",\"title\":\"median\",\"type\":\"quantitative\"},{\"field\":\"q025\",\"title\":\"q025\",\"type\":\"quantitative\"},{\"field\":\"q975\",\"title\":\"q975\",\"type\":\"quantitative\"}],\"x\":{\"field\":\"time\",\"scale\":{\"zero\":false},\"title\":\"Time (days)\",\"type\":\"quantitative\"},\"y\":{\"field\":\"q25\",\"scale\":{\"zero\":false},\"title\":\"Latent response\",\"type\":\"quantitative\"},\"y2\":{\"field\":\"q75\"}},\"mark\":{\"color\":\"#2a78d6\",\"line\":false,\"opacity\":0.6,\"type\":\"area\"}},{\"_lr_layer\":true,\"data\":{\"values\":[{\"q025\":1.388,\"q25\":1.682,\"q50\":1.781,\"q75\":1.866,\"q975\":2.025,\"time\":0.0},{\"q025\":0.8426,\"q25\":1.008,\"q50\":1.089,\"q75\":1.17,\"q975\":1.413,\"time\":0.39450343399489923},{\"q025\":0.72,\"q25\":0.9172,\"q50\":0.9961,\"q75\":1.078,\"q975\":1.247,\"time\":0.8516894971322544},{\"q025\":0.1552,\"q25\":0.3293,\"q50\":0.41,\"q75\":0.5024,\"q975\":0.7427,\"time\":1.4887134342930364},{\"q025\":0.6295,\"q25\":0.8613,\"q50\":0.9536,\"q75\":1.041,\"q975\":1.218,\"time\":2.3368336865916963},{\"q025\":0.446,\"q25\":0.6977,\"q50\":0.7973,\"q75\":0.8822,\"q975\":1.055,\"time\":3.364624891599069},{\"q025\":-1.116,\"q25\":-0.9637,\"q50\":-0.8721,\"q75\":-0.7555,\"q975\":-0.4384,\"time\":4.1181010478659195},{\"q025\":-0.4886,\"q25\":-0.2398,\"q50\":-0.1499,\"q75\":-0.06475,\"q975\":0.1161,\"time\":5.273349658108186},{\"q025\":-1.158,\"q25\":-1.002,\"q50\":-0.9234,\"q75\":-0.8236,\"q975\":-0.5507,\"time\":5.960312150337159},{\"q025\":-0.8833,\"q25\":-0.6733,\"q50\":-0.5863,\"q75\":-0.4992,\"q975\":-0.2996,\"time\":6.829470315036054},{\"q025\":-1.025,\"q25\":-0.8359,\"q50\":-0.7559,\"q75\":-0.6678,\"q975\":-0.4373,\"time\":7.4028061301588455},{\"q025\":-0.593,\"q25\":-0.3963,\"q50\":-0.3103,\"q75\":-0.2253,\"q975\":-0.02007,\"time\":8.341920105583718},{\"q025\":-0.4901,\"q25\":-0.2869,\"q50\":-0.2033,\"q75\":-0.1168,\"q975\":0.07871,\"time\":8.953414195713771},{\"q025\":-0.4237,\"q25\":-0.2271,\"q50\":-0.1401,\"q75\":-0.04301,\"q975\":0.1693,\"time\":10.094768643415078},{\"q025\":0.4819,\"q25\":0.7145,\"q50\":0.8008,\"q75\":0.8796,\"q975\":1.066,\"time\":11.228414023594505},{\"q025\":0.7593,\"q25\":0.9569,\"q50\":1.039,\"q75\":1.122,\"q975\":1.339,\"time\":12.126322628734565},{\"q025\":1.141,\"q25\":1.38,\"q50\":1.477,\"q75\":1.561,\"q975\":1.723,\"time\":12.752962789660291},{\"q025\":0.8381,\"q25\":1.059,\"q50\":1.145,\"q75\":1.229,\"q975\":1.428,\"time\":13.12295840889448},{\"q025\":0.1082,\"q25\":0.2908,\"q50\":0.3763,\"q75\":0.4695,\"q975\":0.7161,\"time\":14.092808839404974},{\"q025\":0.1274,\"q25\":0.3304,\"q50\":0.4173,\"q75\":0.5098,\"q975\":0.719,\"time\":15.301254182266259},{\"q025\":0.1922,\"q25\":0.4302,\"q50\":0.5138,\"q75\":0.6004,\"q975\":0.7836,\"time\":16.054291534574237},{\"q025\":-0.1452,\"q25\":0.03018,\"q50\":0.1152,\"q75\":0.2059,\"q975\":0.4203,\"time\":16.896604106621318},{\"q025\":0.07758,\"q25\":0.2838,\"q50\":0.37,\"q75\":0.4538,\"q975\":0.6619,\"time\":17.607045459608926},{\"q025\":0.3916,\"q25\":0.5977,\"q50\":0.6819,\"q75\":0.7647,\"q975\":0.951,\"time\":18.45947585587963},{\"q025\":0.3701,\"q25\":0.587,\"q50\":0.6695,\"q75\":0.75,\"q975\":0.934,\"time\":18.993334864393116},{\"q025\":0.03005,\"q25\":0.2264,\"q50\":0.3145,\"q75\":0.4023,\"q975\":0.6077,\"time\":20.26111644471066},{\"q025\":-0.5493,\"q25\":-0.3399,\"q50\":-0.2554,\"q75\":-0.17,\"q975\":0.03687,\"time\":21.420653897980436},{\"q025\":-0.6418,\"q25\":-0.4609,\"q50\":-0.3783,\"q75\":-0.2953,\"q975\":-0.08095,\"time\":21.797096202118695},{\"q025\":-0.5113,\"q25\":-0.3277,\"q50\":-0.2457,\"q75\":-0.1608,\"q975\":0.06071,\"time\":22.986017417586066},{\"q025\":-0.5388,\"q25\":-0.3616,\"q50\":-0.2812,\"q75\":-0.188,\"q975\":0.05177,\"time\":23.439047461748697},{\"q025\":0.08684,\"q25\":0.317,\"q50\":0.4069,\"q75\":0.4864,\"q975\":0.6557,\"time\":23.842587283673858},{\"q025\":0.0968,\"q25\":0.2914,\"q50\":0.3749,\"q75\":0.4607,\"q975\":0.6466,\"time\":24.26596780466209},{\"q025\":-0.2166,\"q25\":-0.03955,\"q50\":0.04768,\"q75\":0.1321,\"q975\":0.3395,\"time\":25.49830285930549},{\"q025\":-0.2066,\"q25\":-0.01781,\"q50\":0.06755,\"q75\":0.1487,\"q975\":0.3425,\"time\":25.866428922676587},{\"q025\":-0.38,\"q25\":-0.1852,\"q50\":-0.1035,\"q75\":-0.01627,\"q975\":0.2091,\"time\":26.435448164024088},{\"q025\":-0.344,\"q25\":-0.1745,\"q50\":-0.09095,\"q75\":0.001185,\"q975\":0.2648,\"time\":27.7331607720007},{\"q025\":0.968,\"q25\":1.215,\"q50\":1.308,\"q75\":1.389,\"q975\":1.567,\"time\":28.781208692814612},{\"q025\":1.052,\"q25\":1.256,\"q50\":1.34,\"q75\":1.42,\"q975\":1.598,\"time\":29.38213684239173},{\"q025\":1.033,\"q25\":1.221,\"q50\":1.303,\"q75\":1.389,\"q975\":1.575,\"time\":29.990541931670133},{\"q025\":1.132,\"q25\":1.358,\"q50\":1.451,\"q75\":1.532,\"q975\":1.703,\"time\":30.72583150856143}]},\"encoding\":{\"tooltip\":[{\"field\":\"time\",\"title\":\"Time (days)\",\"type\":\"quantitative\"},{\"field\":\"q50\",\"title\":\"median\",\"type\":\"quantitative\"},{\"field\":\"q025\",\"title\":\"q025\",\"type\":\"quantitative\"},{\"field\":\"q975\",\"title\":\"q975\",\"type\":\"quantitative\"}],\"x\":{\"field\":\"time\",\"scale\":{\"zero\":false},\"title\":\"Time (days)\",\"type\":\"quantitative\"},\"y\":{\"field\":\"q50\",\"scale\":{\"zero\":false},\"title\":\"Latent response\",\"type\":\"quantitative\"}},\"mark\":{\"color\":\"#2a78d6\",\"strokeWidth\":2,\"type\":\"line\"}},{\"data\":{\"values\":[{\"observed\":1.8693120162510166,\"time\":0.0},{\"observed\":1.0404880863474342,\"time\":0.39450343399489923},{\"observed\":1.0226046188288942,\"time\":0.8516894971322544},{\"observed\":0.35644902937981204,\"time\":1.4887134342930364},{\"observed\":0.989540820557099,\"time\":2.3368336865916963},{\"observed\":0.875617875430821,\"time\":3.364624891599069},{\"observed\":-0.9821508756663547,\"time\":4.1181010478659195},{\"observed\":-0.09819359753049312,\"time\":5.273349658108186},{\"observed\":-0.9793177559453107,\"time\":5.960312150337159},{\"observed\":-0.5719569005708678,\"time\":6.829470315036054},{\"observed\":-0.7923459506302116,\"time\":7.4028061301588455},{\"observed\":-0.3072907343773035,\"time\":8.341920105583718},{\"observed\":-0.2045662851735699,\"time\":8.953414195713771},{\"observed\":-0.17278922909421052,\"time\":10.094768643415078},{\"observed\":0.822803783222505,\"time\":11.228414023594505},{\"observed\":1.0287088912734612,\"time\":12.126322628734565},{\"observed\":1.5397004477272258,\"time\":12.752962789660291},{\"observed\":1.1488307882624145,\"time\":13.12295840889448},{\"observed\":0.34554436421221,\"time\":14.092808839404974},{\"observed\":0.41515489206017625,\"time\":15.301254182266259},{\"observed\":0.5331439767103494,\"time\":16.054291534574237},{\"observed\":0.08876233814259143,\"time\":16.896604106621318},{\"observed\":0.36936028696016465,\"time\":17.607045459608926},{\"observed\":0.6983003146938075,\"time\":18.45947585587963},{\"observed\":0.6831680431322209,\"time\":18.993334864393116},{\"observed\":0.32554122778401007,\"time\":20.26111644471066},{\"observed\":-0.2674965968204832,\"time\":21.420653897980436},{\"observed\":-0.404935798257039,\"time\":21.797096202118695},{\"observed\":-0.24726603876301267,\"time\":22.986017417586066},{\"observed\":-0.34365625526641236,\"time\":23.439047461748697},{\"observed\":0.4707523196422142,\"time\":23.842587283673858},{\"observed\":0.38364882118436483,\"time\":24.26596780466209},{\"observed\":0.031153445895560072,\"time\":25.49830285930549},{\"observed\":0.07438206612820261,\"time\":25.866428922676587},{\"observed\":-0.1179439229367979,\"time\":26.435448164024088},{\"observed\":-0.14228059948553604,\"time\":27.7331607720007},{\"observed\":1.3594298635007982,\"time\":28.781208692814612},{\"observed\":1.353461246488444,\"time\":29.38213684239173},{\"observed\":1.3007491552605766,\"time\":29.990541931670133},{\"observed\":1.482552189562244,\"time\":30.72583150856143}]},\"encoding\":{\"tooltip\":[{\"field\":\"time\",\"type\":\"quantitative\"},{\"field\":\"observed\",\"type\":\"quantitative\"}],\"x\":{\"field\":\"time\",\"scale\":{\"zero\":false},\"title\":\"Time (days)\",\"type\":\"quantitative\"},\"y\":{\"field\":\"observed\",\"scale\":{\"zero\":false},\"title\":\"Latent response\",\"type\":\"quantitative\"}},\"mark\":{\"color\":\"#eb6834\",\"filled\":true,\"size\":24.5,\"type\":\"point\"}}],\"width\":640,\"description\":\"Latent states at the observed times: posterior median with 50 % and 95 % credible bands in blue; noisy measurements in orange.\"}","caption":"Latent states at the observed times: posterior median with 50 % and 95 % credible bands in blue; noisy measurements in orange.","alt":"Latent states at the observed times: posterior median with 50 % and 95 % credible bands in blue; noisy measurements in orange."}

The blue intervals describe uncertainty in the latent state after conditioning on the entire series. They are smoothing intervals: later observations can inform an earlier state through the process model. A filtering interval would instead condition only on observations available up to that time. The distinction matters when using a fitted trajectory for retrospective interpretation versus real-time monitoring.

Orange points include measurement noise, so their position outside a state interval is not itself a predictive failure. The script also writes posterior predictive quantiles for a new measurement at each supplied time. Those intervals include both state uncertainty and observation noise and are wider for that reason. The generating path is retained in the trajectory table, allowing a simulation check without treating it as observed data.

Marginalized states ​

For this linear Gaussian process, a Kalman filter can integrate the latent path out of the likelihood exactly. HMC then needs only the population parameters, followed by conditional state recovery. The filters guide explains that likelihood representation and the boundary between exact Gaussian filtering and approximations for nonlinear or non-Gaussian models. The sampled version here keeps the state construction explicit and provides a direct example of posterior state summaries. It is not a claim that sampling every state is preferable for long series.

The earlier multivariate continuous-time examples, including hierarchical stress and mood models and approximate filters, remain in the research directory. They add scientific structure and numerical questions beyond the single exact transition taught here.

Reproduce ​

Commands and fit receipt

The repository's test environment supplies the pinned BRM, StanBlocks, BridgeStan, WarmupHMC and TreeArrays dependencies. These shell commands are manual: the docs build executes the model declarations and figures but reads the completed fit's summaries. Run the fit deliberately when its model, data or settings change; routine documentation edits reuse the cache.

sh
julia --startup-file=no --project=test test/setup_env.jl
julia -t 4 --startup-file=no --project=test research/ema_ctsem/fit.jl

The script writes its receipt before sampling and saves the draws outside the repository. Its key includes the emitted program, data, settings, seed and sampling harness. TreeArrays reduces the named chain and draw axes to small summary tables. A second invocation with the same key rebuilds those tables from the saved draws. The receipt identifies the program behind these numbers.

On a fresh checkout the draws may be absent. An existing receipt guards against repeating a recent fit; set BRM_DOCS_FIT_FORCE=1 for a deliberate rerun. BRM_DOCS_CACHE selects a persistent draw-cache directory; its default is under the system temporary directory. Only the small summary tables belong in git.

fieldvalue
commit21ce3452c70fee5663b0fe4981ae486368814c0f
uncommitted_changesyes
started2026-11-11 02:24 local time
hosta Linux fleet host, 4 Julia threads
julia1.10.12
BayesianRegressionModels0.2.270
samplerWarmupHMC completion_warmup_mcmc over BridgeStan; model already uses explicit innovations
chains4 per fit, quorum 3, 1000 draws each
modelstate-space
sourcesresearch/ema_ctsem/docs_model.jl; seed 144; 40 irregular times
seed20261114
content_sha256d3380971d9131855fd9ff71a66a0f281521cb59bd3cfb7d125c309ec777ee9e9
harness_sha256d3052cea6c5a6ef32afd4bd2d206779996c59789b9e94c95cd6e0fa03e835a15
settings(1000, 4, nothing, Base.Pairs{Symbol, Real, Tuple{Symbol, Symbol}, @NamedTuple{adaptive_centering::Bool, target_acceptance_rate::Float64}}(:adaptive_centering => false, :target_acceptance_rate => 0.99))
WarmupHMC0.2.11
StanBlocks0.2.31
TreeArrays0.2.0
BridgeStan2.9.0
seconds42.69363748
divergent0
omitted_chains4
dimension44
statuscomplete
completed2026-11-11T02:25:13
summaryTreeArrays quantiles pooled over chain and draw; split R-hat, bulk and tail ESS

Research notes and source. :::