How to use the uncertainties.covariance_matrix function in uncertainties

To help you get started, we’ve selected a few uncertainties examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github xraypy / xraylarch / lib / fitting / uncertainties / test_uncertainties.py View on Github external
# matrices_close() is used instead of _numbers_close() because
        # it compares uncertainties too:

        # Test of individual variables:
        assert matrices_close(numpy.array([x]), numpy.array([x2]))
        assert matrices_close(numpy.array([y]), numpy.array([y2]))
        assert matrices_close(numpy.array([z]), numpy.array([z2]))

        # Partial correlation test:
        assert matrices_close(numpy.array([0]), numpy.array([z2-(-3*x2+y2)]))

        # Test of the full covariance matrix:
        assert matrices_close(
            numpy.array(cov_mat),
            numpy.array(uncertainties.covariance_matrix([x2, y2, z2])))
github xraypy / xraylarch / lib / fitting / uncertainties / test_umath.py View on Github external
# in a non-linear way):
    def function(x, y):
        """
        Function that takes two NumPy arrays of the same size.
        """
        # The uncertainty due to x is about equal to the uncertainty
        # due to y:
        return 10 * x**2 - x * sin_uarray_uncert(y**3)

    x = uncertainties.ufloat((0.2, 0.01))
    y = uncertainties.ufloat((10, 0.001))
    function_result_this_module = function(x, y)
    nominal_value_this_module = function_result_this_module.nominal_value

    # Covariances "f*f", "f*x", "f*y":
    covariances_this_module = numpy.array(uncertainties.covariance_matrix(
        (x, y, function_result_this_module)))

    def monte_carlo_calc(n_samples):
        """
        Calculate function(x, y) on n_samples samples and returns the
        median, and the covariances between (x, y, function(x, y)).
        """
        # Result of a Monte-Carlo simulation:
        x_samples = numpy.random.normal(x.nominal_value, x.std_dev(),
                                        n_samples)
        y_samples = numpy.random.normal(y.nominal_value, y.std_dev(),
                                        n_samples)
        function_samples = function(x_samples, y_samples)

        cov_mat = numpy.cov([x_samples, y_samples], function_samples)
github xraypy / xraylarch / lib / fitting / uncertainties / test_uncertainties.py View on Github external
def test_covariances():
    "Covariance matrix"

    x = ufloat((1, 0.1))
    y = -2*x+10
    z = -3*x
    covs = uncertainties.covariance_matrix([x, y, z])
    # Diagonal elements are simple:
    assert _numbers_close(covs[0][0], 0.01)
    assert _numbers_close(covs[1][1], 0.04)
    assert _numbers_close(covs[2][2], 0.09)
    # Non-diagonal elements:
    assert _numbers_close(covs[0][1], -0.02)
github xraypy / xraylarch / lib / fitting / uncertainties / test_uncertainties.py View on Github external
def test_correlated_values():
        """
        Correlated variables.
        Test through the input of the (full) covariance matrix.
        """

        u = uncertainties.ufloat((1, 0.1))
        cov = uncertainties.covariance_matrix([u])
        # "1" is used instead of u.nominal_value because
        # u.nominal_value might return a float.  The idea is to force
        # the new variable u2 to be defined through an integer nominal
        # value:
        u2, = uncertainties.correlated_values([1], cov)
        expr = 2*u2  # Calculations with u2 should be possible, like with u

        ####################

        # Covariances between output and input variables:

        x = ufloat((1, 0.1))
        y = ufloat((2, 0.3))
        z = -3*x+y

        covs = uncertainties.covariance_matrix([x, y, z])
