예제 #1
0
def _generate_observed_trajectories():
    timepoints = np.arange(0, 20, 0.5)

    observed_trajectories = [
        Trajectory(timepoints, [
            301., 290.1919552, 280.58701279, 272.03059275, 264.39179184,
            257.55906225, 251.43680665, 245.94267027, 241.0053685,
            236.56293031, 232.56126687, 228.95299703, 225.69647718,
            222.75499485, 220.09609439, 217.69101017, 215.51418738,
            213.54287512, 211.75677913, 210.13776407, 208.6695974,
            207.33772795, 206.12909392, 205.03195581, 204.03575051,
            203.13096347, 202.30901645, 201.5621687, 200.88342959,
            200.26648137, 199.70561061, 199.19564728, 198.73191049,
            198.3101601, 197.92655342, 197.57760651, 197.26015951,
            196.97134545, 196.70856234, 196.46944793
        ], Moment([1], symbol='y_0')),
        Trajectory(timepoints, [
            0., 20.10320788, 35.54689328, 47.51901615, 56.88242563,
            64.26983231, 70.14921364, 74.86943532, 78.69244584, 81.81623963,
            84.39139622, 86.53309942, 88.32994353, 89.85043353, 91.1478162,
            92.26369294, 93.23073689, 94.07474712, 94.81620873, 95.47148295,
            96.05371852, 96.57355199, 97.03964777, 97.45911583, 97.83783557,
            98.18070779, 98.49185119, 98.77475594, 99.03240415, 99.26736458,
            99.48186728, 99.67786273, 99.85706877, 100.02100803, 100.17103799,
            100.3083751, 100.43411443, 100.54924568, 100.65466632, 100.75119251
        ], Moment([2], symbol='yx1'))
    ]

    return observed_trajectories
예제 #2
0
    def test_a_three_species_third_order_problem(self):
        """
        Given two vectors of Moments: counter and mcounter (up to third moment) and
        Given a vector of three species ymat,
        Then, the answer should match exactlty the expected result
        :return:
        """
        counter = [Moment([0, 0, 0] ,  sympy.Integer(1)), Moment([0, 0, 2] ,  sympy.Symbol("yx1")), Moment([0, 0, 3] ,  sympy.Symbol("yx2")), Moment([0, 1, 1] ,  sympy.Symbol("yx3")), Moment([0, 1, 2] ,  sympy.Symbol("yx4")), Moment([0, 2, 0] ,  sympy.Symbol("yx5")), Moment([0, 2, 1] ,  sympy.Symbol("yx6")), Moment([0, 3, 0] ,  sympy.Symbol("yx7")), Moment([1, 0, 1] ,  sympy.Symbol("yx8")), Moment([1, 0, 2] ,  sympy.Symbol("yx9")), Moment([1, 1, 0] ,  sympy.Symbol("yx10")), Moment([1, 1, 1] ,  sympy.Symbol("yx11")), Moment([1, 2, 0] ,  sympy.Symbol("yx12")), Moment([2, 0, 0] ,  sympy.Symbol("yx13")), Moment([2, 0, 1] ,  sympy.Symbol("yx14")), Moment([2, 1, 0] ,  sympy.Symbol("yx15")), Moment([3, 0, 0] ,  sympy.Symbol("yx16"))]
        ymat = sympy.Matrix(["y_0","y_1","y_2"])
        mcounter = [Moment([0, 0, 0] ,  sympy.Integer(1)), Moment([0, 0, 1] ,  sympy.Symbol("y_2")), Moment([0, 0, 2] ,  sympy.Symbol("x_0_0_2")), Moment([0, 0, 3] ,  sympy.Symbol("x_0_0_3")), Moment([0, 1, 0] ,  sympy.Symbol("y_1")), Moment([0, 1, 1] ,  sympy.Symbol("x_0_1_1")), Moment([0, 1, 2] ,  sympy.Symbol("x_0_1_2")), Moment([0, 2, 0] ,  sympy.Symbol("x_0_2_0")), Moment([0, 2, 1] ,  sympy.Symbol("x_0_2_1")), Moment([0, 3, 0] ,  sympy.Symbol("x_0_3_0")), Moment([1, 0, 0] ,  sympy.Symbol("y_0")), Moment([1, 0, 1] ,  sympy.Symbol("x_1_0_1")), Moment([1, 0, 2] ,  sympy.Symbol("x_1_0_2")), Moment([1, 1, 0] ,  sympy.Symbol("x_1_1_0")), Moment([1, 1, 1] ,  sympy.Symbol("x_1_1_1")), Moment([1, 2, 0] ,  sympy.Symbol("x_1_2_0")), Moment([2, 0, 0] ,  sympy.Symbol("x_2_0_0")), Moment([2, 0, 1] ,  sympy.Symbol("x_2_0_1")), Moment([2, 1, 0] ,  sympy.Symbol("x_2_1_0")), Moment([3, 0, 0] ,  sympy.Symbol("x_3_0_0"))]

        answer = sympy.Matrix(raw_to_central(counter, ymat, mcounter))
        expected = sympy.Matrix(
        [["                                                     1*y_2**2 + x_0_0_2 - 2*y_2**2"],
        ["                                    -1*y_2**3 - 3*x_0_0_2*y_2 + x_0_0_3 + 3*y_2**3"],
        ["                                                   1*y_1*y_2 + x_0_1_1 - 2*y_1*y_2"],
        ["              -1*y_1*y_2**2 - x_0_0_2*y_1 - 2*x_0_1_1*y_2 + x_0_1_2 + 3*y_1*y_2**2"],
        ["                                                     1*y_1**2 + x_0_2_0 - 2*y_1**2"],
        ["              -1*y_1**2*y_2 - 2*x_0_1_1*y_1 - x_0_2_0*y_2 + x_0_2_1 + 3*y_1**2*y_2"],
        ["                                    -1*y_1**3 - 3*x_0_2_0*y_1 + x_0_3_0 + 3*y_1**3"],
        ["                                                   1*y_0*y_2 + x_1_0_1 - 2*y_0*y_2"],
        ["              -1*y_0*y_2**2 - x_0_0_2*y_0 - 2*x_1_0_1*y_2 + x_1_0_2 + 3*y_0*y_2**2"],
        ["                                                   1*y_0*y_1 + x_1_1_0 - 2*y_0*y_1"],
        ["-1*y_0*y_1*y_2 - x_0_1_1*y_0 - x_1_0_1*y_1 - x_1_1_0*y_2 + x_1_1_1 + 3*y_0*y_1*y_2"],
        ["              -1*y_0*y_1**2 - x_0_2_0*y_0 - 2*x_1_1_0*y_1 + x_1_2_0 + 3*y_0*y_1**2"],
        ["                                                     1*y_0**2 + x_2_0_0 - 2*y_0**2"],
        ["              -1*y_0**2*y_2 - 2*x_1_0_1*y_0 - x_2_0_0*y_2 + x_2_0_1 + 3*y_0**2*y_2"],
        ["              -1*y_0**2*y_1 - 2*x_1_1_0*y_0 - x_2_0_0*y_1 + x_2_1_0 + 3*y_0**2*y_1"],
        ["                                    -1*y_0**3 - 3*x_2_0_0*y_0 + x_3_0_0 + 3*y_0**3"]])

        self.assertEqual(answer, expected)
