cuthbert 0.1.0__tar.gz → 0.1.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {cuthbert-0.1.0 → cuthbert-0.1.2}/PKG-INFO +1 -1
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/gaussian/taylor/associative_filter.py +2 -1
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/gaussian/taylor/filter.py +3 -2
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/gaussian/taylor/non_associative_filter.py +73 -23
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/smc/backward_sampler.py +1 -1
- {cuthbert-0.1.0 → cuthbert-0.1.2}/pyproject.toml +1 -1
- {cuthbert-0.1.0 → cuthbert-0.1.2}/.gitignore +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/README.md +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/README.md +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/__init__.py +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/discrete/README.md +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/discrete/__init__.py +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/discrete/filter.py +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/discrete/smoother.py +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/discrete/types.py +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/ensemble_kalman/README.md +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/ensemble_kalman/__init__.py +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/ensemble_kalman/ensemble_kalman_filter.py +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/ensemble_kalman/ensemble_rts_smoother.py +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/ensemble_kalman/types.py +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/factorial/README.md +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/factorial/__init__.py +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/factorial/discrete.py +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/factorial/filtering.py +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/factorial/gaussian.py +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/factorial/smc.py +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/factorial/types.py +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/factorial/utils.py +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/filtering.py +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/gaussian/README.md +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/gaussian/__init__.py +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/gaussian/kalman.py +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/gaussian/moments/__init__.py +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/gaussian/moments/associative_filter.py +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/gaussian/moments/filter.py +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/gaussian/moments/non_associative_filter.py +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/gaussian/moments/smoother.py +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/gaussian/moments/types.py +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/gaussian/taylor/__init__.py +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/gaussian/taylor/smoother.py +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/gaussian/taylor/types.py +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/gaussian/types.py +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/gaussian/utils.py +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/inference.py +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/smc/README.md +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/smc/__init__.py +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/smc/marginal_particle_filter.py +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/smc/particle_filter.py +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/smc/types.py +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/smoothing.py +0 -0
- {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/utils.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: cuthbert
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.2
|
|
4
4
|
Summary: State-space model inference with JAX
|
|
5
5
|
Author-email: Sam Duffield <s@mduffield.com>, Sahel Iqbal <sahel13miqbal@proton.me>, Adrien Corenflos <adrien.corenflos.stats@gmail.com>
|
|
6
6
|
License: Apache-2.0
|
|
@@ -144,13 +144,14 @@ def filter_prepare(
|
|
|
144
144
|
chol_Q = jnp.where(skip_predict_flag, jnp.zeros_like(chol_Q), chol_Q)
|
|
145
145
|
|
|
146
146
|
observation_output = get_observation_func(dummy_state, model_inputs)
|
|
147
|
-
H, d, chol_R, observation = non_associative_filter.process_observation(
|
|
147
|
+
H, d, chol_R, observation, correction = non_associative_filter.process_observation(
|
|
148
148
|
observation_output,
|
|
149
149
|
rtol=rtol,
|
|
150
150
|
ignore_nan_dims=ignore_nan_dims,
|
|
151
151
|
)
|
|
152
152
|
|
|
153
153
|
elem = filtering.associative_params_single(F, c, chol_Q, H, d, chol_R, observation)
|
|
154
|
+
elem = elem._replace(ell=elem.ell + correction)
|
|
154
155
|
|
|
155
156
|
return LinearizedKalmanFilterState(
|
|
156
157
|
elem=elem,
|
|
@@ -15,10 +15,11 @@ $$
|
|
|
15
15
|
and potentials as
|
|
16
16
|
|
|
17
17
|
$$
|
|
18
|
-
G(x) \approx N(x \mid m, L L^T),
|
|
18
|
+
G(x) \approx \exp(c) N(x \mid m, L L^T),
|
|
19
19
|
$$
|
|
20
20
|
|
|
21
|
-
where $
|
|
21
|
+
where $c$ matches the log potential at the expansion point and contributes to the
|
|
22
|
+
log evidence when $L L^T$ is nonsingular on the active coordinates.
|
|
22
23
|
|
|
23
24
|
See `cuthbertlib.linearize` for more details.
|
|
24
25
|
|
|
@@ -15,6 +15,7 @@ from cuthbert.utils import dummy_tree_like
|
|
|
15
15
|
from cuthbertlib.kalman import filtering
|
|
16
16
|
from cuthbertlib.linalg import block_marginal_sqrt_cov
|
|
17
17
|
from cuthbertlib.linearize import linearize_log_density, linearize_taylor
|
|
18
|
+
from cuthbertlib.stats import multivariate_normal
|
|
18
19
|
from cuthbertlib.types import Array, ArrayLike, ArrayTreeLike, KeyArray, LogDensity
|
|
19
20
|
|
|
20
21
|
|
|
@@ -23,8 +24,29 @@ def process_observation(
|
|
|
23
24
|
| tuple[LogPotential, Array],
|
|
24
25
|
rtol: float | None = None,
|
|
25
26
|
ignore_nan_dims: bool = False,
|
|
26
|
-
) -> tuple[Array, Array, Array, Array]:
|
|
27
|
-
"""
|
|
27
|
+
) -> tuple[Array, Array, Array, Array, Array]:
|
|
28
|
+
r"""Prepare a Gaussian observation for the Taylor Kalman update.
|
|
29
|
+
|
|
30
|
+
Returns a tuple with parameters for a Gaussian pseudo-observation. For
|
|
31
|
+
a `LogPotential` input, an additional correction term $\log G(x_0) - \log q(x_0)$
|
|
32
|
+
is computed, where $G$ is the potential and $q$ is the Gaussian pseudo-potential.
|
|
33
|
+
Adding this correction to the log normalizing constant ensures that constant factors
|
|
34
|
+
to the log normalizing constant are properly accounted for.
|
|
35
|
+
|
|
36
|
+
Args:
|
|
37
|
+
observation_output: Either `(log_conditional_density, point, observation)`
|
|
38
|
+
or `(log_potential, point)` for a unary observation potential.
|
|
39
|
+
rtol: Relative singular-value cutoff for inverting precision matrices
|
|
40
|
+
during linearization.
|
|
41
|
+
ignore_nan_dims: Whether to treat coordinates with NaN on the diagonal
|
|
42
|
+
of the precision matrix as missing.
|
|
43
|
+
|
|
44
|
+
Returns:
|
|
45
|
+
Tuple `(H, d, chol_R, y, correction)` with the observation matrix, offset,
|
|
46
|
+
square-root noise covariance, observation or pseudo-observation, and
|
|
47
|
+
additive log-evidence correction. The correction is zero for conditional
|
|
48
|
+
densities, skipped observations, or singular or non-finite active factors.
|
|
49
|
+
"""
|
|
28
50
|
if len(observation_output) == 3:
|
|
29
51
|
observation_cond_log_density, linearization_point, observation = (
|
|
30
52
|
observation_output
|
|
@@ -36,25 +58,47 @@ def process_observation(
|
|
|
36
58
|
rtol=rtol,
|
|
37
59
|
ignore_nan_dims=ignore_nan_dims,
|
|
38
60
|
)
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
)
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
61
|
+
return H, d, chol_R, observation, jnp.zeros((), dtype=d.dtype)
|
|
62
|
+
|
|
63
|
+
observation_log_potential, linearization_point = observation_output
|
|
64
|
+
skip = jnp.isnan(linearization_point).all()
|
|
65
|
+
anchor = jnp.where(skip, 0.0, linearization_point)
|
|
66
|
+
|
|
67
|
+
def log_potential_with_value(x: ArrayLike) -> tuple[Array, Array]:
|
|
68
|
+
value = observation_log_potential(x)
|
|
69
|
+
return value, value
|
|
70
|
+
|
|
71
|
+
d, chol_R, value = linearize_taylor(
|
|
72
|
+
log_potential_with_value,
|
|
73
|
+
anchor,
|
|
74
|
+
has_aux=True,
|
|
75
|
+
rtol=rtol,
|
|
76
|
+
ignore_nan_dims=ignore_nan_dims,
|
|
77
|
+
)
|
|
78
|
+
# With H = -I and y = 0, the pseudo-observation residual is x - d.
|
|
79
|
+
H = -jnp.eye(d.shape[0])
|
|
80
|
+
missing = skip | (ignore_nan_dims & jnp.isnan(jnp.diag(chol_R)))
|
|
81
|
+
observation = jnp.where(missing, jnp.nan, 0.0)
|
|
82
|
+
|
|
83
|
+
# The active block of the Taylor factor is already lower triangular.
|
|
84
|
+
active_factor = jnp.where(missing[:, None] | missing[None, :], 0.0, chol_R)
|
|
85
|
+
apply_correction = (
|
|
86
|
+
~skip
|
|
87
|
+
& jnp.isfinite(active_factor).all()
|
|
88
|
+
& jnp.all(missing | (jnp.diag(active_factor) != 0))
|
|
89
|
+
)
|
|
90
|
+
# A padded coordinate has log density zero at residual zero.
|
|
91
|
+
ignored = missing | ~apply_correction
|
|
92
|
+
padding_scale = jnp.asarray((2 * jnp.pi) ** -0.5, dtype=chol_R.dtype)
|
|
93
|
+
factor = jnp.where(apply_correction, active_factor, 0.0) + jnp.diag(
|
|
94
|
+
jnp.where(ignored, padding_scale, 0.0)
|
|
95
|
+
)
|
|
96
|
+
residual = jnp.where(ignored, 0.0, anchor - d)
|
|
97
|
+
gaussian_value = multivariate_normal.logpdf(
|
|
98
|
+
residual, jnp.zeros_like(residual), factor, nan_support=False
|
|
99
|
+
)
|
|
100
|
+
correction = jnp.where(apply_correction, value - gaussian_value, 0.0)
|
|
101
|
+
return H, d, chol_R, observation, correction
|
|
58
102
|
|
|
59
103
|
|
|
60
104
|
def init_prepare(
|
|
@@ -222,14 +266,20 @@ def filter_combine(
|
|
|
222
266
|
|
|
223
267
|
observation_output = get_observation_func(predict_state, state_2.model_inputs)
|
|
224
268
|
|
|
225
|
-
H, d, chol_R, observation = process_observation(
|
|
269
|
+
H, d, chol_R, observation, correction = process_observation(
|
|
226
270
|
observation_output,
|
|
227
271
|
rtol=rtol,
|
|
228
272
|
ignore_nan_dims=ignore_nan_dims,
|
|
229
273
|
)
|
|
230
274
|
|
|
231
275
|
(update_mean, update_chol_cov), log_normalizing_constant = filtering.update(
|
|
232
|
-
predict_mean,
|
|
276
|
+
predict_mean,
|
|
277
|
+
predict_chol_cov,
|
|
278
|
+
H,
|
|
279
|
+
d,
|
|
280
|
+
chol_R,
|
|
281
|
+
observation,
|
|
282
|
+
log_normalizing_constant=correction,
|
|
233
283
|
)
|
|
234
284
|
|
|
235
285
|
return linearized_kalman_filter_state_dummy_elem(
|
|
@@ -183,7 +183,7 @@ def smoother_combine(
|
|
|
183
183
|
x0_all=state_1.particles,
|
|
184
184
|
x1_all=state_2.particles,
|
|
185
185
|
log_weight_x0_all=state_1.log_weights,
|
|
186
|
-
log_density=lambda s1, s2: log_potential(s1, s2,
|
|
186
|
+
log_density=lambda s1, s2: log_potential(s1, s2, state_1.model_inputs),
|
|
187
187
|
x1_ancestor_indices=state_2.ancestor_indices,
|
|
188
188
|
)
|
|
189
189
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|