cuthbertlib 0.1.2__tar.gz → 0.1.3__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 (63) hide show
  1. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/PKG-INFO +1 -1
  2. cuthbertlib-0.1.3/cuthbertlib/README.md +7 -0
  3. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/quadrature/__init__.py +0 -1
  4. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/quadrature/linearize.py +7 -6
  5. cuthbertlib-0.1.3/hatch_build.py +35 -0
  6. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/pyproject.toml +4 -6
  7. cuthbertlib-0.1.2/README.md +0 -1
  8. cuthbertlib-0.1.2/cuthbertlib/quadrature/utils.py +0 -109
  9. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/.gitignore +0 -0
  10. {cuthbertlib-0.1.2/cuthbertlib → cuthbertlib-0.1.3}/README.md +0 -0
  11. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/__init__.py +0 -0
  12. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/discrete/__init__.py +0 -0
  13. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/discrete/filtering.py +0 -0
  14. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/discrete/smoothing.py +0 -0
  15. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/ensemble_kalman/README.md +0 -0
  16. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/ensemble_kalman/__init__.py +0 -0
  17. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/ensemble_kalman/filtering.py +0 -0
  18. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/ensemble_kalman/localization.py +0 -0
  19. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/ensemble_kalman/smoothing.py +0 -0
  20. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/kalman/README.md +0 -0
  21. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/kalman/__init__.py +0 -0
  22. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/kalman/filtering.py +0 -0
  23. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/kalman/generate.py +0 -0
  24. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/kalman/sampling.py +0 -0
  25. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/kalman/smoothing.py +0 -0
  26. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/linalg/README.md +0 -0
  27. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/linalg/__init__.py +0 -0
  28. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/linalg/collect_nans_chol.py +0 -0
  29. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/linalg/marginal_sqrt_cov.py +0 -0
  30. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/linalg/symmetric_inv_sqrt.py +0 -0
  31. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/linalg/tria.py +0 -0
  32. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/linearize/README.md +0 -0
  33. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/linearize/__init__.py +0 -0
  34. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/linearize/log_density.py +0 -0
  35. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/linearize/moments.py +0 -0
  36. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/linearize/taylor.py +0 -0
  37. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/quadrature/README.md +0 -0
  38. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/quadrature/common.py +0 -0
  39. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/quadrature/cubature.py +0 -0
  40. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/quadrature/gauss_hermite.py +0 -0
  41. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/quadrature/unscented.py +0 -0
  42. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/resampling/README.md +0 -0
  43. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/resampling/__init__.py +0 -0
  44. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/resampling/adaptive.py +0 -0
  45. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/resampling/autodiff.py +0 -0
  46. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/resampling/killing.py +0 -0
  47. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/resampling/multinomial.py +0 -0
  48. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/resampling/no_resampling.py +0 -0
  49. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/resampling/protocols.py +0 -0
  50. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/resampling/systematic.py +0 -0
  51. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/resampling/utils.py +0 -0
  52. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/smc/README.md +0 -0
  53. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/smc/__init__.py +0 -0
  54. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/smc/ess.py +0 -0
  55. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/smc/smoothing/__init__.py +0 -0
  56. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/smc/smoothing/exact_sampling.py +0 -0
  57. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/smc/smoothing/mcmc.py +0 -0
  58. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/smc/smoothing/protocols.py +0 -0
  59. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/smc/smoothing/tracing.py +0 -0
  60. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/stats/README.md +0 -0
  61. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/stats/__init__.py +0 -0
  62. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/stats/multivariate_normal.py +0 -0
  63. {cuthbertlib-0.1.2 → cuthbertlib-0.1.3}/cuthbertlib/types.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: cuthbertlib
3
- Version: 0.1.2
3
+ Version: 0.1.3
4
4
  Summary: Atomic building blocks for 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
