|
| 1 | +from jax import numpy as jnp |
| 2 | +from jax import Array |
| 3 | + |
| 4 | +from thermox.utils import ( |
| 5 | + ProcessedDriftMatrix, |
| 6 | + ProcessedDiffusionMatrix, |
| 7 | + handle_matrix_inputs, |
| 8 | +) |
| 9 | +from thermox.sampler import expm_vp |
| 10 | + |
| 11 | + |
| 12 | +def mean( |
| 13 | + t: float, |
| 14 | + x0: Array, |
| 15 | + A: Array | ProcessedDriftMatrix, |
| 16 | + b: Array, |
| 17 | + D: Array | ProcessedDiffusionMatrix, |
| 18 | +) -> Array: |
| 19 | + """Computes the mean of p(x_t | x_0) |
| 20 | +
|
| 21 | + For x_t evolving according to the SDE: |
| 22 | +
|
| 23 | + dx = - A * (x - b) dt + sqrt(D) dW |
| 24 | +
|
| 25 | + Args: |
| 26 | + ts: Times at which samples are collected. Includes time for x0. |
| 27 | + x0: Initial state of the process. |
| 28 | + A: Drift matrix (Array or thermox.ProcessedDriftMatrix). |
| 29 | + Note: If a thermox.ProcessedDriftMatrix instance is used as input, |
| 30 | + must be the transformed drift matrix, A_y, given by thermox.preprocess, |
| 31 | + not thermox.utils.preprocess_drift_matrix. |
| 32 | + b: Drift displacement vector. |
| 33 | + D: Diffusion matrix (Array or thermox.ProcessedDiffusionMatrix). |
| 34 | +
|
| 35 | + """ |
| 36 | + A_y, D = handle_matrix_inputs(A, D) |
| 37 | + |
| 38 | + y0 = D.sqrt_inv @ (x0 - b) |
| 39 | + return b + D.sqrt @ expm_vp(A_y, y0, t) |
| 40 | + |
| 41 | + |
| 42 | +def covariance( |
| 43 | + t: float, |
| 44 | + A: Array | ProcessedDriftMatrix, |
| 45 | + D: Array | ProcessedDiffusionMatrix, |
| 46 | +) -> Array: |
| 47 | + """Computes the covariance of p(x_t | x_0) |
| 48 | +
|
| 49 | + For x evolving according to the SDE: |
| 50 | +
|
| 51 | + dx = - A * (x - b) dt + sqrt(D) dW |
| 52 | +
|
| 53 | + Args: |
| 54 | + ts: Times at which samples are collected. Includes time for x0. |
| 55 | + A: Drift matrix (Array or thermox.ProcessedDriftMatrix). |
| 56 | + Note: If a thermox.ProcessedDriftMatrix instance is used as input, |
| 57 | + must be the transformed drift matrix, A_y, given by thermox.preprocess, |
| 58 | + not thermox.utils.preprocess_drift_matrix. |
| 59 | + D: Diffusion matrix (Array or thermox.ProcessedDiffusionMatrix). |
| 60 | + """ |
| 61 | + A_y, D = handle_matrix_inputs(A, D) |
| 62 | + |
| 63 | + identity_diffusion_cov = ( |
| 64 | + A_y.sym_eigvecs |
| 65 | + @ jnp.diag((1 - jnp.exp(-2 * A_y.sym_eigvals * t)) / (2 * A_y.sym_eigvals)) |
| 66 | + @ A_y.sym_eigvecs.T |
| 67 | + ) |
| 68 | + return D.sqrt @ identity_diffusion_cov @ D.sqrt.T |
| 69 | + |
| 70 | + |
| 71 | +def mean_and_covariance( |
| 72 | + t: float, |
| 73 | + x0: Array, |
| 74 | + A: Array | ProcessedDriftMatrix, |
| 75 | + b: Array, |
| 76 | + D: Array | ProcessedDiffusionMatrix, |
| 77 | +) -> tuple[Array, Array]: |
| 78 | + """Computes the mean and covariance of p(x_t | x_0) |
| 79 | +
|
| 80 | + For x evolving according to the SDE: |
| 81 | +
|
| 82 | + dx = - A * (x - b) dt + sqrt(D) dW |
| 83 | +
|
| 84 | + Args: |
| 85 | + ts: Times at which samples are collected. Includes time for x0. |
| 86 | + x0: Initial state of the process. |
| 87 | + A: Drift matrix (Array or thermox.ProcessedDriftMatrix). |
| 88 | + Note: If a thermox.ProcessedDriftMatrix instance is used as input, |
| 89 | + must be the transformed drift matrix, A_y, given by thermox.preprocess, |
| 90 | + not thermox.utils.preprocess_drift_matrix. |
| 91 | + b: Drift displacement vector. |
| 92 | + D: Diffusion matrix (Array or thermox.ProcessedDiffusionMatrix). |
| 93 | +
|
| 94 | + """ |
| 95 | + A, D = handle_matrix_inputs(A, D) |
| 96 | + mean_val = mean(t, x0, A, b, D) |
| 97 | + covariance_val = covariance(t, A, D) |
| 98 | + return mean_val, covariance_val |
0 commit comments