示例#1
0
    def test_initialisation_of_propensities_as_matrix(self):
        """
        The model constructor should accept propensities as a sympy matrix
        e.g. sympy.Matrix(['y_0+y_1', 'y_1+y_2'])
        and return them as sympy (column) Matrix of equations
        i.e. sympy.Matrix(['y_0+y_1', 'y_1+y_2'])
        """
        answer = to_sympy_matrix([['c_0*y_0*(y_0 + y_1 - 181)'],
                               ['c_1*(-y_0 - y_1 + 301)'],
                               ['c_2*(-y_0 - y_1 + 301)']])
        # Column
        m = Model(self.SAMPLE_VARIABLES,
                  self.SAMPLE_CONSTANTS,
                  to_sympy_matrix(['c_0*y_0*(y_0 + y_1 - 181)',
                               'c_1*(-y_0 - y_1 + 301)',
                               'c_2*(-y_0 - y_1 + 301)']),
                  self.SAMPLE_STOICHIOMETRY_MATRIX)
        self.assertEqual(m.propensities, answer)

        # Row matrix
        m = Model(
                 self.SAMPLE_VARIABLES,
                 self.SAMPLE_CONSTANTS,
                  [['c_0*y_0*(y_0 + y_1 - 181)',
                   'c_1*(-y_0 - y_1 + 301)',
                   'c_2*(-y_0 - y_1 + 301)']],
                  self.SAMPLE_STOICHIOMETRY_MATRIX)
        self.assertEqual(m.propensities, answer)
示例#2
0
    def test_initialisation_of_propensities_as_matrix(self):
        """
        The model constructor should accept propensities as a sympy matrix
        e.g. sympy.Matrix(['y_0+y_1', 'y_1+y_2'])
        and return them as sympy (column) Matrix of equations
        i.e. sympy.Matrix(['y_0+y_1', 'y_1+y_2'])
        """
        answer = to_sympy_matrix([['c_0*y_0*(y_0 + y_1 - 181)'],
                                  ['c_1*(-y_0 - y_1 + 301)'],
                                  ['c_2*(-y_0 - y_1 + 301)']])
        # Column
        m = Model(
            self.SAMPLE_VARIABLES, self.SAMPLE_CONSTANTS,
            to_sympy_matrix([
                'c_0*y_0*(y_0 + y_1 - 181)', 'c_1*(-y_0 - y_1 + 301)',
                'c_2*(-y_0 - y_1 + 301)'
            ]), self.SAMPLE_STOICHIOMETRY_MATRIX)
        self.assertEqual(m.propensities, answer)

        # Row matrix
        m = Model(self.SAMPLE_VARIABLES, self.SAMPLE_CONSTANTS, [[
            'c_0*y_0*(y_0 + y_1 - 181)', 'c_1*(-y_0 - y_1 + 301)',
            'c_2*(-y_0 - y_1 + 301)'
        ]], self.SAMPLE_STOICHIOMETRY_MATRIX)
        self.assertEqual(m.propensities, answer)
示例#3
0
    def test_TaylorExpansion(self):
        """
        Given the number of moments is 3, the number of species is 2,
        Given the propensities of the 3 reactions in `a_strings`,
        And Given the combination of derivative order in counter,
        Then results of `TaylorExpansion()` should produce a matrix exactly equal to
        exactly equal to the the expected one (`expected_te_matrix`).

        :return:
        """

        mea = MomentExpansionApproximation(None, 3)
        species = ["a", "b", "c"]
        propensities = to_sympy_matrix(["a*2 +w * b**3", "b - a*x /c", "c + a*b /32"])
        stoichiometry_matrix = sp.Matrix([
            [1, 0, 1],
            [-1, -1, 0],
            [0, 1, -1]
        ])

        counter = [
            Moment([0, 0, 2], sp.Symbol("q1")),
            Moment([0, 2, 0], sp.Symbol("q2")),
            Moment([0, 0, 2], sp.Symbol("q3")),
            Moment([2, 0, 0], sp.Symbol("q4")),
            Moment([1, 1, 0], sp.Symbol("q5")),
            Moment([0, 1, 1], sp.Symbol("q6")),
            Moment([1, 0, 1], sp.Symbol("q7"))]

        result = generate_dmu_over_dt(species, propensities, counter, stoichiometry_matrix)
        expected = stoichiometry_matrix * to_sympy_matrix([["        0", "3*b*w", "0", "0", "0", "0", "0"],
                                                           ["-a*x/c**3", "0", "-a*x/c**3", "0", "0", "0", "x/c**2"],
                                                           ["0", "0", "0", "0", "1/32", "0", "0"]])

        self.assertEqual(result, expected)
示例#4
0
    def test_TaylorExpansion(self):
        """
        Given the number of moments is 3, the number of species is 2,
        Given the propensities of the 3 reactions in `a_strings`,
        And Given the combination of derivative order in counter,
        Then results of `TaylorExpansion()` should produce a matrix exactly equal to
        exactly equal to the the expected one (`expected_te_matrix`).

        :return:
        """

        mea = MomentExpansionApproximation(None, 3)
        species = ["a", "b", "c"]
        propensities = to_sympy_matrix(
            ["a*2 +w * b**3", "b - a*x /c", "c + a*b /32"])
        stoichiometry_matrix = sp.Matrix([[1, 0, 1], [-1, -1, 0], [0, 1, -1]])

        counter = [
            Moment([0, 0, 2], sp.Symbol("q1")),
            Moment([0, 2, 0], sp.Symbol("q2")),
            Moment([0, 0, 2], sp.Symbol("q3")),
            Moment([2, 0, 0], sp.Symbol("q4")),
            Moment([1, 1, 0], sp.Symbol("q5")),
            Moment([0, 1, 1], sp.Symbol("q6")),
            Moment([1, 0, 1], sp.Symbol("q7"))
        ]

        result = generate_dmu_over_dt(species, propensities, counter,
                                      stoichiometry_matrix)
        expected = stoichiometry_matrix * to_sympy_matrix(
            [["        0", "3*b*w", "0", "0", "0", "0", "0"],
             ["-a*x/c**3", "0", "-a*x/c**3", "0", "0", "0", "x/c**2"],
             ["0", "0", "0", "0", "1/32", "0", "0"]])

        self.assertEqual(result, expected)
    def test_get_log_covariance(self):


        log_variance_mat =to_sympy_matrix([
                ["log(1+yx7/y_0**2)", "0", "0"],
                ["0", "log(1+yx4/y_1**2)", "0"],
                ["0", "0", "log(1+yx2/y_2**2)"]
                        ])

        log_expectation_symbols = to_sympy_matrix([
                ["log(y_0)-log(1+yx7/y_0**2)/2"],
                ["log(y_1)-log(1+yx4/y_1**2)/2"],
                ["log(y_2)-log(1+yx2/y_2**2)/2"]
                ])

        covariance_matrix = to_sympy_matrix([
                ["yx7","yx6","yx5"],
                ["yx6","yx4","yx3"],
                ["yx5","yx3","yx2"]])

        expected = sympy.sympify("log(1 + yx6/(y_0*y_1))")


        closer = LogNormalClosure(2, multivariate=True)
        answer = closer._get_log_covariance(log_variance_mat, log_expectation_symbols, covariance_matrix, 0,1)

        self.assertEqual(answer, expected)

        answer1 = closer._get_log_covariance(log_variance_mat, log_expectation_symbols, covariance_matrix, 1,2)
        answer2 = closer._get_log_covariance(log_variance_mat, log_expectation_symbols, covariance_matrix, 1,2)
        #logcovariance between species 1 and 2  ==  covariance between sp. 2 and 1
        self.assertEqual(answer1, answer2)
示例#6
0
    def test_ode_rhs_as_function_cache_does_not_persist_between_instances(self):
        """
        Given two ODEProblems, the cache should not persist between these objects.
        :return:
        """

        p1_lhs = [Moment(np.ones(4), i) for i in sympy.Matrix(['y_1', 'y_2', 'y_3', 'y_4'])]
        p1_rhs = to_sympy_matrix(['y_1+y_2+c_2', 'y_2+y_3+c_3', 'y_3+c_1', 'y_1*2'])

        p2_lhs = [Moment(np.ones(3), i) for i in sympy.Matrix(['y_1', 'y_2', 'y_3'])]
        p2_rhs = to_sympy_matrix(['y_1', 'c_1', 'y_2+y_3'])

        p1 = ODEProblem('MEA', p1_lhs, p1_rhs, parameters=sympy.symbols(['c_1', 'c_2', 'c_3']))
        p1_rhs_as_function = p1.right_hand_side_as_function

        p2 = ODEProblem('MEA', p2_lhs, p2_rhs, parameters=sympy.symbols(['c_1', 'c_2', 'c_3']))
        p2_rhs_as_function = p2.right_hand_side_as_function

        constants = [1, 2, 3]
        values_p1 = [4, 5, 6, 5]  # y_1, y_2, y_3, y_4 in that order
        values_p2 = [4, 5, 6]  # y_1, y_2, y_3  in that order

        p1_expected_ans = np.array([11, 14, 7, 8])
        p2_expected_ans = np.array([4, 1, 6+5])
        p1_actual_ans = np.array(p1_rhs_as_function(values_p1, constants))
        p2_actual_ans = np.array(p2_rhs_as_function(values_p2, constants))

        # This checks if by any means p2 is able to "override" the p1 result
        p1_ans_after_p2 = np.array(p1_rhs_as_function(values_p1, constants))

        assert_array_equal(p1_actual_ans, p1_expected_ans)
        assert_array_equal(p2_actual_ans, p2_expected_ans)
        assert_array_equal(p1_ans_after_p2, p1_expected_ans)
示例#7
0
    def test_get_log_covariance(self):

        log_variance_mat = to_sympy_matrix([["log(1+yx7/y_0**2)", "0", "0"],
                                            ["0", "log(1+yx4/y_1**2)", "0"],
                                            ["0", "0", "log(1+yx2/y_2**2)"]])

        log_expectation_symbols = to_sympy_matrix([[
            "log(y_0)-log(1+yx7/y_0**2)/2"
        ], ["log(y_1)-log(1+yx4/y_1**2)/2"], ["log(y_2)-log(1+yx2/y_2**2)/2"]])

        covariance_matrix = to_sympy_matrix([["yx7", "yx6", "yx5"],
                                             ["yx6", "yx4", "yx3"],
                                             ["yx5", "yx3", "yx2"]])

        expected = sympy.sympify("log(1 + yx6/(y_0*y_1))")

        closer = LogNormalClosure(2, multivariate=True)
        answer = closer._get_log_covariance(log_variance_mat,
                                            log_expectation_symbols,
                                            covariance_matrix, 0, 1)

        self.assertEqual(answer, expected)

        answer1 = closer._get_log_covariance(log_variance_mat,
                                             log_expectation_symbols,
                                             covariance_matrix, 1, 2)
        answer2 = closer._get_log_covariance(log_variance_mat,
                                             log_expectation_symbols,
                                             covariance_matrix, 1, 2)
        #logcovariance between species 1 and 2  ==  covariance between sp. 2 and 1
        self.assertEqual(answer1, answer2)
示例#8
0
    def test_transcription_model(self):
        #use simple production and degradation of mRNA and protein for testing
        # mRNA production rate is k1, degradation rate is g1
        # protein production rate is k2, degradation rate is g2
        stoichiometry_matrix = sympy.Matrix([[1, -1, 0,  0],
                                             [0,  0, 1, -1]])

        propensities = to_sympy_matrix(['k1',
                                     'g1*x',
                                     'k2*x',
                                     'g2*y'])

        species = to_sympy_matrix(['x', 'y'])


        correct_rhs = to_sympy_matrix(
            ["k1 - g1 * x",
            "k2 * x - g2 * y",
            "k1 + g1 * x - 2 * g1 * V_0_0",
            "k2 * V_0_0 - (g1 + g2) * V_0_1",
            "k2 * x + g2 * y + k2 * V_0_1 + k2 * V_0_1 - 2 * g2 * V_1_1"])

        correct_lhs = to_sympy_matrix(['x','y','V_0_0', 'V_0_1', 'V_1_1'])

        constants = ["k1","k2","g1","g2"]
        model = Model(species, constants, propensities, stoichiometry_matrix)
        lna = LinearNoiseApproximation(model)
        problem = lna.run()

        answer_rhs = problem.right_hand_side
        answer_lhs = problem.left_hand_side

        assert_sympy_expressions_equal(correct_rhs, answer_rhs)
        self.assertEqual(correct_lhs, answer_lhs)
示例#9
0
    def test_substitute_all_on_expression(self):

        to_substitute = sympy.sympify("a*b + c*d + d*e + e*f")
        pairs = zip(to_sympy_matrix(["a", "d", "e", "c", "b"]),
                    to_sympy_matrix(["z", "w", "v", "x", "y"]))
        expected = sympy.sympify("z*y + x*w + w*v + v*f")
        answer = substitute_all(to_substitute, pairs)
        self.assertEqual(answer, expected)
示例#10
0
    def test_substitute_all_on_expression(self):

        to_substitute = sympy.sympify("a*b + c*d + d*e + e*f")
        pairs = zip(to_sympy_matrix(["a","d","e","c","b"]),
                    to_sympy_matrix(["z","w","v","x","y"]))
        expected = sympy.sympify("z*y + x*w + w*v + v*f")
        answer = substitute_all(to_substitute, pairs)
        self.assertEqual(answer, expected)
示例#11
0
    def test_substitute_all_on_matrix(self):

        to_substitute = to_sympy_matrix(["a*b", "c*d", "d*e", "e*f"])
        pairs = zip(to_sympy_matrix(["a", "d", "e", "c", "b"]),
                    to_sympy_matrix(["z", "w", "v", "x", "y"]))

        expected = sympy.Matrix(["z*y", "x*w", "w*v", "v*f"])
        answer = substitute_all(to_substitute, pairs)
        self.assertEqual(answer, expected)
示例#12
0
    def test_substitute_all_on_matrix(self):

        to_substitute = to_sympy_matrix(["a*b","c*d","d*e","e*f"])
        pairs = zip(to_sympy_matrix(["a","d","e","c","b"]),
                    to_sympy_matrix(["z","w","v","x","y"]))

        expected = sympy.Matrix(["z*y","x*w","w*v","v*f"])
        answer = substitute_all(to_substitute, pairs)
        self.assertEqual(answer, expected)
示例#13
0
文件: model.py 项目: lukauskas/means
    def __init__(self, species, parameters, propensities, stoichiometry_matrix):
        r"""
        Creates a `Model` object that stores the model of reactions we want to analyse
        :param species: variables of the model, as `sympy.Symbol`s, i.e. species
        :param parameters: parameters of the model, as `sympy` symbols
        :param propensities: a matrix of propensities for each of the reaction in the model.
        :param stoichiometry_matrix: stoichiometry matrix for the model
        """
        self.__parameters = to_list_of_symbols(parameters)
        self.__species = to_list_of_symbols(species)
        self.__propensities = to_sympy_column_matrix(to_sympy_matrix(propensities))
        self.__stoichiometry_matrix = to_sympy_matrix(stoichiometry_matrix)

        self.validate()
示例#14
0
 def test_ode_moment_getting_n_vector_from_dict_and_key(self):
     """
     Given a list of descriptor and a list of symbols used to create Moment,
     Then problem descriptor_for_symbol function should return the correct
     descriptor for each corresponding symbol
     :return:
     """
     symbs = to_sympy_matrix(['y_1', 'y_2', 'y_3'])
     desc = [[0, 0, 1], [1, 0, 432], [21, 43, 34]]
     lhs = [Moment(d, s) for d, s in zip(desc, symbs)]
     rhs = to_sympy_matrix(['y_1+y_2+c_2', 'y_2+y_3+c_3', 'y_3+c_1'])
     p = ODEProblem('MEA', lhs, rhs, parameters=sympy.symbols(['c_1', 'c_2', 'c_3']))
     for i, l in enumerate(lhs):
         self.assertEqual(p.descriptor_for_symbol(l.symbol), l)
示例#15
0
    def __init__(self, species, parameters, propensities,
                 stoichiometry_matrix):
        r"""
        Creates a `Model` object that stores the model of reactions we want to analyse
        :param species: variables of the model, as `sympy.Symbol`s, i.e. species
        :param parameters: parameters of the model, as `sympy` symbols
        :param propensities: a matrix of propensities for each of the reaction in the model.
        :param stoichiometry_matrix: stoichiometry matrix for the model
        """
        self.__parameters = to_list_of_symbols(parameters)
        self.__species = to_list_of_symbols(species)
        self.__propensities = to_sympy_column_matrix(
            to_sympy_matrix(propensities))
        self.__stoichiometry_matrix = to_sympy_matrix(stoichiometry_matrix)

        self.validate()
