cuthbertlib 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 (61) hide show
  1. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/PKG-INFO +1 -1
  2. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/resampling/autodiff.py +22 -9
  3. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/smc/smoothing/mcmc.py +1 -1
  4. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/pyproject.toml +1 -1
  5. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/.gitignore +0 -0
  6. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/README.md +0 -0
  7. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/README.md +0 -0
  8. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/__init__.py +0 -0
  9. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/discrete/__init__.py +0 -0
  10. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/discrete/filtering.py +0 -0
  11. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/discrete/smoothing.py +0 -0
  12. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/ensemble_kalman/README.md +0 -0
  13. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/ensemble_kalman/__init__.py +0 -0
  14. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/ensemble_kalman/filtering.py +0 -0
  15. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/ensemble_kalman/localization.py +0 -0
  16. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/ensemble_kalman/smoothing.py +0 -0
  17. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/kalman/README.md +0 -0
  18. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/kalman/__init__.py +0 -0
  19. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/kalman/filtering.py +0 -0
  20. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/kalman/generate.py +0 -0
  21. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/kalman/sampling.py +0 -0
  22. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/kalman/smoothing.py +0 -0
  23. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/linalg/README.md +0 -0
  24. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/linalg/__init__.py +0 -0
  25. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/linalg/collect_nans_chol.py +0 -0
  26. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/linalg/marginal_sqrt_cov.py +0 -0
  27. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/linalg/symmetric_inv_sqrt.py +0 -0
  28. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/linalg/tria.py +0 -0
  29. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/linearize/README.md +0 -0
  30. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/linearize/__init__.py +0 -0
  31. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/linearize/log_density.py +0 -0
  32. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/linearize/moments.py +0 -0
  33. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/linearize/taylor.py +0 -0
  34. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/quadrature/README.md +0 -0
  35. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/quadrature/__init__.py +0 -0
  36. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/quadrature/common.py +0 -0
  37. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/quadrature/cubature.py +0 -0
  38. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/quadrature/gauss_hermite.py +0 -0
  39. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/quadrature/linearize.py +0 -0
  40. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/quadrature/unscented.py +0 -0
  41. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/quadrature/utils.py +0 -0
  42. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/resampling/README.md +0 -0
  43. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/resampling/__init__.py +0 -0
  44. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/resampling/adaptive.py +0 -0
  45. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/resampling/killing.py +0 -0
  46. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/resampling/multinomial.py +0 -0
  47. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/resampling/no_resampling.py +0 -0
  48. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/resampling/protocols.py +0 -0
  49. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/resampling/systematic.py +0 -0
  50. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/resampling/utils.py +0 -0
  51. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/smc/README.md +0 -0
  52. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/smc/__init__.py +0 -0
  53. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/smc/ess.py +0 -0
  54. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/smc/smoothing/__init__.py +0 -0
  55. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/smc/smoothing/exact_sampling.py +0 -0
  56. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/smc/smoothing/protocols.py +0 -0
  57. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/smc/smoothing/tracing.py +0 -0
  58. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/stats/README.md +0 -0
  59. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/stats/__init__.py +0 -0
  60. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/stats/multivariate_normal.py +0 -0
  61. {cuthbertlib-0.1.0 → cuthbertlib-0.1.2}/cuthbertlib/types.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: cuthbertlib
3
- Version: 0.1.0
3
+ Version: 0.1.2
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
@@ -6,6 +6,7 @@ This can be wrapped around a resampling scheme such as multinomial or systematic
6
6
  resampling.
7
7
 
