示例#1
0
    def test_propagate_cov_1D_3var(self):
        prop = MCPropagation(20000)
        covc = [
            util.convert_corr_to_cov(np.eye(len(xerrc.flatten())), xerrc)
            for xerrc in xerrsc
        ]
        ufc, ucorrc = prop.propagate_cov(functionc, xsc, covc, return_corr=True)
        npt.assert_allclose(ucorrc, np.eye(len(ucorrc)), atol=0.06)
        npt.assert_allclose(ufc, yerr_uncorrc, rtol=0.06)

        covc = [
            util.convert_corr_to_cov(
                np.ones((len(xerrc.flatten()), len(xerrc.flatten())))
                + np.eye(len(xerrc)),
                xerrc,
            )
            for xerrc in xerrsc
        ]
        ufc, ucorrc = prop.propagate_cov(functionc, xsc, covc, return_corr=True)
        npt.assert_allclose(ufc, yerr_uncorrc * 2 ** 0.5, rtol=0.06)

        covc = [
            util.convert_corr_to_cov(np.eye(len(xerrc.flatten())), xerrc)
            for xerrc in xerrsc
        ]
        ufc, ucorrc = prop.propagate_cov(
            functionc, xsc, covc, return_corr=True, corr_between=corr_c
        )
        npt.assert_allclose(ucorrc, np.eye(len(ucorrc)), atol=0.06)
        npt.assert_allclose(ufc, yerr_corrc, rtol=0.06)
示例#2
0
    def test_propagate_cov_3D_2out(self):
        prop = LPUPropagation()

        covd = [
            util.convert_corr_to_cov(np.eye(len(xerrd.flatten())), xerrd)
            for xerrd in xerrsd
        ]
        ufd, ucorrd, corr_out = prop.propagate_cov(functiond,
                                                   xsd,
                                                   covd,
                                                   return_corr=True,
                                                   output_vars=2)
        npt.assert_allclose(ucorrd[0], np.eye(len(ucorrd[0])), atol=0.01)
        npt.assert_allclose(ufd, yerr_uncorrd, rtol=0.01)

        covd = [
            util.convert_corr_to_cov(
                np.ones((len(xerrd.flatten()), len(xerrd.flatten()))), xerrd)
            for xerrd in xerrsd
        ]
        ufd, ucorrd, corr_out = prop.propagate_cov(functiond,
                                                   xsd,
                                                   covd,
                                                   return_corr=True,
                                                   output_vars=2)
        npt.assert_allclose(ucorrd[1], np.ones_like(ucorrd[1]), atol=0.01)
        npt.assert_allclose(ufd, yerr_uncorrd, rtol=0.01)
示例#3
0
    def test_propagate_cov_1D(self):
        prop = MCPropagation(20000)

        cov = [
            util.convert_corr_to_cov(np.eye(len(xerr.flatten())), xerr)
            for xerr in xerrs
        ]
        uf, ucorr = prop.propagate_cov(function, xs, cov, return_corr=True)
        npt.assert_allclose(ucorr, np.eye(len(ucorr)), atol=0.06)
        npt.assert_allclose(uf, yerr_uncorr, rtol=0.06)

        cov = [
            util.convert_corr_to_cov(
                np.ones((len(xerr.flatten()), len(xerr.flatten()))) + np.eye(len(xerr)),
                xerr,
            )
            for xerr in xerrs
        ]
        uf, ucorr = prop.propagate_cov(function, xs, cov, return_corr=True)
        npt.assert_allclose(uf, yerr_uncorr * 2 ** 0.5, rtol=0.06)

        cov = [
            util.convert_corr_to_cov(np.eye(len(xerr.flatten())), xerr)
            for xerr in xerrs
        ]
        uf, ucorr = prop.propagate_cov(
            function, xs, cov, return_corr=True, corr_between=np.ones((2, 2))
        )
        npt.assert_allclose(ucorr, np.eye(len(ucorr)), atol=0.06)
        npt.assert_allclose(uf, yerr_corr, rtol=0.06)
