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.
Files changed (51) hide show
  1. {cuthbert-0.1.0 → cuthbert-0.1.2}/PKG-INFO +1 -1
  2. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/gaussian/taylor/associative_filter.py +2 -1
  3. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/gaussian/taylor/filter.py +3 -2
  4. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/gaussian/taylor/non_associative_filter.py +73 -23
  5. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/smc/backward_sampler.py +1 -1
  6. {cuthbert-0.1.0 → cuthbert-0.1.2}/pyproject.toml +1 -1
  7. {cuthbert-0.1.0 → cuthbert-0.1.2}/.gitignore +0 -0
  8. {cuthbert-0.1.0 → cuthbert-0.1.2}/README.md +0 -0
  9. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/README.md +0 -0
  10. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/__init__.py +0 -0
  11. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/discrete/README.md +0 -0
  12. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/discrete/__init__.py +0 -0
  13. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/discrete/filter.py +0 -0
  14. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/discrete/smoother.py +0 -0
  15. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/discrete/types.py +0 -0
  16. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/ensemble_kalman/README.md +0 -0
  17. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/ensemble_kalman/__init__.py +0 -0
  18. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/ensemble_kalman/ensemble_kalman_filter.py +0 -0
  19. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/ensemble_kalman/ensemble_rts_smoother.py +0 -0
  20. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/ensemble_kalman/types.py +0 -0
  21. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/factorial/README.md +0 -0
  22. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/factorial/__init__.py +0 -0
  23. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/factorial/discrete.py +0 -0
  24. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/factorial/filtering.py +0 -0
  25. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/factorial/gaussian.py +0 -0
  26. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/factorial/smc.py +0 -0
  27. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/factorial/types.py +0 -0
  28. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/factorial/utils.py +0 -0
  29. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/filtering.py +0 -0
  30. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/gaussian/README.md +0 -0
  31. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/gaussian/__init__.py +0 -0
  32. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/gaussian/kalman.py +0 -0
  33. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/gaussian/moments/__init__.py +0 -0
  34. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/gaussian/moments/associative_filter.py +0 -0
  35. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/gaussian/moments/filter.py +0 -0
  36. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/gaussian/moments/non_associative_filter.py +0 -0
  37. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/gaussian/moments/smoother.py +0 -0
  38. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/gaussian/moments/types.py +0 -0
  39. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/gaussian/taylor/__init__.py +0 -0
  40. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/gaussian/taylor/smoother.py +0 -0
  41. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/gaussian/taylor/types.py +0 -0
  42. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/gaussian/types.py +0 -0
  43. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/gaussian/utils.py +0 -0
  44. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/inference.py +0 -0
  45. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/smc/README.md +0 -0
  46. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/smc/__init__.py +0 -0
  47. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/smc/marginal_particle_filter.py +0 -0
  48. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/smc/particle_filter.py +0 -0
  49. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/smc/types.py +0 -0
  50. {cuthbert-0.1.0 → cuthbert-0.1.2}/cuthbert/smoothing.py +0 -0
  51. {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.0
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 $L$ is the Cholesky factor of the covariance matrix.
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
- """Process observation for linearized Taylor Kalman filter."""
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
- else:
40
- observation_log_potential, linearization_point = observation_output
41
- d, chol_R = linearize_taylor(
42
- observation_log_potential,
43
- linearization_point,
44
- rtol=rtol,
45
- ignore_nan_dims=ignore_nan_dims,
46
- )
47
- # dummy mat and observation as potential is unconditional
48
- # Note the minus sign as linear potential is -0.5 (x - d)^T (R R^T)^{-1} (x - d)
49
- # and kalman expects -0.5 (y - H @ x - d)^T (R R^T)^{-1} (y - H @ x - d)
50
- H = -jnp.eye(d.shape[0])
51
- observation = jnp.where(
52
- jnp.isnan(jnp.diag(chol_R)) * ignore_nan_dims, jnp.nan, 0.0
53
- ) # Tell the cuthbertlib.kalman to skip these dimensions
54
- observation = jnp.where(
55
- jnp.isnan(linearization_point).all(), jnp.nan, observation
56
- ) # If both linearization points are all NaN, skip the observation
57
- return H, d, chol_R, observation
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, predict_chol_cov, H, d, chol_R, observation
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, state_2.model_inputs),
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
 
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "cuthbert"
7
- version = "0.1.0"
7
+ version = "0.1.2"
8
8
  description = "State-space model inference with JAX"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
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