github xraypy / xraylarch / lib / fitting / uncertainties / test_uncertainties.py View on Github external
# "1" is used instead of u.nominal_value because
        # u.nominal_value might return a float.  The idea is to force
        # the new variable u2 to be defined through an integer nominal
        # value:
        u2, = uncertainties.correlated_values([1], cov)
        expr = 2*u2  # Calculations with u2 should be possible, like with u

        ####################

        # Covariances between output and input variables:

        x = ufloat((1, 0.1))
        y = ufloat((2, 0.3))
        z = -3*x+y

        covs = uncertainties.covariance_matrix([x, y, z])

        # Test of the diagonal covariance elements:
        assert matrices_close(
            numpy.array([v.std_dev()**2 for v in (x, y, z)]),
            numpy.array(covs).diagonal())

        # "Inversion" of the covariance matrix: creation of new
        # variables:
        (x_new, y_new, z_new) = uncertainties.correlated_values(
            [x.nominal_value, y.nominal_value, z.nominal_value],
            covs,
            tags = ['x', 'y', 'z'])

        # Even the uncertainties should be correctly reconstructed:
        assert matrices_close(numpy.array((x, y, z)),
                              numpy.array((x_new, y_new, z_new)))
github xraypy / xraylarch / lib / fitting / uncertainties / test_uncertainties.py View on Github external
numpy.array(covs),
            numpy.array(uncertainties.covariance_matrix([x_new, y_new, z_new])))

        assert matrices_close(
            numpy.array([z_new]), numpy.array([-3*x_new+y_new]))

        ####################

        # ... as well as functional relations:

        u = ufloat((1, 0.05))
        v = ufloat((10, 0.1))
        sum_value = u+2*v

        # Covariance matrices:
        cov_matrix = uncertainties.covariance_matrix([u, v, sum_value])

        # Correlated variables can be constructed from a covariance
        # matrix, if NumPy is available:
        (u2, v2, sum2) = uncertainties.correlated_values(
            [x.nominal_value for x in [u, v, sum_value]],
            cov_matrix)

        # matrices_close() is used instead of _numbers_close() because
        # it compares uncertainties too:
        assert matrices_close(numpy.array([u]), numpy.array([u2]))
        assert matrices_close(numpy.array([v]), numpy.array([v2]))
        assert matrices_close(numpy.array([sum_value]), numpy.array([sum2]))
        assert matrices_close(numpy.array([0]),
                              numpy.array([sum2-(u2+2*v2)]))
github xraypy / xraylarch / lib / fitting / uncertainties / test_uncertainties.py View on Github external
# The relationship between the copy of an expression and the
    # original variables should be preserved:
    t_copy = copy.copy(t)
    # Shallow copy: the variables on which t depends are not copied:
    assert x in t_copy.derivatives
    assert (uncertainties.covariance_matrix([t, z]) ==
            uncertainties.covariance_matrix([t_copy, z]))

    # However, the relationship between a deep copy and the original
    # variables should be broken, since the deep copy created new,
    # independent variables:
    t_deepcopy = copy.deepcopy(t)
    assert x not in t_deepcopy.derivatives
    assert (uncertainties.covariance_matrix([t, z]) !=
            uncertainties.covariance_matrix([t_deepcopy, z]))

    # Test of implementations with weak references:

    # Weak references: destroying a variable should never destroy the
    # integrity of its copies (which would happen if the copy keeps a
    # weak reference to the original, in its derivatives member: the
    # weak reference to the original would become invalid):
    del x

    gc.collect()

    assert y in y.derivatives.keys()
github xraypy / xraylarch / lib / fitting / uncertainties / test_uncertainties.py View on Github external
def test_correlated_values_correlation_mat():
        '''
        Tests the input of correlated value.

        Test through their correlation matrix (instead of the
        covariance matrix).
        '''

        x = ufloat((1, 0.1))
        y = ufloat((2, 0.3))
        z = -3*x+y

        cov_mat = uncertainties.covariance_matrix([x, y, z])

        std_devs = numpy.sqrt(numpy.array(cov_mat).diagonal())

        corr_mat = cov_mat/std_devs/std_devs[numpy.newaxis].T

        # We make sure that the correlation matrix is indeed diagonal:
        assert (corr_mat-corr_mat.T).max() <= 1e-15
        # We make sure that there are indeed ones on the diagonal:
        assert (corr_mat.diagonal()-1).max() <= 1e-15

        # We try to recover the correlated variables through the
        # correlation matrix (not through the covariance matrix):

        nominal_values = [v.nominal_value for v in (x, y, z)]
        std_devs = [v.std_dev() for v in (x, y, z)]
        x2, y2, z2 = uncertainties.correlated_values_norm(