예제 #3
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)
예제 #4
0
def _generate_ode_problem():
    # Create an ODEProblem from dimer instance
    c_0 = Symbol('c_0')
    c_1 = Symbol('c_1')
    c_2 = Symbol('c_2')

    constants = [c_0, c_1, c_2]

    y_0 = Symbol('y_0')

    yx1 = Symbol('yx1')

    right_hand_side = MutableDenseMatrix(
        [[
            -2 * c_0 * y_0 * (y_0 - 1) - 2 * c_0 * yx1 + 2 * c_1 *
            (Float('0.5', prec=15) * c_2 - Float('0.5', prec=15) * y_0)
        ],
         [
             Float('4.0', prec=15) * c_0 * y_0**2 -
             Float('4.0', prec=15) * c_0 * y_0 +
             Float('2.0', prec=15) * c_1 * c_2 -
             Float('2.0', prec=15) * c_1 * y_0 - yx1 *
             (Float('8.0', prec=15) * c_0 * y_0 - Float('8.0', prec=15) * c_0 +
              Float('2.0', prec=15) * c_1)
         ]])

    ode_lhs_terms = [
        Moment(np.array([1]), symbol=y_0),
        Moment(np.array([2]), symbol=yx1)
    ]

    dimer_problem = ODEProblem('MEA', ode_lhs_terms, right_hand_side,
                               constants)

    return dimer_problem
예제 #5
0
    def test_different_descriptions_make_trajectories_different(self):
        """
        Given two Trajectories that differ only by values, they should be treated as different
        """

        t1 = Trajectory([1, 2, 3], [3, 2, 1], Moment([1], symbol='description'))
        t2 = Trajectory([1, 2, 3], [3, 2, 1], Moment([1], symbol='description'))

        self.assertNotEqual(t1, t2)
예제 #6
0
    def test_equality_treats_equal_things_as_equal(self):
        """
        Given two Trajectories that were equal, they should be comparable with ==.
        """

        t1 = Trajectory([1, 2, 3], [3, 2, 1], Moment([1], symbol='description'))
        t2 = Trajectory([1, 2, 3], [3, 2, 1], Moment([1], symbol='description'))

        self.assertEqual(t1, t2)