示例#16
0
    def test_creation_from_matrix_returns_itself(self):
        """
        Given a `sympy.Matrix`, `to_sympy_matrix` should return the said matrix.
        """

        m = sympy.Matrix([[1, 2, 3], [4, 5, 6]])
        assert_sympy_expressions_equal(m, to_sympy_matrix(m))
示例#17
0
    def test_creation_from_matrix_returns_itself(self):
        """
        Given a `sympy.Matrix`, `to_sympy_matrix` should return the said matrix.
        """

        m = sympy.Matrix([[1, 2, 3], [4, 5, 6]])
        assert_sympy_expressions_equal(m, to_sympy_matrix(m))
示例#18
0
 def test_compute_raw_moments(self):
     expected = to_sympy_matrix([
         ["y_2**2+yx2"], ["y_1*y_2+yx3"], ["y_1**2+yx4"], ["y_0*y_2+yx5"],
         ["y_0*y_1+yx6"], ["y_0**2+yx7"],
         ["y_2**3+3*y_2*yx2+3*yx2**2/y_2+yx2**3/y_2**3"],
         [
             "y_1*y_2**2+y_1*yx2+2*y_2*yx3+2*yx2*yx3/y_2+yx3**2/y_1+yx2*yx3**2/(y_1*y_2**2)"
         ],
         [
             "y_1**2*y_2+2*y_1*yx3+y_2*yx4+yx3**2/y_2+2*yx3*yx4/y_1+yx3**2*yx4/(y_1**2*y_2)"
         ], ["y_1**3+3*y_1*yx4+3*yx4**2/y_1+yx4**3/y_1**3"],
         [
             "y_0*y_2**2+y_0*yx2+2*y_2*yx5+2*yx2*yx5/y_2+yx5**2/y_0+yx2*yx5**2/(y_0*y_2**2)"
         ],
         [
             "y_0*y_1*y_2+y_0*yx3+y_1*yx5+y_2*yx6+yx3*yx5/y_2+yx3*yx6/y_1+yx5*yx6/y_0+yx3*yx5*yx6/(y_0*y_1*y_2)"
         ],
         [
             "y_0*y_1**2+y_0*yx4+2*y_1*yx6+2*yx4*yx6/y_1+yx6**2/y_0+yx4*yx6**2/(y_0*y_1**2)"
         ],
         [
             "y_0**2*y_2+2*y_0*yx5+y_2*yx7+yx5**2/y_2+2*yx5*yx7/y_0+yx5**2*yx7/(y_0**2*y_2)"
         ],
         [
             "y_0**2*y_1+2*y_0*yx6+y_1*yx7+yx6**2/y_1+2*yx6*yx7/y_0+yx6**2*yx7/(y_0**2*y_1)"
         ], ["y_0**3+3*y_0*yx7+3*yx7**2/y_0+yx7**3/y_0**3"]
     ])
     closer = LogNormalClosure(2, multivariate=True)
     answer = closer._compute_raw_moments(
         self.__n_counter,
         self.__k_counter,
     )
     self.assertTrue(sympy_expressions_equal(answer, expected))
示例#19
0
    def test_for_p53(self):
        """
        Given the preopensities,
        Given the soichiometry matrix,
        Given the counter (list of Moments),
        Given the species list,
        Given k_vector and
        Given ek_counter (list of moment)
        The answer should match exactly the expected result
        :return:
        """

        stoichio = sympy.Matrix([
            [1, -1, -1, 0,  0,  0],
            [0,  0,  0, 1, -1,  0],
            [0,  0,  0, 0,  1, -1]
        ])

        propensities = to_sympy_matrix([
            ["                    c_0"],
            ["                c_1*y_0"],
            ["c_2*y_0*y_2/(c_6 + y_0)"],
            ["                c_3*y_0"],
            ["                c_4*y_1"],
            ["                c_5*y_2"]])

        counter = [
        Moment([0, 0, 0], 1),
        Moment([0, 0, 2], sympy.Symbol("yx1")),
        Moment([0, 1, 1], sympy.Symbol("yx2")),
        Moment([0, 2, 0], sympy.Symbol("yx3")),
        Moment([1, 0, 1], sympy.Symbol("yx4")),
        Moment([1, 1, 0], sympy.Symbol("yx5")),
        Moment([2, 0, 0], sympy.Symbol("yx6"))
        ]

        species = sympy.Matrix(["y_0", "y_1", "y_2"])

        dbdt_calc = DBetaOverDtCalculator(propensities, counter,stoichio, species)
        k_vec = [1, 0, 0]
        ek_counter = [Moment([1, 0, 0], sympy.Symbol("y_0"))]

        answer = dbdt_calc.get(k_vec,ek_counter).T
        result = to_sympy_matrix(["c_0 - c_1*y_0 - c_2*y_0*y_2/(c_6 + y_0)"," 0"," 0"," 0"," c_2*y_0/(c_6 + y_0)**2 - c_2/(c_6 + y_0)"," 0"," -c_2*y_0*y_2/(c_6 + y_0)**3 + c_2*y_2/(c_6 + y_0)**2"])
        assert_sympy_expressions_equal(answer, result)
示例#20
0
    def test_creation_from_list_of_integers_returns_matrix(self):
        """
        Given a list of integers, to_sympy_matrix should be able to convert it to a matrix of these integers
        :return:
        """

        m = sympy.Matrix([[1, 2, 3], [4, 5, 6]])
        m_as_list = [[1, 2, 3], [4, 5, 6]]

        assert_sympy_expressions_equal(m, to_sympy_matrix(m_as_list))
示例#21
0
    def test_creation_from_list_of_integers_returns_matrix(self):
        """
        Given a list of integers, to_sympy_matrix should be able to convert it to a matrix of these integers
        :return:
        """

        m = sympy.Matrix([[1, 2, 3], [4, 5, 6]])
        m_as_list = [[1, 2, 3], [4, 5, 6]]

        assert_sympy_expressions_equal(m, to_sympy_matrix(m_as_list))
示例#22
0
    def test_log_normal_closer_wrapper(self):


        central_from_raw_exprs = to_sympy_matrix(
                    [["x_0_0_2-y_2**2"],
                    ["x_0_1_1-y_1*y_2"],
                    ["x_0_2_0-y_1**2"],
                    ["x_1_0_1-y_0*y_2"],
                    ["x_1_1_0-y_0*y_1"],
                    ["x_2_0_0-y_0**2"],
                    ["-3*x_0_0_2*y_2+x_0_0_3+2*y_2**3"],
                    ["-x_0_0_2*y_1-2*x_0_1_1*y_2+x_0_1_2+2*y_1*y_2**2"],
                    ["-2*x_0_1_1*y_1-x_0_2_0*y_2+x_0_2_1+2*y_1**2*y_2"],
                    ["-3*x_0_2_0*y_1+x_0_3_0+2*y_1**3"],
                    ["-x_0_0_2*y_0-2*x_1_0_1*y_2+x_1_0_2+2*y_0*y_2**2"],
                    ["-x_0_1_1*y_0-x_1_0_1*y_1-x_1_1_0*y_2+x_1_1_1+2*y_0*y_1*y_2"],
                    ["-x_0_2_0*y_0-2*x_1_1_0*y_1+x_1_2_0+2*y_0*y_1**2"],
                    ["-2*x_1_0_1*y_0-x_2_0_0*y_2+x_2_0_1+2*y_0**2*y_2"],
                    ["-2*x_1_1_0*y_0-x_2_0_0*y_1+x_2_1_0+2*y_0**2*y_1"],
                    ["-3*x_2_0_0*y_0+x_3_0_0+2*y_0**3"]
         ])


        max_order = 2

        expected = to_sympy_matrix([
                    ["c_0-c_1*y_0-(c_2*c_6*yx5)/(c_6+y_0) ** 2-(c_2*y_0*y_2)/(c_6+y_0)+(c_2*c_6*y_2*yx7)/(c_6+y_0) ** 3+(c_2*c_6*yx5*(yx5*y_0 ** 2+2*y_2*yx7*y_0+yx5*yx7))/(y_0 ** 2*y_2*(c_6+y_0) ** 3)-(c_2*c_6*y_2*yx7 ** 2*(3*y_0 ** 2+yx7))/(y_0 ** 3*(c_6+y_0) ** 4)"],
                    ["c_3*y_0-c_4*y_1"],
                    ["c_4*y_1-c_5*y_2"],
                    ["c_4*y_1+c_5*y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x5*yx7+c_2*c_6*y_0 ** 4*yx5 ** 2-6*c_2*c_6*y_0 ** 3*y_2 ** 2*yx7 ** 2+2*c_2*c_6*y_0 ** 3*y_2*yx5*yx7+2*c_2*c_6*y_0 ** 3*yx5 ** 2*yx7-3*c_2*c_6*y_0 ** 2*y_2 ** 2*yx7 ** 2+c_2*c_6*y_0 ** 2*yx5 ** 2*yx7-2*c_2*c_6*y_0*y_2 ** 2*yx7 ** 3-c_2*c_6*y_2 ** 2*yx7 ** 3-c_1*y_0 ** 8*y_2-c_2*y_0 ** 7*y_2 ** 2+2*c_2*y_0 ** 7*y_2*yx5+2*c_1*y_0 ** 7*y_2*yx7-c_0*y_0 ** 7*y_2)/(y_0 ** 3*y_2*(c_6+y_0) ** 4)"]
                ])
        closer = LogNormalClosure(max_order,multivariate=True)
        answer = closer.close(self.__mfk, central_from_raw_exprs, self.__n_counter, self.__k_counter)


        #print (answer -expected).applyfunc(sympy.simplify)
        self.assertTrue(sympy_expressions_equal(answer, expected))
示例#23
0
    def test_creation_from_list_of_strings_returns_matrix(self):
        """
        Given a list of strings, to_sympy_matrix should be able to convert them into a matrix of expressions.
        """

        m = sympy.Matrix([[sympy.sympify('x+y+3'), sympy.sympify('x+3')],
                          [sympy.sympify('y-x'), sympy.sympify('x+y+166')]])

        m_as_string = [['x+y+3', 'x+3'], ['y-x', 'x+y+166']]
        matrix = to_sympy_matrix(m_as_string)
        assert_sympy_expressions_equal(m, matrix)
示例#24
0
    def test_creation_of_column_matrix_from_list_of_strings(self):
        """
        Given a list of strings, to_sympy_matrix should be able to convert them into a column matrix of expresions
        """
        m = sympy.Matrix([sympy.sympify('x+y+3'), sympy.sympify('x+3'), sympy.sympify('y-x'),
                           sympy.sympify('x+y+166')])

        m_as_string = ['x+y+3', 'x+3', 'y-x', 'x+y+166']

        matrix = to_sympy_matrix(m_as_string)
        assert_sympy_expressions_equal(m, matrix)
示例#25
0
    def test_log_normal_closer_wrapper(self):

        central_from_raw_exprs = to_sympy_matrix(
            [["x_0_0_2-y_2**2"], ["x_0_1_1-y_1*y_2"], ["x_0_2_0-y_1**2"],
             ["x_1_0_1-y_0*y_2"], ["x_1_1_0-y_0*y_1"], ["x_2_0_0-y_0**2"],
             ["-3*x_0_0_2*y_2+x_0_0_3+2*y_2**3"],
             ["-x_0_0_2*y_1-2*x_0_1_1*y_2+x_0_1_2+2*y_1*y_2**2"],
             ["-2*x_0_1_1*y_1-x_0_2_0*y_2+x_0_2_1+2*y_1**2*y_2"],
             ["-3*x_0_2_0*y_1+x_0_3_0+2*y_1**3"],
             ["-x_0_0_2*y_0-2*x_1_0_1*y_2+x_1_0_2+2*y_0*y_2**2"],
             ["-x_0_1_1*y_0-x_1_0_1*y_1-x_1_1_0*y_2+x_1_1_1+2*y_0*y_1*y_2"],
             ["-x_0_2_0*y_0-2*x_1_1_0*y_1+x_1_2_0+2*y_0*y_1**2"],
             ["-2*x_1_0_1*y_0-x_2_0_0*y_2+x_2_0_1+2*y_0**2*y_2"],
             ["-2*x_1_1_0*y_0-x_2_0_0*y_1+x_2_1_0+2*y_0**2*y_1"],
             ["-3*x_2_0_0*y_0+x_3_0_0+2*y_0**3"]])

        max_order = 2

        expected = to_sympy_matrix([
            [
                "c_0-c_1*y_0-(c_2*c_6*yx5)/(c_6+y_0) ** 2-(c_2*y_0*y_2)/(c_6+y_0)+(c_2*c_6*y_2*yx7)/(c_6+y_0) ** 3+(c_2*c_6*yx5*(yx5*y_0 ** 2+2*y_2*yx7*y_0+yx5*yx7))/(y_0 ** 2*y_2*(c_6+y_0) ** 3)-(c_2*c_6*y_2*yx7 ** 2*(3*y_0 ** 2+yx7))/(y_0 ** 3*(c_6+y_0) ** 4)"
            ], ["c_3*y_0-c_4*y_1"], ["c_4*y_1-c_5*y_2"],
            ["c_4*y_1+c_5*y_2+2*c_4*yx3-2*c_5*yx2"],
            ["c_3*yx5-c_4*yx3-c_4*y_1+c_4*yx4-c_5*yx3"],
            ["c_3*y_0+c_4*y_1-2*c_4*yx4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2*yx5*yx7+2*c_2*c_6*y_0 ** 3*yx5 ** 2*yx7-3*c_2*c_6*y_0 ** 2*y_2 ** 2*yx7 ** 2+c_2*c_6*y_0 ** 2*yx5 ** 2*yx7-2*c_2*c_6*y_0*y_2 ** 2*yx7 ** 3-c_2*c_6*y_2 ** 2*yx7 ** 3-c_1*y_0 ** 8*y_2-c_2*y_0 ** 7*y_2 ** 2+2*c_2*y_0 ** 7*y_2*yx5+2*c_1*y_0 ** 7*y_2*yx7-c_0*y_0 ** 7*y_2)/(y_0 ** 3*y_2*(c_6+y_0) ** 4)"
            ]
        ])
        closer = LogNormalClosure(max_order, multivariate=True)
        answer = closer.close(self.__mfk, central_from_raw_exprs,
                              self.__n_counter, self.__k_counter)

        #print (answer -expected).applyfunc(sympy.simplify)
        self.assertTrue(sympy_expressions_equal(answer, expected))
    def test_centralmoments_using_MM_model(self):
        """
        Given the MM model hard codded bellow,the result of central moment should match exactly the expected one
        :return:
        """
        counter_nvecs = [[0, 0], [0, 2], [1, 1], [2, 0]]
        mcounter_nvecs = [[0, 0], [0, 1], [1, 0], [0, 2], [1, 1], [2, 0]]

        counter = [Moment(c,sympy.Symbol("YU{0}".format(i))) for i,c in enumerate(counter_nvecs)]
        mcounter = [Moment(c,sympy.Symbol("y_{0}".format(i))) for i,c in enumerate(mcounter_nvecs)]

        m = to_sympy_matrix([
                              ['-c_0*y_0*(y_0 + y_1 - 181) + c_1*(-y_0 - y_1 + 301)',
                               0,
                               '-c_0',
                               '-c_0'],
                          [
                              'c_2*(-y_0 - y_1 + 301)',
                              0,
                              0,
                              0]
        ])


        species = sympy.Matrix(map(sympy.var, ['y_0', 'y_1']))

        propensities = to_sympy_matrix(['c_0*y_0*(y_0 + y_1 - 181)',
                                                        'c_1*(-y_0 - y_1 + 301)',
                                                        'c_2*(-y_0 - y_1 + 301)'])

        stoichiometry_matrix = sympy.Matrix([[-1, 1, 0],
                                             [0, 0, 1]])

        expected = to_sympy_matrix([
        ["c_2*(-y_0 - y_1 + 301)"," -2*c_2"," -2*c_2"," 0"],
        ["-c_0*y_0*y_1*(y_0 + y_1 - 181) + c_1*y_1*(-y_0 - y_1 + 301) + c_2*y_0*(-y_0 - y_1 + 301) - c_2*y_2*(-y_0 - y_1 + 301) - y_1*(-c_0*y_0*(y_0 + y_1 - 181) + c_1*(-y_0 - y_1 + 301))"," -c_0*y_0 - c_1"," -c_0*y_0 - c_0*(y_0 + y_1 - 181) - c_1 - c_2"," -c_2"],
        ["-2*c_0*y_0**2*(y_0 + y_1 - 181) + c_0*y_0*(y_0 + y_1 - 181) + 2*c_1*y_0*(-y_0 - y_1 + 301) + c_1*(-y_0 - y_1 + 301) - 2*y_2*(-c_0*y_0*(y_0 + y_1 - 181) + c_1*(-y_0 - y_1 + 301))"," 0"," -4*c_0*y_0 + 2*c_0*y_2 + c_0 - 2*c_1"," -4*c_0*y_0 + 2*c_0*y_2 - 2*c_0*(y_0 + y_1 - 181) + c_0 - 2*c_1"]
        ])
        answer = eq_central_moments(counter, mcounter, m, species, propensities, stoichiometry_matrix, 2)

        assert_sympy_expressions_equal(answer, expected)
