Example #1
0
    def test_sample(self):
        # setup backend
        dummy = BackendDummy()

        # define a uniform prior distribution
        lb = np.array([-5, 0])
        ub = np.array([5, 10])
        prior = Uniform(lb, ub, seed=1)

        # define a Gaussian model
        model = Gaussian(prior, mu=2.1, sigma=5.0, seed=1)

        # define sufficient statistics for the model
        stat_calc = Identity(degree=2, cross=0)

        # define a distance function
        dist_calc = Euclidean(stat_calc)

        # create fake observed data
        y_obs = model.simulate(1)

        # use the rejection sampling scheme
        sampler = RejectionABC(model, dist_calc, dummy, seed=1)
        journal = sampler.sample(y_obs, 10, 1, 0.1)
        samples = journal.get_parameters()

        # test shape of samples
        samples_shape = np.shape(samples)
        self.assertEqual(samples_shape, (10, 2))

        # Compute posterior mean
        self.assertEqual((np.average(np.asarray(
            samples[:, 0])), np.average(np.asarray(samples[:, 1]))),
                         (1.6818856447333246, 8.4384177826766518))
Example #2
0
 def setUp(self):
     self.prior = Uniform(np.array([1, .1]), np.array([3, .3]), seed=1)
     self.model = StochLorenz95(self.prior,
                                np.array([2.1, .1]),
                                initial_state=None,
                                n_timestep=160,
                                seed=1)
Example #3
0
def infer_parameters():
    # define observation for true parameters mean=170, std=15
    y_obs = [
        160.82499176, 167.24266737, 185.71695756, 153.7045709, 163.40568812,
        140.70658699, 169.59102084, 172.81041696, 187.38782738, 179.66358934,
        176.63417241, 189.16082803, 181.98288443, 170.18565017, 183.78493886,
        166.58387299, 161.9521899, 155.69213073, 156.17867343, 144.51580379,
        170.29847515, 197.96767899, 153.36646527, 162.22710198, 158.70012047,
        178.53470703, 170.77697743, 164.31392633, 165.88595994, 177.38083686,
        146.67058471763457, 179.41946565658628, 238.02751620619537,
        206.22458790620766, 220.89530574344568, 221.04082532837026,
        142.25301427453394, 261.37656571434275, 171.63761180867033,
        210.28121820385866, 237.29130237612236, 175.75558340169619,
        224.54340549862235, 197.42448680731226, 165.88273684581381,
        166.55094082844519, 229.54308602661584, 222.99844054358519,
        185.30223966014586, 152.69149367593846, 206.94372818527413,
        256.35498655339154, 165.43140916577741, 250.19273595481803,
        148.87781549665536, 223.05547559193792, 230.03418198709608,
        146.13611923127021, 138.24716809523139, 179.26755740864527,
        141.21704876815426, 170.89587081800852, 222.96391329259626,
        188.27229523693822, 202.67075179617672, 211.75963110985992,
        217.45423324370509
    ]

    # define prior
    from abcpy.distributions import Uniform
    prior = Uniform([150, 5], [200, 25])

    # define the model
    model = Gaussian(prior)

    # define statistics
    from abcpy.statistics import Identity
    statistics_calculator = Identity(degree=2, cross=False)

    # define distance
    from abcpy.distances import LogReg
    distance_calculator = LogReg(statistics_calculator)

    # define kernel
    from abcpy.distributions import MultiStudentT
    mean, cov, df = np.array([.0, .0]), np.eye(2), 3.
    kernel = MultiStudentT(mean, cov, df)

    # define backend
    from abcpy.backends import BackendDummy as Backend
    backend = Backend()

    # define sampling scheme
    from abcpy.inferences import PMCABC
    sampler = PMCABC(model, distance_calculator, kernel, backend)

    # sample from scheme
    T, n_sample, n_samples_per_param = 3, 250, 10
    eps_arr = np.array([.75])
    epsilon_percentile = 10
    journal = sampler.sample(y_obs, T, eps_arr, n_sample, n_samples_per_param,
                             epsilon_percentile)

    return journal
Example #4
0
 def setUp(self):
     self.prior = Uniform([-5.0, 5.0], [5.0, 10.0], seed=1)
     self.model = Gaussian(self.prior, mu=1.1, sigma=1.0, seed=1)
     self.stat_calc = Identity(degree=2, cross=0)
     self.likfun = PenLogReg(self.stat_calc,
                             self.model,
                             n_simulate=100,
                             n_folds=10)
Example #5
0
    def setUp(self):
        # define observation for true parameters mean=170, std=15
        self.y_obs = [160.82499176]
        self.model_array = [None] * 2
        #Model 1: Gaussian
        # define prior
        prior = Uniform([150, 5], [200, 25])
        # define the model
        self.model_array[0] = Gaussian(prior, seed=1)
        #Model 2: Student t
        # define prior
        prior = Uniform([150, 1], [200, 30])
        # define the model
        self.model_array[1] = Student_t(prior, seed=1)

        # define statistics
        self.statistics_calc = Identity(degree=2, cross=False)
        # define backend
        self.backend = Backend()