예제 #7
0
    def test_equality_treats_equal_things_as_equal(self):
        """
        Given two Trajectories that were equal, they should be comparable with ==.
        """
        t_sensitivity_1 = Trajectory([1,2,3], [3, 2, 1], Moment([1], symbol='description'))
        t_sensitivity_2 = Trajectory([1,2, 3], [-5, -9, -1], Moment([1], symbol='description'))

        t1 = TrajectoryWithSensitivityData([1, 2, 3], [3, 2, 1], Moment([1], symbol='description'), [t_sensitivity_1, t_sensitivity_2])
        t2 = TrajectoryWithSensitivityData([1, 2, 3], [3, 2, 1], Moment([1], symbol='description'), [t_sensitivity_1, t_sensitivity_2])

        self.assertEqual(t1, t2)
예제 #8
0
    def test_traj_collection_to_file(self):

        tr2 = Trajectory([1, 2, 3, 4, 5, 6], [3, 2, 1, 5, 2, 4], Moment([1], symbol='y_1'))
        tr1 = Trajectory([1, 2, 3, 4, 5, 6], [3, 2, 1, 5, 2, 4], Moment([1], symbol='y_2'))
        tc = TrajectoryCollection([tr1,tr2])

        file = tempfile.mktemp(suffix=".csv")
        try:
            with open(file,"w") as out:
                tc.to_csv(out)
        finally:
            os.unlink(file)
예제 #9
0
    def test_different_timepoints_make_trajectories_different(self):
        """
        Given two TrajectoriesWithSensitivityData that differ only by sensitivity data
        they should be reported as different
        """

        t_sensitivity_1 = Trajectory([1,2,3], [3, 2, 1], Moment([1], symbol='sensitivity1'))
        t_sensitivity_2 = Trajectory([1,2, 3], [-5, -9, -1], Moment([1], symbol='sensitivity2'))
        t_sensitivity_3 = Trajectory([1,2,3], [-5, -9, -100], Moment([1], symbol='sensitivity2'))

        t1 = TrajectoryWithSensitivityData([1, 2, 3], [3, 2, 1], Moment([1], symbol='description'), [t_sensitivity_1, t_sensitivity_2])
        t2 = TrajectoryWithSensitivityData([1, 2, 3], [3, 2, 1], Moment([1], symbol='description'), [t_sensitivity_1, t_sensitivity_3])

        self.assertNotEqual(t1, t2)
예제 #10
0
    def generate_single_simulation(self, x):
        """
        Generate a single SSA simulation
        :param x: an integer to reset the random seed. If None, the initial random number generator is used
        :return: a list of :class:`~means.simulation.Trajectory` one per species in the problem
        :rtype: list[:class:`~means.simulation.Trajectory`]
        """
        #reset random seed
        if x:
            self.__rng = np.random.RandomState(x)

        # perform one stochastic simulation
        time_points, species_over_time = self._gssa(self.__initial_conditions, self.__t_max)

        # build descriptors for first order raw moments aka expectations (e.g. [1, 0, 0], [0, 1, 0] and [0, 0, 1])
        descriptors = []
        for i, s in enumerate(self.__species):
            row = [0] * len(self.__species)
            row[i] = 1
            descriptors.append(Moment(row, s))

        # build trajectories
        trajectories = [Trajectory(time_points, spot, desc) for
                        spot, desc in zip(species_over_time, descriptors)]

        return trajectories
예제 #11
0
 def test_trajectory_with_sensitivities_serialisation(self):
     term = Moment([1, 0, 0], 'x')
     x = Trajectory([1, 2, 3], [3, 2, 1], SensitivityTerm(term, 'x'))
     y = Trajectory([1, 2, 3], [7, 8, 9], SensitivityTerm(term, 'y'))
     t = TrajectoryWithSensitivityData([1, 2, 3], [-1, -2, -3],
                                       term,
                                       sensitivity_data=[x, y])
     self._roundtrip(t)
예제 #12
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)
예제 #13
0
    def test_single_traj_to_file(self):

        trajectory = Trajectory([1, 2, 3, 4, 5, 6], [3, 2, 1,5, 2, 4], Moment([1], symbol='description'))
        file = tempfile.mktemp(suffix=".csv")
        try:
            with open(file,"w") as out:
                trajectory.to_csv(out)
        finally:
            os.unlink(file)
예제 #14
0
    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)
예제 #15
0
def _sample_inference():

    r = Inference(problem=_sample_problem(),
                  starting_parameters=[1, 2, 3, 4, 5, 6, 7],
                  starting_conditions=[1, 2, 3],
                  variable_parameters=['c_0', 'c_1'],
                  observed_trajectories=[
                      Trajectory([1, 2], [2, 3], Moment([1, 0, 0], 'x'))
                  ],
                  distance_function_type='gamma',
                  maxh=0.01)  # Some simulation kwargs
    return r
