-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathffbs.jl
95 lines (84 loc) · 2.62 KB
/
ffbs.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
export FFBS
abstract type AbstractSmoother <: AbstractSampler end
struct FFBS{T<:AbstractParticleFilter}
filter::T
end
"""
smooth(rng::AbstractRNG, alg::AbstractSmooterh, model::AbstractStateSpaceModel, obs::AbstractVector, M::Integer; callback, kwargs...)
"""
function smooth end
struct WeightedParticleRecorderCallback{T,WT}
particles::Array{T}
log_weights::Array{WT}
end
function (callback::WeightedParticleRecorderCallback)(
model, filter, step, states, data; kwargs...
)
filtered_states = states.filtered
callback.particles[step, :] = filtered_states.particles
callback.log_weights[step, :] = filtered_states.log_weights
return nothing
end
function gen_trajectory(
rng::Random.AbstractRNG,
model::StateSpaceModel,
particles::AbstractMatrix{T}, # Need better container
log_weights::AbstractMatrix{WT},
forward_state,
n_timestep::Int;
kwargs...,
) where {T,WT}
trajectory = Vector{T}(undef, n_timestep)
trajectory[end] = forward_state
for step in (n_timestep - 1):-1:1
backward_weights = backward(
model,
step,
trajectory[step + 1],
particles[step, :],
log_weights[step, :];
kwargs...,
)
ancestor = rand(rng, Categorical(softmax(backward_weights)))
trajectory[step] = particles[step, ancestor]
end
return trajectory
end
function backward(
model::StateSpaceModel, step::Integer, state, particles::T, log_weights::WT; kwargs...
) where {T,WT}
transitions = map(particles) do prev_state
SSMProblems.logdensity(model.dyn, step, prev_state, state; kwargs...)
end
return log_weights + transitions
end
function sample(
rng::Random.AbstractRNG,
model::StateSpaceModel{T,LDT},
alg::FFBS{<:BootstrapFilter{N}},
obs::AbstractVector,
M::Integer;
callback=nothing,
kwargs...,
) where {T,LDT,N}
n_timestep = length(obs)
recorder = WeightedParticleRecorderCallback(
Array{eltype(model.dyn)}(undef, n_timestep, N), Array{T}(undef, n_timestep, N)
)
particles, _ = filter(rng, model, alg.filter, obs; callback=recorder, kwargs...)
# Backward sampling - exact
idx_ref = rand(rng, Categorical(weights(particles.filtered)), M)
trajectories = Array{eltype(model.dyn)}(undef, n_timestep, M)
trajectories[end, :] = particles.filtered[idx_ref]
for j in 1:M
trajectories[:, j] = gen_trajectory(
rng,
model,
recorder.particles,
recorder.log_weights,
trajectories[end, j],
n_timestep,
)
end
return trajectories
end