Example #6
0
    def setUp(self):
        self.stat_calc = Identity(degree = 1, cross = 0)
        
        # define prior and model
        prior = Uniform([150, 5],[200, 25])
        self.model = Gaussian(prior, seed = 1)

        # define backend
        self.backend = Backend()

        # define statistics
        self.statistics_cal = Identity(degree = 2, cross = False)
        
        #Initialize summaryselection
        self.summaryselection = Semiautomatic(self.model, self.statistics_cal, self.backend, n_samples = 1000, seed = 1)
Example #7
0
    def test_set_parameters(self):
        distribution = Uniform([-101], [-100], seed=1)
        sample = distribution.sample(1)[0, 0]
        self.assertLessEqual(sample, -100)
        self.assertGreaterEqual(sample, -101)

        distribution.set_parameters([[100], [101]])
        sample = distribution.sample(1)[0, 0]
        self.assertLessEqual(sample, 101)
        self.assertGreaterEqual(sample, 100)
Example #8
0
class UniformTests(unittest.TestCase):
    def setUp(self):
        self.prior = Uniform([-1.0, -1.0], [1.0, 1.0], seed=1)

    def test_init(self):
        self.assertRaises(TypeError, Uniform, 3.14, [1.0, 1.0])
        self.assertRaises(TypeError, Uniform, [-1.0, -1.0], 3.14)
        self.assertRaises(BaseException, Uniform, [-1.0, -1.0], [.0, 1.0, 1.0])

    def test_sample(self):
        samples = self.prior.sample(1000)
        samples_avg = samples.mean(axis=0)
        samples_min = samples.min(axis=0)
        samples_max = samples.max(axis=0)
        for (avg, min, max) in zip(samples_avg, samples_min, samples_max):
            self.assertLess(abs(avg), 0.05)
            self.assertLess(abs(min + 1.0), 0.05)
            self.assertLess(abs(max - 1.0), 0.05)

        samples_shape = np.shape(samples)
        self.assertEqual(samples_shape, (1000, 2))

    def test_set_parameters(self):
        distribution = Uniform([-101], [-100], seed=1)
        sample = distribution.sample(1)[0, 0]
        self.assertLessEqual(sample, -100)
        self.assertGreaterEqual(sample, -101)

        distribution.set_parameters([[100], [101]])
        sample = distribution.sample(1)[0, 0]
        self.assertLessEqual(sample, 101)
        self.assertGreaterEqual(sample, 100)

    def test_pdf(self):
        new_prior = Uniform(np.array([0.0]), np.array([10.0]), seed=1)
        self.assertEqual(new_prior.pdf(0), 0.1)
        self.assertEqual(new_prior.pdf(-1), 0)
        self.assertEqual(new_prior.pdf(11), 0)
Example #9
0
    def setUp(self):
        # find spark and initialize it
        self.backend = BackendDummy()

        # define a uniform prior distribution
        lb = np.array([-5, 0])
        ub = np.array([5, 10])
        prior = Uniform(lb, ub, seed=1)

        # define a Gaussian model
        self.model = Gaussian(prior, mu=2.1, sigma=5.0, seed=1)

        # define a distance function
        stat_calc = Identity(degree=2, cross=0)
        self.dist_calc = Euclidean(stat_calc)

        # create fake observed data
        self.observation = self.model.simulate(1)

        # define kernel
        mean = np.array([-13.0, .0, 7.0])
        cov = np.eye(3)
        self.kernel = MultiNormal(mean, cov, seed=1)
Example #10
0
 def setUp(self):
     self.prior = Uniform([-1.0, -1.0], [1.0, 1.0], seed=1)
Example #11
0
 def setUp(self):
     self.prior = Uniform([-1.0, 0.0], [1.0, 1.0], seed=1)
     self.model = Gaussian(self.prior, 0, 1, seed=1)
Example #12
0
 def setUp(self):
     self.prior = Uniform(np.array(-10 * np.ones(shape=(2, ))),
                          np.array(10 * np.ones(shape=(2, ))),
                          seed=1)
     self.model = MixtureNormal(self.prior, np.array([3, 5]), seed=1)
Example #13
0
 def setUp(self):
     self.prior = Uniform([3., 0., 1.], [5., 1., 20.], seed=1)
     self.model = Ricker(self.prior,
                         np.array([3.8, .3, 10]),
                         n_timestep=100,
                         seed=1)
Example #14
0
 def setUp(self):
     self.prior = Uniform([-5.0, 5.0], [5.0, 10.0], seed=1)
     self.model = Gaussian(self.prior, mu=1.1, sigma=1.0, seed=1)
     self.stat_calc = Identity(degree=2, cross=0)
     self.likfun = SynLiklihood(self.stat_calc)