예제 #16
0
    def test_a_two_species_problem(self):
        """
        Given two vectors of Moments: counter and mcounter (up to second moment) and
        Given a vector of two species ymat,
        Then, the answer should match exactlty the expected result
        :return:
        """
        ymat = sympy.Matrix(["y_0","y_1"])
        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)]


        answer = raw_to_central(counter, ymat, mcounter)

        expected =  sympy.Matrix([
            ["y_0*y_1**2 - 2*y_1**2 + y_3"],
            ["y_0**2*y_1 - y_0*y_1 - y_1*y_2 + y_4"],
            ["y_0**3 - 2*y_0*y_2 + y_5"]])
        self.assertEqual(answer, expected)
예제 #17
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)
예제 #18
0
        def check_initialisation(variable_parameters,
                                 expected_parameters_with_variability,
                                 expected_initial_conditions_with_variability,
                                 expected_constraints):
            p = Inference(
                self.dimer_problem, parameters, initial_conditions,
                variable_parameters,
                [Trajectory([1, 2, 3], [1, 2, 3], Moment([1], symbol='x'))])

            self.assertEquals(p.starting_parameters_with_variability,
                              expected_parameters_with_variability)
            self.assertEqual(p.starting_conditions_with_variability,
                             expected_initial_conditions_with_variability)
            self.assertEqual(p.constraints, expected_constraints)
