-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add conditional mean and cov * Change log_prob test to asymmetric A * Doc
- Loading branch information
1 parent
2a43ef6
commit ea286e6
Showing
5 changed files
with
141 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import jax | ||
from jax import numpy as jnp | ||
|
||
import thermox | ||
|
||
|
||
def test_mean_and_cov(): | ||
jax.config.update("jax_enable_x64", True) | ||
dim = 2 | ||
t = 1.0 | ||
|
||
A = jnp.array([[3, 2.5], [2, 4.0]]) | ||
b = jax.random.normal(jax.random.PRNGKey(1), (dim,)) | ||
x0 = jax.random.normal(jax.random.PRNGKey(2), (dim,)) | ||
D = 2 * jnp.eye(dim) | ||
|
||
mean = thermox.conditional.mean(t, x0, A, b, D) | ||
samples = jax.vmap( | ||
lambda k: thermox.sample(k, jnp.array([0.0, t]), x0, A, b, D)[-1] | ||
)(jax.random.split(jax.random.PRNGKey(0), 1000000)) | ||
assert mean.shape == (dim,) | ||
assert jnp.allclose(mean, jnp.mean(samples, axis=0), atol=1e-2) | ||
|
||
cov = thermox.conditional.covariance(t, A, D) | ||
assert cov.shape == (dim, dim) | ||
assert jnp.allclose(cov, jnp.cov(samples.T), atol=1e-3) | ||
|
||
mean_and_cov = thermox.conditional.mean_and_covariance(t, x0, A, b, D) | ||
assert mean_and_cov[0].shape == (dim,) | ||
assert mean_and_cov[1].shape == (dim, dim) | ||
assert jnp.allclose(mean_and_cov[0], mean, atol=1e-5) | ||
assert jnp.allclose(mean_and_cov[1], cov, atol=1e-5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
from jax import numpy as jnp | ||
from jax import Array | ||
|
||
from thermox.utils import ( | ||
ProcessedDriftMatrix, | ||
ProcessedDiffusionMatrix, | ||
handle_matrix_inputs, | ||
) | ||
from thermox.sampler import expm_vp | ||
|
||
|
||
def mean( | ||
t: float, | ||
x0: Array, | ||
A: Array | ProcessedDriftMatrix, | ||
b: Array, | ||
D: Array | ProcessedDiffusionMatrix, | ||
) -> Array: | ||
"""Computes the mean of p(x_t | x_0) | ||
For x_t evolving according to the SDE: | ||
dx = - A * (x - b) dt + sqrt(D) dW | ||
Args: | ||
ts: Times at which samples are collected. Includes time for x0. | ||
x0: Initial state of the process. | ||
A: Drift matrix (Array or thermox.ProcessedDriftMatrix). | ||
Note: If a thermox.ProcessedDriftMatrix instance is used as input, | ||
must be the transformed drift matrix, A_y, given by thermox.preprocess, | ||
not thermox.utils.preprocess_drift_matrix. | ||
b: Drift displacement vector. | ||
D: Diffusion matrix (Array or thermox.ProcessedDiffusionMatrix). | ||
""" | ||
A_y, D = handle_matrix_inputs(A, D) | ||
|
||
y0 = D.sqrt_inv @ (x0 - b) | ||
return b + D.sqrt @ expm_vp(A_y, y0, t) | ||
|
||
|
||
def covariance( | ||
t: float, | ||
A: Array | ProcessedDriftMatrix, | ||
D: Array | ProcessedDiffusionMatrix, | ||
) -> Array: | ||
"""Computes the covariance of p(x_t | x_0) | ||
For x evolving according to the SDE: | ||
dx = - A * (x - b) dt + sqrt(D) dW | ||
Args: | ||
ts: Times at which samples are collected. Includes time for x0. | ||
A: Drift matrix (Array or thermox.ProcessedDriftMatrix). | ||
Note: If a thermox.ProcessedDriftMatrix instance is used as input, | ||
must be the transformed drift matrix, A_y, given by thermox.preprocess, | ||
not thermox.utils.preprocess_drift_matrix. | ||
D: Diffusion matrix (Array or thermox.ProcessedDiffusionMatrix). | ||
""" | ||
A_y, D = handle_matrix_inputs(A, D) | ||
|
||
identity_diffusion_cov = ( | ||
A_y.sym_eigvecs | ||
@ jnp.diag((1 - jnp.exp(-2 * A_y.sym_eigvals * t)) / (2 * A_y.sym_eigvals)) | ||
@ A_y.sym_eigvecs.T | ||
) | ||
return D.sqrt @ identity_diffusion_cov @ D.sqrt.T | ||
|
||
|
||
def mean_and_covariance( | ||
t: float, | ||
x0: Array, | ||
A: Array | ProcessedDriftMatrix, | ||
b: Array, | ||
D: Array | ProcessedDiffusionMatrix, | ||
) -> tuple[Array, Array]: | ||
"""Computes the mean and covariance of p(x_t | x_0) | ||
For x evolving according to the SDE: | ||
dx = - A * (x - b) dt + sqrt(D) dW | ||
Args: | ||
ts: Times at which samples are collected. Includes time for x0. | ||
x0: Initial state of the process. | ||
A: Drift matrix (Array or thermox.ProcessedDriftMatrix). | ||
Note: If a thermox.ProcessedDriftMatrix instance is used as input, | ||
must be the transformed drift matrix, A_y, given by thermox.preprocess, | ||
not thermox.utils.preprocess_drift_matrix. | ||
b: Drift displacement vector. | ||
D: Diffusion matrix (Array or thermox.ProcessedDiffusionMatrix). | ||
""" | ||
A, D = handle_matrix_inputs(A, D) | ||
mean_val = mean(t, x0, A, b, D) | ||
covariance_val = covariance(t, A, D) | ||
return mean_val, covariance_val |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters