示例#1
0
    def simple_est_setup_mean_only_summary(self):
        # see jupyter notebook ex_docs_tests for more info and plots
        ### define network
        net = me.Network('net_min_2_4')
        net.structure([{
            'start': 'X_t',
            'end': 'Y_t',
            'rate_symbol': 'd',
            'type': 'S -> E',
            'reaction_steps': 2
        }, {
            'start': 'Y_t',
            'end': 'Y_t',
            'rate_symbol': 'l',
            'type': 'S -> S + S',
            'reaction_steps': 4
        }])

        ### create data with known values
        time_values = np.array([0.0, 20.0, 40.0])
        data_mean = np.array([[[1., 0.67, 0.37], [0., 0.45, 1.74]],
                              [[0.01, 0.0469473, 0.04838822],
                               [0.01, 0.07188642, 0.1995514]]])
        data_variance = np.array([[[0., 0.22333333, 0.23545455],
                                   [0., 0.51262626, 4.03272727]],
                                  [[0.01, 0.01631605, 0.01293869],
                                   [0.01, 0.08878719, 0.68612036]]])
        data_covariance = np.array([[[0., -0.30454545, -0.65030303]],
                                    [[0.01, 0.0303608, 0.06645246]]])
        data = me.Data('data_test_est_min_2_4')
        data.load(['X_t', 'Y_t'],
                  time_values,
                  None,
                  'summary',
                  mean_data=data_mean,
                  var_data=data_variance,
                  cov_data=data_covariance)

        ### run estimation and return object
        variables = {'X_t': ('X_t', ), 'Y_t': ('Y_t', )}
        initial_values_type = 'synchronous'
        initial_values = {('X_t', ): 1.0, ('Y_t', ): 0.0}
        theta_bounds = {'d': (0.0, 0.15), 'l': (0.0, 0.15)}
        time_values = None
        sim_mean_only = True
        fit_mean_only = True

        nlive = 1000  # 250 # 1000
        tolerance = 0.01  # 0.1 (COARSE) # 0.05 # 0.01 (NORMAL)
        bound = 'multi'
        sample = 'unif'

        est = me.Estimation('est_min_2_4', net, data)
        est.estimate(variables, initial_values_type, initial_values,
                     theta_bounds, time_values, sim_mean_only, fit_mean_only,
                     nlive, tolerance, bound, sample)
        return est
示例#2
0
 def test_get_credible_interval(self):
     net = me.Network('net_test')
     data = me.Data('data_test')
     est = me.Estimation('est_test', net, data)
     samples = np.array([[0.2, 3.4], [0.4, 3.2], [0.25, 3.65]])
     params_cred_res = np.array(est.get_credible_interval(samples))
     params_cred_sol = np.array([[0.25, 0.2025, 0.3925],
                                 [3.4, 3.21, 3.6375]])
     np.testing.assert_allclose(params_cred_sol, params_cred_res)
示例#3
0
 def test_compute_bayesian_information_criterion(self):
     net = me.Network('net_test')
     data = me.Data('data_test')
     est = me.Estimation('est_test', net, data)
     bic = est.compute_bayesian_information_criterion(15, 2, 35.49)
     np.testing.assert_allclose(-65.56389959779558, bic)
     bic = est.compute_bayesian_information_criterion(15.0, 2, 35.49)
     np.testing.assert_allclose(-65.56389959779558, bic)
     bic = est.compute_bayesian_information_criterion(15, 2.0, 35.49)
     np.testing.assert_allclose(-65.56389959779558, bic)
     bic = est.compute_bayesian_information_criterion(15.0, 2.0, 35.49)
     np.testing.assert_allclose(-65.56389959779558, bic)
示例#4
0
 def test_initialise_net_theta_bounds(self):
     net = me.Network('net_test')
     data = me.Data('data_test')
     est = me.Estimation('est_test', net, data)
     net_theta_bounds_res = est.initialise_net_theta_bounds(
         ['theta_0', 'theta_1', 'theta_2'], {
             'theta_2': 'p3',
             'theta_0': 'p1',
             'theta_1': 'p2'
         }, {
             'p2': (0.0, 0.1),
             'p3': (0.0, 0.2),
             'p1': (0.0, 0.3)
         })
     net_theta_bounds_sol = np.array([[0., 0.3], [0., 0.1], [0., 0.2]])
     np.testing.assert_allclose(net_theta_bounds_sol, net_theta_bounds_res)