示例#4
0
    def test_propagate_cov_2D(self):
        prop = LPUPropagation(parallel_cores=2)

        covb = [
            util.convert_corr_to_cov(np.eye(len(xerrb.flatten())), xerrb)
            for xerrb in xerrsb
        ]
        ufb, ucorrb = prop.propagate_cov(functionb,
                                         xsb,
                                         covb,
                                         return_corr=True,
                                         Jx_diag=True)
        npt.assert_allclose(ucorrb, np.eye(len(ucorrb)), atol=0.01)
        npt.assert_allclose(ufb, yerr_uncorrb, rtol=0.01)

        covb = [
            util.convert_corr_to_cov(np.eye(len(xerrb[:, 0].flatten())),
                                     xerrb[:, 0]) for xerrb in xerrsb
        ]

        ufb, ucorrb = prop.propagate_cov(functionb,
                                         xsb,
                                         covb,
                                         return_corr=True,
                                         repeat_dims=1,
                                         Jx_diag=True)
        npt.assert_allclose(ucorrb, np.eye(len(ucorrb)), atol=0.06)
        npt.assert_allclose(ufb, yerr_uncorrb, rtol=0.06)

        covb = [
            util.convert_corr_to_cov(
                np.ones((len(xerrb.flatten()), len(xerrb.flatten()))) +
                np.eye(len(xerrb.flatten())),
                xerrb,
            ) for xerrb in xerrsb
        ]
        ufb, ucorrb = prop.propagate_cov(functionb,
                                         xsb,
                                         covb,
                                         return_corr=True,
                                         Jx_diag=True)
        npt.assert_allclose(ufb, yerr_uncorrb * 2**0.5, rtol=0.01)

        covb = [
            util.convert_corr_to_cov(np.eye(len(xerrb.flatten())), xerrb)
            for xerrb in xerrsb
        ]
        ufb, ucorrb = prop.propagate_cov(
            functionb,
            xsb,
            covb,
            return_corr=True,
            corr_between=np.ones((2, 2)),
            Jx_diag=True,
        )
        npt.assert_allclose(ucorrb, np.eye(len(ucorrb)), atol=0.01)
        npt.assert_allclose(ufb, yerr_corrb, atol=0.03)
示例#5
0
    def test_propagate_cov_1D(self):
        prop = LPUPropagation()

        cov = [
            util.convert_corr_to_cov(np.eye(len(xerr.flatten())), xerr)
            for xerr in xerrs
        ]

        Jx = Jac_function(*xs)
        uf, ucorr = prop.propagate_cov(function,
                                       xs,
                                       cov,
                                       return_corr=True,
                                       Jx=Jx)
        npt.assert_allclose(ucorr, np.eye(len(ucorr)), atol=0.01)
        npt.assert_allclose(uf, yerr_uncorr_1order, rtol=0.01)

        cov = [
            util.convert_corr_to_cov(
                np.ones((len(xerr.flatten()), len(xerr.flatten()))), xerr)
            for xerr in xerrs
        ]
        uf, ucorr = prop.propagate_cov(function,
                                       xs,
                                       cov,
                                       return_corr=True,
                                       Jx=Jx)
        npt.assert_allclose(uf, yerr_uncorr_1order, atol=0.01)
        npt.assert_allclose(ucorr,
                            np.ones((len(ucorr), len(ucorr))),
                            atol=0.01)

        cov = [
            util.convert_corr_to_cov(np.eye(len(xerr)), xerr) for xerr in xerrs
        ]
        uf, ucorr = prop.propagate_cov(function,
                                       xs,
                                       cov,
                                       return_corr=True,
                                       corr_between=np.ones((2, 2)),
                                       Jx=Jx)
        # ucorr is full of nans because the uncertainties are 0
        npt.assert_allclose(uf, yerr_corr_1order, atol=0.01)