@@ -0,0 +1,7 @@
1
+ # `cuthbertlib`: Atomic modules for `cuthbert`
2
+
3
+ A collection of atomic, smaller-scoped tools useful for state-space model inference.
4
+ This package contains the building blocks that power the main `cuthbert` package.
5
+
6
+ For more information, including installation and contributing guidelines, see the
7
+ main `cuthbert` [README](https://github.com/state-space-models/cuthbert).
@@ -1,4 +1,3 @@
1
1
  from cuthbertlib.quadrature import cubature, gauss_hermite, unscented
2
2
  from cuthbertlib.quadrature.common import Quadrature, SigmaPoints
3
3
  from cuthbertlib.quadrature.linearize import conditional_moments, functional
4
- from cuthbertlib.quadrature.utils import cholesky_update_many
@@ -8,7 +8,6 @@ from jax.scipy.linalg import cho_solve
8
8
 
9
9
  from cuthbertlib.linalg import tria
10
10
  from cuthbertlib.quadrature.common import Quadrature, SigmaPoints
11
- from cuthbertlib.quadrature.utils import cholesky_update_many
12
11
  from cuthbertlib.types import Array, ArrayLike
13
12
 
14
13
  __all__ = ["conditional_moments", "functional"]
@@ -39,10 +38,11 @@ def conditional_moments(
39
38
  a full covariance matrix or as a square root factor depending on
40
39
  the `mode`.
41
40
  """
41
+ cov = jnp.asarray(cov)
42
42
  if mode == "covariance":
43
43
  chol = jnp.linalg.cholesky(cov)
44
44
  else:
45
- chol = cov
45
+ chol = tria(cov)
46
46
  x_pts: SigmaPoints = quadrature.get_sigma_points(m, chol)
47
47
 
48
48
  f_pts = SigmaPoints(vmap(mean_fn)(x_pts.points), x_pts.wm, x_pts.wc)
@@ -51,16 +51,17 @@ def conditional_moments(
51
51
  A = cho_solve((chol, True), Psi_x).T
52
52
  b = f_pts.mean - A @ m
53
53
  if mode != "covariance":
54
- # This can probably be abstracted better.
55
- sqrt_Phi = f_pts.sqrt
54
+ # Factor the regression residuals directly to avoid covariance downdates,
55
+ # which can lose small noise variances or fail for singular residuals.
56
+ residuals = f_pts.points - f_pts.mean - (x_pts.points - m) @ A.T
57
+ weighted_residuals = jnp.sqrt(x_pts.wc[:, None]) * residuals
56
58
 
57
59
  chol_pts = vmap(cov_fn)(x_pts.points)
58
60
  temp = jnp.sqrt(x_pts.wc[:, None, None]) * chol_pts
59
61
 
60
62
  # concatenate the blocks properly, it's a bit urk, but what can you do...
61
63
  temp = jnp.transpose(temp, [1, 0, 2]).reshape(temp.shape[1], -1)
62
- chol_Q = tria(jnp.concatenate([sqrt_Phi, temp], axis=1))
63
- chol_Q = cholesky_update_many(chol_Q, (A @ chol).T, -1.0)
64
+ chol_Q = tria(jnp.concatenate([weighted_residuals.T, temp], axis=1))
64
65
  return A, b, chol_Q
65
66
 
66
67
  V_pts = vmap(cov_fn)(x_pts.points)
@@ -0,0 +1,35 @@
1
+ """Hatch build hook that packages the top-level `cuthbertlib` source dir."""
2
+
3
+ from pathlib import Path
4
+
5
+ from hatchling.builders.hooks.plugin.interface import BuildHookInterface
6
+
7
+
8
+ class CustomBuildHook(BuildHookInterface):
9
+ """Include the source package in wheels and resolve the README symlink in sdists."""
10
+
11
+ def initialize(self, version, build_data):
12
+ """Adjust the files of the artifact being built."""
13
+ force_include = build_data["force_include"]
14
+ if self.target_name == "sdist":
15
+ # An sdist stores symlinks as links, and the README link points
16
+ # outside the archive, so ship the file it points at instead.
17
+ for source in [s for s in force_include if Path(s).is_symlink()]:
18
+ target = Path(source).resolve()
19
+ if not target.exists():
20
+ raise RuntimeError(f"Symlink {source} points at missing {target}")
21
+ force_include[str(target)] = force_include.pop(source)
22
+ return
23
+ # Editable installs reach the source through a .pth file (dev-mode-dirs);
24
+ # a copy in site-packages would shadow the working tree.
25
+ if self.target_name != "wheel" or version == "editable":
26
+ return
27
+ # A wheel built from the sdist: the sdist already holds the package.
28
+ if Path(self.root, "PKG-INFO").is_file():
29
+ return
30
+ # A wheel built from the workspace: the source sits two levels up.
31
+ name = self.metadata.name
32
+ source = Path(self.root, "..", "..", name)
33
+ if not source.is_dir():
34
+ raise RuntimeError(f"Cannot find the {name} source dir at {source}")
35
+ force_include[str(source)] = name
@@ -1,10 +1,10 @@
1
1
  [build-system]
2
- requires = ["hatchling"]
2
+ requires = ["hatchling>=1.19"]
3
3
  build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "cuthbertlib"
7
- version = "0.1.2"
7
+ version = "0.1.3"
8
8
  description = "Atomic building blocks for state-space model inference with JAX"
9
9
  requires-python = ">=3.10"
10
10
  readme = "README.md"
@@ -22,12 +22,10 @@ dependencies = [
22
22
  [tool.hatch.build]
23
23
  dev-mode-dirs = ["../.."]
24
24
 
25
+ [tool.hatch.build.hooks.custom]
26
+
25
27
  [tool.hatch.build.targets.wheel]
26
28
  packages = ["cuthbertlib"]
27
29
 
28
- [tool.hatch.build.targets.wheel.force-include]
29
- "../../cuthbertlib" = "cuthbertlib"
30
-
31
30
  [tool.hatch.build.targets.sdist.force-include]
32
31
  "../../cuthbertlib" = "cuthbertlib"
33
- "../../cuthbertlib/README.md" = "README.md"
@@ -1 +0,0 @@
1
- ../../cuthbertlib/README.md
@@ -1,109 +0,0 @@
1
- """Utility functions (Cholesky updating) for quadrature."""
2
-
3
- import jax
4
- import jax.numpy as jnp
5
-
6
- from cuthbertlib.types import Array, ArrayLike
7
-
8
- __all__ = ["cholesky_update_many"]
9
-
10
-
11
- def cholesky_update_many(
12
- chol_init: ArrayLike, update_vectors: ArrayLike, multiplier: float
13
- ) -> Array:
14
- r"""Update the Cholesky decomposition of a matrix with multiple update vectors.
15
-
16
- In mathematical terms, we compute :math:`A + \sum_{i=1}^{n} \alpha v_i v_i^T`
17
- where :math:`A` is the original matrix, :math:`v_i` are the update vectors and
18
- :math:`\alpha` is the multiplier.
19
-
20
- Args:
21
- chol_init: Initial Cholesky decomposition of the matrix, :math:`A`.
22
- update_vectors: Update vectors, :math:`v_i`.
23
- multiplier: The multiplier, :math:`\alpha`.
24
-
25
- Returns:
26
- The updated Cholesky decomposition of the matrix.
27
-
28
- Notes:
29
- If the updated matrix does not correspond to a positive definite matrix, the
30
- function has undefined behaviour. It is the responsibility of the caller to
31
- ensure that the updated matrix is positive definite as we cannot check this at runtime.
32
- """
33
-
34
- def body(chol, update_vector):
35
- res = _cholesky_update(chol, update_vector, multiplier=multiplier)
36
- return res, None
37
-
38
- final_chol, _ = jax.lax.scan(body, jnp.asarray(chol_init), update_vectors)
39
- return final_chol
40
-
41
-
42
- def _set_diagonal(x: Array, y: Array) -> Array:
43
- N, _ = x.shape
44
- i, j = jnp.diag_indices(N)
45
- return x.at[i, j].set(y)
46
-
47
-
48
- def _set_triu(x: Array, val: ArrayLike) -> Array:
49
- N, _ = x.shape
50
- i = jnp.triu_indices(N, 1)
51
- return x.at[i].set(val)
52
-
53
-
54
- def _cholesky_update(
55
- chol: ArrayLike, update_vector: ArrayLike, multiplier: float = 1.0
56
- ) -> Array:
57
- chol = jnp.asarray(chol)
58
- chol_diag = jnp.diag(chol)
59
-
60
- # The algorithm in [1] is implemented as a double for loop. We can treat
61
- # the inner loop in Algorithm 3.1 as a vector operation, and thus the
62
- # whole algorithm as a single for loop, and hence can use a `tf.scan`
63
- # on it.
64
-
65
- # We use for accumulation omega and b as defined in Algorithm 3.1, since
66
- # these are updated per iteration.
67
-
68
- def scan_body(carry, inp):
69
- _, _, omega, b = carry
70
- index, diagonal_member, col = inp
71
- omega_at_index = omega[..., index]
72
-
73
- # Line 4
74
- new_diagonal_member = jnp.sqrt(
75
- jnp.square(diagonal_member) + multiplier / b * jnp.square(omega_at_index)
76
- )
77
- # `scaling_factor` is the same as `gamma` on Line 5.
78
- scaling_factor = jnp.square(diagonal_member) * b + multiplier * jnp.square(
79
- omega_at_index
80
- )
81
-
82
- # The following updates are the same as the for loop in lines 6-8.
83
- omega = omega - (omega_at_index / diagonal_member)[..., None] * col
84
- new_col = new_diagonal_member[..., None] * (
85
- col / diagonal_member[..., None]
86
- + (multiplier * omega_at_index / scaling_factor)[..., None] * omega
87
- )
88
- b = b + multiplier * jnp.square(omega_at_index / diagonal_member)
89
- return (new_diagonal_member, new_col, omega, b), (
90
- new_diagonal_member,
91
- new_col,
92
- omega,
93
- b,
94
- )
95
-
96
- # We will scan over the columns.
97
- chol = chol.T
98
-
99
- _, (new_diag, new_chol, _, _) = jax.lax.scan(
100
- scan_body,
101
- (0.0, jnp.zeros_like(chol[0]), update_vector, 1.0),
102
- (jnp.arange(0, chol.shape[0]), chol_diag, chol),
103
- )
104
-
105
- new_chol = new_chol.T
106
- new_chol = _set_diagonal(new_chol, new_diag)
107
- new_chol = _set_triu(new_chol, 0.0)
108
- new_chol = jnp.where(jnp.isfinite(new_chol), new_chol, 0.0)
109
- return new_chol
File without changes