Example #15
0
    def test_sample(self):
        # setup backend
        backend = BackendDummy()

        # define a uniform prior distribution
        lb = np.array([-5, 0])
        ub = np.array([5, 10])
        prior = Uniform(lb, ub, seed=1)

        # define a Gaussian model
        model = Gaussian(prior, mu=2.1, sigma=5.0, seed=1)

        # define sufficient statistics for the model
        stat_calc = Identity(degree=2, cross=0)

        # create fake observed data
        y_obs = model.simulate(1)

        # Define the likelihood function
        likfun = SynLiklihood(stat_calc)

        # use the PMC scheme for T = 1
        mean = np.array([-13.0, .0, 7.0])
        cov = np.eye(3)
        kernel = MultiNormal(mean, cov, seed=1)

        T, n_sample, n_samples_per_param = 1, 10, 100
        sampler = PMC(model, likfun, kernel, backend, seed=1)
        journal = sampler.sample(y_obs,
                                 T,
                                 n_sample,
                                 n_samples_per_param,
                                 covFactor=np.array([.1, .1]),
                                 iniPoints=None)
        samples = (journal.get_parameters(), journal.get_weights())

        # Compute posterior mean
        mu_post_sample, sigma_post_sample, post_weights = np.array(
            samples[0][:, 0]), np.array(samples[0][:,
                                                   1]), np.array(samples[1][:,
                                                                            0])
        mu_post_mean, sigma_post_mean = np.average(
            mu_post_sample,
            weights=post_weights), np.average(sigma_post_sample,
                                              weights=post_weights)

        # test shape of sample
        mu_sample_shape, sigma_sample_shape, weights_sample_shape = np.shape(
            mu_post_sample), np.shape(mu_post_sample), np.shape(post_weights)
        self.assertEqual(mu_sample_shape, (10, ))
        self.assertEqual(sigma_sample_shape, (10, ))
        self.assertEqual(weights_sample_shape, (10, ))
        self.assertLess(abs(mu_post_mean - (-1.48953333102)), 1e-10)
        self.assertLess(abs(sigma_post_mean - 6.50695612708), 1e-10)

        # use the PMC scheme for T = 2
        T, n_sample, n_samples_per_param = 2, 10, 100
        sampler = PMC(model, likfun, kernel, backend, seed=1)
        journal = sampler.sample(y_obs,
                                 T,
                                 n_sample,
                                 n_samples_per_param,
                                 covFactor=np.array([.1, .1]),
                                 iniPoints=None)
        samples = (journal.get_parameters(), journal.get_weights())

        # Compute posterior mean
        mu_post_sample, sigma_post_sample, post_weights = np.asarray(
            samples[0][:, 0]), np.asarray(samples[0][:, 1]), np.asarray(
                samples[1][:, 0])
        mu_post_mean, sigma_post_mean = np.average(
            mu_post_sample,
            weights=post_weights), np.average(sigma_post_sample,
                                              weights=post_weights)

        # test shape of sample
        mu_sample_shape, sigma_sample_shape, weights_sample_shape = np.shape(
            mu_post_sample), np.shape(mu_post_sample), np.shape(post_weights)
        self.assertEqual(mu_sample_shape, (10, ))
        self.assertEqual(sigma_sample_shape, (10, ))
        self.assertEqual(weights_sample_shape, (10, ))
        self.assertLess(abs(mu_post_mean - (-1.4033145848)), 1e-10)
        self.assertLess(abs(sigma_post_mean - 7.05175546876), 1e-10)
Example #16
0
 def test_pdf(self):
     new_prior = Uniform(np.array([0.0]), np.array([10.0]), seed=1)
     self.assertEqual(new_prior.pdf(0), 0.1)
     self.assertEqual(new_prior.pdf(-1), 0)
     self.assertEqual(new_prior.pdf(11), 0)
Example #17
0
 def setUp(self):
     self.prior = Uniform([-1.0, 0.0], [1.0, 1.0], seed=1)
     self.model = Student_t(self.prior, 2.0, 2.0, seed=1)
Example #18
0
import numpy as np
from Model import Deposition
from Statistics import DepositionStatistics
from Distance import DepositionDistance
from abcpy.distributions import Uniform, MultiNormal
from abcpy.inferences import SABC
from abcpy.output import Journal
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde

from abcpy.backends import BackendDummy
backend = BackendDummy()

#Define model
lb, ub = [50, 5, 0.1, 0.5e-3, 0], [150, 20, 1.5, 3e-3, 10]
prior = Uniform(lb=[50, 5, 0.1, 0.5e-3, 0], ub=[150, 20, 1.5, 3e-3, 10])
pAd, pAg, pT, pF, aT = 110, 14.6, 0.6, 1.7e-3, 6
model = Deposition(prior, pAd, pAg, pT, pF, aT, seed=1)
# Observed data
a = np.array([[0, 0, 0, 172200, 4808], [20, 1689, 26.8, 155100, 1683],
              [60, 2004, 29.9, 149400, 0], [120, 1968, 31.3, 140700, 0],
              [300, 1946, 36.6, 125800, 0]])

np.save('depo_experimental.npy', a)
BE = np.zeros(shape=(10, 5))
index = 5
y_obs = np.load('depo_experimental.npy')[index]

# Define summary stat and distance
stat_calc = DepositionStatistics(degree=1, cross=0)
dist_calc = DepositionDistance(stat_calc)