๐งช ifnt.testing#
ifnt.testing wraps common assertions from numpy.testing and implements stochastic tests through assert_samples_close().
- ifnt.testing.assert_allfinite(x: Array) None#
- ifnt.testing.assert_allfinite(x: dict, *args, **kwargs)
Assert all elements are finite.
- Parameters:
x โ Array or a dictionary of arrays to check.
Example
>>> x = jnp.arange(3) >>> ifnt.testing.assert_allfinite(x) >>> ifnt.testing.assert_allfinite(jnp.log(x)) Traceback (most recent call last): ... AssertionError: Array with shape `(3,)` has 1 non-finite elements. >>> ifnt.testing.assert_allfinite({"a": x, "b": jnp.log(x)}) Traceback (most recent call last): ... AssertionError: <function assert_allfinite at 0x...> failed for key `b`. Array with shape `(3,)` has 1 non-finite elements.
- ifnt.testing.assert_circulant(x: Array, rtol: float = 1e-07, atol: float = 0.0) None#
Assert that a matrix is circulant, i.e., has periodic boundary conditions and the same values on all diagonals.
- Parameters:
x โ Matrix or batch of matrices to check.
rtol โ Relative tolerance.
atol โ Absolute tolerance.
Example
>>> c = jsp.linalg.toeplitz(jnp.array([2, 1, 0, 1])) >>> c Array([[2, 1, 0, 1], [1, 2, 1, 0], [0, 1, 2, 1], [1, 0, 1, 2]], dtype=int32) >>> ifnt.testing.assert_circulant(c) >>> >>> t = jsp.linalg.toeplitz(jnp.arange(3)) >>> t Array([[0, 1, 2], [1, 0, 1], [2, 1, 0]], dtype=int32) >>> ifnt.testing.assert_circulant(t) Traceback (most recent call last): ... AssertionError: Arrays are not equal Matrix is not circulant. Mismatched elements: 1 / 1 (100%) ACTUAL: array(False) DESIRED: array(True)
- ifnt.testing.assert_positive_definite(x: Array, atol: float = 0.0) None#
- ifnt.testing.assert_positive_definite(x: dict, *args, **kwargs)
Assert that a matrix is positive definite.
- Parameters:
x โ Matrix to check.
atol โ Absolute tolerance for the smallest eigenvalue, i.e., the smallest eigenvalue must not be smaller than
-atol.
Example
>>> x = jnp.eye(3) >>> ifnt.testing.assert_positive_definite(x) >>> ifnt.testing.assert_positive_definite(-x) Traceback (most recent call last): ... AssertionError: Matrices are not positive definite: min(eigenvalues) = -1.0.
- ifnt.testing.assert_samples_close(samples: Array, expected: Array, q: float = 0.001, on_weak: str = 'raise', rtol: float = 1e-07, atol: float = 0.0) None#
Assert that i.i.d samples are close to a target using a normal approximation of the sample mean. If samples are not scalars, a Bonferroni correction is applied to the tail probability
qto avoid incorrect rejection of the null hypothesis (thatexpectedis the mean of the distribution that generatedsamples) because of sampling noise. The assertion also passes without failure if all samples are close to the target as determined byrtolandatol.- Parameters:
samples โ Samples with shape
(n_samples, ...).expected โ Target with shape
(...).q โ Tail probability where to reject the null that
expectedis the mean of the distribution that generatedsamples.on_weak โ Action if the sample size is too small to confidently reject the null hypothesis.
rtol โ Relative tolerance.
atol โ Absolute tolerance.
Example
>>> samples = jax.random.normal(jax.random.key(7), (1000,)) >>> ifnt.testing.assert_samples_close(samples, 0.0) >>> ifnt.testing.assert_samples_close(samples, 1.0) Traceback (most recent call last): ... AssertionError: Sample mean: 0.01749... with standard error: 0.03088... is not consistent with the expected value: 1.0 The z-score is: 31.81... >>> ifnt.testing.assert_samples_close(samples, -1.0) Traceback (most recent call last): ... AssertionError: Sample mean: 0.01749... with standard error: 0.03088... is not consistent with the expected value: -1.0 The z-score is: -32.94...
- ifnt.testing.assert_shape(x: Array, shape: tuple) None#
Assert an array has the desired shape.
- Parameters:
ACTUAL โ array to check.
shape โ Desired shape.
- Returns:
Input array
x.
Example
>>> ifnt.testing.assert_shape(jnp.arange(3), (3,)) >>> ifnt.testing.assert_shape(jnp.arange(3), ()) Traceback (most recent call last): ... AssertionError: Expected shape `()` but got `(3,)`.
- ifnt.testing.assert_toeplitz(x: Array, rtol: float = 1e-07, atol: float = 0.0) None#
Assert that a matrix is Toeplitz, i.e., has the same value on all diagonals.
- Parameters:
x โ Matrix or batch of matrices to check.
rtol โ Relative tolerance.
atol โ Absolute tolerance.
Example
>>> t = jsp.linalg.toeplitz(jnp.arange(3)) >>> t Array([[0, 1, 2], [1, 0, 1], [2, 1, 0]], dtype=int32) >>> ifnt.testing.assert_toeplitz(t) >>> a = t.at[0, 0].set(7) >>> a Array([[7, 1, 2], [1, 0, 1], [2, 1, 0]], dtype=int32) >>> ifnt.testing.assert_toeplitz(a) Traceback (most recent call last): ... AssertionError: Arrays are not equal Matrix is not Toeplitz. Mismatched elements: 1 / 1 (100%) ACTUAL: array(False) DESIRED: array(True)
- ifnt.testing.is_circulant(x: Array, rtol: float = 1e-07, atol: float = 0.0) Array#
Determine if a matrix is circulant, i.e., has periodic boundary conditions and the same values on all diagonals.
- Parameters:
x โ Matrix or batch of matrices to check with shape (โฆ, n, m).
rtol โ Relative tolerance.
atol โ Absolute tolerance.
- Returns:
Boolean array with shape (โฆ) indicating whether
xis circulant.
Example
>>> c = jsp.linalg.toeplitz(jnp.array([2, 1, 0, 1])) >>> c Array([[2, 1, 0, 1], [1, 2, 1, 0], [0, 1, 2, 1], [1, 0, 1, 2]], dtype=int32) >>> ifnt.testing.is_circulant(c) Array(True, dtype=bool) >>> >>> t = jsp.linalg.toeplitz(jnp.arange(3)) >>> t Array([[0, 1, 2], [1, 0, 1], [2, 1, 0]], dtype=int32) >>> ifnt.testing.is_circulant(t) Array(False, dtype=bool)
- ifnt.testing.is_toeplitz(x: Array, rtol: float = 1e-07, atol: float = 0.0) Array#
Determine if a matrix is Toeplitz, i.e., has the same value on all diagonals.
- Parameters:
x โ Matrix or batch of matrices to check with shape (โฆ, n, m).
rtol โ Relative tolerance.
atol โ Absolute tolerance.
- Returns:
Boolean array with shape (โฆ) indicating whether
xis Toeplitz.
Example
>>> t = jsp.linalg.toeplitz(jnp.arange(3)) >>> t Array([[0, 1, 2], [1, 0, 1], [2, 1, 0]], dtype=int32) >>> ifnt.testing.is_toeplitz(t) Array(True, dtype=bool) >>> a = t.at[0, 0].set(7) >>> a Array([[7, 1, 2], [1, 0, 1], [2, 1, 0]], dtype=int32) >>> ifnt.testing.is_toeplitz(a) Array(False, dtype=bool)