Exemplo n.º 1
0
    def test_gaussian_prior(self):
        mean = 10
        std = 2
        p = pints.GaussianLogPrior(mean, std)

        n = 10000
        r = 6 * np.sqrt(std)

        # Test left half of distribution
        x = np.linspace(mean - r, mean, n)
        px = [p([i]) for i in x]
        self.assertTrue(np.all(px[1:] >= px[:-1]))

        # Test right half of distribution
        y = np.linspace(mean, mean + std, n)
        py = [p([i]) for i in y]
        self.assertTrue(np.all(py[1:] <= py[:-1]))

        # Test means
        self.assertAlmostEqual(p.mean(), mean)

        # Test derivatives
        x = [8]
        y, dy = p.evaluateS1(x)
        self.assertEqual(y, p(x))
        self.assertEqual(dy.shape, (1, ))
        self.assertEqual(dy[0], (mean - x[0]) / std**2)

        p = pints.GaussianLogPrior(-1, 4.5)
        x = [3.75]
        self.assertAlmostEqual(p(x), -2.9801146954130457)
        p = pints.GaussianLogPrior(10.4, 0.5)
        x = [5.5]
        y, dy = p.evaluateS1(x)
        self.assertAlmostEqual(y, -48.245791352644737)
        self.assertEqual(dy, 19.6)

        # Test deprecated alias
        p = pints.NormalLogPrior(mean, std)
        self.assertIsInstance(p, pints.GaussianLogPrior)

        # Test assertRaises with negative sd
        self.assertRaises(ValueError, pints.GaussianLogPrior, 0, 0)
        self.assertRaises(ValueError, pints.GaussianLogPrior, 0, -1)
Exemplo n.º 2
0
                                  rate_dict,
                                  transform,
                                  sine_wave=1)
n_params = model.n_params
#
# Define problem
#
problem = pints.SingleOutputProblem(model, time, current)

#
# Define log-posterior
#
log_likelihood = LogLikelihood.tiLogLikelihood(problem, sigma_noise, voltage)
log_prior_rates = prior.LogPrior(rate_dict, lower_conductance, n_params,
                                 transform)
log_prior_discrp = pints.NormalLogPrior(0., 1.5)
log_prior = pints.ComposedLogPrior(log_prior_rates, log_prior_discrp)
log_posterior = pints.LogPosterior(log_likelihood, log_prior)
rate_checker = Rates.ratesPrior(transform, lower_conductance)

#
# Run
#
nchains = 1

# Define starting point for mcmc routine
xs = []

x0 = np.loadtxt(cmaes_result_files + model_name + '-cell-' + str(cell) +
                '-cmaes.txt')
ds = log_prior_discrp.sample().reshape((1, ))
        self._offset = -0.5 * self._nt * np.log(2 * np.pi)
        self._offset -= self._nt * np.log(sigma)
        self._multip = -1 / (2.0 * sigma**2)

    def __call__(self, x):

        error = self._values - self._problem.evaluate(x)
        return self.temperature * np.sum(self._offset + self._multip *
                                         np.sum(error**2, axis=0))


# In[ ]:

sigma_noise = 1.
log_prior_a = pints.NormalLogPrior(2., 1.)
log_prior_b = pints.NormalLogPrior(3., 1.)
log_prior = pints.ComposedLogPrior(log_prior_a, log_prior_b)

# In[ ]:

import mcmcsampling
from joblib import Parallel, delayed
import multiprocessing
niter = 10000


def mcmc_runner(temps):

    nchains = 1
    #print('temperature', temps)