示例#6
0
    def test_propagate_random_1D(self):
        prop = MCPropagation(20000, parallel_cores=0)

        uf, ucorr = prop.propagate_random(function, xs, xerrs, return_corr=True)
        npt.assert_allclose(ucorr, np.eye(len(ucorr)), atol=0.06)
        npt.assert_allclose(uf, yerr_uncorr, rtol=0.06)
        ucov = util.convert_corr_to_cov(ucorr, uf)
        ucorr2 = util.convert_cov_to_corr(ucov, uf)
        npt.assert_allclose(ucorr, ucorr2, atol=0.01)

        uf = prop.propagate_random(function, xs, xerrs, corr_between=np.ones((2, 2)))
        npt.assert_allclose(uf, yerr_corr, rtol=0.06)

        uf, ucorr, yvalues, xvalues = prop.propagate_random(
            function, xs, xerrs, return_corr=True, return_samples=True
        )

        npt.assert_allclose(uf, yerr_uncorr, rtol=0.06)

        uf, yvalues, xvalues = prop.propagate_random(
            function, xs, xerrs, corr_between=np.ones((2, 2)), return_samples=True
        )
        npt.assert_allclose(uf, yerr_corr, rtol=0.06)

        uf, yvalues, xvalues = prop.propagate_standard(
            function,
            xs,
            xerrs,
            corr_x=["rand", "rand"],
            corr_between=np.ones((2, 2)),
            return_samples=True,
        )
        npt.assert_allclose(uf, yerr_corr, rtol=0.06)

        uf, yvalues, xvalues = prop.propagate_random(
            function,
            xs,
            xerrs,
            corr_x=["rand", None],
            corr_between=np.ones((2, 2)),
            return_samples=True,
        )
        npt.assert_allclose(uf, yerr_corr, rtol=0.06)

        uf, yvalues, xvalues = prop.propagate_random(
            function,
            xs,
            xerrs,
            corr_x=[np.diag(np.ones(xs[0].shape)), None],
            corr_between=np.ones((2, 2)),
            return_samples=True,
        )
        npt.assert_allclose(uf, yerr_corr, rtol=0.06)
示例#7
0
    def test_propagate_random_1D(self):
        prop = LPUPropagation()
        Jx = Jac_function(*xs)
        uf, ucorr = prop.propagate_random(function,
                                          xs,
                                          xerrs,
                                          return_corr=True,
                                          Jx=Jx)
        npt.assert_allclose(ucorr, np.eye(len(ucorr)), atol=0.01)
        npt.assert_allclose(uf, yerr_uncorr_1order, rtol=0.01)

        uf, ucorr = prop.propagate_random(function,
                                          xs,
                                          xerrs,
                                          corr_x=["rand", None],
                                          return_corr=True,
                                          Jx=Jx)
        npt.assert_allclose(ucorr, np.eye(len(ucorr)), atol=0.01)
        npt.assert_allclose(uf, yerr_uncorr_1order, rtol=0.01)

        uf, ucorr = prop.propagate_standard(function,
                                            xs,
                                            xerrs, ["rand", "rand"],
                                            return_corr=True,
                                            Jx=Jx)
        npt.assert_allclose(ucorr, np.eye(len(ucorr)), atol=0.01)
        npt.assert_allclose(uf, yerr_uncorr_1order, rtol=0.01)

        ucov = util.convert_corr_to_cov(ucorr, uf)
        ucorr2 = util.convert_cov_to_corr(ucov, uf)
        npt.assert_allclose(ucorr, ucorr2, atol=0.01)

        uf = prop.propagate_random(function,
                                   xs,
                                   xerrs,
                                   corr_between=np.ones((2, 2)),
                                   Jx=Jx)
        npt.assert_allclose(uf, yerr_corr_1order, atol=0.01)

        uf, ucorr = prop.propagate_random(function,
                                          xs,
                                          xerrs,
                                          return_corr=True,
                                          Jx=Jx)
        npt.assert_allclose(uf, yerr_uncorr_1order, rtol=0.01)

        uf = prop.propagate_random(function,
                                   xs,
                                   xerrs,
                                   corr_between=np.ones((2, 2)),
                                   Jx=Jx)
        npt.assert_allclose(uf, yerr_corr_1order, atol=0.01)
