Skip to content

Stats¤

The zodiax.stats module provides statistical helpers for z-scores, log-likelihoods, chi-squared values, degrees of freedom, and covariance-derived quantities.

zodiax.stats ¤

calc_entropy(cov) ¤

Calculates the entropy of a covariance matrix.

Parameters:

Name Type Description Default
cov Array

The covariance matrix to calculate the entropy of.

required

Returns:

Name Type Description
entropy Array

The entropy of the covariance matrix.

Source code in zodiax/stats.py
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
def calc_entropy(cov: Array) -> Array:
    """
    Calculates the entropy of a covariance matrix.

    Parameters
    ----------
    cov : Array
        The covariance matrix to calculate the entropy of.

    Returns
    -------
    entropy : Array
        The entropy of the covariance matrix.
    """
    sign, logdet = np.linalg.slogdet(cov)
    return 0.5 * (np.log(2 * np.pi * np.e) + (sign * logdet))

check_positive_semi_definite(mat) ¤

Checks if a matrix is positive semi-definite.

Parameters:

Name Type Description Default
mat Array

The matrix to check.

required

Returns:

Name Type Description
is_positive_semi_definite bool

True if all eigenvalues are non-negative and the matrix contains no NaN values, otherwise False.

Source code in zodiax/stats.py
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
def check_positive_semi_definite(mat: Array) -> bool:
    """
    Checks if a matrix is positive semi-definite.

    Parameters
    ----------
    mat : Array
        The matrix to check.

    Returns
    -------
    is_positive_semi_definite : bool
        ``True`` if all eigenvalues are non-negative and the matrix contains no
        NaN values, otherwise ``False``.
    """
    return lax.cond(
        np.isnan(mat).any(),
        lambda x: False,
        lambda x: np.all(np.linalg.eigvals(mat) >= 0),
        mat,
    )

check_symmetric(mat) ¤

Checks if a matrix is symmetric.

Parameters:

Name Type Description Default
mat Array

The matrix to check.

required

Returns:

Name Type Description
is_symmetric bool

True if the matrix is symmetric within numerical tolerance, otherwise False.

Source code in zodiax/stats.py
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
def check_symmetric(mat: Array) -> bool:
    """
    Checks if a matrix is symmetric.

    Parameters
    ----------
    mat : Array
        The matrix to check.

    Returns
    -------
    is_symmetric : bool
        ``True`` if the matrix is symmetric within numerical tolerance,
        otherwise ``False``.
    """
    return np.allclose(mat, mat.T)

chi2(x, mean, std) ¤

Calculates the chi-squared statistic from values, means, and standard deviations.

Parameters:

Name Type Description Default
x Array

The observed values.

required
mean Array

The expected mean values.

required
std Array

The standard deviations of the observations.

required

Returns:

Name Type Description
chi2 Array

The chi-squared statistic.

Source code in zodiax/stats.py
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
def chi2(x: Array, mean: Array, std: Array) -> Array:
    """
    Calculates the chi-squared statistic from values, means, and standard deviations.

    Parameters
    ----------
    x : Array
        The observed values.
    mean : Array
        The expected mean values.
    std : Array
        The standard deviations of the observations.

    Returns
    -------
    chi2 : Array
        The chi-squared statistic.
    """
    return np.square(z_score(x, mean, std)).sum()

chi2r(x, mean, std, ddof) ¤

Calculates the reduced chi-squared statistic.

Parameters:

Name Type Description Default
x Array

The observed values.

required
mean Array

The expected mean values.

required
std Array

The standard deviations of the observations.

required
ddof int

The degrees of freedom used to normalise the chi-squared value.

required

Returns:

Name Type Description
chi2r Array

The reduced chi-squared statistic.

Source code in zodiax/stats.py
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
def chi2r(x: Array, mean: Array, std: Array, ddof: int) -> Array:
    """
    Calculates the reduced chi-squared statistic.

    Parameters
    ----------
    x : Array
        The observed values.
    mean : Array
        The expected mean values.
    std : Array
        The standard deviations of the observations.
    ddof : int
        The degrees of freedom used to normalise the chi-squared value.

    Returns
    -------
    chi2r : Array
        The reduced chi-squared statistic.
    """
    return chi2(x, mean, std) / ddof

chi2r_from_z(z_score, ddof) ¤

Calculates the reduced chi-squared statistic directly from z-scores.

Parameters:

Name Type Description Default
z_score Array

The z-score values.

required
ddof int

The degrees of freedom used to normalise the chi-squared value.

required

Returns:

Name Type Description
chi2r Array

The reduced chi-squared statistic.

Source code in zodiax/stats.py
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
def chi2r_from_z(z_score: Array, ddof: int) -> Array:
    """
    Calculates the reduced chi-squared statistic directly from z-scores.

    Parameters
    ----------
    z_score : Array
        The z-score values.
    ddof : int
        The degrees of freedom used to normalise the chi-squared value.

    Returns
    -------
    chi2r : Array
        The reduced chi-squared statistic.
    """
    return np.square(z_score).sum() / ddof

ddof(params, data) ¤

Calculates the degrees of freedom as the difference between flattened data size and flattened parameter size.

Parameters:

Name Type Description Default
params Array

The model parameters, provided as a pytree.

required
data Array

The observed data, provided as a pytree.

required

Returns:

Name Type Description
ddof int

The number of degrees of freedom, computed as flatten(data).size - flatten(params).size.

