Utilities¶
Numerical solvers, linear-algebra helpers and shared type aliases.
Solvers¶
probjax.utils.odeint
¶
odeint
¶
odeint(drift, y0, ts, *args, method='rk4', dtype=float32, filter_state=None, collect_trace=True, check_points=None, step_size_adaptor=None, logdet_rng=None, trace_estimator='exact', num_samples=1, sample_dist='rademacher')
Solve an ordinary differential equation dy/dt = drift(t, y, *args).
drift may be any of:
- a plain Python callable
drift(t, y, *args)— it is automatically wrapped in :class:probjax.utils.functions.generic_driftso it rides as a pytree throughjax.jitand thecustom_inverseprimitive; - a registered JAX pytree node (
eqx.Module,flax.struct.PyTreeNode, :class:~probjax.utils.functions.Driftsubclass, ...) — its array leaves participate in transformations natively, non-array fields ride as aux.
Drift keyword arguments are no longer supported. Pass parameters
positionally via *args, or bind them up-front with functools.partial
(or drift.bind_args(...) on a :class:~probjax.utils.functions.Drift).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
drift
|
Callable[..., PyTree[Array]]
|
The drift function |
required |
y0
|
PyTree[Array]
|
Initial state. Single array or pytree of arrays. |
required |
ts
|
Array
|
Time points at which to evaluate the solution. |
required |
*args
|
Any
|
Positional arguments forwarded to |
()
|
method
|
str
|
Integration method name. |
'rk4'
|
dtype
|
Optional[dtype]
|
Computation dtype (default |
float32
|
filter_state
|
Optional[Callable[[PyTree[Array]], Optional[PyTree[Array]]]]
|
Optional function to filter the state during integration. |
None
|
collect_trace
|
bool
|
If |
True
|
check_points
|
Optional[Sequence[int]]
|
Optional index sequence for checkpointed grid integration. |
None
|
step_size_adaptor
|
Optional[StepSizeAdaptor]
|
Step-size controller for adaptive solver
methods ( |
None
|
logdet_rng
|
Optional[Array]
|
RNG key for stochastic log-determinant estimators.
Required when |
None
|
trace_estimator
|
TraceEstimator
|
Log-det trace estimator used when the function is
inverted via
|
'exact'
|
num_samples
|
int
|
Hutchinson probe-vector count per trajectory. Higher values reduce variance linearly in cost. |
1
|
sample_dist
|
SampleDist
|
Hutchinson probe distribution — |
'rademacher'
|
Returns:
| Type | Description |
|---|---|
Optional[PyTree[Array]]
|
Pytree containing either the time-series trace (when |
Optional[PyTree[Array]]
|
|
Example
import jax.numpy as jnp from probjax.utils.odeint import odeint
def lotka_volterra(t, y, alpha, beta, delta, gamma): ... prey, predator = y ... dprey = alpha * prey - beta * prey * predator ... dpredator = delta * prey * predator - gamma * predator ... return jnp.array([dprey, dpredator])
y0 = jnp.array([40.0, 9.0]) ts = jnp.linspace(0, 10, 100) ys = odeint(lotka_volterra, y0, ts, 1.0, 0.1, 0.075, 0.5, ... method="dopri5")
Source code in probjax/utils/odeint.py
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | |
probjax.utils.sdeint
¶
sdeint
¶
sdeint(rng, drift, diffusion, y0, ts, *args, method='euler_maruyama', dtype=float32, sde_type='ito', return_brownian=False, return_state=False, filter_state=None, collect_trace=True, check_points=None, step_size_adaptor=None)
Solve a stochastic differential equation
dy = drift(t, y, *args) dt + diffusion(t, y, *args) dW.
drift and diffusion may be plain Python callables (automatically
wrapped as :class:~probjax.utils.functions.generic_drift) or any
pytree-registered callable (marker
:class:~probjax.utils.functions.Drift subclass, eqx.Module, etc.).
Keyword arguments to drift/diffusion are no longer supported. Pass
parameters positionally via *args, or bind them with
functools.partial / drift.bind_args(...).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rng
|
Key
|
Random number generator key. |
required |
drift
|
Callable[..., PyTree[Array]]
|
Drift function |
required |
diffusion
|
Callable[..., PyTree[Array]]
|
Diffusion function |
required |
y0
|
PyTree[Array]
|
Initial state. Single array or pytree of arrays. |
required |
ts
|
Array
|
Strictly increasing 1D array of time points. |
required |
*args
|
Any
|
Positional arguments forwarded to both drift and diffusion. |
()
|
method
|
str
|
Integration method name. Available:
|
'euler_maruyama'
|
dtype
|
Optional[dtype]
|
Computation dtype (default |
float32
|
sde_type
|
str
|
|
'ito'
|
return_brownian
|
bool
|
Whether to return Brownian paths (requires
|
False
|
return_state
|
bool
|
Whether to return solver state. |
False
|
filter_state
|
Optional[Callable[[PyTree[Array]], Optional[PyTree[Array]]]]
|
Optional state filter; returning |
None
|
collect_trace
|
bool
|
Record the filtered quantity at every time step
( |
True
|
check_points
|
Optional[Sequence[int]]
|
Optional index sequence for checkpointed grid integration. |
None
|
step_size_adaptor
|
Optional[SDEStepSizeAdaptor]
|
When provided, switches integration to an
adaptive step-doubling Euler-Maruyama scheme driven by the
given controller. Pass
:class: |
None
|
Returns:
| Type | Description |
|---|---|
Union[Optional[PyTree[Array]], Tuple[Any, Optional[PyTree[Array]]], Tuple[Optional[PyTree[Array]], Optional[PyTree[Array]]], Tuple[Any, Tuple[Optional[PyTree[Array]], Optional[PyTree[Array]]]]]
|
When |
Union[Optional[PyTree[Array]], Tuple[Any, Optional[PyTree[Array]]], Tuple[Optional[PyTree[Array]], Optional[PyTree[Array]]], Tuple[Any, Tuple[Optional[PyTree[Array]], Optional[PyTree[Array]]]]]
|
|
Union[Optional[PyTree[Array]], Tuple[Any, Optional[PyTree[Array]]], Tuple[Optional[PyTree[Array]], Optional[PyTree[Array]]], Tuple[Any, Tuple[Optional[PyTree[Array]], Optional[PyTree[Array]]]]]
|
|
Union[Optional[PyTree[Array]], Tuple[Any, Optional[PyTree[Array]]], Tuple[Optional[PyTree[Array]], Optional[PyTree[Array]]], Tuple[Any, Tuple[Optional[PyTree[Array]], Optional[PyTree[Array]]]]]
|
|
Example
import jax.numpy as jnp from probjax.utils.sdeint import sdeint from jax import random
def drift(t, state, mu, sigma): ... return {"price": mu * state["price"]} def diffusion(t, state, mu, sigma): ... return {"price": sigma * state["price"]}
rng = random.PRNGKey(0) y0 = {"price": jnp.array([1.0])} ts = jnp.linspace(0, 1, 100) ys = sdeint(rng, drift, diffusion, y0, ts, 0.1, 0.2)
Source code in probjax/utils/sdeint.py
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | |
probjax.utils.root
¶
Find a root of a function, using a fixed point iteration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fun
|
Callable
|
Function to find root of. |
required |
x0
|
Array
|
Initial value. |
required |
args
|
tuple
|
Extra arguments to pass to function. Defaults to (). |
()
|
method
|
str
|
Method to use. Defaults to 'fixpoint'. |
'newton-raphson'
|
tol
|
float
|
Tolerance. Defaults to 1e-3. |
0.001
|
Returns:
| Name | Type | Description |
|---|---|---|
Array |
Root of function. |
Source code in probjax/utils/solver.py
probjax.utils.newton_raphson
¶
Newton-Raphson root-finding algorithm for a vector-valued function.
Args: - f: A function that takes a vector x and returns a vector of the same shape. - x0: Initial guess for the root. - tol: Tolerance for stopping criterion (default: 1e-6). - max_iter: Maximum number of iterations (default: 100).
Returns: - x: The estimated root of the function.
Source code in probjax/utils/solver.py
Special functions¶
probjax.utils.betaincinv
¶
bracket_x
¶
Return (lo, hi) such that betainc(a,b,lo) <= p <= betainc(a,b,hi).
Source code in probjax/utils/special/betaincinv.py
betaincinv
¶
Inverse of the regularized incomplete beta function: returns x in [0,1] s.t. betainc(a,b,x) = p.
a, b > 0 p in [0,1]
max_halley_steps, max_bisection_steps control solver refinement; they are treated as static "tuning knobs", not differentiable inputs.
Source code in probjax/utils/special/betaincinv.py
probjax.utils.gammaincinv
¶
gammaincinv
¶
Inverse of the regularized lower incomplete gamma function. Solves for x >= 0 such that gammainc(a, x) = p.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
ndarray
|
Shape parameter (a > 0). |
required |
p
|
ndarray
|
Probability in [0, 1]. |
required |
Returns:
| Type | Description |
|---|---|
|
jnp.ndarray: x in [0, ∞) satisfying gammainc(a, x) = p. |
Source code in probjax/utils/special/gammaincinv.py
probjax.utils.digammainv
¶
digammainv
¶
Inverse of the digamma function using Newton's method with asymptotic approximations.
Implementation follows the approach described in the literature: For Ψ(x) = y, use Newton's method with the update: x_new = x_old - (Ψ(x) - y) / Ψ'(x)
NOTE: Digamm is only invertible for x > 0. and this function assumes that y is in the domain of invertibility.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
Union[float, ndarray]
|
The value to find the inverse digamma for |
required |
maxiter
|
int
|
Maximum number of Newton iterations (5 iterations typically sufficient for 14 digits) |
5
|
tol
|
float
|
Tolerance for convergence |
1e-14
|
Returns:
| Type | Description |
|---|---|
Union[float, ndarray]
|
x such that digamma(x) = y |
Source code in probjax/utils/special/digammainv.py
Interpolation and linear algebra¶
probjax.utils.linear_interpolation
¶
Linear interpolation function for a given set of points (ts, ys). Here ts must be a one dimensional sorted array and ys can be any array with the same length as ts on axis 0. Outside of the data range, the function returns the value of the nearest data point.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ts
|
Array
|
Time points |
required |
ys
|
Array
|
Values at time points |
required |
Returns:
| Type | Description |
|---|---|
Callable[[Float], Array]
|
Callable[[Float], Array]: Interpolation function that can be evaluated at any |
Callable[[Float], Array]
|
time point. |
Source code in probjax/utils/interpolation.py
probjax.utils.polynomial_interpolation
¶
Polynomial interpolation function for a given set of points (ts, ys). Here ts must be a one dimensional sorted array and ys can be any array with the same length as ts on axis 0.
The interpolation is done using a polynomial of degree 'degree'. The window parameter can be used to limit the range of data points used for interpolation. If window is None, the interpolation is done using all the data points.
Outside of the data range, the function does return the value of the
interpolant.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ts
|
Array
|
description |
required |
ys
|
Array
|
description |
required |
degree
|
Int
|
description. Defaults to 3. |
3
|
window
|
Optional[Int]
|
description. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
_type_ |
Callable[[Float], Array]
|
description |
Source code in probjax/utils/interpolation.py
probjax.utils.cholesky_update
¶
Update the Cholesky decomposition of a matrix after a rank-1 update i.e.
C = L @ L.T + multiplier * u @ u.T
Args: L: A [D, D] lower triangular matrix, the Cholesky factor of the original matrix. u: A [D,] vector, the update vector.
Returns: The updated [D, D] lower triangular matrix.
Source code in probjax/utils/linalg.py
probjax.utils.mv_diag_or_dense
¶
Dot product of a diagonal matrix and a dense matrix
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
A
|
Array
|
Diagonal matrix |
required |
B
|
Array
|
Dense matrix |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Array |
Array
|
Dot product |