示例#8
0
    def test_propagate_cov_2D(self):
        prop = MCPropagation(20000)

        covb = [
            util.convert_corr_to_cov(np.eye(len(xerrb.flatten())), xerrb)
            for xerrb in xerrsb
        ]
        ufb, ucorrb = prop.propagate_cov(functionb, xsb, covb, return_corr=True)
        npt.assert_allclose(ucorrb, np.eye(len(ucorrb)), atol=0.06)
        npt.assert_allclose(ufb, yerr_uncorrb, rtol=0.06)

        covb = [
            util.convert_corr_to_cov(np.eye(len(xerrb[:, 0].flatten())), xerrb[:, 0])
            for xerrb in xerrsb
        ]

        ufb, ucorrb = prop.propagate_cov(
            functionb, xsb, covb, return_corr=True, repeat_dims=1
        )
        npt.assert_allclose(ucorrb, np.eye(len(ucorrb)), atol=0.06)
        npt.assert_allclose(ufb, yerr_uncorrb, rtol=0.06)

        xsb2 = [50, xsb[1]]
        covb = [
            1,
            util.convert_corr_to_cov(
                np.ones(
                    (len(xerrsb[1][:, 0].flatten()), len(xerrsb[1][:, 0].flatten()))
                ),
                xerrsb[1][:, 0],
            ),
        ]

        ufb, ucorrb = prop.propagate_cov(
            functionb,
            xsb2,
            covb,
            return_corr=True,
            repeat_dims=1,
            param_fixed=[True, False],
        )
        npt.assert_allclose(ucorrb, np.ones((len(ucorrb), len(ucorrb))), atol=0.06)
        npt.assert_allclose(ufb, yerr_uncorrb, rtol=0.06)

        covb = [
            util.convert_corr_to_cov(
                np.ones((len(xerrb.flatten()), len(xerrb.flatten())))
                + np.eye(len(xerrb.flatten())),
                xerrb,
            )
            for xerrb in xerrsb
        ]
        ufb, ucorrb = prop.propagate_cov(functionb, xsb, covb, return_corr=True)
        npt.assert_allclose(ufb, yerr_uncorrb * 2 ** 0.5, rtol=0.06)

        covb = [
            util.convert_corr_to_cov(np.eye(len(xerrb.flatten())), xerrb)
            for xerrb in xerrsb
        ]
        ufb, ucorrb = prop.propagate_cov(
            functionb, xsb, covb, return_corr=True, corr_between=np.ones((2, 2))
        )
        npt.assert_allclose(ucorrb, np.eye(len(ucorrb)), atol=0.06)
        npt.assert_allclose(ufb, yerr_corrb, atol=0.03)