Source code in zodiax/stats.py
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
def ddof(params: Array, data: Array) -> int:
    """
    Calculates the degrees of freedom as the difference between flattened data
    size and flattened parameter size.

    Parameters
    ----------
    params : Array
        The model parameters, provided as a pytree.
    data : Array
        The observed data, provided as a pytree.

    Returns
    -------
    ddof : int
        The number of degrees of freedom, computed as
        ``flatten(data).size - flatten(params).size``.
    """
    X, _ = ravel_pytree(params)
    Y, _ = ravel_pytree(data)
    return Y.size - X.size

gauss_hessian(J, cov=None) ¤

Calculates the Gauss-Newton Hessian approximation under a Gaussian likelihood model.

Parameters:

Name Type Description Default
J Array

The Jacobian matrix.

required
cov Array

The covariance matrix used to weight the Jacobian. If None, the identity weighting is assumed.

None

Returns:

Name Type Description
hessian Array

The Gauss-Newton Hessian approximation, either J.T @ J or J.T @ (cov^{-1} @ J).

Source code in zodiax/stats.py
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
def gauss_hessian(J: Array, cov: Array = None) -> Array:
    """
    Calculates the Gauss-Newton Hessian approximation under a Gaussian likelihood model.

    Parameters
    ----------
    J : Array
        The Jacobian matrix.
    cov : Array, optional
        The covariance matrix used to weight the Jacobian. If ``None``, the
        identity weighting is assumed.

    Returns
    -------
    hessian : Array
        The Gauss-Newton Hessian approximation, either ``J.T @ J`` or
        ``J.T @ (cov^{-1} @ J)``.
    """
    # Gauss-Newton hessian approximation under the assumption of a multivariate normal
    if cov is None:
        return J.T @ J
    return J.T @ (np.linalg.inv(cov) @ J)

loglike(x, mean, std) ¤

Calculates the element-wise log-likelihood under a univariate normal distribution.

Parameters:

Name Type Description Default
x Array

The observed values.

required
mean Array

The mean of the normal distribution.

required
std Array

The standard deviation of the normal distribution.

required

Returns:

Name Type Description
loglike Array

The element-wise log-likelihood values.

Source code in zodiax/stats.py
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
def loglike(x: Array, mean: Array, std: Array) -> Array:
    """
    Calculates the element-wise log-likelihood under a univariate normal distribution.

    Parameters
    ----------
    x : Array
        The observed values.
    mean : Array
        The mean of the normal distribution.
    std : Array
        The standard deviation of the normal distribution.

    Returns
    -------
    loglike : Array
        The element-wise log-likelihood values.
    """
    return stats.norm.logpdf(x, mean, std)

mv_loglike(x, mean, cov) ¤

Calculates the log-likelihood under a multivariate normal distribution.

Parameters:

Name Type Description Default
x Array

The observed vector.

required
mean Array

The mean vector of the multivariate normal distribution.

required
cov Array

The covariance matrix of the multivariate normal distribution.

required

Returns:

Name Type Description
mv_loglike Array

The multivariate normal log-likelihood value.

Source code in zodiax/stats.py
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
def mv_loglike(x: Array, mean: Array, cov: Array) -> Array:
    """
    Calculates the log-likelihood under a multivariate normal distribution.

    Parameters
    ----------
    x : Array
        The observed vector.
    mean : Array
        The mean vector of the multivariate normal distribution.
    cov : Array
        The covariance matrix of the multivariate normal distribution.

    Returns
    -------
    mv_loglike : Array
        The multivariate normal log-likelihood value.
    """
    return stats.multivariate_normal.logpdf(x, mean, cov)

mv_z_score(x, mean, cov) ¤

Calculates the squared Mahalanobis distance \((\mu - x)^ op\Sigma^{-1}(\mu - x)\) of a value given a mean and covariance matrix.

Parameters:

Name Type Description Default
x Array

The value to calculate the multivariate z-score of.

required
mean Array

The mean vector to use in the calculation.

required
cov Array

The covariance matrix to use in the calculation.

required

Returns:

Name Type Description
mv_z_score Array

The squared Mahalanobis distance of the value.

Source code in zodiax/stats.py
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def mv_z_score(x: Array, mean: Array, cov: Array) -> Array:
    """
    Calculates the squared Mahalanobis distance $(\mu - x)^\top\Sigma^{-1}(\mu - x)$
    of a value given a mean and covariance matrix.

    Parameters
    ----------
    x : Array
        The value to calculate the multivariate z-score of.
    mean : Array
        The mean vector to use in the calculation.
    cov : Array
        The covariance matrix to use in the calculation.

    Returns
    -------
    mv_z_score : Array
        The squared Mahalanobis distance of the value.
    """
    # res = x - mean
    res = mean - x
    return res @ np.linalg.inv(cov) @ res

z_score(x, mean, std) ¤

Calculates the z-score \((\mu - x) / \sigma\) of a value given a mean and standard deviation.

Parameters:

Name Type Description Default
x Array

The value to calculate the z-score of.

required
mean Array

The mean to use in the z-score calculation.

required
std Array

The standard deviation to use in the z-score calculation.

required

Returns:

Name Type Description
z_score Array

The z-score of the value.

Source code in zodiax/stats.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
def z_score(x: Array, mean: Array, std: Array) -> Array:
    """
    Calculates the z-score $(\mu - x) / \sigma$ of a value given a mean and standard
    deviation.

    Parameters
    ----------
    x : Array
        The value to calculate the z-score of.
    mean : Array
        The mean to use in the z-score calculation.
    std : Array
        The standard deviation to use in the z-score calculation.

    Returns
    -------
    z_score : Array
        The z-score of the value.
    """
    return (mean - x) / std