示例#27
0
    def test_ode_moment_no_description_from_variance_terms(self):
        """
        Given  Variance terms as left hand side terms, the generated descriptions
        dict should have nones
        for each of the symbols
        """
        lhs = [VarianceTerm(pos, term) for term, pos in [('V34', (3, 4)), ('V32', (3, 2)), ('V11', (1, 1))]]
        rhs = to_sympy_matrix(['y_1+y_2+c_2', 'y_2+y_3+c_3', 'y_3+c_1'])
        p = ODEProblem('LNA', lhs, rhs, parameters=sympy.symbols(['c_1', 'c_2', 'c_3']))

        for i,l in enumerate(lhs):
            self.assertIsNone(p._descriptions_dict[l.symbol].descriptor)
示例#28
0
    def test_log_normal_closer_wrapper_univariate(self):

        central_from_raw_exprs = to_sympy_matrix(
            [["x_0_0_2-y_2**2"], ["x_0_1_1-y_1*y_2"], ["x_0_2_0-y_1**2"],
             ["x_1_0_1-y_0*y_2"], ["x_1_1_0-y_0*y_1"], ["x_2_0_0-y_0**2"],
             ["-3*x_0_0_2*y_2+x_0_0_3+2*y_2**3"],
             ["-x_0_0_2*y_1-2*x_0_1_1*y_2+x_0_1_2+2*y_1*y_2**2"],
             ["-2*x_0_1_1*y_1-x_0_2_0*y_2+x_0_2_1+2*y_1**2*y_2"],
             ["-3*x_0_2_0*y_1+x_0_3_0+2*y_1**3"],
             ["-x_0_0_2*y_0-2*x_1_0_1*y_2+x_1_0_2+2*y_0*y_2**2"],
             ["-x_0_1_1*y_0-x_1_0_1*y_1-x_1_1_0*y_2+x_1_1_1+2*y_0*y_1*y_2"],
             ["-x_0_2_0*y_0-2*x_1_1_0*y_1+x_1_2_0+2*y_0*y_1**2"],
             ["-2*x_1_0_1*y_0-x_2_0_0*y_2+x_2_0_1+2*y_0**2*y_2"],
             ["-2*x_1_1_0*y_0-x_2_0_0*y_1+x_2_1_0+2*y_0**2*y_1"],
             ["-3*x_2_0_0*y_0+x_3_0_0+2*y_0**3"]])

        max_order = 2

        expected = to_sympy_matrix([
            [
                "c_0-c_1*y_0-(c_2*c_6*yx5)/(c_6+y_0) ** 2-(c_2*y_0*y_2)/(c_6+y_0)+(c_2*c_6*y_2*yx7)/(c_6+y_0) ** 3-(c_2*c_6*y_2*yx7 ** 2*(3*y_0 ** 2+yx7))/(y_0 ** 3*(c_6+y_0) ** 4)"
            ], ["c_3*y_0-c_4*y_1"], ["c_4*y_1-c_5*y_2"],
            ["c_4*y_1+c_5*y_2+2*c_4*yx3-2*c_5*yx2"],
            ["c_3*yx5-c_4*yx3-c_4*y_1+c_4*yx4-c_5*yx3"],
            ["c_3*y_0+c_4*y_1-2*c_4*yx4+2*c_3*yx6"],
            [
                "c_4*yx6-c_1*yx5-c_5*yx5-(c_2*y_0*yx2)/(c_6+y_0)-(c_2*y_2*yx5)/(c_6+y_0)+(c_2*y_0*y_2*yx5)/(c_6+y_0) ** 2"
            ],
            [
                "c_3*yx7-c_1*yx6-c_4*yx6-(c_2*y_0*yx3)/(c_6+y_0)-(c_2*y_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!
        closer = LogNormalClosure(max_order, multivariate=False)
        answer = closer.close(self.__mfk, central_from_raw_exprs,
                              self.__n_counter, self.__k_counter)
        self.assertTrue(sympy_expressions_equal(answer, expected))
示例#29
0
    def test_creation_from_list_of_strings_returns_matrix(self):
        """
        Given a list of strings, to_sympy_matrix should be able to convert them into a matrix of expressions.
        """

        m = sympy.Matrix([[sympy.sympify('x+y+3'),
                           sympy.sympify('x+3')],
                          [sympy.sympify('y-x'),
                           sympy.sympify('x+y+166')]])

        m_as_string = [['x+y+3', 'x+3'], ['y-x', 'x+y+166']]
        matrix = to_sympy_matrix(m_as_string)
        assert_sympy_expressions_equal(m, matrix)
示例#30
0
    def test_close_type_one(self):
        central_from_raw_exprs = to_sympy_matrix(
                    [["x_0_0_2-y_2**2"],
                    ["x_0_1_1-y_1*y_2"],
                    ["x_0_2_0-y_1**2"],
                    ["x_1_0_1-y_0*y_2"],
                    ["x_1_1_0-y_0*y_1"],
                    ["x_2_0_0-y_0**2"],
                    ["-3*x_0_0_2*y_2+x_0_0_3+2*y_2**3"],
                    ["-x_0_0_2*y_1-2*x_0_1_1*y_2+x_0_1_2+2*y_1*y_2**2"],
                    ["-2*x_0_1_1*y_1-x_0_2_0*y_2+x_0_2_1+2*y_1**2*y_2"],
                    ["-3*x_0_2_0*y_1+x_0_3_0+2*y_1**3"],
                    ["-x_0_0_2*y_0-2*x_1_0_1*y_2+x_1_0_2+2*y_0*y_2**2"],
                    ["-x_0_1_1*y_0-x_1_0_1*y_1-x_1_1_0*y_2+x_1_1_1+2*y_0*y_1*y_2"],
                    ["-x_0_2_0*y_0-2*x_1_1_0*y_1+x_1_2_0+2*y_0*y_1**2"],
                    ["-2*x_1_0_1*y_0-x_2_0_0*y_2+x_2_0_1+2*y_0**2*y_2"],
                    ["-2*x_1_1_0*y_0-x_2_0_0*y_1+x_2_1_0+2*y_0**2*y_1"],
                    ["-3*x_2_0_0*y_0+x_3_0_0+2*y_0**3"]
         ])


        max_order = 2

        expected = to_sympy_matrix([
            ["yx5*(c_2*y_0/(c_6 + y_0)**2 - c_2/(c_6 + y_0)) + yx7*(-c_2*y_0*y_2/(c_6 + y_0)**3 + c_2*y_2/(c_6 + y_0)**2) + c_0 - c_1*y_0 - c_2*y_0*y_2/(c_6 + y_0) + (-c_2*y_0/(c_6 + y_0)**3 + c_2/(c_6 + y_0)**2)*(2*yx5*yx7/y_0 + 2*yx5*y_0 + yx7*y_2 + 3*y_0**2*y_2 - 2*y_0*(yx5 + y_0*y_2) - y_2*(yx7 + y_0**2)) + (c_2*y_0*y_2/(c_6 + y_0)**4 - c_2*y_2/(c_6 + y_0)**3)*(2*yx7**2/y_0 + 3*yx7*y_0 + 3*y_0**3 - 3*y_0*(yx7 + y_0**2))"],
            ["c_3*y_0-c_4*y_1"],
            ["c_4*y_1-c_5*y_2"],
            ["c_4*y_1+c_5*y_2+2*c_4*yx3-2*c_5*yx2"],
            ["c_3*yx5-c_4*yx3-c_4*y_1+c_4*yx4-c_5*yx3"],
            ["c_3*y_0+c_4*y_1-2*c_4*yx4+2*c_3*yx6"],
            ["-yx2*c_2*y_0/(c_6 + y_0) + yx5*(-c_1 + 2*c_2*y_0*y_2/(c_6 + y_0)**2 - 2*c_2*y_2/(c_6 + y_0) - c_5 - y_2*(c_2*y_0/(c_6 + y_0)**2 - c_2/(c_6 + y_0))) + yx6*c_4 + yx7*(-c_2*y_0*y_2**2/(c_6 + y_0)**3 + c_2*y_2**2/(c_6 + y_0)**2 - y_2*(-c_2*y_0*y_2/(c_6 + y_0)**3 + c_2*y_2/(c_6 + y_0)**2)) + c_0*y_2 - c_1*y_0*y_2 - c_2*y_0*y_2**2/(c_6 + y_0) + c_4*y_0*y_1 - c_5*y_0*y_2 - y_0*(c_4*y_1 - c_5*y_2) - y_2*(c_0 - c_1*y_0 - c_2*y_0*y_2/(c_6 + y_0)) + (c_2*y_0/(c_6 + y_0)**2 - c_2/(c_6 + y_0))*(2*yx2*yx5/y_2 + yx2*y_0 + 2*yx5*y_2 + 3*y_0*y_2**2 - y_0*(yx2 + y_2**2) - 2*y_2*(yx5 + y_0*y_2)) + (-2*c_2*y_0*y_2/(c_6 + y_0)**3 + 2*c_2*y_2/(c_6 + y_0)**2 - y_2*(-c_2*y_0/(c_6 + y_0)**3 + c_2/(c_6 + y_0)**2))*(2*yx5*yx7/y_0 + 2*yx5*y_0 + yx7*y_2 + 3*y_0**2*y_2 - 2*y_0*(yx5 + y_0*y_2) - y_2*(yx7 + y_0**2)) + (c_2*y_0*y_2**2/(c_6 + y_0)**4 - c_2*y_2**2/(c_6 + y_0)**3 - y_2*(c_2*y_0*y_2/(c_6 + y_0)**4 - c_2*y_2/(c_6 + y_0)**3))*(2*yx7**2/y_0 + 3*yx7*y_0 + 3*y_0**3 - 3*y_0*(yx7 + y_0**2))"],
            ["-yx3*c_2*y_0/(c_6 + y_0) + yx5*(c_2*y_0*y_1/(c_6 + y_0)**2 - c_2*y_1/(c_6 + y_0) - y_1*(c_2*y_0/(c_6 + y_0)**2 - c_2/(c_6 + y_0))) + yx6*(-c_1 + c_2*y_0*y_2/(c_6 + y_0)**2 - c_2*y_2/(c_6 + y_0) - c_4) + yx7*(-c_2*y_0*y_1*y_2/(c_6 + y_0)**3 + c_2*y_1*y_2/(c_6 + y_0)**2 + c_3 - y_1*(-c_2*y_0*y_2/(c_6 + y_0)**3 + c_2*y_2/(c_6 + y_0)**2)) + c_0*y_1 - c_1*y_0*y_1 - c_2*y_0*y_1*y_2/(c_6 + y_0) + c_3*y_0**2 - c_4*y_0*y_1 - y_0*(c_3*y_0 - c_4*y_1) - y_1*(c_0 - c_1*y_0 - c_2*y_0*y_2/(c_6 + y_0)) + (c_2*y_0/(c_6 + y_0)**2 - c_2/(c_6 + y_0))*(yx3*y_0 + yx5*y_1 + yx6*y_2 + 3*y_0*y_1*y_2 - y_0*(yx3 + y_1*y_2) - y_1*(yx5 + y_0*y_2) - y_2*(yx6 + y_0*y_1)) + (-c_2*y_0*y_2/(c_6 + y_0)**3 + c_2*y_2/(c_6 + y_0)**2)*(2*yx6*yx7/y_0 + 2*yx6*y_0 + yx7*y_1 + 3*y_0**2*y_1 - 2*y_0*(yx6 + y_0*y_1) - y_1*(yx7 + y_0**2)) + (-c_2*y_0*y_1/(c_6 + y_0)**3 + c_2*y_1/(c_6 + y_0)**2 - y_1*(-c_2*y_0/(c_6 + y_0)**3 + c_2/(c_6 + y_0)**2))*(2*yx5*yx7/y_0 + 2*yx5*y_0 + yx7*y_2 + 3*y_0**2*y_2 - 2*y_0*(yx5 + y_0*y_2) - y_2*(yx7 + y_0**2)) + (c_2*y_0*y_1*y_2/(c_6 + y_0)**4 - c_2*y_1*y_2/(c_6 + y_0)**3 - y_1*(c_2*y_0*y_2/(c_6 + y_0)**4 - c_2*y_2/(c_6 + y_0)**3))*(2*yx7**2/y_0 + 3*yx7*y_0 + 3*y_0**3 - 3*y_0*(yx7 + y_0**2))"],
            ["yx5*(2*c_2*y_0**2/(c_6 + y_0)**2 - 4*c_2*y_0/(c_6 + y_0) - c_2*y_0/(c_6 + y_0)**2 + c_2/(c_6 + y_0) - 2*y_0*(c_2*y_0/(c_6 + y_0)**2 - c_2/(c_6 + y_0))) + yx7*(-2*c_1 - 2*c_2*y_0**2*y_2/(c_6 + y_0)**3 + 4*c_2*y_0*y_2/(c_6 + y_0)**2 + c_2*y_0*y_2/(c_6 + y_0)**3 - 2*c_2*y_2/(c_6 + y_0) - c_2*y_2/(c_6 + y_0)**2 - 2*y_0*(-c_2*y_0*y_2/(c_6 + y_0)**3 + c_2*y_2/(c_6 + y_0)**2)) + 2*c_0*y_0 + c_0 - 2*c_1*y_0**2 + c_1*y_0 - 2*c_2*y_0**2*y_2/(c_6 + y_0) + c_2*y_0*y_2/(c_6 + y_0) - 2*y_0*(c_0 - c_1*y_0 - c_2*y_0*y_2/(c_6 + y_0)) + (2*yx7**2/y_0 + 3*yx7*y_0 + 3*y_0**3 - 3*y_0*(yx7 + y_0**2))*(2*c_2*y_0**2*y_2/(c_6 + y_0)**4 - 4*c_2*y_0*y_2/(c_6 + y_0)**3 - c_2*y_0*y_2/(c_6 + y_0)**4 + 2*c_2*y_2/(c_6 + y_0)**2 + c_2*y_2/(c_6 + y_0)**3 - 2*y_0*(c_2*y_0*y_2/(c_6 + y_0)**4 - c_2*y_2/(c_6 + y_0)**3)) + (2*yx5*yx7/y_0 + 2*yx5*y_0 + yx7*y_2 + 3*y_0**2*y_2 - 2*y_0*(yx5 + y_0*y_2) - y_2*(yx7 + y_0**2))*(-2*c_2*y_0**2/(c_6 + y_0)**3 + 4*c_2*y_0/(c_6 + y_0)**2 + c_2*y_0/(c_6 + y_0)**3 - 2*c_2/(c_6 + y_0) - c_2/(c_6 + y_0)**2 - 2*y_0*(-c_2*y_0/(c_6 + y_0)**3 + c_2/(c_6 + y_0)**2))"]
        ])

        closer = GammaClosure(max_order, multivariate=True)
        answer = closer.close(self.__mfk, central_from_raw_exprs, self.__n_counter, self.__k_counter)
        self.assertTrue(sympy_expressions_equal(answer, expected))
示例#31
0
    def test_log_normal_closer_wrapper_univariate(self):

        central_from_raw_exprs = to_sympy_matrix(
                    [["x_0_0_2-y_2**2"],
                    ["x_0_1_1-y_1*y_2"],
                    ["x_0_2_0-y_1**2"],
                    ["x_1_0_1-y_0*y_2"],
                    ["x_1_1_0-y_0*y_1"],
                    ["x_2_0_0-y_0**2"],
                    ["-3*x_0_0_2*y_2+x_0_0_3+2*y_2**3"],
                    ["-x_0_0_2*y_1-2*x_0_1_1*y_2+x_0_1_2+2*y_1*y_2**2"],
                    ["-2*x_0_1_1*y_1-x_0_2_0*y_2+x_0_2_1+2*y_1**2*y_2"],
                    ["-3*x_0_2_0*y_1+x_0_3_0+2*y_1**3"],
                    ["-x_0_0_2*y_0-2*x_1_0_1*y_2+x_1_0_2+2*y_0*y_2**2"],
                    ["-x_0_1_1*y_0-x_1_0_1*y_1-x_1_1_0*y_2+x_1_1_1+2*y_0*y_1*y_2"],
                    ["-x_0_2_0*y_0-2*x_1_1_0*y_1+x_1_2_0+2*y_0*y_1**2"],
                    ["-2*x_1_0_1*y_0-x_2_0_0*y_2+x_2_0_1+2*y_0**2*y_2"],
                    ["-2*x_1_1_0*y_0-x_2_0_0*y_1+x_2_1_0+2*y_0**2*y_1"],
                    ["-3*x_2_0_0*y_0+x_3_0_0+2*y_0**3"]
         ])

        max_order = 2

        expected = to_sympy_matrix([
            ["c_0-c_1*y_0-(c_2*c_6*yx5)/(c_6+y_0) ** 2-(c_2*y_0*y_2)/(c_6+y_0)+(c_2*c_6*y_2*yx7)/(c_6+y_0) ** 3-(c_2*c_6*y_2*yx7 ** 2*(3*y_0 ** 2+yx7))/(y_0 ** 3*(c_6+y_0) ** 4)"],
            ["c_3*y_0-c_4*y_1"],
            ["c_4*y_1-c_5*y_2"],
            ["c_4*y_1+c_5*y_2+2*c_4*yx3-2*c_5*yx2"],
            ["c_3*yx5-c_4*yx3-c_4*y_1+c_4*yx4-c_5*yx3"],
            ["c_3*y_0+c_4*y_1-2*c_4*yx4+2*c_3*yx6"],
            ["c_4*yx6-c_1*yx5-c_5*yx5-(c_2*y_0*yx2)/(c_6+y_0)-(c_2*y_2*yx5)/(c_6+y_0)+(c_2*y_0*y_2*yx5)/(c_6+y_0) ** 2"],
            ["c_3*yx7-c_1*yx6-c_4*yx6-(c_2*y_0*yx3)/(c_6+y_0)-(c_2*y_2*yx6)/(c_6+y_0)+(c_2*y_0*y_2*yx6)/(c_6+y_0) ** 2"],
            ["(c_0*y_0 ** 7+c_1*y_0 ** 8+c_2*y_0 ** 7*y_2-2*c_2*y_0 ** 7*yx5-2*c_1*y_0 ** 7*yx7+6*c_0*c_6 ** 2*y_0 ** 5+4*c_0*c_6 ** 3*y_0 ** 4+c_0*c_6 ** 4*y_0 ** 3+6*c_1*c_6 ** 2*y_0 ** 6+4*c_1*c_6 ** 3*y_0 ** 5+c_1*c_6 ** 4*y_0 ** 4+4*c_0*c_6*y_0 ** 6+4*c_1*c_6*y_0 ** 7+3*c_2*c_6*y_0 ** 6*y_2+c_2*c_6*y_0 ** 5*yx5-6*c_2*c_6*y_0 ** 6*yx5-8*c_1*c_6*y_0 ** 6*yx7+c_2*c_6*y_2*yx7 ** 3+3*c_2*c_6 ** 2*y_0 ** 5*y_2+c_2*c_6 ** 3*y_0 ** 4*y_2+2*c_2*c_6 ** 2*y_0 ** 4*yx5+c_2*c_6 ** 3*y_0 ** 3*yx5-6*c_2*c_6 ** 2*y_0 ** 5*yx5-2*c_2*c_6 ** 3*y_0 ** 4*yx5-12*c_1*c_6 ** 2*y_0 ** 5*yx7-8*c_1*c_6 ** 3*y_0 ** 4*yx7-2*c_1*c_6 ** 4*y_0 ** 3*yx7+2*c_2*c_6 ** 2*y_2*yx7 ** 3+3*c_2*c_6*y_0 ** 2*y_2*yx7 ** 2+6*c_2*c_6*y_0 ** 3*y_2*yx7 ** 2-c_2*c_6 ** 2*y_0 ** 3*y_2*yx7-4*c_2*c_6 ** 2*y_0 ** 4*y_2*yx7-2*c_2*c_6 ** 3*y_0 ** 3*y_2*yx7+6*c_2*c_6 ** 2*y_0 ** 2*y_2*yx7 ** 2+2*c_2*c_6*y_0*y_2*yx7 ** 3-c_2*c_6*y_0 ** 4*y_2*yx7-2*c_2*c_6*y_0 ** 5*y_2*yx7)/(y_0 ** 3*(c_6+y_0) ** 4)"]
        ])
        #here, we set univariate!
        closer = LogNormalClosure(max_order, multivariate=False)
        answer = closer.close(self.__mfk, central_from_raw_exprs, self.__n_counter, self.__k_counter)
        self.assertTrue(sympy_expressions_equal(answer, expected))
示例#32
0
    def test_close_type_zero(self):

        central_from_raw_exprs = to_sympy_matrix(
                    [["x_0_0_2-y_2**2"],
                    ["x_0_1_1-y_1*y_2"],
                    ["x_0_2_0-y_1**2"],
                    ["x_1_0_1-y_0*y_2"],
                    ["x_1_1_0-y_0*y_1"],
                    ["x_2_0_0-y_0**2"],
                    ["-3*x_0_0_2*y_2+x_0_0_3+2*y_2**3"],
                    ["-x_0_0_2*y_1-2*x_0_1_1*y_2+x_0_1_2+2*y_1*y_2**2"],
                    ["-2*x_0_1_1*y_1-x_0_2_0*y_2+x_0_2_1+2*y_1**2*y_2"],
                    ["-3*x_0_2_0*y_1+x_0_3_0+2*y_1**3"],
                    ["-x_0_0_2*y_0-2*x_1_0_1*y_2+x_1_0_2+2*y_0*y_2**2"],
                    ["-x_0_1_1*y_0-x_1_0_1*y_1-x_1_1_0*y_2+x_1_1_1+2*y_0*y_1*y_2"],
                    ["-x_0_2_0*y_0-2*x_1_1_0*y_1+x_1_2_0+2*y_0*y_1**2"],
                    ["-2*x_1_0_1*y_0-x_2_0_0*y_2+x_2_0_1+2*y_0**2*y_2"],
                    ["-2*x_1_1_0*y_0-x_2_0_0*y_1+x_2_1_0+2*y_0**2*y_1"],
                    ["-3*x_2_0_0*y_0+x_3_0_0+2*y_0**3"]
         ])

        max_order = 2
        expected = to_sympy_matrix([
            ["c_0-c_1*y_0-(c_2*c_6*yx5)/(c_6+y_0) ** 2-(c_2*y_0*y_2)/(c_6+y_0)+(c_2*c_6*y_2*yx7)/(c_6+y_0) ** 3-(2*c_2*c_6*y_2*yx7 ** 2)/(y_0*(c_6+y_0) ** 4)"],
            ["c_3*y_0-c_4*y_1"],
            ["c_4*y_1-c_5*y_2"],
            ["c_4*y_1+c_5*y_2+2*c_4*yx3-2*c_5*yx2"],
            ["c_3*yx5-c_4*yx3-c_4*y_1+c_4*yx4-c_5*yx3"],
            ["c_3*y_0+c_4*y_1-2*c_4*yx4+2*c_3*yx6"],
            ["c_4*yx6-c_1*yx5-c_5*yx5-(c_2*y_0*yx2)/(c_6+y_0)-(c_2*y_2*yx5)/(c_6+y_0)+(c_2*y_0*y_2*yx5)/(c_6+y_0) ** 2"],
            ["c_3*yx7-c_1*yx6-c_4*yx6-(c_2*y_0*yx3)/(c_6+y_0)-(c_2*y_2*yx6)/(c_6+y_0)+(c_2*y_0*y_2*yx6)/(c_6+y_0) ** 2"],
            ["(c_0*y_0 ** 5+c_1*y_0 ** 6+c_2*y_0 ** 5*y_2-2*c_2*y_0 ** 5*yx5-2*c_1*y_0 ** 5*yx7+6*c_0*c_6 ** 2*y_0 ** 3+4*c_0*c_6 ** 3*y_0 ** 2+6*c_1*c_6 ** 2*y_0 ** 4+4*c_1*c_6 ** 3*y_0 ** 3+c_1*c_6 ** 4*y_0 ** 2+4*c_0*c_6*y_0 ** 4+c_0*c_6 ** 4*y_0+4*c_1*c_6*y_0 ** 5+3*c_2*c_6*y_0 ** 4*y_2+c_2*c_6*y_0 ** 3*yx5+c_2*c_6 ** 3*y_0*yx5-6*c_2*c_6*y_0 ** 4*yx5-8*c_1*c_6*y_0 ** 4*yx7-2*c_1*c_6 ** 4*y_0*yx7+2*c_2*c_6*y_2*yx7 ** 2+3*c_2*c_6 ** 2*y_0 ** 3*y_2+c_2*c_6 ** 3*y_0 ** 2*y_2+2*c_2*c_6 ** 2*y_0 ** 2*yx5-6*c_2*c_6 ** 2*y_0 ** 3*yx5-2*c_2*c_6 ** 3*y_0 ** 2*yx5-12*c_1*c_6 ** 2*y_0 ** 3*yx7-8*c_1*c_6 ** 3*y_0 ** 2*yx7+4*c_2*c_6 ** 2*y_2*yx7 ** 2-4*c_2*c_6 ** 2*y_0 ** 2*y_2*yx7+4*c_2*c_6*y_0*y_2*yx7 ** 2-c_2*c_6*y_0 ** 2*y_2*yx7-c_2*c_6 ** 2*y_0*y_2*yx7-2*c_2*c_6*y_0 ** 3*y_2*yx7-2*c_2*c_6 ** 3*y_0*y_2*yx7)/(y_0*(c_6+y_0) ** 4)"]
        ])
        closer = GammaClosure(max_order, multivariate=False)
        answer = closer.close(self.__mfk, central_from_raw_exprs, self.__n_counter, self.__k_counter)
        self.assertTrue(sympy_expressions_equal(answer, expected))
示例#33
0
    def test_creation_of_column_matrix_from_list_of_strings(self):
        """
        Given a list of strings, to_sympy_matrix should be able to convert them into a column matrix of expresions
        """
        m = sympy.Matrix([
            sympy.sympify('x+y+3'),
            sympy.sympify('x+3'),
            sympy.sympify('y-x'),
            sympy.sympify('x+y+166')
        ])

        m_as_string = ['x+y+3', 'x+3', 'y-x', 'x+y+166']

        matrix = to_sympy_matrix(m_as_string)
        assert_sympy_expressions_equal(m, matrix)
示例#34
0
    def __init__(self, method, left_hand_side_descriptors, right_hand_side, parameters):
        """
        :param method: a string describing the method used to generate the problem.
        Currently, 'MEA' and 'LNA' are supported"
        :param left_hand_side_descriptors: the left hand side of equations as a list of
            :class:`~means.core.descriptors.Descriptor` objects (such as :class:`~means.core.descriptors.Moment`)
        :param right_hand_side: the right hand side of equations
        :param parameters: the parameters of the model
        """

        self.__left_hand_side_descriptors = left_hand_side_descriptors
        self.__left_hand_side = to_sympy_column_matrix(to_sympy_matrix(
                    [plhs.symbol for plhs in left_hand_side_descriptors])
                    )
        self.__right_hand_side = to_sympy_column_matrix(right_hand_side)
        self.__parameters = to_list_of_symbols(parameters)
        self.__method = method
示例#35
0
    def test_ode_rhs_as_function(self):
        """
        Given an ODEProblem with well specified LHS, RHS expressions as well as list of constants,
        the value of rhs_as_function given the appropriate params should be the same as the value of
        rhs evaluated for these params. The returned answer should also be an one-dimensional numpy array.
        :return:
        """
        lhs = [Moment(np.ones(3),i) for i in sympy.Matrix(['y_1', 'y_2', 'y_3'])]
        rhs = to_sympy_matrix(['y_1+y_2+c_2', 'y_2+y_3+c_3', 'y_3+c_1'])

        p = ODEProblem('MEA', lhs, rhs, parameters=sympy.symbols(['c_1', 'c_2', 'c_3']))

        rhs_as_function = p.right_hand_side_as_function

        values = [4, 5, 6]  # y_1, y_2, y_3 in that order
        expected_ans = np.array([11, 14, 7])
        actual_ans = np.array(rhs_as_function(values, [1, 2, 3]))
        self.assertEqual(actual_ans.ndim, 1)  # Returned answer must be an one-dimensional array,
                                              # otherwise ExplicitEuler solver would fail.
        assert_array_equal(actual_ans, expected_ans)
示例#36
0
 def test_compute_raw_moments(self):
     expected = to_sympy_matrix([
         ["y_2**2+yx2"],
         ["y_1*y_2+yx3"],
         ["y_1**2+yx4"],
         ["y_0*y_2+yx5"],
         ["y_0*y_1+yx6"],
         ["y_0**2+yx7"],
         ["y_2**3+3*y_2*yx2+3*yx2**2/y_2+yx2**3/y_2**3"],
         ["y_1*y_2**2+y_1*yx2+2*y_2*yx3+2*yx2*yx3/y_2+yx3**2/y_1+yx2*yx3**2/(y_1*y_2**2)"],
         ["y_1**2*y_2+2*y_1*yx3+y_2*yx4+yx3**2/y_2+2*yx3*yx4/y_1+yx3**2*yx4/(y_1**2*y_2)"],
         ["y_1**3+3*y_1*yx4+3*yx4**2/y_1+yx4**3/y_1**3"],
         ["y_0*y_2**2+y_0*yx2+2*y_2*yx5+2*yx2*yx5/y_2+yx5**2/y_0+yx2*yx5**2/(y_0*y_2**2)"],
         ["y_0*y_1*y_2+y_0*yx3+y_1*yx5+y_2*yx6+yx3*yx5/y_2+yx3*yx6/y_1+yx5*yx6/y_0+yx3*yx5*yx6/(y_0*y_1*y_2)"],
         ["y_0*y_1**2+y_0*yx4+2*y_1*yx6+2*yx4*yx6/y_1+yx6**2/y_0+yx4*yx6**2/(y_0*y_1**2)"],
         ["y_0**2*y_2+2*y_0*yx5+y_2*yx7+yx5**2/y_2+2*yx5*yx7/y_0+yx5**2*yx7/(y_0**2*y_2)"],
         ["y_0**2*y_1+2*y_0*yx6+y_1*yx7+yx6**2/y_1+2*yx6*yx7/y_0+yx6**2*yx7/(y_0**2*y_1)"],
         ["y_0**3+3*y_0*yx7+3*yx7**2/y_0+yx7**3/y_0**3"]
     ])
     closer = LogNormalClosure(2,multivariate=True)
     answer = closer._compute_raw_moments(self.__n_counter, self.__k_counter,)
     self.assertTrue(sympy_expressions_equal(answer, expected))
示例#37
0
    def test_normal_closer_wrapper(self):


        central_from_raw_exprs = to_sympy_matrix([
            ["x_0_0_2 - y_2**2"],
            ["x_0_1_1 - y_1*y_2"],
            ["x_0_2_0 - y_1**2"],
            ["x_1_0_1 - y_0*y_2"],
            ["x_1_1_0 - y_0*y_1"],
            ["x_2_0_0 - y_0**2"],
            ["-3*x_0_0_2*y_2 + x_0_0_3 + 2*y_2**3"],
            ["-x_0_0_2*y_1 - 2*x_0_1_1*y_2 + x_0_1_2 + 2*y_1*y_2**2"],
            ["-2*x_0_1_1*y_1 - x_0_2_0*y_2 + x_0_2_1 + 2*y_1**2*y_2"],
            ["-3*x_0_2_0*y_1 + x_0_3_0 + 2*y_1**3"],
            ["-x_0_0_2*y_0 - 2*x_1_0_1*y_2 + x_1_0_2 + 2*y_0*y_2**2"],
            ["-x_0_1_1*y_0 - x_1_0_1*y_1 - x_1_1_0*y_2 + x_1_1_1 + 2*y_0*y_1*y_2"],
            ["-x_0_2_0*y_0 - 2*x_1_1_0*y_1 + x_1_2_0 + 2*y_0*y_1**2"],
            ["-2*x_1_0_1*y_0 - x_2_0_0*y_2 + x_2_0_1 + 2*y_0**2*y_2"],
            ["-2*x_1_1_0*y_0 - x_2_0_0*y_1 + x_2_1_0 + 2*y_0**2*y_1"],
            ["-3*x_2_0_0*y_0 + x_3_0_0 + 2*y_0**3"]
            ])


        k_counter = [
                    Moment([0, 0, 0], symbol=sympy.Integer(1)),
                    Moment([1, 0, 0], symbol=sympy.Symbol("y_0")),
                    Moment([0, 1, 0], symbol=sympy.Symbol("y_1")),
                    Moment([0, 0, 1], symbol=sympy.Symbol("y_2")),
                    Moment([0, 0, 2], symbol=sympy.Symbol("x_0_0_2")),
                    Moment([0, 1, 1], symbol=sympy.Symbol("x_0_1_1")),
                    Moment([0, 2, 0], symbol=sympy.Symbol("x_0_2_0")),
                    Moment([1, 0, 1], symbol=sympy.Symbol("x_1_0_1")),
                    Moment([1, 1, 0], symbol=sympy.Symbol("x_1_1_0")),
                    Moment([2, 0, 0], symbol=sympy.Symbol("x_2_0_0")),
                    Moment([0, 0, 3], symbol=sympy.Symbol("x_0_0_3")),
                    Moment([0, 1, 2], symbol=sympy.Symbol("x_0_1_2")),
                    Moment([0, 2, 1], symbol=sympy.Symbol("x_0_2_1")),
                    Moment([0, 3, 0], symbol=sympy.Symbol("x_0_3_0")),
                    Moment([1, 0, 2], symbol=sympy.Symbol("x_1_0_2")),
                    Moment([1, 1, 1], symbol=sympy.Symbol("x_1_1_1")),
                    Moment([1, 2, 0], symbol=sympy.Symbol("x_1_2_0")),
                    Moment([2, 0, 1], symbol=sympy.Symbol("x_2_0_1")),
                    Moment([2, 1, 0], symbol=sympy.Symbol("x_2_1_0")),
                    Moment([3, 0, 0], symbol=sympy.Symbol("x_3_0_0"))
                ]

        max_order = 2
        prob_moments = self.__problem_moments
        expected = to_sympy_matrix([
            ["c_0-c_1*y_0-(c_2*c_6*yx5)/(c_6+y_0) ** 2-(c_2*y_0*y_2)/(c_6+y_0)+(c_2*c_6*y_2*yx7)/(c_6+y_0) ** 3"],
            ["c_3*y_0-c_4*y_1"],
            ["c_4*y_1-c_5*y_2"],
            ["c_4*y_1+c_5*y_2+2*c_4*yx3-2*c_5*yx2"],
            ["c_3*yx5-c_4*yx3-c_4*y_1+c_4*yx4-c_5*yx3"],
            ["c_3*y_0+c_4*y_1-2*c_4*yx4+2*c_3*yx6"],
            ["c_4*yx6-c_1*yx5-c_5*yx5-(c_2*y_0*yx2)/(c_6+y_0)-(c_2*y_2*yx5)/(c_6+y_0)+(c_2*y_0*y_2*yx5)/(c_6+y_0) ** 2"],
            ["c_3*yx7-c_1*yx6-c_4*yx6-(c_2*y_0*yx3)/(c_6+y_0)-(c_2*y_2*yx6)/(c_6+y_0)+(c_2*y_0*y_2*yx6)/(c_6+y_0) ** 2"],
            ["(c_0*c_6 ** 3+c_0*y_0 ** 3+c_1*y_0 ** 4+c_2*y_0 ** 3*y_2-2*c_2*y_0 ** 3*yx5-2*c_1*y_0 ** 3*yx7+3*c_1*c_6 ** 2*y_0 ** 2+3*c_0*c_6*y_0 ** 2+3*c_0*c_6 ** 2*y_0+3*c_1*c_6*y_0 ** 3+c_1*c_6 ** 3*y_0+c_2*c_6 ** 2*yx5-2*c_1*c_6 ** 3*yx7+c_2*c_6*y_0*yx5-c_2*c_6*y_2*yx7+2*c_2*c_6*y_0 ** 2*y_2+c_2*c_6 ** 2*y_0*y_2-4*c_2*c_6*y_0 ** 2*yx5-2*c_2*c_6 ** 2*y_0*yx5-6*c_1*c_6*y_0 ** 2*yx7-6*c_1*c_6 ** 2*y_0*yx7-2*c_2*c_6 ** 2*y_2*yx7-2*c_2*c_6*y_0*y_2*yx7)/(c_6+y_0) ** 3"]
        ])
        closer = NormalClosure(max_order, multivariate=True)
        answer = closer.close(self.__mfk, central_from_raw_exprs,self.__n_counter, k_counter)

        self.assertTrue(sympy_expressions_equal(answer, expected))
示例#38
0
class TestLogNormalCloser(unittest.TestCase):
    __n_counter = [
    Moment([0, 0, 0], symbol=sympy.Integer(0)),
    Moment([0, 0, 2], symbol=sympy.Symbol("yx2")),
    Moment([0, 1, 1], symbol=sympy.Symbol("yx3")),
    Moment([0, 2, 0], symbol=sympy.Symbol("yx4")),
    Moment([1, 0, 1], symbol=sympy.Symbol("yx5")),
    Moment([1, 1, 0], symbol=sympy.Symbol("yx6")),
    Moment([2, 0, 0], symbol=sympy.Symbol("yx7")),
    Moment([0, 0, 3], symbol=sympy.Symbol("yx8")),
    Moment([0, 1, 2], symbol=sympy.Symbol("yx9")),
    Moment([0, 2, 1], symbol=sympy.Symbol("yx10")),
    Moment([0, 3, 0], symbol=sympy.Symbol("yx11")),
    Moment([1, 0, 2], symbol=sympy.Symbol("yx12")),
    Moment([1, 1, 1], symbol=sympy.Symbol("yx13")),
    Moment([1, 2, 0], symbol=sympy.Symbol("yx14")),
    Moment([2, 0, 1], symbol=sympy.Symbol("yx15")),
    Moment([2, 1, 0], symbol=sympy.Symbol("yx16")),
    Moment([3, 0, 0], symbol=sympy.Symbol("yx17")),

    ]
    __k_counter = [
        Moment([0, 0, 0], symbol=sympy.Integer(1)),
        Moment([1, 0, 0], symbol=sympy.Symbol("y_0")),
        Moment([0, 1, 0], symbol=sympy.Symbol("y_1")),
        Moment([0, 0, 1], symbol=sympy.Symbol("y_2")),
        Moment([0, 0, 2], symbol=sympy.Symbol("x_0_0_2")),
        Moment([0, 1, 1], symbol=sympy.Symbol("x_0_1_1")),
        Moment([0, 2, 0], symbol=sympy.Symbol("x_0_2_0")),
        Moment([1, 0, 1], symbol=sympy.Symbol("x_1_0_1")),
        Moment([1, 1, 0], symbol=sympy.Symbol("x_1_1_0")),
        Moment([2, 0, 0], symbol=sympy.Symbol("x_2_0_0")),
        Moment([0, 0, 3], symbol=sympy.Symbol("x_0_0_3")),
        Moment([0, 1, 2], symbol=sympy.Symbol("x_0_1_2")),
        Moment([0, 2, 1], symbol=sympy.Symbol("x_0_2_1")),
        Moment([0, 3, 0], symbol=sympy.Symbol("x_0_3_0")),
        Moment([1, 0, 2], symbol=sympy.Symbol("x_1_0_2")),
        Moment([1, 1, 1], symbol=sympy.Symbol("x_1_1_1")),
        Moment([1, 2, 0], symbol=sympy.Symbol("x_1_2_0")),
        Moment([2, 0, 1], symbol=sympy.Symbol("x_2_0_1")),
        Moment([2, 1, 0], symbol=sympy.Symbol("x_2_1_0")),
        Moment([3, 0, 0], symbol=sympy.Symbol("x_3_0_0"))
    ]
    __mfk = to_sympy_matrix([
    ["c_0 - c_1*y_0 - c_2*y_0*y_2/(c_6 + y_0) - c_2*y_2*yx17*(-y_0/(c_6 + y_0) + 1)/(c_6 + y_0)**3 - c_2*y_2*yx7*(y_0/(c_6 + y_0) - 1)/(c_6 + y_0)**2 - c_2*yx15*(y_0/(c_6 + y_0) - 1)/(c_6 + y_0)**2 - c_2*yx5*(-y_0/(c_6 + y_0) + 1)/(c_6 + y_0)"],
    ["c_3*y_0 - c_4*y_1"],
    ["c_4*y_1 - c_5*y_2"],
    ["2*c_4*y_1*y_2 + c_4*y_1 + 2*c_4*yx3 - 2*c_5*y_2**2 + c_5*y_2 - 2*c_5*yx2 - 2*y_2*(c_4*y_1 - c_5*y_2)"],
    ["c_3*y_0*y_2 + c_3*yx5 + c_4*y_1**2 - c_4*y_1*y_2 - c_4*y_1 + c_4*yx4 - c_5*y_1*y_2 - y_1*(c_4*y_1 - c_5*y_2) - y_2*(c_3*y_0 - c_4*y_1) + yx3*(-c_4 - c_5)"],
    ["2*c_3*y_0*y_1 + c_3*y_0 + 2*c_3*yx6 - 2*c_4*y_1**2 + c_4*y_1 - 2*c_4*yx4 - 2*y_1*(c_3*y_0 - c_4*y_1)"],
    ["c_0*y_2 - c_1*y_0*y_2 - c_2*y_0*y_2**2/(c_6 + y_0) - c_2*y_0*yx2/(c_6 + y_0) - c_2*y_2*yx15*(y_0/(c_6 + y_0) - 1)/(c_6 + y_0)**2 - c_2*yx12*(-y_0/(c_6 + y_0) + 1)/(c_6 + y_0) + c_4*y_0*y_1 + c_4*yx6 - c_5*y_0*y_2 - y_0*(c_4*y_1 - c_5*y_2) - y_2*(c_0 - c_1*y_0 - c_2*y_0*y_2/(c_6 + y_0)) + yx5*(-c_1 - c_2*y_2*(-y_0/(c_6 + y_0) + 1)/(c_6 + y_0) - c_5)"],
    ["c_0*y_1 - c_1*y_0*y_1 - c_2*y_0*y_1*y_2/(c_6 + y_0) - c_2*y_0*yx3/(c_6 + y_0) - c_2*y_2*yx16*(y_0/(c_6 + y_0) - 1)/(c_6 + y_0)**2 - c_2*yx13*(-y_0/(c_6 + y_0) + 1)/(c_6 + y_0) + c_3*y_0**2 + c_3*yx7 - c_4*y_0*y_1 - y_0*(c_3*y_0 - c_4*y_1) - y_1*(c_0 - c_1*y_0 - c_2*y_0*y_2/(c_6 + y_0)) + yx6*(-c_1 - c_2*y_2*(-y_0/(c_6 + y_0) + 1)/(c_6 + y_0) - c_4)"],
    ["2*c_0*y_0 + c_0 - 2*c_1*y_0**2 + c_1*y_0 - 2*c_2*y_0**2*y_2/(c_6 + y_0) + c_2*y_0*y_2/(c_6 + y_0) - 2*y_0*(c_0 - c_1*y_0 - c_2*y_0*y_2/(c_6 + y_0)) + yx15*(2*c_2*y_0*(y_0/(c_6 + y_0) - 1)/(c_6 + y_0)**2 - 2*c_2*(y_0**2/(c_6 + y_0)**2 - 2*y_0/(c_6 + y_0) + 1)/(c_6 + y_0) + c_2*(y_0/(c_6 + y_0) - 1)/(c_6 + y_0)**2) + yx17*(2*c_2*y_0*y_2*(-y_0/(c_6 + y_0) + 1)/(c_6 + y_0)**3 - 2*c_2*y_2*(-y_0**2/(c_6 + y_0)**2 + 2*y_0/(c_6 + y_0) - 1)/(c_6 + y_0)**2 + c_2*y_2*(-y_0/(c_6 + y_0) + 1)/(c_6 + y_0)**3) + yx5*(2*c_2*y_0*(-y_0/(c_6 + y_0) + 1)/(c_6 + y_0) - 2*c_2*y_0*(-y_0/(c_6 + y_0) + 2)/(c_6 + y_0) + c_2*(-y_0/(c_6 + y_0) + 1)/(c_6 + y_0)) + yx7*(-2*c_1 + 2*c_2*y_0*y_2*(y_0/(c_6 + y_0) - 1)/(c_6 + y_0)**2 - 2*c_2*y_2*(y_0**2/(c_6 + y_0)**2 - 2*y_0/(c_6 + y_0) + 1)/(c_6 + y_0) + c_2*y_2*(y_0/(c_6 + y_0) - 1)/(c_6 + y_0)**2)"]
    ])

    def test_close_type_one(self):
        central_from_raw_exprs = to_sympy_matrix(
                    [["x_0_0_2-y_2**2"],
                    ["x_0_1_1-y_1*y_2"],
                    ["x_0_2_0-y_1**2"],
                    ["x_1_0_1-y_0*y_2"],
                    ["x_1_1_0-y_0*y_1"],
                    ["x_2_0_0-y_0**2"],
                    ["-3*x_0_0_2*y_2+x_0_0_3+2*y_2**3"],
                    ["-x_0_0_2*y_1-2*x_0_1_1*y_2+x_0_1_2+2*y_1*y_2**2"],
                    ["-2*x_0_1_1*y_1-x_0_2_0*y_2+x_0_2_1+2*y_1**2*y_2"],
                    ["-3*x_0_2_0*y_1+x_0_3_0+2*y_1**3"],
                    ["-x_0_0_2*y_0-2*x_1_0_1*y_2+x_1_0_2+2*y_0*y_2**2"],
                    ["-x_0_1_1*y_0-x_1_0_1*y_1-x_1_1_0*y_2+x_1_1_1+2*y_0*y_1*y_2"],
                    ["-x_0_2_0*y_0-2*x_1_1_0*y_1+x_1_2_0+2*y_0*y_1**2"],
                    ["-2*x_1_0_1*y_0-x_2_0_0*y_2+x_2_0_1+2*y_0**2*y_2"],
                    ["-2*x_1_1_0*y_0-x_2_0_0*y_1+x_2_1_0+2*y_0**2*y_1"],
                    ["-3*x_2_0_0*y_0+x_3_0_0+2*y_0**3"]
         ])


        max_order = 2

        expected = to_sympy_matrix([
            ["yx5*(c_2*y_0/(c_6 + y_0)**2 - c_2/(c_6 + y_0)) + yx7*(-c_2*y_0*y_2/(c_6 + y_0)**3 + c_2*y_2/(c_6 + y_0)**2) + c_0 - c_1*y_0 - c_2*y_0*y_2/(c_6 + y_0) + (-c_2*y_0/(c_6 + y_0)**3 + c_2/(c_6 + y_0)**2)*(2*yx5*yx7/y_0 + 2*yx5*y_0 + yx7*y_2 + 3*y_0**2*y_2 - 2*y_0*(yx5 + y_0*y_2) - y_2*(yx7 + y_0**2)) + (c_2*y_0*y_2/(c_6 + y_0)**4 - c_2*y_2/(c_6 + y_0)**3)*(2*yx7**2/y_0 + 3*yx7*y_0 + 3*y_0**3 - 3*y_0*(yx7 + y_0**2))"],
            ["c_3*y_0-c_4*y_1"],
            ["c_4*y_1-c_5*y_2"],
            ["c_4*y_1+c_5*y_2+2*c_4*yx3-2*c_5*yx2"],
            ["c_3*yx5-c_4*yx3-c_4*y_1+c_4*yx4-c_5*yx3"],
            ["c_3*y_0+c_4*y_1-2*c_4*yx4+2*c_3*yx6"],
            ["-yx2*c_2*y_0/(c_6 + y_0) + yx5*(-c_1 + 2*c_2*y_0*y_2/(c_6 + y_0)**2 - 2*c_2*y_2/(c_6 + y_0) - c_5 - y_2*(c_2*y_0/(c_6 + y_0)**2 - c_2/(c_6 + y_0))) + yx6*c_4 + yx7*(-c_2*y_0*y_2**2/(c_6 + y_0)**3 + c_2*y_2**2/(c_6 + y_0)**2 - y_2*(-c_2*y_0*y_2/(c_6 + y_0)**3 + c_2*y_2/(c_6 + y_0)**2)) + c_0*y_2 - c_1*y_0*y_2 - c_2*y_0*y_2**2/(c_6 + y_0) + c_4*y_0*y_1 - c_5*y_0*y_2 - y_0*(c_4*y_1 - c_5*y_2) - y_2*(c_0 - c_1*y_0 - c_2*y_0*y_2/(c_6 + y_0)) + (c_2*y_0/(c_6 + y_0)**2 - c_2/(c_6 + y_0))*(2*yx2*yx5/y_2 + yx2*y_0 + 2*yx5*y_2 + 3*y_0*y_2**2 - y_0*(yx2 + y_2**2) - 2*y_2*(yx5 + y_0*y_2)) + (-2*c_2*y_0*y_2/(c_6 + y_0)**3 + 2*c_2*y_2/(c_6 + y_0)**2 - y_2*(-c_2*y_0/(c_6 + y_0)**3 + c_2/(c_6 + y_0)**2))*(2*yx5*yx7/y_0 + 2*yx5*y_0 + yx7*y_2 + 3*y_0**2*y_2 - 2*y_0*(yx5 + y_0*y_2) - y_2*(yx7 + y_0**2)) + (c_2*y_0*y_2**2/(c_6 + y_0)**4 - c_2*y_2**2/(c_6 + y_0)**3 - y_2*(c_2*y_0*y_2/(c_6 + y_0)**4 - c_2*y_2/(c_6 + y_0)**3))*(2*yx7**2/y_0 + 3*yx7*y_0 + 3*y_0**3 - 3*y_0*(yx7 + y_0**2))"],
            ["-yx3*c_2*y_0/(c_6 + y_0) + yx5*(c_2*y_0*y_1/(c_6 + y_0)**2 - c_2*y_1/(c_6 + y_0) - y_1*(c_2*y_0/(c_6 + y_0)**2 - c_2/(c_6 + y_0))) + yx6*(-c_1 + c_2*y_0*y_2/(c_6 + y_0)**2 - c_2*y_2/(c_6 + y_0) - c_4) + yx7*(-c_2*y_0*y_1*y_2/(c_6 + y_0)**3 + c_2*y_1*y_2/(c_6 + y_0)**2 + c_3 - y_1*(-c_2*y_0*y_2/(c_6 + y_0)**3 + c_2*y_2/(c_6 + y_0)**2)) + c_0*y_1 - c_1*y_0*y_1 - c_2*y_0*y_1*y_2/(c_6 + y_0) + c_3*y_0**2 - c_4*y_0*y_1 - y_0*(c_3*y_0 - c_4*y_1) - y_1*(c_0 - c_1*y_0 - c_2*y_0*y_2/(c_6 + y_0)) + (c_2*y_0/(c_6 + y_0)**2 - c_2/(c_6 + y_0))*(yx3*y_0 + yx5*y_1 + yx6*y_2 + 3*y_0*y_1*y_2 - y_0*(yx3 + y_1*y_2) - y_1*(yx5 + y_0*y_2) - y_2*(yx6 + y_0*y_1)) + (-c_2*y_0*y_2/(c_6 + y_0)**3 + c_2*y_2/(c_6 + y_0)**2)*(2*yx6*yx7/y_0 + 2*yx6*y_0 + yx7*y_1 + 3*y_0**2*y_1 - 2*y_0*(yx6 + y_0*y_1) - y_1*(yx7 + y_0**2)) + (-c_2*y_0*y_1/(c_6 + y_0)**3 + c_2*y_1/(c_6 + y_0)**2 - y_1*(-c_2*y_0/(c_6 + y_0)**3 + c_2/(c_6 + y_0)**2))*(2*yx5*yx7/y_0 + 2*yx5*y_0 + yx7*y_2 + 3*y_0**2*y_2 - 2*y_0*(yx5 + y_0*y_2) - y_2*(yx7 + y_0**2)) + (c_2*y_0*y_1*y_2/(c_6 + y_0)**4 - c_2*y_1*y_2/(c_6 + y_0)**3 - y_1*(c_2*y_0*y_2/(c_6 + y_0)**4 - c_2*y_2/(c_6 + y_0)**3))*(2*yx7**2/y_0 + 3*yx7*y_0 + 3*y_0**3 - 3*y_0*(yx7 + y_0**2))"],
            ["yx5*(2*c_2*y_0**2/(c_6 + y_0)**2 - 4*c_2*y_0/(c_6 + y_0) - c_2*y_0/(c_6 + y_0)**2 + c_2/(c_6 + y_0) - 2*y_0*(c_2*y_0/(c_6 + y_0)**2 - c_2/(c_6 + y_0))) + yx7*(-2*c_1 - 2*c_2*y_0**2*y_2/(c_6 + y_0)**3 + 4*c_2*y_0*y_2/(c_6 + y_0)**2 + c_2*y_0*y_2/(c_6 + y_0)**3 - 2*c_2*y_2/(c_6 + y_0) - c_2*y_2/(c_6 + y_0)**2 - 2*y_0*(-c_2*y_0*y_2/(c_6 + y_0)**3 + c_2*y_2/(c_6 + y_0)**2)) + 2*c_0*y_0 + c_0 - 2*c_1*y_0**2 + c_1*y_0 - 2*c_2*y_0**2*y_2/(c_6 + y_0) + c_2*y_0*y_2/(c_6 + y_0) - 2*y_0*(c_0 - c_1*y_0 - c_2*y_0*y_2/(c_6 + y_0)) + (2*yx7**2/y_0 + 3*yx7*y_0 + 3*y_0**3 - 3*y_0*(yx7 + y_0**2))*(2*c_2*y_0**2*y_2/(c_6 + y_0)**4 - 4*c_2*y_0*y_2/(c_6 + y_0)**3 - c_2*y_0*y_2/(c_6 + y_0)**4 + 2*c_2*y_2/(c_6 + y_0)**2 + c_2*y_2/(c_6 + y_0)**3 - 2*y_0*(c_2*y_0*y_2/(c_6 + y_0)**4 - c_2*y_2/(c_6 + y_0)**3)) + (2*yx5*yx7/y_0 + 2*yx5*y_0 + yx7*y_2 + 3*y_0**2*y_2 - 2*y_0*(yx5 + y_0*y_2) - y_2*(yx7 + y_0**2))*(-2*c_2*y_0**2/(c_6 + y_0)**3 + 4*c_2*y_0/(c_6 + y_0)**2 + c_2*y_0/(c_6 + y_0)**3 - 2*c_2/(c_6 + y_0) - c_2/(c_6 + y_0)**2 - 2*y_0*(-c_2*y_0/(c_6 + y_0)**3 + c_2/(c_6 + y_0)**2))"]
        ])

        closer = GammaClosure(max_order, multivariate=True)
        answer = closer.close(self.__mfk, central_from_raw_exprs, self.__n_counter, self.__k_counter)
        self.assertTrue(sympy_expressions_equal(answer, expected))


    def test_close_type_zero(self):

        central_from_raw_exprs = to_sympy_matrix(
                    [["x_0_0_2-y_2**2"],
                    ["x_0_1_1-y_1*y_2"],
                    ["x_0_2_0-y_1**2"],
                    ["x_1_0_1-y_0*y_2"],
                    ["x_1_1_0-y_0*y_1"],
                    ["x_2_0_0-y_0**2"],
                    ["-3*x_0_0_2*y_2+x_0_0_3+2*y_2**3"],
                    ["-x_0_0_2*y_1-2*x_0_1_1*y_2+x_0_1_2+2*y_1*y_2**2"],
                    ["-2*x_0_1_1*y_1-x_0_2_0*y_2+x_0_2_1+2*y_1**2*y_2"],
                    ["-3*x_0_2_0*y_1+x_0_3_0+2*y_1**3"],
                    ["-x_0_0_2*y_0-2*x_1_0_1*y_2+x_1_0_2+2*y_0*y_2**2"],
                    ["-x_0_1_1*y_0-x_1_0_1*y_1-x_1_1_0*y_2+x_1_1_1+2*y_0*y_1*y_2"],
                    ["-x_0_2_0*y_0-2*x_1_1_0*y_1+x_1_2_0+2*y_0*y_1**2"],
                    ["-2*x_1_0_1*y_0-x_2_0_0*y_2+x_2_0_1+2*y_0**2*y_2"],
                    ["-2*x_1_1_0*y_0-x_2_0_0*y_1+x_2_1_0+2*y_0**2*y_1"],
                    ["-3*x_2_0_0*y_0+x_3_0_0+2*y_0**3"]
         ])

        max_order = 2
        expected = to_sympy_matrix([
            ["c_0-c_1*y_0-(c_2*c_6*yx5)/(c_6+y_0) ** 2-(c_2*y_0*y_2)/(c_6+y_0)+(c_2*c_6*y_2*yx7)/(c_6+y_0) ** 3-(2*c_2*c_6*y_2*yx7 ** 2)/(y_0*(c_6+y_0) ** 4)"],
            ["c_3*y_0-c_4*y_1"],
            ["c_4*y_1-c_5*y_2"],
            ["c_4*y_1+c_5*y_2+2*c_4*yx3-2*c_5*yx2"],
            ["c_3*yx5-c_4*yx3-c_4*y_1+c_4*yx4-c_5*yx3"],
            ["c_3*y_0+c_4*y_1-2*c_4*yx4+2*c_3*yx6"],
            ["c_4*yx6-c_1*yx5-c_5*yx5-(c_2*y_0*yx2)/(c_6+y_0)-(c_2*y_2*yx5)/(c_6+y_0)+(c_2*y_0*y_2*yx5)/(c_6+y_0) ** 2"],
            ["c_3*yx7-c_1*yx6-c_4*yx6-(c_2*y_0*yx3)/(c_6+y_0)-(c_2*y_2*yx6)/(c_6+y_0)+(c_2*y_0*y_2*yx6)/(c_6+y_0) ** 2"],
            ["(c_0*y_0 ** 5+c_1*y_0 ** 6+c_2*y_0 ** 5*y_2-2*c_2*y_0 ** 5*yx5-2*c_1*y_0 ** 5*yx7+6*c_0*c_6 ** 2*y_0 ** 3+4*c_0*c_6 ** 3*y_0 ** 2+6*c_1*c_6 ** 2*y_0 ** 4+4*c_1*c_6 ** 3*y_0 ** 3+c_1*c_6 ** 4*y_0 ** 2+4*c_0*c_6*y_0 ** 4+c_0*c_6 ** 4*y_0+4*c_1*c_6*y_0 ** 5+3*c_2*c_6*y_0 ** 4*y_2+c_2*c_6*y_0 ** 3*yx5+c_2*c_6 ** 3*y_0*yx5-6*c_2*c_6*y_0 ** 4*yx5-8*c_1*c_6*y_0 ** 4*yx7-2*c_1*c_6 ** 4*y_0*yx7+2*c_2*c_6*y_2*yx7 ** 2+3*c_2*c_6 ** 2*y_0 ** 3*y_2+c_2*c_6 ** 3*y_0 ** 2*y_2+2*c_2*c_6 ** 2*y_0 ** 2*yx5-6*c_2*c_6 ** 2*y_0 ** 3*yx5-2*c_2*c_6 ** 3*y_0 ** 2*yx5-12*c_1*c_6 ** 2*y_0 ** 3*yx7-8*c_1*c_6 ** 3*y_0 ** 2*yx7+4*c_2*c_6 ** 2*y_2*yx7 ** 2-4*c_2*c_6 ** 2*y_0 ** 2*y_2*yx7+4*c_2*c_6*y_0*y_2*yx7 ** 2-c_2*c_6*y_0 ** 2*y_2*yx7-c_2*c_6 ** 2*y_0*y_2*yx7-2*c_2*c_6*y_0 ** 3*y_2*yx7-2*c_2*c_6 ** 3*y_0*y_2*yx7)/(y_0*(c_6+y_0) ** 4)"]
        ])
        closer = GammaClosure(max_order, multivariate=False)
        answer = closer.close(self.__mfk, central_from_raw_exprs, self.__n_counter, self.__k_counter)
        self.assertTrue(sympy_expressions_equal(answer, expected))
示例#39
0
class TestLogNormalCloser(unittest.TestCase):
    __n_counter = [
        Moment([0, 0, 0], symbol=sympy.Integer(0)),
        Moment([0, 0, 2], symbol=sympy.Symbol("yx2")),
        Moment([0, 1, 1], symbol=sympy.Symbol("yx3")),
        Moment([0, 2, 0], symbol=sympy.Symbol("yx4")),
        Moment([1, 0, 1], symbol=sympy.Symbol("yx5")),
        Moment([1, 1, 0], symbol=sympy.Symbol("yx6")),
        Moment([2, 0, 0], symbol=sympy.Symbol("yx7")),
        Moment([0, 0, 3], symbol=sympy.Symbol("yx8")),
        Moment([0, 1, 2], symbol=sympy.Symbol("yx9")),
        Moment([0, 2, 1], symbol=sympy.Symbol("yx10")),
        Moment([0, 3, 0], symbol=sympy.Symbol("yx11")),
        Moment([1, 0, 2], symbol=sympy.Symbol("yx12")),
        Moment([1, 1, 1], symbol=sympy.Symbol("yx13")),
        Moment([1, 2, 0], symbol=sympy.Symbol("yx14")),
        Moment([2, 0, 1], symbol=sympy.Symbol("yx15")),
        Moment([2, 1, 0], symbol=sympy.Symbol("yx16")),
        Moment([3, 0, 0], symbol=sympy.Symbol("yx17")),
    ]
    __k_counter = [
        Moment([0, 0, 0], symbol=sympy.Integer(1)),
        Moment([1, 0, 0], symbol=sympy.Symbol("y_0")),
        Moment([0, 1, 0], symbol=sympy.Symbol("y_1")),
        Moment([0, 0, 1], symbol=sympy.Symbol("y_2")),
        Moment([0, 0, 2], symbol=sympy.Symbol("x_0_0_2")),
        Moment([0, 1, 1], symbol=sympy.Symbol("x_0_1_1")),
        Moment([0, 2, 0], symbol=sympy.Symbol("x_0_2_0")),
        Moment([1, 0, 1], symbol=sympy.Symbol("x_1_0_1")),
        Moment([1, 1, 0], symbol=sympy.Symbol("x_1_1_0")),
        Moment([2, 0, 0], symbol=sympy.Symbol("x_2_0_0")),
        Moment([0, 0, 3], symbol=sympy.Symbol("x_0_0_3")),
        Moment([0, 1, 2], symbol=sympy.Symbol("x_0_1_2")),
        Moment([0, 2, 1], symbol=sympy.Symbol("x_0_2_1")),
        Moment([0, 3, 0], symbol=sympy.Symbol("x_0_3_0")),
        Moment([1, 0, 2], symbol=sympy.Symbol("x_1_0_2")),
        Moment([1, 1, 1], symbol=sympy.Symbol("x_1_1_1")),
        Moment([1, 2, 0], symbol=sympy.Symbol("x_1_2_0")),
        Moment([2, 0, 1], symbol=sympy.Symbol("x_2_0_1")),
        Moment([2, 1, 0], symbol=sympy.Symbol("x_2_1_0")),
        Moment([3, 0, 0], symbol=sympy.Symbol("x_3_0_0"))
    ]
    __mfk = to_sympy_matrix([
        [
            "c_0 - c_1*y_0 - c_2*y_0*y_2/(c_6 + y_0) - c_2*y_2*yx17*(-y_0/(c_6 + y_0) + 1)/(c_6 + y_0)**3 - c_2*y_2*yx7*(y_0/(c_6 + y_0) - 1)/(c_6 + y_0)**2 - c_2*yx15*(y_0/(c_6 + y_0) - 1)/(c_6 + y_0)**2 - c_2*yx5*(-y_0/(c_6 + y_0) + 1)/(c_6 + y_0)"
        ], ["c_3*y_0 - c_4*y_1"], ["c_4*y_1 - c_5*y_2"],
        [
            "2*c_4*y_1*y_2 + c_4*y_1 + 2*c_4*yx3 - 2*c_5*y_2**2 + c_5*y_2 - 2*c_5*yx2 - 2*y_2*(c_4*y_1 - c_5*y_2)"
        ],
        [
            "c_3*y_0*y_2 + c_3*yx5 + c_4*y_1**2 - c_4*y_1*y_2 - c_4*y_1 + c_4*yx4 - c_5*y_1*y_2 - y_1*(c_4*y_1 - c_5*y_2) - y_2*(c_3*y_0 - c_4*y_1) + yx3*(-c_4 - c_5)"
        ],
        [
            "2*c_3*y_0*y_1 + c_3*y_0 + 2*c_3*yx6 - 2*c_4*y_1**2 + c_4*y_1 - 2*c_4*yx4 - 2*y_1*(c_3*y_0 - c_4*y_1)"
        ],
        [
            "c_0*y_2 - c_1*y_0*y_2 - c_2*y_0*y_2**2/(c_6 + y_0) - c_2*y_0*yx2/(c_6 + y_0) - c_2*y_2*yx15*(y_0/(c_6 + y_0) - 1)/(c_6 + y_0)**2 - c_2*yx12*(-y_0/(c_6 + y_0) + 1)/(c_6 + y_0) + c_4*y_0*y_1 + c_4*yx6 - c_5*y_0*y_2 - y_0*(c_4*y_1 - c_5*y_2) - y_2*(c_0 - c_1*y_0 - c_2*y_0*y_2/(c_6 + y_0)) + yx5*(-c_1 - c_2*y_2*(-y_0/(c_6 + y_0) + 1)/(c_6 + y_0) - c_5)"
        ],
        [
            "c_0*y_1 - c_1*y_0*y_1 - c_2*y_0*y_1*y_2/(c_6 + y_0) - c_2*y_0*yx3/(c_6 + y_0) - c_2*y_2*yx16*(y_0/(c_6 + y_0) - 1)/(c_6 + y_0)**2 - c_2*yx13*(-y_0/(c_6 + y_0) + 1)/(c_6 + y_0) + c_3*y_0**2 + c_3*yx7 - c_4*y_0*y_1 - y_0*(c_3*y_0 - c_4*y_1) - y_1*(c_0 - c_1*y_0 - c_2*y_0*y_2/(c_6 + y_0)) + yx6*(-c_1 - c_2*y_2*(-y_0/(c_6 + y_0) + 1)/(c_6 + y_0) - c_4)"
        ],
        [
            "2*c_0*y_0 + c_0 - 2*c_1*y_0**2 + c_1*y_0 - 2*c_2*y_0**2*y_2/(c_6 + y_0) + c_2*y_0*y_2/(c_6 + y_0) - 2*y_0*(c_0 - c_1*y_0 - c_2*y_0*y_2/(c_6 + y_0)) + yx15*(2*c_2*y_0*(y_0/(c_6 + y_0) - 1)/(c_6 + y_0)**2 - 2*c_2*(y_0**2/(c_6 + y_0)**2 - 2*y_0/(c_6 + y_0) + 1)/(c_6 + y_0) + c_2*(y_0/(c_6 + y_0) - 1)/(c_6 + y_0)**2) + yx17*(2*c_2*y_0*y_2*(-y_0/(c_6 + y_0) + 1)/(c_6 + y_0)**3 - 2*c_2*y_2*(-y_0**2/(c_6 + y_0)**2 + 2*y_0/(c_6 + y_0) - 1)/(c_6 + y_0)**2 + c_2*y_2*(-y_0/(c_6 + y_0) + 1)/(c_6 + y_0)**3) + yx5*(2*c_2*y_0*(-y_0/(c_6 + y_0) + 1)/(c_6 + y_0) - 2*c_2*y_0*(-y_0/(c_6 + y_0) + 2)/(c_6 + y_0) + c_2*(-y_0/(c_6 + y_0) + 1)/(c_6 + y_0)) + yx7*(-2*c_1 + 2*c_2*y_0*y_2*(y_0/(c_6 + y_0) - 1)/(c_6 + y_0)**2 - 2*c_2*y_2*(y_0**2/(c_6 + y_0)**2 - 2*y_0/(c_6 + y_0) + 1)/(c_6 + y_0) + c_2*y_2*(y_0/(c_6 + y_0) - 1)/(c_6 + y_0)**2)"
        ]
    ])

    def test_get_log_covariance(self):

        log_variance_mat = to_sympy_matrix([["log(1+yx7/y_0**2)", "0", "0"],
                                            ["0", "log(1+yx4/y_1**2)", "0"],
                                            ["0", "0", "log(1+yx2/y_2**2)"]])

        log_expectation_symbols = to_sympy_matrix([[
            "log(y_0)-log(1+yx7/y_0**2)/2"
        ], ["log(y_1)-log(1+yx4/y_1**2)/2"], ["log(y_2)-log(1+yx2/y_2**2)/2"]])

        covariance_matrix = to_sympy_matrix([["yx7", "yx6", "yx5"],
                                             ["yx6", "yx4", "yx3"],
                                             ["yx5", "yx3", "yx2"]])

        expected = sympy.sympify("log(1 + yx6/(y_0*y_1))")

        closer = LogNormalClosure(2, multivariate=True)
        answer = closer._get_log_covariance(log_variance_mat,
                                            log_expectation_symbols,
                                            covariance_matrix, 0, 1)

        self.assertEqual(answer, expected)

        answer1 = closer._get_log_covariance(log_variance_mat,
                                             log_expectation_symbols,
                                             covariance_matrix, 1, 2)
        answer2 = closer._get_log_covariance(log_variance_mat,
                                             log_expectation_symbols,
                                             covariance_matrix, 1, 2)
        #logcovariance between species 1 and 2  ==  covariance between sp. 2 and 1
        self.assertEqual(answer1, answer2)

    def test_get_covariance_symbol(self):
        closer = LogNormalClosure(3, multivariate=True)
        expected = sympy.Symbol("yx3")
        answer = closer._get_covariance_symbol(self.__n_counter, 1, 2)
        self.assertEqual(answer, expected)

    def test_get_covariance_symbol2(self):
        closer = LogNormalClosure(3, multivariate=True)
        expected = sympy.Symbol("yx6")
        answer = closer._get_covariance_symbol(self.__n_counter, 1, 0)
        self.assertEqual(answer, expected)

    def test_get_covariance_symbol_is_triangular(self):
        closer = LogNormalClosure(3, multivariate=True)

        #covariance between species 1 and 2  ==  covariance between sp. 2 and 1
        answer1 = closer._get_covariance_symbol(self.__n_counter, 1, 0)
        answer2 = closer._get_covariance_symbol(self.__n_counter, 0, 1)
        self.assertEqual(answer1, answer2)

    def test_compute_raw_moments(self):
        expected = to_sympy_matrix([
            ["y_2**2+yx2"], ["y_1*y_2+yx3"], ["y_1**2+yx4"], ["y_0*y_2+yx5"],
            ["y_0*y_1+yx6"], ["y_0**2+yx7"],
            ["y_2**3+3*y_2*yx2+3*yx2**2/y_2+yx2**3/y_2**3"],
            [
                "y_1*y_2**2+y_1*yx2+2*y_2*yx3+2*yx2*yx3/y_2+yx3**2/y_1+yx2*yx3**2/(y_1*y_2**2)"
            ],
            [
                "y_1**2*y_2+2*y_1*yx3+y_2*yx4+yx3**2/y_2+2*yx3*yx4/y_1+yx3**2*yx4/(y_1**2*y_2)"
            ], ["y_1**3+3*y_1*yx4+3*yx4**2/y_1+yx4**3/y_1**3"],
            [
                "y_0*y_2**2+y_0*yx2+2*y_2*yx5+2*yx2*yx5/y_2+yx5**2/y_0+yx2*yx5**2/(y_0*y_2**2)"
            ],
            [
                "y_0*y_1*y_2+y_0*yx3+y_1*yx5+y_2*yx6+yx3*yx5/y_2+yx3*yx6/y_1+yx5*yx6/y_0+yx3*yx5*yx6/(y_0*y_1*y_2)"
            ],
            [
                "y_0*y_1**2+y_0*yx4+2*y_1*yx6+2*yx4*yx6/y_1+yx6**2/y_0+yx4*yx6**2/(y_0*y_1**2)"
            ],
            [
                "y_0**2*y_2+2*y_0*yx5+y_2*yx7+yx5**2/y_2+2*yx5*yx7/y_0+yx5**2*yx7/(y_0**2*y_2)"
            ],
            [
                "y_0**2*y_1+2*y_0*yx6+y_1*yx7+yx6**2/y_1+2*yx6*yx7/y_0+yx6**2*yx7/(y_0**2*y_1)"
            ], ["y_0**3+3*y_0*yx7+3*yx7**2/y_0+yx7**3/y_0**3"]
        ])
        closer = LogNormalClosure(2, multivariate=True)
        answer = closer._compute_raw_moments(
            self.__n_counter,
            self.__k_counter,
        )
        self.assertTrue(sympy_expressions_equal(answer, expected))

    def test_log_normal_closer_wrapper(self):

        central_from_raw_exprs = to_sympy_matrix(
            [["x_0_0_2-y_2**2"], ["x_0_1_1-y_1*y_2"], ["x_0_2_0-y_1**2"],
             ["x_1_0_1-y_0*y_2"], ["x_1_1_0-y_0*y_1"], ["x_2_0_0-y_0**2"],
             ["-3*x_0_0_2*y_2+x_0_0_3+2*y_2**3"],
             ["-x_0_0_2*y_1-2*x_0_1_1*y_2+x_0_1_2+2*y_1*y_2**2"],
             ["-2*x_0_1_1*y_1-x_0_2_0*y_2+x_0_2_1+2*y_1**2*y_2"],
             ["-3*x_0_2_0*y_1+x_0_3_0+2*y_1**3"],
             ["-x_0_0_2*y_0-2*x_1_0_1*y_2+x_1_0_2+2*y_0*y_2**2"],
             ["-x_0_1_1*y_0-x_1_0_1*y_1-x_1_1_0*y_2+x_1_1_1+2*y_0*y_1*y_2"],
             ["-x_0_2_0*y_0-2*x_1_1_0*y_1+x_1_2_0+2*y_0*y_1**2"],
             ["-2*x_1_0_1*y_0-x_2_0_0*y_2+x_2_0_1+2*y_0**2*y_2"],
             ["-2*x_1_1_0*y_0-x_2_0_0*y_1+x_2_1_0+2*y_0**2*y_1"],
             ["-3*x_2_0_0*y_0+x_3_0_0+2*y_0**3"]])

        max_order = 2

        expected = to_sympy_matrix([
            [
                "c_0-c_1*y_0-(c_2*c_6*yx5)/(c_6+y_0) ** 2-(c_2*y_0*y_2)/(c_6+y_0)+(c_2*c_6*y_2*yx7)/(c_6+y_0) ** 3+(c_2*c_6*yx5*(yx5*y_0 ** 2+2*y_2*yx7*y_0+yx5*yx7))/(y_0 ** 2*y_2*(c_6+y_0) ** 3)-(c_2*c_6*y_2*yx7 ** 2*(3*y_0 ** 2+yx7))/(y_0 ** 3*(c_6+y_0) ** 4)"
            ], ["c_3*y_0-c_4*y_1"], ["c_4*y_1-c_5*y_2"],
            ["c_4*y_1+c_5*y_2+2*c_4*yx3-2*c_5*yx2"],
            ["c_3*yx5-c_4*yx3-c_4*y_1+c_4*yx4-c_5*yx3"],
            ["c_3*y_0+c_4*y_1-2*c_4*yx4+2*c_3*yx6"],
            [
                "-(c_2*y_0 ** 5*y_2 ** 2*yx2+c_1*y_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y_0 ** 2*y_2 ** 2*yx7 ** 2+c_2*c_6*y_0 ** 2*yx5 ** 2*yx7-2*c_2*c_6*y_0*y_2 ** 2*yx7 ** 3-c_2*c_6*y_2 ** 2*yx7 ** 3-c_1*y_0 ** 8*y_2-c_2*y_0 ** 7*y_2 ** 2+2*c_2*y_0 ** 7*y_2*yx5+2*c_1*y_0 ** 7*y_2*yx7-c_0*y_0 ** 7*y_2)/(y_0 ** 3*y_2*(c_6+y_0) ** 4)"
            ]
        ])
        closer = LogNormalClosure(max_order, multivariate=True)
        answer = closer.close(self.__mfk, central_from_raw_exprs,
                              self.__n_counter, self.__k_counter)

        #print (answer -expected).applyfunc(sympy.simplify)
        self.assertTrue(sympy_expressions_equal(answer, expected))

    def test_log_normal_closer_wrapper_univariate(self):

        central_from_raw_exprs = to_sympy_matrix(
            [["x_0_0_2-y_2**2"], ["x_0_1_1-y_1*y_2"], ["x_0_2_0-y_1**2"],
             ["x_1_0_1-y_0*y_2"], ["x_1_1_0-y_0*y_1"], ["x_2_0_0-y_0**2"],
             ["-3*x_0_0_2*y_2+x_0_0_3+2*y_2**3"],
             ["-x_0_0_2*y_1-2*x_0_1_1*y_2+x_0_1_2+2*y_1*y_2**2"],
             ["-2*x_0_1_1*y_1-x_0_2_0*y_2+x_0_2_1+2*y_1**2*y_2"],
             ["-3*x_0_2_0*y_1+x_0_3_0+2*y_1**3"],
             ["-x_0_0_2*y_0-2*x_1_0_1*y_2+x_1_0_2+2*y_0*y_2**2"],
             ["-x_0_1_1*y_0-x_1_0_1*y_1-x_1_1_0*y_2+x_1_1_1+2*y_0*y_1*y_2"],
             ["-x_0_2_0*y_0-2*x_1_1_0*y_1+x_1_2_0+2*y_0*y_1**2"],
             ["-2*x_1_0_1*y_0-x_2_0_0*y_2+x_2_0_1+2*y_0**2*y_2"],
             ["-2*x_1_1_0*y_0-x_2_0_0*y_1+x_2_1_0+2*y_0**2*y_1"],
             ["-3*x_2_0_0*y_0+x_3_0_0+2*y_0**3"]])

        max_order = 2

        expected = to_sympy_matrix([
            [
                "c_0-c_1*y_0-(c_2*c_6*yx5)/(c_6+y_0) ** 2-(c_2*y_0*y_2)/(c_6+y_0)+(c_2*c_6*y_2*yx7)/(c_6+y_0) ** 3-(c_2*c_6*y_2*yx7 ** 2*(3*y_0 ** 2+yx7))/(y_0 ** 3*(c_6+y_0) ** 4)"
            ], ["c_3*y_0-c_4*y_1"], ["c_4*y_1-c_5*y_2"],
            ["c_4*y_1+c_5*y_2+2*c_4*yx3-2*c_5*yx2"],
            ["c_3*yx5-c_4*yx3-c_4*y_1+c_4*yx4-c_5*yx3"],
            ["c_3*y_0+c_4*y_1-2*c_4*yx4+2*c_3*yx6"],
            [
                "c_4*yx6-c_1*yx5-c_5*yx5-(c_2*y_0*yx2)/(c_6+y_0)-(c_2*y_2*yx5)/(c_6+y_0)+(c_2*y_0*y_2*yx5)/(c_6+y_0) ** 2"
            ],
            [
                "c_3*yx7-c_1*yx6-c_4*yx6-(c_2*y_0*yx3)/(c_6+y_0)-(c_2*y_2*yx6)/(c_6+y_0)+(c_2*y_0*y_2*yx6)/(c_6+y_0) ** 2"
            ],
            [
                "(c_0*y_0 ** 7+c_1*y_0 ** 8+c_2*y_0 ** 7*y_2-2*c_2*y_0 ** 7*yx5-2*c_1*y_0 ** 7*yx7+6*c_0*c_6 ** 2*y_0 ** 5+4*c_0*c_6 ** 3*y_0 ** 4+c_0*c_6 ** 4*y_0 ** 3+6*c_1*c_6 ** 2*y_0 ** 6+4*c_1*c_6 ** 3*y_0 ** 5+c_1*c_6 ** 4*y_0 ** 4+4*c_0*c_6*y_0 ** 6+4*c_1*c_6*y_0 ** 7+3*c_2*c_6*y_0 ** 6*y_2+c_2*c_6*y_0 ** 5*yx5-6*c_2*c_6*y_0 ** 6*yx5-8*c_1*c_6*y_0 ** 6*yx7+c_2*c_6*y_2*yx7 ** 3+3*c_2*c_6 ** 2*y_0 ** 5*y_2+c_2*c_6 ** 3*y_0 ** 4*y_2+2*c_2*c_6 ** 2*y_0 ** 4*yx5+c_2*c_6 ** 3*y_0 ** 3*yx5-6*c_2*c_6 ** 2*y_0 ** 5*yx5-2*c_2*c_6 ** 3*y_0 ** 4*yx5-12*c_1*c_6 ** 2*y_0 ** 5*yx7-8*c_1*c_6 ** 3*y_0 ** 4*yx7-2*c_1*c_6 ** 4*y_0 ** 3*yx7+2*c_2*c_6 ** 2*y_2*yx7 ** 3+3*c_2*c_6*y_0 ** 2*y_2*yx7 ** 2+6*c_2*c_6*y_0 ** 3*y_2*yx7 ** 2-c_2*c_6 ** 2*y_0 ** 3*y_2*yx7-4*c_2*c_6 ** 2*y_0 ** 4*y_2*yx7-2*c_2*c_6 ** 3*y_0 ** 3*y_2*yx7+6*c_2*c_6 ** 2*y_0 ** 2*y_2*yx7 ** 2+2*c_2*c_6*y_0*y_2*yx7 ** 3-c_2*c_6*y_0 ** 4*y_2*yx7-2*c_2*c_6*y_0 ** 5*y_2*yx7)/(y_0 ** 3*(c_6+y_0) ** 4)"
            ]
        ])
        #here, we set univariate!
        closer = LogNormalClosure(max_order, multivariate=False)
        answer = closer.close(self.__mfk, central_from_raw_exprs,
                              self.__n_counter, self.__k_counter)
        self.assertTrue(sympy_expressions_equal(answer, expected))
示例#40
0
    def test_normal_closer_wrapper(self):

        central_from_raw_exprs = to_sympy_matrix([
            ["x_0_0_2 - y_2**2"],
            ["x_0_1_1 - y_1*y_2"],
            ["x_0_2_0 - y_1**2"],
            ["x_1_0_1 - y_0*y_2"],
            ["x_1_1_0 - y_0*y_1"],
            ["x_2_0_0 - y_0**2"],
            ["-3*x_0_0_2*y_2 + x_0_0_3 + 2*y_2**3"],
            ["-x_0_0_2*y_1 - 2*x_0_1_1*y_2 + x_0_1_2 + 2*y_1*y_2**2"],
            ["-2*x_0_1_1*y_1 - x_0_2_0*y_2 + x_0_2_1 + 2*y_1**2*y_2"],
            ["-3*x_0_2_0*y_1 + x_0_3_0 + 2*y_1**3"],
            ["-x_0_0_2*y_0 - 2*x_1_0_1*y_2 + x_1_0_2 + 2*y_0*y_2**2"],
            ["-x_0_1_1*y_0 - x_1_0_1*y_1 - x_1_1_0*y_2 + x_1_1_1 + 2*y_0*y_1*y_2"],
            ["-x_0_2_0*y_0 - 2*x_1_1_0*y_1 + x_1_2_0 + 2*y_0*y_1**2"],
            ["-2*x_1_0_1*y_0 - x_2_0_0*y_2 + x_2_0_1 + 2*y_0**2*y_2"],
            ["-2*x_1_1_0*y_0 - x_2_0_0*y_1 + x_2_1_0 + 2*y_0**2*y_1"],
            ["-3*x_2_0_0*y_0 + x_3_0_0 + 2*y_0**3"],
            ["6*x_0_0_2*y_2**2 - 4*x_0_0_3*y_2 + x_0_0_4 - 3*y_2**4"],
            ["3*x_0_0_2*y_1*y_2 - x_0_0_3*y_1 + 3*x_0_1_1*y_2**2 - 3*x_0_1_2*y_2 + x_0_1_3 - 3*y_1*y_2**3"],
            ["x_0_0_2*y_1**2 + 4*x_0_1_1*y_1*y_2 - 2*x_0_1_2*y_1 + x_0_2_0*y_2**2 - 2*x_0_2_1*y_2 + x_0_2_2 - 3*y_1**2*y_2**2"],
            ["3*x_0_1_1*y_1**2 + 3*x_0_2_0*y_1*y_2 - 3*x_0_2_1*y_1 - x_0_3_0*y_2 + x_0_3_1 - 3*y_1**3*y_2"],
            ["6*x_0_2_0*y_1**2 - 4*x_0_3_0*y_1 + x_0_4_0 - 3*y_1**4"],
            ["3*x_0_0_2*y_0*y_2 - x_0_0_3*y_0 + 3*x_1_0_1*y_2**2 - 3*x_1_0_2*y_2 + x_1_0_3 - 3*y_0*y_2**3"],
            ["x_0_0_2*y_0*y_1 + 2*x_0_1_1*y_0*y_2 - x_0_1_2*y_0 + 2*x_1_0_1*y_1*y_2 - x_1_0_2*y_1 + x_1_1_0*y_2**2 - 2*x_1_1_1*y_2 + x_1_1_2 - 3*y_0*y_1*y_2**2"],
            ["2*x_0_1_1*y_0*y_1 + x_0_2_0*y_0*y_2 - x_0_2_1*y_0 + x_1_0_1*y_1**2 + 2*x_1_1_0*y_1*y_2 - 2*x_1_1_1*y_1 - x_1_2_0*y_2 + x_1_2_1 - 3*y_0*y_1**2*y_2"],
            ["3*x_0_2_0*y_0*y_1 - x_0_3_0*y_0 + 3*x_1_1_0*y_1**2 - 3*x_1_2_0*y_1 + x_1_3_0 - 3*y_0*y_1**3"],
            ["x_0_0_2*y_0**2 + 4*x_1_0_1*y_0*y_2 - 2*x_1_0_2*y_0 + x_2_0_0*y_2**2 - 2*x_2_0_1*y_2 + x_2_0_2 - 3*y_0**2*y_2**2"],
            ["x_0_1_1*y_0**2 + 2*x_1_0_1*y_0*y_1 + 2*x_1_1_0*y_0*y_2 - 2*x_1_1_1*y_0 + x_2_0_0*y_1*y_2 - x_2_0_1*y_1 - x_2_1_0*y_2 + x_2_1_1 - 3*y_0**2*y_1*y_2"],
            ["x_0_2_0*y_0**2 + 4*x_1_1_0*y_0*y_1 - 2*x_1_2_0*y_0 + x_2_0_0*y_1**2 - 2*x_2_1_0*y_1 + x_2_2_0 - 3*y_0**2*y_1**2"],
            ["3*x_1_0_1*y_0**2 + 3*x_2_0_0*y_0*y_2 - 3*x_2_0_1*y_0 - x_3_0_0*y_2 + x_3_0_1 - 3*y_0**3*y_2"],
            ["3*x_1_1_0*y_0**2 + 3*x_2_0_0*y_0*y_1 - 3*x_2_1_0*y_0 - x_3_0_0*y_1 + x_3_1_0 - 3*y_0**3*y_1"],
            ["6*x_2_0_0*y_0**2 - 4*x_3_0_0*y_0 + x_4_0_0 - 3*y_0**4"]
            ])



        k_counter = [
            Moment([0, 0, 0], symbol=sympy.Symbol("1")),
            Moment([1, 0, 0], symbol=sympy.Symbol("y_0")),
            Moment([0, 1, 0], symbol=sympy.Symbol("y_1")),
            Moment([0, 0, 1], symbol=sympy.Symbol("y_2")),
            Moment([0, 0, 2], symbol=sympy.Symbol("x_0_0_2")),
            Moment([0, 1, 1], symbol=sympy.Symbol("x_0_1_1")),
            Moment([0, 2, 0], symbol=sympy.Symbol("x_0_2_0")),
            Moment([1, 0, 1], symbol=sympy.Symbol("x_1_0_1")),
            Moment([1, 1, 0], symbol=sympy.Symbol("x_1_1_0")),
            Moment([2, 0, 0], symbol=sympy.Symbol("x_2_0_0")),
            Moment([0, 0, 3], symbol=sympy.Symbol("x_0_0_3")),
            Moment([0, 1, 2], symbol=sympy.Symbol("x_0_1_2")),
            Moment([0, 2, 1], symbol=sympy.Symbol("x_0_2_1")),
            Moment([0, 3, 0], symbol=sympy.Symbol("x_0_3_0")),
            Moment([1, 0, 2], symbol=sympy.Symbol("x_1_0_2")),
            Moment([1, 1, 1], symbol=sympy.Symbol("x_1_1_1")),
            Moment([1, 2, 0], symbol=sympy.Symbol("x_1_2_0")),
            Moment([2, 0, 1], symbol=sympy.Symbol("x_2_0_1")),
            Moment([2, 1, 0], symbol=sympy.Symbol("x_2_1_0")),
            Moment([3, 0, 0], symbol=sympy.Symbol("x_3_0_0")),
            Moment([0, 0, 4], symbol=sympy.Symbol("x_0_0_4")),
            Moment([0, 1, 3], symbol=sympy.Symbol("x_0_1_3")),
            Moment([0, 2, 2], symbol=sympy.Symbol("x_0_2_2")),
            Moment([0, 3, 1], symbol=sympy.Symbol("x_0_3_1")),
            Moment([0, 4, 0], symbol=sympy.Symbol("x_0_4_0")),
            Moment([1, 0, 3], symbol=sympy.Symbol("x_1_0_3")),
            Moment([1, 1, 2], symbol=sympy.Symbol("x_1_1_2")),
            Moment([1, 2, 1], symbol=sympy.Symbol("x_1_2_1")),
            Moment([1, 3, 0], symbol=sympy.Symbol("x_1_3_0")),
            Moment([2, 0, 2], symbol=sympy.Symbol("x_2_0_2")),
            Moment([2, 1, 1], symbol=sympy.Symbol("x_2_1_1")),
            Moment([2, 2, 0], symbol=sympy.Symbol("x_2_2_0")),
            Moment([3, 0, 1], symbol=sympy.Symbol("x_3_0_1")),
            Moment([3, 1, 0], symbol=sympy.Symbol("x_3_1_0")),
            Moment([4, 0, 0], symbol=sympy.Symbol("x_4_0_0"))
            ]

        max_order = 3

        expected = to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*c_6 ** 3*y_0*y_2*yx7+3*c_2*c_6*y_0 ** 2*y_2*yx17+6*c_2*c_6 ** 2*y_0*y_2*yx17+3*c_2*c_6*y_0 ** 3*y_2*yx17+9*c_2*c_6 ** 3*y_0*y_2*yx17-9*c_2*c_6*y_0*yx5*yx7 ** 2+9*c_2*c_6*y_0 ** 2*yx5*yx7+18*c_2*c_6 ** 2*y_0*yx5*yx7+6*c_2*c_6*y_0 ** 3*yx5*yx7+18*c_2*c_6 ** 3*y_0*yx5*yx7+3*c_2*c_6*y_0 ** 2*yx7*yx15+6*c_2*c_6 ** 2*y_0*yx7*yx15-3*c_2*c_6 ** 2*y_2*yx7*yx17-3*c_2*c_6*y_0*y_2*yx7*yx17)/(c_6+y_0) ** 5"]
        ])


        closer = NormalClosure(max_order, multivariate=True)
        answer = closer.close(self.__mfk, central_from_raw_exprs,self.__n_counter, k_counter)

        self.assertTrue(sympy_expressions_equal(answer, expected))
    def test_centralmoments_using_p53model(self):
        """
        Given the p53 model hard codded bellow,the result of central moment should match exactly the expected one
        :return:
        """
        counter_nvecs = [[0, 0, 0], [0, 0, 2], [0, 1, 1], [0, 2, 0], [1, 0, 1], [1, 1, 0], [2, 0, 0]]

        mcounter_nvecs = [[0, 0, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0], [0, 0, 2], [0, 1, 1], [0, 2, 0],
                    [1, 0, 1], [1, 1, 0], [2, 0, 0]]

        counter = [Moment(c,sympy.Symbol("YU{0}".format(i))) for i,c in enumerate(counter_nvecs)]
        mcounter = [Moment(c,sympy.Symbol("y_{0}".format(i))) for i,c in enumerate(mcounter_nvecs)]

        m = to_sympy_matrix([
                              ['c_0 - c_1*y_0 - c_2*y_0*y_2/(c_6 + y_0)',
                               0,
                               0,
                               0,
                               'c_2*y_0/(c_6 + y_0)**2 - c_2/(c_6 + y_0)',
                               0,
                               '-c_2*y_0*y_2/(c_6 + y_0)**3 + c_2*y_2/(c_6 + y_0)**2'],
                         [
                              'c_3*y_0 - c_4*y_1',
                              0,
                              0,
                              0,
                              0,
                              0,
                              0],
                          [
                              'c_4*y_1 - c_5*y_2',
                              0,
                              0,
                              0,
                              0,
                              0,
                              0
                          ]])
        species = to_sympy_matrix(['y_0', 'y_1', 'y_2'])
        propensities = to_sympy_matrix(['c_0',
                                                        'c_1 * y_0',
                                                        'c_2*y_0*y_2/(c_6 + y_0)',
                                                        'c_3*y_0',
                                                        'c_4*y_1',
                                                        'c_5*y_2'])

        stoichiometry_matrix = to_sympy_matrix([[1, -1, -1, 0, 0, 0],
                                             [0, 0, 0, 1, -1, 0],
                                             [0, 0, 0, 0, 1, -1]])


        answer = eq_central_moments(counter, mcounter, m, species, propensities, stoichiometry_matrix, 2)

        expected = to_sympy_matrix([
            [" 2*c_4*y_1*y_2 + c_4*y_1 - 2*c_5*y_2**2 + c_5*y_2 - 2*y_1*(c_4*y_1 - c_5*y_2)","               -2*c_5","                2*c_4","      0","                                                                                                                                                 0","                                                             0","                                                                                                                                                                                                                    0"],
            ["c_3*y_0*y_2 + c_4*y_1**2 - c_4*y_1*y_2 - c_4*y_1 - c_5*y_1*y_2 - y_1*(c_3*y_0 - c_4*y_1) - y_2*(c_4*y_1 - c_5*y_2)","                    0","           -c_4 - c_5","    c_4","                                                                                                                                               c_3","                                                             0","                                                                                                                                                                                                                    0"],
            ["2*c_3*y_0*y_1 + c_3*y_0 - 2*c_4*y_1**2 + c_4*y_1 - 2*y_2*(c_3*y_0 - c_4*y_1)","                    0","                    0"," -2*c_4","                                                                                                                                                 0","                                                         2*c_3","0"],
            ["c_0*y_2 - c_1*y_0*y_2 - c_2*y_0*y_2**2/(c_6 + y_0) + c_4*y_0*y_1 - c_5*y_0*y_2 - y_1*(c_0 - c_1*y_0 - c_2*y_0*y_2/(c_6 + y_0)) - y_3*(c_4*y_1 - c_5*y_2)"," -c_2*y_0/(c_6 + y_0)","                    0","      0","                                -c_1 + 2*c_2*y_0*y_2/(c_6 + y_0)**2 - 2*c_2*y_2/(c_6 + y_0) - c_5 - y_1*(c_2*y_0/(c_6 + y_0)**2 - c_2/(c_6 + y_0))","c_4","                                                                                              -c_2*y_0*y_2**2/(c_6 + y_0)**3 + c_2*y_2**2/(c_6 + y_0)**2 - y_1*(-c_2*y_0*y_2/(c_6 + y_0)**3 + c_2*y_2/(c_6 + y_0)**2)"],
            ["c_0*y_1 - c_1*y_0*y_1 - c_2*y_0*y_1*y_2/(c_6 + y_0) + c_3*y_0**2 - c_4*y_0*y_1 - y_2*(c_0 - c_1*y_0 - c_2*y_0*y_2/(c_6 + y_0)) - y_3*(c_3*y_0 - c_4*y_1)","                    0"," -c_2*y_0/(c_6 + y_0)","      0","                                                 c_2*y_0*y_1/(c_6 + y_0)**2 - c_2*y_1/(c_6 + y_0) - y_2*(c_2*y_0/(c_6 + y_0)**2 - c_2/(c_6 + y_0))"," -c_1 + c_2*y_0*y_2/(c_6 + y_0)**2 - c_2*y_2/(c_6 + y_0) - c_4","                                                                                      -c_2*y_0*y_1*y_2/(c_6 + y_0)**3 + c_2*y_1*y_2/(c_6 + y_0)**2 + c_3 - y_2*(-c_2*y_0*y_2/(c_6 + y_0)**3 + c_2*y_2/(c_6 + y_0)**2)"],
            ["2*c_0*y_0 + c_0 - 2*c_1*y_0**2 + c_1*y_0 - 2*c_2*y_0**2*y_2/(c_6 + y_0) + c_2*y_0*y_2/(c_6 + y_0) - 2*y_3*(c_0 - c_1*y_0 - c_2*y_0*y_2/(c_6 + y_0))","                    0","                    0","      0"," 2*c_2*y_0**2/(c_6 + y_0)**2 - 4*c_2*y_0/(c_6 + y_0) - c_2*y_0/(c_6 + y_0)**2 + c_2/(c_6 + y_0) - 2*y_3*(c_2*y_0/(c_6 + y_0)**2 - c_2/(c_6 + y_0))","                                                             0"," -2*c_1 - 2*c_2*y_0**2*y_2/(c_6 + y_0)**3 + 4*c_2*y_0*y_2/(c_6 + y_0)**2 + c_2*y_0*y_2/(c_6 + y_0)**3 - 2*c_2*y_2/(c_6 + y_0) - c_2*y_2/(c_6 + y_0)**2 - 2*y_3*(-c_2*y_0*y_2/(c_6 + y_0)**3 + c_2*y_2/(c_6 + y_0)**2)"]
       ])

        assert_sympy_expressions_equal(answer, expected)