|
| 1 | +""" |
| 2 | +Unit tests for the validity of the SMC algorithms included in this package. |
| 3 | +
|
| 4 | +We test each SMC algorithm on a one-dimensional linear Gaussian state space model for which |
| 5 | +an analytic filtering distribution can be computed using the Kalman filter provided by the |
| 6 | +`Kalman.jl` package. |
| 7 | +
|
| 8 | +The validity of the algorithm is tested by comparing the final estimated filtering |
| 9 | +distribution ground truth using a one-sided Kolmogorov-Smirnov test. |
| 10 | +""" |
| 11 | + |
| 12 | +using DynamicIterators |
| 13 | +using GaussianDistributions |
| 14 | +using HypothesisTests |
| 15 | +using Kalman |
| 16 | + |
| 17 | +function test_algorithm(rng, algorithm, model, N_SAMPLES, Xf) |
| 18 | + chains = sample(rng, model, algorithm, N_SAMPLES; progress=false) |
| 19 | + particles = hcat([chain.trajectory.model.X for chain in chains]...) |
| 20 | + final_particles = particles[:, end] |
| 21 | + |
| 22 | + test = ExactOneSampleKSTest(final_particles, Normal(Xf.x[end].μ, sqrt(Xf.x[end].Σ))) |
| 23 | + return pvalue(test) |
| 24 | +end |
| 25 | + |
| 26 | +@testset "linear-gaussian.jl" begin |
| 27 | + T = 3 |
| 28 | + N_PARTICLES = 100 |
| 29 | + N_SAMPLES = 50 |
| 30 | + |
| 31 | + # Model dynamics |
| 32 | + a = 0.5 |
| 33 | + b = 0.2 |
| 34 | + q = 0.1 |
| 35 | + E = LinearEvolution(a, Gaussian(b, q)) |
| 36 | + |
| 37 | + H = 1.0 |
| 38 | + R = 0.1 |
| 39 | + Obs = LinearObservationModel(H, R) |
| 40 | + |
| 41 | + x0 = 0.0 |
| 42 | + P0 = 1.0 |
| 43 | + G0 = Gaussian(x0, P0) |
| 44 | + |
| 45 | + M = LinearStateSpaceModel(E, Obs) |
| 46 | + O = LinearObservation(E, H, R) |
| 47 | + |
| 48 | + # Simulate from model |
| 49 | + rng = StableRNG(1234) |
| 50 | + initial = rand(rng, StateObs(G0, M.obs)) |
| 51 | + trajectory = trace(DynamicIterators.Sampled(M, rng), 1 => initial, endtime(T)) |
| 52 | + y_pairs = collect(t => y for (t, (x, y)) in pairs(trajectory)) |
| 53 | + ys = [y for (t, (x, y)) in pairs(trajectory)] |
| 54 | + |
| 55 | + # Ground truth smoothing |
| 56 | + Xf, ll = kalmanfilter(M, 1 => G0, y_pairs) |
| 57 | + |
| 58 | + # Define AdvancedPS model |
| 59 | + mutable struct LinearGaussianParams |
| 60 | + a::Float64 |
| 61 | + b::Float64 |
| 62 | + q::Float64 |
| 63 | + h::Float64 |
| 64 | + r::Float64 |
| 65 | + x0::Float64 |
| 66 | + p0::Float64 |
| 67 | + end |
| 68 | + |
| 69 | + mutable struct LinearGaussianModel <: SSMProblems.AbstractStateSpaceModel |
| 70 | + X::Vector{Float64} |
| 71 | + observations::Vector{Float64} |
| 72 | + θ::LinearGaussianParams |
| 73 | + function LinearGaussianModel(y::Vector{Float64}, θ::LinearGaussianParams) |
| 74 | + return new(Vector{Float64}(), y, θ) |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + function SSMProblems.transition!!(rng::AbstractRNG, model::LinearGaussianModel) |
| 79 | + return rand(rng, Normal(model.θ.x0, model.θ.p0)) |
| 80 | + end |
| 81 | + function SSMProblems.transition!!( |
| 82 | + rng::AbstractRNG, model::LinearGaussianModel, state, step |
| 83 | + ) |
| 84 | + return rand(rng, Normal(model.θ.a * state + model.θ.b, model.θ.q)) |
| 85 | + end |
| 86 | + function SSMProblems.transition_logdensity( |
| 87 | + model::LinearGaussianModel, prev_state, current_state, step |
| 88 | + ) |
| 89 | + return logpdf(Normal(model.θ.a * prev_state + model.θ.b, model.θ.q), current_state) |
| 90 | + end |
| 91 | + function SSMProblems.emission_logdensity(model::LinearGaussianModel, state, step) |
| 92 | + return logpdf(Normal(model.θ.h * state, model.θ.r), model.observations[step]) |
| 93 | + end |
| 94 | + |
| 95 | + AdvancedPS.isdone(::LinearGaussianModel, step) = step > T |
| 96 | + |
| 97 | + params = LinearGaussianParams(a, b, q, H, R, x0, P0) |
| 98 | + model = LinearGaussianModel(ys, params) |
| 99 | + |
| 100 | + @testset "PGAS" begin |
| 101 | + pgas = AdvancedPS.PGAS(N_PARTICLES) |
| 102 | + p = test_algorithm(rng, pgas, model, N_SAMPLES, Xf) |
| 103 | + @test p > 0.05 |
| 104 | + end |
| 105 | + |
| 106 | + @testset "PG" begin |
| 107 | + pg = AdvancedPS.PG(N_PARTICLES) |
| 108 | + p = test_algorithm(rng, pg, model, N_SAMPLES, Xf) |
| 109 | + @test p > 0.05 |
| 110 | + end |
| 111 | +end |
0 commit comments