예제 #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_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)
예제 #21
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_0 ** 5*y_2 ** 2*yx5-c_4*y_0 ** 5*y_2 ** 2*yx6+c_5*y_0 ** 5*y_2 ** 2*yx5+2*c_2*c_6*y_0 ** 4*y_2 ** 2*yx2+3*c_1*c_6*y_0 ** 4*y_2 ** 2*yx5+c_2*c_6*y_0 ** 3*y_2 ** 3*yx5-3*c_4*c_6*y_0 ** 4*y_2 ** 2*yx6+3*c_5*c_6*y_0 ** 4*y_2 ** 2*yx5+c_2*c_6*y_0 ** 2*yx2*yx5 ** 2+c_2*c_6 ** 2*y_0*yx2*yx5 ** 2-c_2*c_6*y_2 ** 2*yx5 ** 2*yx7+c_2*c_6 ** 2*y_0 ** 3*y_2 ** 2*yx2+3*c_1*c_6 ** 2*y_0 ** 3*y_2 ** 2*yx5+c_1*c_6 ** 3*y_0 ** 2*y_2 ** 2*yx5+c_2*c_6 ** 2*y_0*y_2 ** 2*yx5 ** 2+c_2*c_6 ** 2*y_0 ** 2*y_2 ** 3*yx5-3*c_4*c_6 ** 2*y_0 ** 3*y_2 ** 2*yx6-c_4*c_6 ** 3*y_0 ** 2*y_2 ** 2*yx6+3*c_5*c_6 ** 2*y_0 ** 3*y_2 ** 2*yx5+c_5*c_6 ** 3*y_0 ** 2*y_2 ** 2*yx5+2*c_2*c_6*y_0 ** 3*y_2*yx2*yx5-2*c_2*c_6*y_0*y_2 ** 3*yx5*yx7+2*c_2*c_6 ** 2*y_0 ** 2*y_2*yx2*yx5)/(y_0 ** 2*y_2 ** 2*(c_6+y_0) ** 3)"
            ],
            [
                "-(c_2*y_0 ** 5*y_1*y_2*yx3+c_1*y_0 ** 5*y_1*y_2*yx6-c_3*y_0 ** 5*y_1*y_2*yx7+c_4*y_0 ** 5*y_1*y_2*yx6-c_2*c_6*y_2 ** 2*yx6 ** 2*yx7-c_2*c_6*y_0 ** 2*y_2 ** 2*yx6 ** 2+c_2*c_6 ** 2*y_0 ** 2*y_1*y_2 ** 2*yx6+2*c_2*c_6*y_0 ** 4*y_1*y_2*yx3+3*c_1*c_6*y_0 ** 4*y_1*y_2*yx6-3*c_3*c_6*y_0 ** 4*y_1*y_2*yx7+3*c_4*c_6*y_0 ** 4*y_1*y_2*yx6+c_2*c_6*y_0 ** 3*y_1*yx3*yx5+c_2*c_6*y_0 ** 3*y_2*yx3*yx6+c_2*c_6*y_0 ** 2*yx3*yx5*yx6+c_2*c_6 ** 2*y_0*yx3*yx5*yx6+c_2*c_6 ** 2*y_0 ** 3*y_1*y_2*yx3+3*c_1*c_6 ** 2*y_0 ** 3*y_1*y_2*yx6+c_1*c_6 ** 3*y_0 ** 2*y_1*y_2*yx6+c_2*c_6*y_0 ** 3*y_1*y_2 ** 2*yx6-3*c_3*c_6 ** 2*y_0 ** 3*y_1*y_2*yx7-c_3*c_6 ** 3*y_0 ** 2*y_1*y_2*yx7+3*c_4*c_6 ** 2*y_0 ** 3*y_1*y_2*yx6+c_4*c_6 ** 3*y_0 ** 2*y_1*y_2*yx6+c_2*c_6 ** 2*y_0 ** 2*y_1*yx3*yx5+c_2*c_6 ** 2*y_0 ** 2*y_2*yx3*yx6+c_2*c_6*y_0 ** 2*y_1*y_2*yx5*yx6+c_2*c_6 ** 2*y_0*y_1*y_2*yx5*yx6-2*c_2*c_6*y_0*y_1*y_2 ** 2*yx6*yx7)/(y_0 ** 2*y_1*y_2*(c_6+y_0) ** 3)"
            ],
            [
                "-(-c_1*c_6 ** 4*y_0 ** 4*y_2+2*c_1*c_6 ** 4*y_0 ** 3*y_2*yx7-c_0*c_6 ** 4*y_0 ** 3*y_2-4*c_1*c_6 ** 3*y_0 ** 5*y_2-c_2*c_6 ** 3*y_0 ** 4*y_2 ** 2+2*c_2*c_6 ** 3*y_0 ** 4*y_2*yx5+8*c_1*c_6 ** 3*y_0 ** 4*y_2*yx7-4*c_0*c_6 ** 3*y_0 ** 4*y_2+2*c_2*c_6 ** 3*y_0 ** 3*y_2 ** 2*yx7-c_2*c_6 ** 3*y_0 ** 3*y_2*yx5+2*c_2*c_6 ** 3*y_0 ** 3*yx5 ** 2+4*c_2*c_6 ** 3*y_0 ** 2*y_2*yx5*yx7+2*c_2*c_6 ** 3*y_0*yx5 ** 2*yx7-6*c_1*c_6 ** 2*y_0 ** 6*y_2-3*c_2*c_6 ** 2*y_0 ** 5*y_2 ** 2+6*c_2*c_6 ** 2*y_0 ** 5*y_2*yx5+12*c_1*c_6 ** 2*y_0 ** 5*y_2*yx7-6*c_0*c_6 ** 2*y_0 ** 5*y_2+4*c_2*c_6 ** 2*y_0 ** 4*y_2 ** 2*yx7-2*c_2*c_6 ** 2*y_0 ** 4*y_2*yx5+4*c_2*c_6 ** 2*y_0 ** 4*yx5 ** 2+c_2*c_6 ** 2*y_0 ** 3*y_2 ** 2*yx7+8*c_2*c_6 ** 2*y_0 ** 3*y_2*yx5*yx7+c_2*c_6 ** 2*y_0 ** 3*yx5 ** 2-6*c_2*c_6 ** 2*y_0 ** 2*y_2 ** 2*yx7 ** 2+2*c_2*c_6 ** 2*y_0 ** 2*y_2*yx5*yx7+4*c_2*c_6 ** 2*y_0 ** 2*yx5 ** 2*yx7+c_2*c_6 ** 2*y_0*yx5 ** 2*yx7-2*c_2*c_6 ** 2*y_2 ** 2*yx7 ** 3-4*c_1*c_6*y_0 ** 7*y_2-3*c_2*c_6*y_0 ** 6*y_2 ** 2+6*c_2*c_6*y_0 ** 6*y_2*yx5+8*c_1*c_6*y_0 ** 6*y_2*yx7-4*c_0*c_6*y_0 ** 6*y_2+2*c_2*c_6*y_0 ** 5*y_2 ** 2*yx7-c_2*c_6*y_0 ** 5*y_2*yx5+2*c_2*c_6*y_0 ** 5*yx5 ** 2+c_2*c_6*y_0 ** 4*y_2 ** 2*yx7+4*c_2*c_6*y_0 ** 4*y_2*yx5*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))

    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))
예제 #22
0
    def test_trajectory_collection_serialisation(self):

        t1 = Trajectory([1, 2, 3], [3, 2, 1], Moment([1, 2, 3], 'x'))
        t2 = Trajectory([1, 2, 3], [3, 2, 1], Moment([1, 0, 0], 'y'))
        tc = TrajectoryCollection([t1, t2])
        self._roundtrip(tc)
예제 #23
0
 def test_trajectory_serialisation(self):
     t = Trajectory([1, 2, 3], [3, 2, 1], Moment([1, 2, 3], 'x'))
     self._roundtrip(t)
