Compute Diffusion#
diffusion
#
| FUNCTION | DESCRIPTION |
|---|---|
local_diffusion |
Estimate position-dependent diffusion from trajectory displacements. |
diffusion_tensor |
Compute diffusion tensor \(D = J G^{-1} J^T\) from model Jacobian. |
log_det_diffusion_tensor |
Compute log determinant of diffusion tensor \(D = J G^{-1} J^T\). |
inverse_diffusion_tensor |
Compute inverse metric tensor from diffusion tensor \(D = J G^{-1} J^T\). |
decompose_diffusion_tensor |
Compute inverse and log-determinant of a diffusion tensor via single |
Functions#
local_diffusion(X: torch.Tensor, dt: float = 1.0, bandwidth: float | None = None) -> torch.Tensor
#
Estimate position-dependent diffusion from trajectory displacements.
Uses the short-time displacement covariance to estimate the diagonal diffusion tensor at each frame:
\(D_j(x_i) = \frac{\sum_k K(x_i - x_k)\, (\Delta x_{k,j})^2} {2\, \Delta t \sum_k K(x_i - x_k)}\)
where \(K\) is a Gaussian kernel and \(\Delta x_k = x_{k+1} - x_k\). This exploits the fact that the displacement covariance at short times is \(\operatorname{Cov}(\Delta x \mid x) = 2 k_BT D_0(x)\, \Delta t + O(\Delta t^2)\), so the diffusion is determined purely by the fluctuations, independent of the drift (forces).
The result can be passed as metric_inv to compute_diffusion_tensor
for the Fixman-corrected invariant free energy.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Trajectory positions with shape (N, d). Frames must be consecutive
(constant timestep
TYPE:
|
dt
|
Timestep between consecutive frames.
TYPE:
|
bandwidth
|
Gaussian kernel bandwidth for smoothing. If
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Per-sample diagonal diffusion tensor with shape (N, d). The last frame is assigned the same value as the second-to-last frame since no forward displacement is available. |
Source code in spectre/compute/diffusion.py
diffusion_tensor(X: torch.Tensor, model: torch.nn.Module, mode: Literal['forward', 'reverse', 'auto'] = 'auto', metric_inv: torch.Tensor | None = None) -> torch.Tensor
#
Compute diffusion tensor \(D = J G^{-1} J^T\) from model Jacobian.
When metric_inv is None, assumes a Euclidean metric (\(G = I\)) and
computes \(D = J J^T\). When provided, computes the Fixman-corrected
diffusion tensor \(D = J G^{-1} J^T\), where \(G^{-1}\) is the inverse metric
of the input coordinate space (e.g., inverse mass matrix).
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data tensor with shape (N, d).
TYPE:
|
model
|
Neural network model mapping R^d -> R^k.
TYPE:
|
mode
|
Autodiff mode for Jacobian computation. Forward mode scales with input
dimension
TYPE:
|
metric_inv
|
Inverse metric tensor of the input coordinate space. Supports:
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Diffusion tensor with shape (N,) for k=1 or (N, k, k) for k>1. |
Source code in spectre/compute/diffusion.py
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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | |
log_det_diffusion_tensor(X: torch.Tensor, model: torch.nn.Module, mode: Literal['forward', 'reverse', 'auto'] = 'auto', metric_inv: torch.Tensor | None = None) -> torch.Tensor
#
Compute log determinant of diffusion tensor \(D = J G^{-1} J^T\).
Computes the Jacobian of a neural network model and calculates the log determinant of the diffusion tensor for invariant free energy.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data tensor with shape (N, d).
TYPE:
|
model
|
Neural network model mapping R^d -> R^k.
TYPE:
|
mode
|
Autodiff mode for Jacobian computation. Forward mode scales with input
dimension
TYPE:
|
metric_inv
|
Inverse metric tensor of the input coordinate space. See
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Log determinant of diffusion tensor with shape (N,). |
Examples:
>>> import torch
>>> model = torch.nn.Linear(3, 2)
>>> X = torch.randn(10, 3)
>>> log_det = compute_log_det_diffusion_tensor(X, model)
>>> log_det.shape
torch.Size([10])
Source code in spectre/compute/diffusion.py
inverse_diffusion_tensor(X: torch.Tensor, model: torch.nn.Module, mode: Literal['forward', 'reverse', 'auto'] = 'auto', metric_inv: torch.Tensor | None = None) -> torch.Tensor
#
Compute inverse metric tensor from diffusion tensor \(D = J G^{-1} J^T\).
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input tensor with shape (n_samples, in_features).
TYPE:
|
model
|
Neural network model mapping R^d -> R^k.
TYPE:
|
mode
|
Autodiff mode for Jacobian computation. Forward mode scales with input
dimension
TYPE:
|
metric_inv
|
Inverse metric tensor of the input coordinate space. See
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Inverse metric tensors with shape (n_samples, out_features, out_features). |
Source code in spectre/compute/diffusion.py
decompose_diffusion_tensor(D: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]
#
Compute inverse and log-determinant of a diffusion tensor via single Cholesky factorization.
Avoids redundant computation when both \(D^{-1}\) and \(\log\det D\) are needed (e.g., Mahalanobis distance and invariant kernel weighting). For the matrix case (\(k > 1\)), a single Cholesky factor \(L\) yields \(D^{-1}\) via back-substitution and \(\log\det D = 2\sum\log \operatorname{diag}(L)\).
| PARAMETER | DESCRIPTION |
|---|---|
D
|
Diffusion tensor with shape
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[Tensor, Tensor]
|
|