示例#5
0
    def test_prior_transform(self):
        net = me.Network('net_test')
        data = me.Data('data_test')
        est = me.Estimation('est_test', net, data)

        est.net_theta_bounds = np.array([[0., 0.15], [0., 0.15]])
        theta_unit = np.array([0.03 / 0.15, 0.075 / 0.15])
        theta_orig_res = est.prior_transform(theta_unit)
        theta_orig_sol = np.array([0.03, 0.075])
        np.testing.assert_allclose(theta_orig_sol, theta_orig_res)

        est.net_theta_bounds = np.array([[0.5, 2.0], [0.1, 4.0]])
        theta_unit = np.array([0.8, 0.2])
        theta_orig_res = est.prior_transform(theta_unit)
        theta_orig_sol = np.array(
            [0.8 * (2.0 - 0.5) + 0.5, 0.2 * (4.0 - 0.1) + 0.1])
        np.testing.assert_allclose(theta_orig_sol, theta_orig_res)
示例#6
0
 def test_compute_log_likelihood_norm_mean_only_true(self):
     net = me.Network('net_test')
     data = me.Data('data_test')
     est = me.Estimation('est_test', net, data)
     mean_data = np.array([[[1., 0.67, 0.37], [0., 0.45, 1.74]],
                           [[0.01, 0.0469473, 0.04838822],
                            [0.01, 0.07188642, 0.1995514]]])
     var_data = np.array([[[0., 0.22333333, 0.23545455],
                           [0., 0.51262626, 4.03272727]],
                          [[0.01, 0.01631605, 0.01293869],
                           [0.01, 0.08878719, 0.68612036]]])
     cov_data = np.array([[[0., -0.30454545, -0.65030303]],
                          [[0.01, 0.0303608, 0.06645246]]])
     norm_res = est.compute_log_likelihood_norm(mean_data, var_data,
                                                cov_data, True)
     norm_sol = 14.028288976285737
     np.testing.assert_allclose(norm_sol, norm_res)
示例#7
0
    def simple_est_setup_mean_only(self):
        # see jupyter notebook ex_docs_tests for more info and plots
        ### define network
        net = me.Network('net_min_2_4')
        net.structure([{
            'start': 'X_t',
            'end': 'Y_t',
            'rate_symbol': 'd',
            'type': 'S -> E',
            'reaction_steps': 2
        }, {
            'start': 'Y_t',
            'end': 'Y_t',
            'rate_symbol': 'l',
            'type': 'S -> S + S',
            'reaction_steps': 4
        }])

        ### create data with known values
        num_iter = 100
        initial_values_type = 'synchronous'
        initial_values = {'X_t': 1, 'Y_t': 0}
        theta_values = {'d': 0.03, 'l': 0.07}
        time_values = np.array([0.0, 20.0, 40.0])
        variables = {'X_t': ('X_t', ), 'Y_t': ('Y_t', )}

        sim = me.Simulation(net)
        res_list = list()

        for __ in range(num_iter):
            res_list.append(
                sim.simulate('gillespie',
                             variables,
                             theta_values,
                             time_values,
                             initial_values_type,
                             initial_gillespie=initial_values)[1])

        sims = np.array(res_list)

        data = me.Data('data_test_est_min_2_4')
        data.load(['X_t', 'Y_t'],
                  time_values,
                  sims,
                  bootstrap_samples=10000,
                  basic_sigma=1 / num_iter)

        # overwrite with fixed values (from a num_iter = 100 simulation)
        data.data_mean = np.array([[[1., 0.67, 0.37], [0., 0.45, 1.74]],
                                   [[0.01, 0.0469473, 0.04838822],
                                    [0.01, 0.07188642, 0.1995514]]])
        data.data_variance = np.array([[[0., 0.22333333, 0.23545455],
                                        [0., 0.51262626, 4.03272727]],
                                       [[0.01, 0.01631605, 0.01293869],
                                        [0.01, 0.08878719, 0.68612036]]])
        data.data_covariance = np.array([[[0., -0.30454545, -0.65030303]],
                                         [[0.01, 0.0303608, 0.06645246]]])

        ### run estimation and return object
        variables = {'X_t': ('X_t', ), 'Y_t': ('Y_t', )}
        initial_values_type = 'synchronous'
        initial_values = {('X_t', ): 1.0, ('Y_t', ): 0.0}
        theta_bounds = {'d': (0.0, 0.15), 'l': (0.0, 0.15)}
        time_values = None
        sim_mean_only = True
        fit_mean_only = True

        nlive = 1000  # 250 # 1000
        tolerance = 0.01  # 0.1 (COARSE) # 0.05 # 0.01 (NORMAL)
        bound = 'multi'
        sample = 'unif'

        est = me.Estimation('est_min_2_4', net, data)
        est.estimate(variables, initial_values_type, initial_values,
                     theta_bounds, time_values, sim_mean_only, fit_mean_only,
                     nlive, tolerance, bound, sample)
        return est
示例#8
0
 def test_compute_log_evidence_from_bic(self):
     net = me.Network('net_test')
     data = me.Data('data_test')
     est = me.Estimation('est_test', net, data)
     log_evid_from_bic = est.compute_log_evidence_from_bic(-65.5)
     np.testing.assert_allclose(32.75, log_evid_from_bic)