-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGeneralisedFilters.jl
143 lines (111 loc) · 3.69 KB
/
GeneralisedFilters.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
module GeneralisedFilters
using AbstractMCMC: AbstractMCMC, AbstractSampler
import Distributions: MvNormal
import Random: AbstractRNG, default_rng, rand
using GaussianDistributions: pairs, Gaussian
using OffsetArrays
using SSMProblems
using StatsBase
# TODO: heavy modules—move to extension
using CUDA
using NNlib
# Filtering utilities
include("callbacks.jl")
include("containers.jl")
include("resamplers.jl")
## FILTERING BASE ##########################################################################
abstract type AbstractFilter <: AbstractSampler end
abstract type AbstractBatchFilter <: AbstractFilter end
"""
initialise([rng,] model, alg; kwargs...)
Propose an initial state distribution.
"""
function initialise end
"""
step([rng,] model, alg, iter, state, observation; kwargs...)
Perform a combined predict and update call of the filtering on the state.
"""
function step end
"""
predict([rng,] model, alg, iter, filtered; kwargs...)
Propagate the filtered distribution forward in time.
"""
function predict end
"""
update(model, alg, iter, proposed, observation; kwargs...)
Update beliefs on the propagated distribution given an observation.
"""
function update end
function initialise(model, alg; kwargs...)
return initialise(default_rng(), model, alg; kwargs...)
end
function predict(model, alg, step, filtered, observation; kwargs...)
return predict(default_rng(), model, alg, step, filtered; kwargs...)
end
function filter(
rng::AbstractRNG,
model::AbstractStateSpaceModel,
alg::AbstractFilter,
observations::AbstractVector;
callback::Union{AbstractCallback,Nothing}=nothing,
kwargs...,
)
state = initialise(rng, model, alg; kwargs...)
isnothing(callback) || callback(model, alg, state, observations, PostInit; kwargs...)
log_evidence = initialise_log_evidence(alg, model)
for t in eachindex(observations)
state, ll_increment = step(
rng, model, alg, t, state, observations[t]; callback, kwargs...
)
log_evidence += ll_increment
end
return state, log_evidence
end
function initialise_log_evidence(::AbstractFilter, model::AbstractStateSpaceModel)
return zero(SSMProblems.arithmetic_type(model))
end
function initialise_log_evidence(alg::AbstractBatchFilter, model::AbstractStateSpaceModel)
return CUDA.zeros(SSMProblems.arithmetic_type(model), alg.batch_size)
end
function filter(
model::AbstractStateSpaceModel,
alg::AbstractFilter,
observations::AbstractVector;
kwargs...,
)
return filter(default_rng(), model, alg, observations; kwargs...)
end
function step(
rng::AbstractRNG,
model::AbstractStateSpaceModel,
alg::AbstractFilter,
iter::Integer,
state,
observation;
callback::Union{AbstractCallback,Nothing}=nothing,
kwargs...,
)
state = predict(rng, model, alg, iter, state, observation; kwargs...)
isnothing(callback) ||
callback(model, alg, iter, state, observation, PostPredict; kwargs...)
state, ll_increment = update(model, alg, iter, state, observation; kwargs...)
isnothing(callback) ||
callback(model, alg, iter, state, observation, PostUpdate; kwargs...)
return state, ll_increment
end
## SMOOTHING BASE ##########################################################################
abstract type AbstractSmoother <: AbstractSampler end
# function smooth end
# function backward end
# Model types
include("models/linear_gaussian.jl")
include("models/discrete.jl")
include("models/hierarchical.jl")
# Filtering/smoothing algorithms
include("algorithms/particles.jl")
include("algorithms/kalman.jl")
include("algorithms/forward.jl")
include("algorithms/rbpf.jl")
# Unit-testing helper module
include("GFTest/GFTest.jl")
end