예제 #24
0
    def test_ode_problem_lna_serialisation_works(self):

        c_0 = Symbol('c_0')
        c_1 = Symbol('c_1')
        y_0 = Symbol('y_0')
        c_2 = Symbol('c_2')
        y_2 = Symbol('y_2')
        c_6 = Symbol('c_6')
        c_3 = Symbol('c_3')
        c_4 = Symbol('c_4')
        y_1 = Symbol('y_1')
        c_5 = Symbol('c_5')
        V_00 = Symbol('V_00')
        V_02 = Symbol('V_02')
        V_20 = Symbol('V_20')
        V_01 = Symbol('V_01')
        V_21 = Symbol('V_21')
        V_22 = Symbol('V_22')
        V_10 = Symbol('V_10')
        V_12 = Symbol('V_12')
        V_11 = Symbol('V_11')
        right_hand_side = MutableDenseMatrix(
            [[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_4 * y_1 - c_5 * y_2],
             [
                 2 * V_00 * (-c_1 + c_2 * y_0 * y_2 /
                             (c_6 + y_0)**2 - c_2 * y_2 /
                             (c_6 + y_0)) - V_02 * c_2 * y_0 / (c_6 + y_0) -
                 V_20 * c_2 * y_0 / (c_6 + y_0) + c_0**Float('1.0', prec=15) +
                 (c_1 * y_0)**Float('1.0', prec=15) +
                 (c_2 * y_0 * y_2 / (c_6 + y_0))**Float('1.0', prec=15)
             ],
             [
                 V_00 * c_3 - V_01 * c_4 + V_01 *
                 (-c_1 + c_2 * y_0 * y_2 / (c_6 + y_0)**2 - c_2 * y_2 /
                  (c_6 + y_0)) - V_21 * c_2 * y_0 / (c_6 + y_0)
             ],
             [
                 V_01 * c_4 - V_02 * c_5 + V_02 *
                 (-c_1 + c_2 * y_0 * y_2 / (c_6 + y_0)**2 - c_2 * y_2 /
                  (c_6 + y_0)) - V_22 * c_2 * y_0 / (c_6 + y_0)
             ],
             [
                 V_00 * c_3 - V_10 * c_4 + V_10 *
                 (-c_1 + c_2 * y_0 * y_2 / (c_6 + y_0)**2 - c_2 * y_2 /
                  (c_6 + y_0)) - V_12 * c_2 * y_0 / (c_6 + y_0)
             ],
             [
                 V_01 * c_3 + V_10 * c_3 - 2 * V_11 * c_4 +
                 (c_3 * y_0)**Float('1.0', prec=15) +
                 (c_4 * y_1)**Float('1.0', prec=15)
             ],
             [
                 V_02 * c_3 + V_11 * c_4 - V_12 * c_4 - V_12 * c_5 -
                 (c_4 * y_1)**Float('1.0', prec=15)
             ],
             [
                 V_10 * c_4 - V_20 * c_5 + V_20 *
                 (-c_1 + c_2 * y_0 * y_2 / (c_6 + y_0)**2 - c_2 * y_2 /
                  (c_6 + y_0)) - V_22 * c_2 * y_0 / (c_6 + y_0)
             ],
             [
                 V_11 * c_4 + V_20 * c_3 - V_21 * c_4 - V_21 * c_5 -
                 (c_4 * y_1)**Float('1.0', prec=15)
             ],
             [
                 V_12 * c_4 + V_21 * c_4 - 2 * V_22 * c_5 +
                 (c_4 * y_1)**Float('1.0', prec=15) +
                 (c_5 * y_2)**Float('1.0', prec=15)
             ]])

        ode_lhs_terms = [
            Moment(np.array([1, 0, 0]), symbol=y_0),
            Moment(np.array([0, 1, 0]), symbol=y_1),
            Moment(np.array([0, 0, 1]), symbol=y_2),
            VarianceTerm((0, 0), V_00),
            VarianceTerm((0, 1), V_01),
            VarianceTerm((0, 2), V_02),
            VarianceTerm((1, 0), V_10),
            VarianceTerm((1, 1), V_11),
            VarianceTerm((1, 2), V_12),
            VarianceTerm((2, 0), V_20),
            VarianceTerm((2, 1), V_21),
            VarianceTerm((2, 2), V_22)
        ]

        constants = ['c_0', 'c_1', 'c_2', 'c_3', 'c_4', 'c_5', 'c_6']

        problem = ODEProblem('LNA', ode_lhs_terms, right_hand_side, constants)
        self._roundtrip(problem)

        # Now make sure to access problem.right_hand_side_as_function as this sometimes breaks pickle
        f = problem.right_hand_side_as_function
        # Do roundtrip again
        self._roundtrip(problem)