示例#9
0
    def propagate_cov(
        self,
        func,
        x,
        cov_x,
        param_fixed=None,
        corr_between=None,
        return_corr=False,
        return_Jacobian=False,
        repeat_dims=-99,
        corr_axis=-99,
        fixed_corr_var=False,
        output_vars=1,
        Jx=None,
        Jx_diag=None,
    ):
        """
        Propagate uncertainties with given covariance matrix through measurement function with n input quantities.
        Input quantities can be floats, vectors (1d-array) or images (2d-array).
        The covariance matrix can represent the full covariance matrix between all measurements in all dimensions.
        Alternatively if there are repeated measurements specified in repeat_dims, the covariance matrix is given
        for the covariance along the dimension that is not one of the repeat_dims.

        :param func: measurement function
        :type func: function
        :param x: list of input quantities (usually numpy arrays)
        :type x: list[array]
        :param cov_x: list of covariance matrices on input quantities (usually numpy arrays). In case the input quantity is an array of shape (m,o), the covariance matrix needs to be given as an array of shape (m*o,m*o).
        :type cov_x: list[array]
        :param param_fixed: when repeat_dims>=0, set to true or false to indicate for each input quantity whether it has repeated measurements that should be split (param_fixed=False) or whether the input is fixed (param fixed=True), defaults to None (no inputs fixed).
        :type param_fixed: list of bools, optional
        :param corr_between: covariance matrix (n,n) between input quantities, defaults to None
        :type corr_between: array, optional
        :param return_corr: set to True to return correlation matrix of measurand, defaults to True
        :type return_corr: bool, optional
        :param return_Jacobian: set to True to return Jacobian matrix, defaults to False
        :type return_Jacobian: bool, optional
        :param repeat_dims: set to positive integer(s) to select the axis which has repeated measurements. The calculations will be performed seperately for each of the repeated measurments and then combined, in order to save memory and speed up the process.  Defaults to -99, for which there is no reduction in dimensionality..
        :type repeat_dims: integer or list of 2 integers, optional
        :param corr_axis: set to positive integer to select the axis used in the correlation matrix. The correlation matrix will then be averaged over other dimensions. Defaults to -99, for which the input array will be flattened and the full correlation matrix calculated.
        :type corr_axis: integer, optional
        :param fixed_corr_var: set to integer to copy the correlation matrix of the dimension the integer refers to. Set to True to automatically detect if only one uncertainty is present and the correlation matrix of that dimension should be copied. Defaults to False.
        :type fixed_corr_var: bool or integer, optional
        :param output_vars: number of output parameters in the measurement function. Defaults to 1.
        :type output_vars: integer, optional
        :param Jx: Jacobian matrix, evaluated at x. This allows to give a precomputed jacobian matrix, which could potentially be calculated using analytical prescription. Defaults to None, in which case Jx is calculated numerically as part of the propagation.
        :rtype Jx: array, optional
        :param Jx_diag: Bool to indicate whether the Jacobian matrix can be described with semi-diagonal elements. With this we mean that the measurand has the same shape as each of the input quantities and the square jacobain between the measurand and each of the input quantities individually, only has diagonal elements. Defaults to False
        :rtype Jx_diag: bool, optional
        :return: uncertainties on measurand
        :rtype: array
        """
        u_x = [
            util.uncertainty_from_covariance(cov_x[i]) for i in range(len(x))
        ]
        corr_x = [
            util.correlation_from_covariance(cov_x[i]) for i in range(len(x))
        ]

        for i in range(len(x)):
            if len(x[i].shape) > 1:
                if len(u_x[i]) == len(x[i][:, 0]):
                    u_x[i] = np.tile(u_x[i], (len(x[i][0]), 1)).T
                else:
                    u_x[i] = u_x[i].reshape(x[i].shape)

        (
            fun,
            xflat,
            u_xflat,
            yshape,
            u_x,
            repeat_dims,
            n_repeats,
            repeat_shape,
            corr_axis,
            fixed_corr,
            Jx_diag,
        ) = self.perform_checks(
            func,
            x,
            u_x,
            corr_x,
            repeat_dims,
            corr_axis,
            output_vars,
            fixed_corr_var,
            Jx_diag,
            param_fixed,
        )

        if n_repeats > 0:
            inputs = np.empty(n_repeats, dtype=object)
            for i in range(n_repeats):
                xb, u_xb = util.select_repeated_x(x, u_x, param_fixed, i,
                                                  repeat_dims, repeat_shape)
                if Jx is not None:
                    Jxi = Jx[i]
                else:
                    Jxi = Jx

                inputs[i] = [
                    func,
                    xb,
                    cov_x,
                    None,
                    corr_between,
                    return_corr,
                    return_Jacobian,
                    -99,
                    corr_axis,
                    fixed_corr_var,
                    output_vars,
                    Jxi,
                    Jx_diag,
                ]

            if self.parallel_cores > 1:
                pool = Pool(self.parallel_cores)
                outs = pool.starmap(self.propagate_cov, inputs)
                pool.close()

            else:
                outs = np.empty(n_repeats, dtype=object)
                for i in range(n_repeats):
                    outs[i] = self.propagate_cov(*inputs[i])

            return self.combine_repeated_outs(
                outs,
                yshape,
                n_repeats,
                repeat_shape,
                repeat_dims,
                return_corr,
                return_Jacobian,
                output_vars,
            )

        else:
            if Jx is None:
                Jx = util.calculate_Jacobian(fun, xflat, Jx_diag, self.step)

            if corr_between is None:
                corr_between = np.eye(len(x))

            corr_x = util.calculate_flattened_corr(corr_x, corr_between)
            cov_x = util.convert_corr_to_cov(corr_x, u_xflat)

            return self.process_jacobian(
                Jx,
                cov_x,
                yshape,
                return_corr,
                corr_axis,
                fixed_corr,
                output_vars,
                return_Jacobian,
            )