kinisi.diffusion#

Calculate the diffusion coefficient.

class kinisi.diffusion.Diffusion(dg)#

Bases: object

The class for the calculation of the self-diffusion coefficient.

Parameters:

dg (DataGroup) – A scipp.DataGroup object containing the relevant mean-squared displacement data and number of independent samples.

property covariance_matrix: Variable#
Returns:

The covariance matrix as a scipp object, 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 is 1e16.

  • recondition (bool) – Whether to recondition the covariance matrix. Optional, default is False.

  • 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 is True.

  • z_threshold (float) – The magnitude of the consistency statistic, consistency_z, above which a warning is raised. Optional, default is 3.0.

  • fit_intercept (bool) – Whether to fit an intercept. Optional, default is True.

  • n_samples (int) – The number of MCMC samples to take. Optional, default is 1000.

  • n_walkers (int) – The number of walkers to use in the MCMC. Optional, default is 32.

  • n_burn (int) – The number of burn-in samples to discard. Optional, default is 500.

  • n_thin (int) – The thinning factor for the MCMC samples. Optional, default is 10.

  • progress (bool) – Whether to show the progress bar. Optional, default is True.

  • random_state (RandomState) – The random state to use for the MCMC. Optional, default is None.

Raises:

ValueError – If start_dt leaves fewer than two time intervals, or the mean-squared displacements from start_dt onwards 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_z instead. None if 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), nan if the covariance matrix is too close to singular for the statistic to be computed, and None if the check has not been run.

Returns:

The consistency statistic.

property D: Variable#
Returns:

The diffusion coefficient as a scipp object.

property D_J: Variable#
Returns:

The jump diffusion coefficient as a scipp object.

property sigma: Variable#
Returns:

The conductivity as a scipp object, or None if it has not been calculated.

compute_covariance_matrix()#

Compute the covariance matrix for the diffusion coefficient calculation.

Return type:

Variable

Returns:

A scipp object 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 is 256.

  • progress (bool) – Show tqdm progress for sampling. Optional, default is True.

Return type:

Variable

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_max based 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 distribution

  • lambda_ (float) – lambda parameter

  • sigma (float) – standard deviation of the distribution

Return type:

ndarray