8
8
  See [Scibior and Wood (2021)](https://arxiv.org/abs/2106.10314) for more details.
9
+ For the discount, see [Tan et al.](https://arxiv.org/abs/2407.03085v2).
9
10
  """
10
11
 
11
12
  from functools import wraps
@@ -15,19 +16,31 @@ import jax.numpy as jnp
15
16
 
16
17
  from cuthbertlib.resampling.protocols import Resampling
17
18
  from cuthbertlib.resampling.utils import apply_resampling_indices
18
- from cuthbertlib.types import Array, ArrayLike, ArrayTree, ArrayTreeLike
19
+ from cuthbertlib.types import (
20
+ Array,
21
+ ArrayLike,
22
+ ArrayTree,
23
+ ArrayTreeLike,
24
+ ScalarArrayLike,
25
+ )
19
26
 
20
27
 
21
- def stop_gradient_decorator(func: Resampling) -> Resampling:
28
+ def stop_gradient_decorator(
29
+ func: Resampling, alpha: ScalarArrayLike = 1.0
30
+ ) -> Resampling:
22
31
  """Wrap a Resampling function to use stop gradient resampling.
23
32
 
24
33
  Args:
25
34
  func: A resampling function with signature
26
35
  (key, logits, positions, n) -> (indices, logits_out, positions_out).
36
+ alpha: A scalar multiplier for the log-weight correction, clipped to [0, 1].
37
+ The default, 1, preserves the original stop-gradient estimator;
38
+ 0 removes the correction.
27
39
 
28
40
  Returns:
29
41
  A Resampling function implementing stop gradient resampling.
30
42
  """
43
+ alpha = jnp.clip(jnp.broadcast_to(alpha, ()), 0, 1)
31
44
  # Build a descriptive docstring that includes the wrapped function doc
32
45
  wrapped_doc = func.__doc__ or ""
33
46
  doc = f"""
@@ -35,9 +48,11 @@ def stop_gradient_decorator(func: Resampling) -> Resampling:
35
48
 
36
49
  This wrapper will call the provided resampling function, and then apply
37
50
  the stop gradient trick of [Scibior and Wood (2021)](https://arxiv.org/abs/2106.10314).
38
- Resulting estimates of the score function (i.e., the gradient of the
51
+ With alpha=1, resulting estimates of the score function (i.e., the gradient of the
39
52
  log-likelihood with respect to model parameters) are unbiased,
40
- corresponding to the classical Fisher estimate.
53
+ corresponding to the classical Fisher estimate. Values below 1 discount
54
+ the log-weight correction at resampling as in [Tan et al.](https://arxiv.org/abs/2407.03085v2).
55
+ With alpha=0, the correction is zero and gradients still pass through the selected particles.
41
56
 
42
57
  Wrapped resampler documentation:
43
58
  {wrapped_doc}
@@ -51,11 +66,9 @@ def stop_gradient_decorator(func: Resampling) -> Resampling:
51
66
  key, jax.lax.stop_gradient(logits), positions, n
52
67
  )
53
68
 
54
- logits = jnp.asarray(
55
- logits_base
56
- + apply_resampling_indices(logits, idx_base)
57
- - jax.lax.stop_gradient(apply_resampling_indices(logits, idx_base))
58
- )
69
+ selected = apply_resampling_indices(logits, idx_base)
70
+ correction = alpha * (selected - jax.lax.stop_gradient(selected))
71
+ logits = jnp.asarray(logits_base + correction)
59
72
  return idx_base, logits, positions_base
60
73
 
61
74
  # Attach the composed docstring and return a jitted version
@@ -49,7 +49,7 @@ def simulate(
49
49
  )
50
50
  n_samples = x1_ancestor_indices.shape[0]
51
51
 
52
- keys = random.split(key, (n_steps * 2)).reshape((n_steps, 2))
52
+ keys = random.split(key, (n_steps, 2))
53
53
 
54
54
  def body(carry, keys_t):
55
55
  # IMH proposal
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "cuthbertlib"
7
- version = "0.1.0"
7
+ version = "0.1.2"
8
8
  description = "Atomic building blocks for state-space model inference with JAX"
9
9
  requires-python = ">=3.10"
10
10
  readme = "README.md"
File without changes
File without changes