예제 #25
0
def generate_n_and_k_counters(max_order,
                              species,
                              central_symbols_prefix="M_",
                              raw_symbols_prefix="x_"):
    r"""
        Makes a counter for central moments (n_counter) and a counter for raw moment (k_counter).
        Each is a list of :class:`~means.approximation.ode_problem.Moment`s.
        Therefore, each :class:`~means.approximation.ode_problem.Moments` is represented by both
        a vector of integer and a symbol.

        :param max_order: the maximal order of moment to be computer (will generate a list of moments up to `max_order + 1`)
        :param species: the name of the species
        :return: a pair of lists of :class:`~means.core.descriptors.Moment`s corresponding to central,
        and raw moments, respectively.
        :rtype: (list[:class:`~mea ns.core.descriptors.Moment`],list[:class:`~mea ns.core.descriptors.Moment`])
        """
    n_moments = max_order + 1
    # first order moments are always 1
    k_counter = [Moment([0] * len(species), sp.Integer(1))]
    n_counter = [Moment([0] * len(species), sp.Integer(1))]

    # build descriptors for first order raw moments aka expectations (e.g. [1, 0, 0], [0, 1, 0] and [0, 0, 1])
    descriptors = []
    for i in range(len(species)):
        row = [0] * len(species)
        row[i] = 1
        descriptors.append(row)

    # We use species name as symbols for first order raw moment
    k_counter += [Moment(d, s) for d, s in zip(descriptors, species)]

    # Higher order raw moment descriptors
    k_counter_descriptors = [
        i for i in itertools.product(range(n_moments + 1), repeat=len(species))
        if 1 < sum(i) <= n_moments
    ]

    #this mimics the order in the original code
    k_counter_descriptors = sorted(k_counter_descriptors,
                                   lambda x, y: sum(x) - sum(y))
    #k_counter_descriptors = [[r for r in reversed(k)] for k in k_counter_descriptors]

    k_counter_symbols = [
        sp.Symbol(raw_symbols_prefix + "_".join([str(s) for s in count]))
        for count in k_counter_descriptors
    ]
    k_counter += [
        Moment(d, s) for d, s in zip(k_counter_descriptors, k_counter_symbols)
    ]

    #  central moments
    n_counter_descriptors = [m for m in k_counter_descriptors if sum(m) > 1]
    # arbitrary symbols
    n_counter_symbols = [
        sp.Symbol(central_symbols_prefix + "_".join([str(s) for s in count]))
        for count in n_counter_descriptors
    ]

    n_counter += [
        Moment(c, s) for c, s in zip(n_counter_descriptors, n_counter_symbols)
    ]

    return n_counter, k_counter
예제 #26
0
def _sample_problem():
    lhs_terms = [
        Moment(np.array([1, 0, 0]), symbol='y_0'),
        Moment(np.array([0, 1, 0]), symbol='y_1'),
        Moment(np.array([0, 0, 1]), symbol='y_2'),
        Moment(np.array([0, 0, 2]), symbol='yx1'),
        Moment(np.array([0, 1, 1]), symbol='yx2'),
        Moment(np.array([0, 2, 0]), symbol='yx3'),
        Moment(np.array([1, 0, 1]), symbol='yx4'),
        Moment(np.array([1, 1, 0]), symbol='yx5'),
        Moment(np.array([2, 0, 0]), symbol='yx6')
    ]
    constants = ['c_0', 'c_1', 'c_2', 'c_3', 'c_4', 'c_5', 'c_6']

    c_0 = Symbol('c_0')
    c_1 = Symbol('c_1')
    y_0 = Symbol('y_0')
    c_2 = Symbol('c_2')
    y_2 = Symbol('y_2')
    c_6 = Symbol('c_6')
    yx4 = Symbol('yx4')
    yx6 = Symbol('yx6')
    c_3 = Symbol('c_3')
    c_4 = Symbol('c_4')
    y_1 = Symbol('y_1')
    c_5 = Symbol('c_5')
    yx2 = Symbol('yx2')
    yx1 = Symbol('yx1')
    yx3 = Symbol('yx3')
    yx5 = Symbol('yx5')
    rhs = MutableDenseMatrix([
        [
            c_0 - c_1 * y_0 - c_2 * y_0 * y_2 / (c_6 + y_0) + yx4 *
            (c_2 * y_0 / (c_6 + y_0)**2 - c_2 / (c_6 + y_0)) + yx6 *
            (-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], [c_4 * y_1 - c_5 * y_2],
        [
            2 * c_4 * y_1 * y_2 + c_4 * y_1 + 2 * c_4 * yx2 -
            2 * c_5 * y_2**2 + c_5 * y_2 - 2 * c_5 * yx1 - 2 * y_2 *
            (c_4 * y_1 - c_5 * y_2)
        ],
        [
            c_3 * y_0 * y_2 + c_3 * yx4 + c_4 * y_1**2 - c_4 * y_1 * y_2 -
            c_4 * y_1 + c_4 * yx3 - 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) + yx2 *
            (-c_4 - c_5)
        ],
        [
            2 * c_3 * y_0 * y_1 + c_3 * y_0 + 2 * c_3 * yx5 -
            2 * c_4 * y_1**2 + c_4 * y_1 - 2 * c_4 * yx3 - 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 * yx1 / (c_6 + y_0) + c_4 * y_0 * y_1 + c_4 * yx5 -
            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)) + yx4 *
            (-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_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_1 - c_1 * y_0 * y_1 - c_2 * y_0 * y_1 * y_2 / (c_6 + y_0) -
            c_2 * y_0 * yx2 / (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)) + yx4 *
            (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))) + yx5 *
            (-c_1 + c_2 * y_0 * y_2 / (c_6 + y_0)**2 - c_2 * y_2 /
             (c_6 + y_0) - c_4) + yx6 *
            (-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))
        ],
        [
            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)) + yx4 *
            (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))) + yx6 *
            (-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))
        ]
    ])

    problem = ODEProblem(method='MEA',
                         left_hand_side_descriptors=lhs_terms,
                         right_hand_side=rhs,
                         parameters=constants)
    return problem
