Beispiel #1
0
def test_from_err():
    # Test with default: no correlation
    mean = [10, 20, 30]
    err = [1, 2, 3]
    correlation = None
    mn = MultiNorm.from_error(mean, err, correlation)
    assert_allclose(mn.mean, mean)
    assert_allclose(mn.cov, [[1, 0, 0], [0, 4, 0], [0, 0, 9]])

    # Test with given correlation
    correlation = [[1, 0.8, 0], [0.8, 1, 0.1], [0.0, 0.1, 1]]
    mn = MultiNorm.from_error(error=err, correlation=correlation)
    assert_allclose(mn.correlation, correlation)

    with pytest.raises(ValueError):
        MultiNorm.from_error(mean)
Beispiel #2
0
def mn3():
    """Example test case with very large and small numbers.

    This is numerically challenging for several computations.
    Ideally all methods in `MultiNorm` should be able to handle
    this case and give accurate results, because it's quite common.
    """
    mean = np.array([1e-10, 1, 1e10])
    error = 1.0 * mean
    return MultiNorm.from_error(mean, error)
Beispiel #3
0
"""
Check the numpy / scipy methods we use.

Mostly the question at the moment is
how to get numerically stable `from_product`,
i.e. which matrix inverse function to use,
or how to rewrite `from_product` in a better way.
"""
import numpy as np

from multinorm import MultiNorm

mean = np.array([1e-20, 1, 1e20])
err = 1 * mean
names = ["a", "b", "c"]
mn = MultiNorm.from_error(mean, err, names=names)

print(mn)
print(mn.cov.values)

# BAD
try:
    print(mn.precision.values)
except np.linalg.LinAlgError:
    print("SINGULAR m3.precision")

# GOOD
print(np.linalg.inv(mn.cov.values))