Repository navigation
Expand file tree
/
Copy pathimm.cpp
More file actions
160 lines (138 loc) · 8.56 KB
/
Copy pathimm.cpp
File metadata and controls
160 lines (138 loc) · 8.56 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// A target in the plane that switches between cruising and maneuvering, tracked from noisy position
// fixes by an interacting multiple model (IMM) filter. The filter runs one Kalman filter per mode,
// mixes them through a Markov chain at each prediction, and weighs the modes by how well each one
// predicted the fix. The program simulates the target's modes and motion from the same switching
// model the filter assumes, prints how well the mode probabilities follow the true mode, and checks
// the combined estimate against its covariance and against a Kalman filter with a single model. It
// returns nonzero if either check fails.
#include <EmrysFilter/EmrysFilter.h>
#include <cmath>
#include <print>
#include <random>
// The state is a position and a velocity in the plane, as in constant_velocity.cpp.
struct Position : Emrys::Linear<2>
{};
struct Velocity : Emrys::Linear<2>
{};
using State = Emrys::Composite<Position, Velocity>;
// The mode is a discrete space whose values are named by types: Cruising is mode 0, Maneuvering 1.
struct Cruising
{};
struct Maneuvering
{};
struct Maneuver : Emrys::Named<Cruising, Maneuvering>
{};
int
main()
{
double const dt = 1.0; // seconds between fixes
int const runs = 1000;
int const steps = 100;
// Both modes move at nearly constant velocity. A cruising target's acceleration is white noise of
// spectral density 0.01 m^2/s^3; a maneuvering target's is 4 m^2/s^3, so its velocity can change
// by about 2 m/s in a second. The fixes arrive at a fixed rate, so each model is taken over that
// one step, as in constant_velocity.cpp.
double const cruiseDensity = 0.01;
double const maneuverDensity = 4.0;
auto const cruise = Emrys::fixedStep(Emrys::ConstantVelocity<Position, Velocity>{cruiseDensity}, dt);
auto const maneuver = Emrys::fixedStep(Emrys::ConstantVelocity<Position, Velocity>{maneuverDensity}, dt);
// The mode is a continuous-time Markov chain. Entry (i, j) of its rate matrix is the rate, per
// second, at which mode i switches to mode j: a cruise ends at the rate 0.03 /s, so it lasts
// about 33 s on average, and a maneuver ends at 0.1 /s, so it lasts about 10 s. The switching
// model pairs the chain with the motion, a process model for each value of the mode, labeled by
// the value it serves, and takes the chain over the models' step once: switching.transition() is
// the Markov chain whose row i is the distribution of the mode one step after mode i.
auto const chain = Emrys::ContinuousTimeMarkovChain<Maneuver>::create(Eigen::Matrix2d{{0, 0.03}, {0.1, 0}}).value();
auto const motion = Emrys::byMode<Maneuver>(Emrys::when<Cruising>(cruise), Emrys::when<Maneuvering>(maneuver));
auto const switching = Emrys::SwitchingModel{chain, motion};
// A fix measures the position with noise of 1 m^2 variance on each axis.
double const fixVariance = 1.0;
auto const gps = Emrys::measurementModel(
Emrys::affineIn(Emrys::on<Position>(Emrys::DifferenceMatrix<Position>::Identity())),
Emrys::additiveNoise(fixVariance * Emrys::DifferenceMatrix<Position>::Identity())
);
// The IMM's state is a hybrid: a probability for each mode, and a Gaussian given each mode. Both
// modes start from the same Gaussian, and the target most likely starts out cruising.
auto const prior = Emrys::GaussianParams<State>{
Emrys::ValueVector<State>{0.0, 0.0, 10.0, 0.0},
Emrys::DifferenceVector<State>{10.0, 10.0, 4.0, 4.0}.asDiagonal()
};
auto const modePrior = Emrys::CategoricalParams<Maneuver>::fromWeights(0.9, 0.1);
Eigen::Index const maneuvering = Emrys::valueOf<Maneuver, Maneuvering>();
// One run's errors are correlated from step to step, so the estimation error is tested over
// independent runs, one final estimate from each (the Monte Carlo test of constant_velocity.cpp).
// The mode tracking and the position errors are counted at every step of every run.
auto nees = Emrys::NeesMonitor{runs};
auto rng = std::mt19937_64{7};
int modeMatches = 0;
double immSquaredError = 0;
double singleSquaredError = 0;
for (int run = 0; run < runs; ++run)
{
auto filter = Emrys::ImmFilter{chain, motion, Emrys::HybridParams{modePrior, prior}};
// For comparison, a Kalman filter that assumes the maneuvering model all the time: the single
// model that never falls behind a maneuver.
auto single = Emrys::KalmanFilter{maneuver, prior};
// The true mode and state are drawn from the priors. The covariance is positive definite and
// the weights are positive, by construction, so neither toDensity() can fail here.
Emrys::ValueVector<State> truth = Emrys::drawSamples(prior.toDensity().value(), 1, rng);
Eigen::Index mode = Emrys::drawSample(modePrior.toDensity().value(), rng);
for (int k = 0; k < steps; ++k)
{
// The true mode takes one step of the chain over dt, a draw from row `mode` of
// switching.transition(), and the true state moves by that mode's model: visit() calls
// the lambda with the process model that the mode follows.
mode = Emrys::simulate(mode, switching.transition(), rng);
truth = switching.visit(mode, [&](auto const & model) {
return Emrys::simulate<State>(truth, model, rng);
});
Emrys::ValueVector<Position> const z = Emrys::simulate<State>(truth, gps, rng);
filter.predict();
single.predict();
auto const fix = Emrys::observe(gps, z);
if (!filter.update(fix) || !single.update(fix))
{
// An innovation covariance was not positive definite, and the state is unchanged.
std::println("run {}, step {}: the update failed", run, k);
return 1;
}
// The mode probabilities are a CategoricalParams, weights that toDensity() checks define
// a distribution before it gives the probabilities.
auto const modes = filter.state().modes().toDensity();
if (!modes)
{
std::println("run {}, step {}: the mode probabilities are not a distribution", run, k);
return 1;
}
bool const saysManeuvering = modes->probability<Maneuvering>() > 0.5;
modeMatches += saysManeuvering == (mode == maneuvering) ? 1 : 0;
// collapsed() is the combined estimate: the single Gaussian with the mean and covariance
// of the mixture over the modes.
Emrys::DifferenceVector<State> const immError =
Emrys::minus<State>(filter.state().toDensity().value().collapsed().mean(), truth);
Emrys::DifferenceVector<State> const singleError = Emrys::minus<State>(single.state().mean(), truth);
immSquaredError += Emrys::diffBlock<State, Position>(immError).squaredNorm();
singleSquaredError += Emrys::diffBlock<State, Position>(singleError).squaredNorm();
}
nees.add(truth, filter.state().toDensity().value().collapsed());
}
double const immRms = std::sqrt(immSquaredError / (runs * steps));
double const singleRms = std::sqrt(singleSquaredError / (runs * steps));
std::println("the more probable mode is the true mode at {} of {} steps", modeMatches, runs * steps);
std::println("position RMS error {:.2f} m, against {:.2f} m for the single-model filter", immRms, singleRms);
// The average is the sum divided by its degrees of freedom, so 1 is ideal.
double const average = nees.average().value();
std::println("average NEES {:.2f} per degree of freedom", average);
// The combined estimate is the moments of a mixture, not a Gaussian, so its NEES is not
// chi-squared. The two-sided chi-squared test of constant_velocity.cpp fails here for one seed
// in three to two in five, even over independent runs. The NEES averages 4 per run, as it
// should, but varies far more than a chi-squared value: a run that ends just after a maneuver
// starts can reach a NEES in the hundreds, while a run that ends in a cruise is underconfident.
// The check asks instead that the average be within a factor of 1.5 of 1, which a maneuvering
// model with a tenth of the true noise fails, and that the IMM track the position better than
// the single model does.
bool const consistent = average > 1 / 1.5 && average < 1.5;
bool const better = immRms < singleRms;
std::println("{}, {}", consistent ? "consistent" : "NOT consistent", better ? "better" : "NOT better");
return consistent && better ? 0 : 1;
}