/
test_string_to_measmodel.py
77 lines (63 loc) · 2.42 KB
/
test_string_to_measmodel.py
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
"""Previously, this module contained the tests for functions in the
`diffeq.odefiltsmooth.gaussian.ivp2filter` module, since this module has become
obsolete, we test its replacement (`GaussianIVPFilter.string_to_measurement_model`)
here.
They need different fixtures anyway.
"""
import numpy as np
import pytest
import probnum.problems.zoo.diffeq as diffeq_zoo
from probnum import diffeq, filtsmooth, randprocs, randvars
@pytest.fixture
def ivp():
y0 = np.array([20.0, 15.0])
return diffeq_zoo.lotkavolterra(t0=0.4124, tmax=1.15124, y0=y0)
@pytest.fixture
def prior(ivp):
ode_dim = ivp.dimension
prior = randprocs.markov.integrator.IntegratedWienerTransition(
num_derivatives=2, wiener_process_dimension=ode_dim
)
initrv = randvars.Normal(
mean=np.zeros(prior.dimension),
cov=np.eye(prior.dimension),
cov_cholesky=np.eye(prior.dimension),
)
prior_process = randprocs.markov.MarkovProcess(
transition=prior, initrv=initrv, initarg=0.0
)
return prior_process
@pytest.mark.parametrize(
"string, expected_type",
[
("EK0", filtsmooth.gaussian.approx.DiscreteEKFComponent),
("EK1", filtsmooth.gaussian.approx.DiscreteEKFComponent),
],
)
def test_output_type(string, expected_type, ivp, prior):
"""Assert that the output type matches."""
received = diffeq.odefiltsmooth.GaussianIVPFilter.string_to_measurement_model(
string, ivp, prior
)
assert isinstance(received, expected_type)
def test_string_not_supported(ivp, prior):
with pytest.raises(ValueError):
diffeq.odefiltsmooth.GaussianIVPFilter.string_to_measurement_model(
"abc", ivp, prior
)
@pytest.mark.parametrize(
"string",
["EK0", "EK1"],
)
def test_true_mean_ek(string, ivp, prior):
"""Assert that a forwarded realization is x[1] - f(t, x[0]) with zero added covariance."""
received = diffeq.odefiltsmooth.GaussianIVPFilter.string_to_measurement_model(
string, ivp, prior
)
some_real = 1.0 + 0.01 * np.random.rand(prior.transition.dimension)
some_time = 1.0 + 0.01 * np.random.rand()
received, _ = received.forward_realization(some_real, some_time)
e0, e1 = prior.transition.proj2coord(0), prior.transition.proj2coord(1)
expected = e1 @ some_real - ivp.f(some_time, e0 @ some_real)
np.testing.assert_allclose(received.mean, expected)
np.testing.assert_allclose(received.cov, 0.0, atol=1e-12)