kinisi.diffusion#
Calculate the diffusion coefficient.
- class kinisi.diffusion.Diffusion(dg)#
Bases:
objectThe class for the calculation of the self-diffusion coefficient.
- Parameters:
dg (
DataGroup) – Ascipp.DataGroupobject containing the relevant mean-squared displacement data and number of independent samples.
- property covariance_matrix: Variable#
- Returns:
The covariance matrix as a
scippobject, with dimensions of time_interval1 and time_interval2.
- bayesian_regression(start_dt, cond_max=1e+16, recondition=False, check_consistency=True, z_threshold=3.0, fit_intercept=True, n_samples=1000, n_walkers=32, n_burn=500, n_thin=10, progress=True, random_state=None)#
Perform the Bayesian regression with a linear model against the observed data.
- Parameters:
start_dt (
Variable) – The time at which the diffusion regime begins.cond_max (
float) – The maximum condition number of the covariance matrix. Optional, default is1e16.recondition (
bool) – Whether to recondition the covariance matrix. Optional, default isFalse.check_consistency (
bool) – Whether to compare the generalised least squares gradient against an ordinary least squares estimate and warn if they disagree by more than the noise in the data allows, if the covariance matrix is too close to singular for the comparison, or if the data show no increase. Optional, default isTrue.z_threshold (
float) – The magnitude of the consistency statistic,consistency_z, above which a warning is raised. Optional, default is3.0.fit_intercept (
bool) – Whether to fit an intercept. Optional, default isTrue.n_samples (
int) – The number of MCMC samples to take. Optional, default is1000.n_walkers (
int) – The number of walkers to use in the MCMC. Optional, default is32.n_burn (
int) – The number of burn-in samples to discard. Optional, default is500.n_thin (
int) – The thinning factor for the MCMC samples. Optional, default is10.progress (
bool) – Whether to show the progress bar. Optional, default isTrue.random_state (
RandomState) – The random state to use for the MCMC. Optional, default isNone.
- Raises:
ValueError – If
start_dtleaves fewer than two time intervals, or the mean-squared displacements fromstart_dtonwards are not all finite.
- property consistency_ratio: float#
The ratio of the median fitted gradient to an ordinary least squares estimate of the same straight line, with or without an intercept as fitted.
This is for information only: how far the ratio can stray from unity by chance depends on the noise in the data, so the warning raised by the consistency check uses
consistency_zinstead.Noneif the check has not been run or the ordinary least squares gradient is not positive.- Returns:
The ratio of the two gradient estimates.
- property consistency_z: float#
The difference between the generalised and ordinary least squares gradients of the fitted data, divided by the standard deviation of that difference expected from the covariance matrix.
If the covariance matrix is correct and a straight line describes the data, this follows approximately a standard normal distribution, so a magnitude well above three indicates that the covariance matrix is unreliable, that the fitted time intervals extend outside the diffusive regime, or that an intercept is needed but not fitted. Zero if the two estimators coincide (for example, for a straight line through two points),
nanif the covariance matrix is too close to singular for the statistic to be computed, andNoneif the check has not been run.- Returns:
The consistency statistic.
- property sigma: Variable#
- Returns:
The conductivity as a
scippobject, orNoneif it has not been calculated.
- compute_covariance_matrix()#
Compute the covariance matrix for the diffusion coefficient calculation.
- Return type:
- Returns:
A
scippobject containing the covariance matrix.
- posterior_predictive(n_posterior_samples=None, n_predictive_samples=256, progress=True)#
Sample the posterior predictive distribution of the mean-squared displacement in the diffusive regime. The result has dimensions of samples (of length n_posterior_samples * n_predictive_samples) and time interval (the time intervals from start_dt onwards).
- Parameters:
n_posterior_samples (
int) – Number of samples from the posterior distribution. Optional, default is the number of posterior samples.n_predictive_samples (
int) – Number of random samples per sample from the posterior distribution. Optional, default is256.progress (
bool) – Show tqdm progress for sampling. Optional, default isTrue.
- Return type:
- Returns:
Samples from the posterior predictive distribution.
- kinisi.diffusion.minimum_eigenvalue_method(cov, cond_max=1e+16)#
Implementation of the matrix reconditioning method known as the minimum eigenvalue method, as outlined in doi:10.1080/16000870.2019.1696646. This should produce a matrix with a condition number of
cond_maxbased on the eigenvalues and eigenvectors of the input matrix.- Parameters:
cov (
ndarray) – Symmetric matrix to recondition.cond_max – Expected condition number of output matrix. Optional, default is
1e16.
- Return type:
ndarray- Returns:
Reconditioned matrix.
- kinisi.diffusion.eigenvalue_clipping(cov)#
Eigenvalue clipping method for matrix reconditioning.
- Parameters:
cov (
ndarray) – Covariance matrix to recondition.- Return type:
ndarray- Returns:
Reconditioned covariance matrix.
- kinisi.diffusion.marchenkopastur(x, lambda_, sigma)#
Marchenko-Pastur distribution
- Parameters:
x (
ndarray) – points at which to evaluate the distributionlambda_ (
float) – lambda parametersigma (
float) – standard deviation of the distribution
- Return type:
ndarray