def test_get_experimental_error (self):
        """ Tests if we can have an experimental error on the list of
            random parameters. """
        p1 = RandomParameter ('p1', Gamma (2, 2))
        p2 = RandomParameter ('p2', Gamma (3, 2))
        p1.value = 1
        p2.value = 2
        sigma = RandomParameter ('sigma', Gamma (2, .1))
        sigma.value = 123

        theta = RandomParameterList ()
        theta.append (p1)
        theta.set_experimental_error (sigma)
        theta.append (p2)
        
        self.assertEqual (theta.get_experimental_error (), 123)
 def create_starting_sample (self):
     my_artificial_sample = []
     log_likelihoods = []
     sample_mean = [0.05, 0.1, 0.2, .3]
     S = np.eye (4) / 5
     mu = np.log (np.array (sample_mean)) - S.diagonal () / 2
     my_sample_dist = MultivariateLognormal (mu, S)
     for i in range (50):
         theta = RandomParameterList ()
         values = my_sample_dist.rvs ()
         for v in values[:-1]:
             p = RandomParameter ('p', Gamma (2, 2))
             p.value = v
             theta.append (p)
         exp_error = RandomParameter ('sigma', Gamma (2, 2))
         theta.set_experimental_error (exp_error)
         log_likelihoods.append (1)
         my_artificial_sample.append (theta)
     return (my_artificial_sample, log_likelihoods)
    def test_get_iterator (self):
        """ Tests if we can get an iterator of the object. """
        p1 = RandomParameter ('p1', Gamma (2, 2))
        p2 = RandomParameter ('p2', Gamma (3, 2))
        sigma = RandomParameter ('sigma', Gamma (2, .1))
        p1.value = 1
        p2.value = 2
        sigma.value = 123
        theta = RandomParameterList ()
        theta.append (p1)
        theta.append (p2)
        theta.set_experimental_error (sigma)

        my_iterator = iter (theta)
        it_1 = next (my_iterator)
        assert (it_1.name == 'p1')
        it_2 = next (my_iterator)
        assert (it_2.name == 'p2')
        it_3 = next (my_iterator)
        assert (it_3.name == 'sigma')
Пример #4
0
def define_sbml_params_priors(sbml, filename):
    """ Defined the prior distribution for all reaction parameters of an
        SBML model.
        
        Parameters
            sbml: an SBML object, with the model of interest.
            filename: a file path that contains the definition of all
                model parameters priors.

        Returns
            a RandomParameterList object that has the prior definition
            of all parameters of the SBML object, in the same order as
            they are seen on SBML.get_all_param ().
    """
    default_priors = read_priors_file(filename)
    priors = RandomParameterList()
    original_names = []
    for param in sbml.get_all_param():
        original_name = sbml.get_original_param_name(param)
        original_names.append(original_name)
        param_prior = None
        for prior_p in default_priors.get_model_parameters():
            if original_name == prior_p.name:
                param_prior = prior_p.copy()
                param_prior.name = param
                break

        if param_prior == None:
            raise ValueError ("Could not find a prior for " + \
                    original_name + " in priors defined in " + filename)

        priors.append(param_prior)

    for prior in default_priors.get_model_parameters():
        if prior.name not in original_names:
            warnings.warn ("Prior of " + prior.name + " do not " + \
                    "correspond to any of the model parameters.")

    sigma_p = default_priors.get_experimental_error_distribution()
    priors.set_experimental_error(sigma_p)
    return priors
Пример #5
0
def read_priors_file(filename):
    """ Reads a priors definition file.

        Parameters
            filename: the path of the priors file.

        Returns 
            a RandomParameterList object, containing all defined
            parameters and its prior distributions and also some
            experimental error.
    """
    tree = etree.parse(filename)
    root = tree.getroot()

    if clean_tag(root) != "priors":
        print("Wrong experiment data syntax. Root tag should be " +
              "<dataset>")
        return None

    priors = RandomParameterList()
    for children in root.getchildren():
        attribs = children.attrib
        if clean_tag(children) == "prior":
            name = attribs["name"]
            a = float(attribs["a"])
            b = float(attribs["b"])
            dist_type = attribs["distribution"]
            dist = __create_distribution(dist_type, [a, b])
            priors.append(RandomParameter(name, dist))
        elif clean_tag(children) == "experimental_error":
            name = attribs["name"]
            a = float(attribs["a"])
            b = float(attribs["b"])
            gamma = Gamma(a, b)
            priors.set_experimental_error(RandomParameter(name, gamma))
        else:
            print("Warning: unindentified prior definition on " + filename)

    return priors
Пример #6
0
class TestLikelihoodFunction(unittest.TestCase):
    def setUp(self):
        # dx1 (t)/dt = x1 (t), x1 (0) = 1
        # x1 (t) = e ^ t
        self.odes = ODES()
        self.odes.add_equation("x1", "x1")
        self.odes.define_initial_value("x1", 1.0)
        self.theta = RandomParameterList()
        sigma_dist = Gamma(1, 1)
        sigma = RandomParameter("sigma", sigma_dist)
        sigma.value = 1.0
        self.theta.set_experimental_error(sigma)

    def __gaussian(self, mu, sigma, x):
        """ Calculates a point of the gaussian function. """
        l = np.exp (-.5 * ((x - mu) / sigma) ** 2) * \
                (1 / (sigma * np.sqrt (2 * np.pi)))
        return l

    def test_get_likelihood_point(self):
        """ Tests if the likelihood can return the correct value when
            there's only one observation. """
        # x1(0) = 1.0
        # D ~ Gaussian (x1(0), 1) ~ Gaussian (1, 1)
        # f_D (1) = e ^ -{[(0) ^ 2] / [2 * 1]} * {1 * sqrt (2pi)} ^ -1
        f_D = self.__gaussian(1, 1, 1)
        analytic = np.log(f_D)

        t = [0]
        values = [1.0]
        var = "x1"
        experiments = [Experiment(t, values, var)]
        likelihood_f = LikelihoodFunction(self.odes)
        l = likelihood_f.get_log_likelihood(experiments, self.theta)
        assert (abs(analytic - l) < 1e-8)

    def test_get_likelihood_over_time(self):
        """ Tests if the likelihood can return the correct value when 
            the observation occurs in multiple time points. """
        t = [0, .25, .5, .75, 1]
        D = [np.exp(x) for x in t]
        experiments = [Experiment(t, D, "x1")]
        f_D = 1
        for y in D:
            f_D *= self.__gaussian(y, 1, y)
        analytic = np.log(f_D)

        likelihood_f = LikelihoodFunction(self.odes)
        l = likelihood_f.get_log_likelihood(experiments, self.theta)
        assert (abs(analytic - l) < 1e-2)

    def test_get_likelihood_experiment_set(self):
        """ Tests if can return the likelihood of data in respect to
            multiple experimnets. """
        t = [0, .25, .5, .75, 1]
        D = [np.exp(x) for x in t]
        experiment = Experiment(t, D, "x1")
        experiments = [experiment, experiment]
        f_D = 1
        for y in D:
            f_D *= self.__gaussian(y, 1, y)
        f_D **= 2
        analytic = np.log(f_D)

        likelihood_f = LikelihoodFunction(self.odes)
        l = likelihood_f.get_log_likelihood(experiments, self.theta)
        assert (abs(analytic - l) < 1e-2)