예제 #27
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))
예제 #28
0
파일: lna.py 프로젝트: pihop/means
    def run(self):
        """
        Overrides the default _run() private method.
        Performs the complete analysis
        :return: A fully computed set of Ordinary Differential Equations that can be used for further simulation
        :rtype: :class:`~means.core.problems.ODEProblem`
        """

        S = self.model.stoichiometry_matrix
        amat = self.model.propensities
        ymat = self.model.species
        n_species = len(ymat)

        # dPdt is matrix of each species differentiated w.r.t. time
        # The code below literally multiplies the stoichiometry matrix to a column vector of propensities
        # from the right (::math::`\frac{dP}{dt} = \mathbf{Sa}`)
        dPdt = S * amat

        # A Is a matrix of each species (rows) and the derivatives of their stoichiometry matrix rows
        # against each other species
        # Code below computes the matrix A, that is of size `len(ymat) x len(ymat)`, for which each entry
        # ::math::`A_{ik} = \sum_j S_{ij} \frac{\partial a_j}{\partial y_k} = \mathfb{S_i} \frac{\partial \mathbf{a}}{\partial y_k}`
        A = sp.Matrix(len(ymat), len(ymat), lambda i, j: 0)
        for i in range(A.rows):
            for k in range(A.cols):
                A[i, k] = reduce(operator.add, [
                    S[i, j] * sp.diff(amat[j], ymat[k])
                    for j in range(len(amat))
                ])

        # `diagA` is a matrix that has values sqrt(a[i]) on the diagonal (0 elsewhere)
        diagA = sp.Matrix(
            len(amat), len(amat), lambda i, j: amat[i]**sp.Rational(1, 2)
            if i == j else 0)
        # E is stoichiometry matrix times diagA
        E = S * diagA

        variance_terms = []
        cov_matrix = []
        for i in range(len(ymat)):
            row = []
            for j in range(len(ymat)):
                if i <= j:
                    symbol = 'V_{0}_{1}'.format(i, j)
                    variance_terms.append(
                        VarianceTerm(position=(i, j), symbol=symbol))
                else:
                    # Since Vi,j = Vj,i, i.e. covariance are equal, we only record Vi,j but not Vj,i
                    symbol = 'V_{0}_{1}'.format(j, i)
                    variance_terms.append(
                        VarianceTerm(position=(j, i), symbol=symbol))
                row.append(symbol)
            cov_matrix.append(row)

        V = sp.Matrix(cov_matrix)

        # Matrix of variances (diagonal) and covariances of species i and j differentiated wrt time.
        # I.e. if i=j, V_ij is the variance, and if i!=j, V_ij is the covariance between species i and species j
        dVdt = A * V + V * (A.T) + E * (E.T)

        # build ODEProblem object
        rhs_redundant = sp.Matrix([i for i in dPdt] + [i for i in dVdt])

        #generate ODE terms
        n_vectors = [
            tuple([1 if i == j else 0 for i in range(n_species)])
            for j in range(n_species)
        ]
        moment_terms = [
            Moment(nvec, lhs) for (lhs, nvec) in zip(ymat, n_vectors)
        ]

        ode_description = moment_terms + variance_terms

        non_redundant_idx = []
        ode_terms = []
        # remove repetitive covariances, as Vij = Vji
        for i, cov in enumerate(ode_description):
            if cov in ode_terms:
                continue
            else:
                ode_terms.append(cov)
                non_redundant_idx.append(i)
        rhs = []
        for i in non_redundant_idx:
            rhs.append(rhs_redundant[i])

        out_problem = ODEProblem("LNA", ode_terms, rhs,
                                 sp.Matrix(self.model.parameters))

        return out_problem
예제 #29
0
    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)