コード例 #1
0
def infer_parameters(backend,
                     steps=3,
                     n_sample=250,
                     n_samples_per_param=10,
                     logging_level=logging.WARN):
    logging.basicConfig(level=logging_level)
    # define observation for true parameters mean=170, std=15
    height_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.continuousmodels import Uniform
    mu = Uniform([[150], [200]], name='mu')
    sigma = Uniform([[5], [25]], name='sigma')

    # define the model
    from abcpy.continuousmodels import Normal
    height = Normal([mu, sigma], name='height')

    # 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, seed=42)

    # define sampling scheme
    from abcpy.inferences import PMCABC
    sampler = PMCABC([height], [distance_calculator], backend, seed=1)

    # sample from scheme
    eps_arr = np.array([.75])
    epsilon_percentile = 10
    journal = sampler.sample([height_obs], steps, eps_arr, n_sample,
                             n_samples_per_param, epsilon_percentile)

    return journal
コード例 #2
0
    def setUp(self):
        self.stat_calc1 = Identity(degree=1, cross=0)
        self.stat_calc2 = Identity(degree=1, cross=0)
        self.likfun1 = SynLiklihood(self.stat_calc1)
        self.likfun2 = SynLiklihood(self.stat_calc2)
        ## Define Models
        # define a uniform prior distribution
        self.mu = Uniform([[-5.0], [5.0]], name='mu')
        self.sigma = Uniform([[0.0], [10.0]], name='sigma')
        # define a Gaussian model
        self.model1 = Normal([self.mu, self.sigma])
        self.model2 = Normal([self.mu, self.sigma])

        #Check whether wrong sized distnacefuncs gives an error
        self.assertRaises(ValueError, ProductCombination,
                          [self.model1, self.model2], [self.likfun1])

        self.jointapprox_lhd = ProductCombination([self.model1, self.model2],
                                                  [self.likfun1, self.likfun2])
コード例 #3
0
ファイル: approx_lhd_tests.py プロジェクト: uweschmitt/abcpy
 def setUp(self):
     self.mu = Uniform([[-5.0], [5.0]], name='mu')
     self.sigma = Uniform([[5.0], [10.0]], name='sigma')
     self.model = Normal([self.mu, self.sigma])
     self.stat_calc = Identity(degree=2, cross=0)
     self.likfun = PenLogReg(self.stat_calc, [self.model],
                             n_simulate=100,
                             n_folds=10,
                             max_iter=100000,
                             seed=1)
コード例 #4
0
ファイル: approx_lhd_tests.py プロジェクト: ummavi/abcpy
 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,
                             max_iter=100000,
                             seed=1)
コード例 #5
0
 def setUp(self):
     self.mu = Uniform([[-5.0], [5.0]], name='mu')
     self.sigma = Uniform([[5.0], [10.0]], name='sigma')
     self.model = Normal([self.mu, self.sigma])
     self.stat_calc = Identity(degree=2, cross=False)
     self.likfun = SynLikelihood(self.stat_calc)
     # create fake simulated data
     self.mu._fixed_values = [1.1]
     self.sigma._fixed_values = [1.0]
     self.y_sim = self.model.forward_simulate(self.model.get_input_values(), 100, rng=np.random.RandomState(1))
コード例 #6
0
    def setUp(self):
        self.stat_calc1 = Identity(degree=1, cross=0)
        self.stat_calc2 = Identity(degree=1, cross=0)
        self.distancefunc1 = Euclidean(self.stat_calc1)
        self.distancefunc2 = Euclidean(self.stat_calc2)
        ## Define Models
        # define a uniform prior distribution
        mu = Uniform([[-5.0], [5.0]], name='mu')
        sigma = Uniform([[0.0], [10.0]], name='sigma')
        # define a Gaussian model
        self.model1 = Normal([mu, sigma])
        self.model2 = Normal([mu, sigma])

        # Check whether wrong sized distnacefuncs gives an error
        self.assertRaises(ValueError, LinearCombination, [self.model1, self.model2], [self.distancefunc1], [1.0, 1.0])

        # Check whether wrong sized weights gives an error
        self.assertRaises(ValueError, LinearCombination, [self.model1, self.model2],
                          [self.distancefunc1, self.distancefunc2], [1.0, 1.0, 1.0])

        self.jointdistancefunc = LinearCombination([self.model1, self.model2], [self.distancefunc1, self.distancefunc2],
                                                   [1.0, 1.0])
コード例 #7
0
    def test(self):
        N1 = Normal([1, 0.1])
        N2 = Normal([N1, 0.1])
        N2.visited = True
        N1.visited = True

        statistics_calculator = Identity(degree=2, cross=False)
        distance_calculator = LogReg(statistics_calculator)
        backend = Backend()

        sampler = RejectionABC([N2], [distance_calculator], backend)

        sampler._reset_flags()

        self.assertFalse(N1.visited)
        self.assertFalse(N2.visited)
コード例 #8
0
    def setUp(self):
        self.B1 = Binomial([10, 0.2])
        self.N1 = Normal([0.03, 0.01])
        self.N2 = Normal([0.1, self.N1])
        self.graph = Normal([self.B1, self.N2])

        statistics_calculator = Identity(degree=2, cross=False)
        distance_calculator = LogReg(statistics_calculator)
        backend = Backend()

        self.sampler = RejectionABC([self.graph], [distance_calculator],
                                    backend)

        self.rng = np.random.RandomState(1)

        self.sampler.sample_from_prior(rng=self.rng)
コード例 #9
0
ファイル: inferences_tests.py プロジェクト: uweschmitt/abcpy
    def setUp(self):
        # find spark and initialize it
        self.backend = BackendDummy()

        # define a uniform prior distribution
        mu = Uniform([[-5.0], [5.0]], name='mu')
        sigma = Uniform([[0.0], [10.0]], name='sigma')
        # define a Gaussian model
        self.model = Normal([mu, sigma])

        # 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.forward_simulate(1, np.random.RandomState(1))[0].tolist()
        self.observation = [np.array(9.8)]
コード例 #10
0
    def setUp(self):
        self.mu = Uniform([[-5.0], [5.0]], name='mu')
        self.sigma = Uniform([[5.0], [10.0]], name='sigma')
        self.model = Normal([self.mu, self.sigma])
        self.model_bivariate = Uniform([[0, 0], [1, 1]], name="model")
        self.stat_calc = Identity(degree=2, cross=1)
        self.likfun = PenLogReg(self.stat_calc, [self.model],
                                n_simulate=100,
                                n_folds=10,
                                max_iter=100000,
                                seed=1)
        self.likfun_wrong_n_sim = PenLogReg(self.stat_calc, [self.model],
                                            n_simulate=10,
                                            n_folds=10,
                                            max_iter=100000,
                                            seed=1)
        self.likfun_bivariate = PenLogReg(self.stat_calc,
                                          [self.model_bivariate],
                                          n_simulate=100,
                                          n_folds=10,
                                          max_iter=100000,
                                          seed=1)

        self.y_obs = self.model.forward_simulate(self.model.get_input_values(),
                                                 1,
                                                 rng=np.random.RandomState(1))
        self.y_obs_bivariate = self.model_bivariate.forward_simulate(
            self.model_bivariate.get_input_values(),
            1,
            rng=np.random.RandomState(1))
        self.y_obs_double = self.model.forward_simulate(
            self.model.get_input_values(), 2, rng=np.random.RandomState(1))
        self.y_obs_bivariate_double = self.model_bivariate.forward_simulate(
            self.model_bivariate.get_input_values(),
            2,
            rng=np.random.RandomState(1))
        # create fake simulated data
        self.mu._fixed_values = [1.1]
        self.sigma._fixed_values = [1.0]
        self.y_sim = self.model.forward_simulate(self.model.get_input_values(),
                                                 100,
                                                 rng=np.random.RandomState(1))
        self.y_sim_bivariate = self.model_bivariate.forward_simulate(
            self.model_bivariate.get_input_values(),
            100,
            rng=np.random.RandomState(1))
コード例 #11
0
    def setUp(self):
        # define prior and model
        sigma = Uniform([[10], [20]])
        mu = Normal([0, 1])
        self.Y = Normal([mu, sigma])

        # define backend
        self.backend = Backend()

        # define statistics
        self.statistics_cal = Identity(degree=3, cross=False)

        if has_torch:
            # Initialize statistics learning
            self.statisticslearning = SemiautomaticNN([self.Y],
                                                      self.statistics_cal,
                                                      self.backend,
                                                      n_samples=100,
                                                      n_samples_val=100,
                                                      n_samples_per_param=1,
                                                      seed=1,
                                                      n_epochs=2,
                                                      scale_samples=False,
                                                      use_tqdm=False)
            self.statisticslearning2 = SemiautomaticNN([self.Y],
                                                       self.statistics_cal,
                                                       self.backend,
                                                       n_samples=10,
                                                       n_samples_val=10,
                                                       n_samples_per_param=1,
                                                       seed=1,
                                                       n_epochs=5,
                                                       scale_samples=False,
                                                       use_tqdm=False)
            # with sample scaler:
            self.statisticslearning_with_scaler = SemiautomaticNN(
                [self.Y],
                self.statistics_cal,
                self.backend,
                n_samples=100,
                n_samples_per_param=1,
                seed=1,
                n_epochs=2,
                scale_samples=True,
                use_tqdm=False)
コード例 #12
0
    def test(self):
        B1 = Binomial([10, 0.2])
        N1 = Normal([0.03, 0.01])
        N2 = Normal([0.1, N1])
        graph = Normal([B1, N2])

        statistics_calculator = Identity(degree=2, cross=False)
        distance_calculator = LogReg(statistics_calculator)
        backend = Backend()

        sampler = RejectionABC([graph], [distance_calculator], backend)

        rng = np.random.RandomState(1)

        sampler.sample_from_prior(rng=rng)
        self.assertIsNotNone(B1._fixed_values)
        self.assertIsNotNone(N1._fixed_values)
        self.assertIsNotNone(N2._fixed_values)
コード例 #13
0
    def setUp(self):
        # define prior and model
        sigma = Uniform([[10], [20]])
        mu = Normal([0, 1])
        Y = Normal([mu, sigma])

        # define backend
        self.backend = Backend()

        # define statistics
        self.statistics_cal = Identity(degree=3, cross=False)

        # Initialize statistics learning
        self.statisticslearning = Semiautomatic([Y],
                                                self.statistics_cal,
                                                self.backend,
                                                n_samples=1000,
                                                n_samples_per_param=1,
                                                seed=1)
コード例 #14
0
ファイル: modelselections_tests.py プロジェクト: ummavi/abcpy
    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()
コード例 #15
0
def infer_parameters_rsmcabc():
    # define observation for true parameters mean=170, 65
    rng = np.random.RandomState()
    y_obs = [
        np.array(
            rng.multivariate_normal([170, 65], np.eye(2), 1).reshape(2, ))
    ]

    # define prior
    from abcpy.continuousmodels import Uniform
    mu0 = Uniform([[150], [200]], )
    mu1 = Uniform([[25], [100]], )

    # define the model
    height_weight_model = NestedBivariateGaussian([mu0, mu1])

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

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

    # define sampling scheme
    from abcpy.inferences import RSMCABC
    sampler = RSMCABC([height_weight_model], [distance_calculator],
                      backend,
                      seed=1)
    print('sampling')
    steps, n_samples, n_samples_per_param, alpha, epsilon_init, epsilon_final = 2, 10, 1, 0.1, 10000, 500
    print('RSMCABC Inferring')
    journal = sampler.sample([y_obs],
                             steps,
                             n_samples,
                             n_samples_per_param,
                             alpha,
                             epsilon_init,
                             epsilon_final,
                             full_output=1)

    return journal
コード例 #16
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], seed=1)
    
    # define the model
    from abcpy.models import Gaussian
    model = Gaussian(prior, seed=1)
    
    # 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, seed=1)
    
    # define backend
    # Note, the dummy backend does not parallelize the code!
    from abcpy.backends import BackendDummy as Backend
    backend = Backend()
    
    # define sampling scheme
    from abcpy.inferences import PMCABC
    sampler = PMCABC(model, distance_calculator, kernel, backend, seed=1)
    
    # 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
コード例 #17
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
        self.mu1 = Uniform([[150], [200]], name='mu1')
        self.sigma1 = Uniform([[5.0], [25.0]], name='sigma1')
        # define the model
        self.model_array[0] = Normal([self.mu1, self.sigma1])
        # Model 2: Student t
        # define prior
        self.mu2 = Uniform([[150], [200]], name='mu2')
        self.sigma2 = Uniform([[1], [30.0]], name='sigma2')
        # define the model
        self.model_array[1] = StudentT([self.mu2, self.sigma2])

        # define statistics
        self.statistics_calc = Identity(degree=2, cross=False)
        # define backend
        self.backend = Backend()
コード例 #18
0
    def test(self):
        B1 = Binomial([10, 0.2])
        N1 = Normal([0.03, 0.01])
        N2 = Normal([0.1, N1])
        graph1 = Normal([B1, N2])
        graph2 = Normal([1, N2])

        statistics_calculator = Identity(degree=2, cross=False)
        distance_calculator = LogReg(statistics_calculator)
        backend = Backend()

        sampler = RejectionABC([graph1, graph2],
                               [distance_calculator, distance_calculator],
                               backend)

        rng = np.random.RandomState(1)

        sampler.sample_from_prior(rng=rng)

        mapping, index = sampler._get_mapping()
        self.assertTrue(mapping == [(B1, 0), (N2, 1), (N1, 2)])
コード例 #19
0
class IdentityTests(unittest.TestCase):
    def setUp(self):
        self.stat_calc = Identity(degree=1, cross=0)

    def test_statistics(self):
        self.assertRaises(TypeError, self.stat_calc.statistics, 3.4)
        vec1 = np.array([1, 2])
        vec2 = np.array([1])
        self.assertTrue(
            (self.stat_calc.statistics([vec1]) == np.array([vec1])).all())
        self.assertTrue(
            (self.stat_calc.statistics([vec1,
                                        vec1]) == np.array([[vec1],
                                                            [vec1]])).all())
        self.assertTrue(
            (self.stat_calc.statistics([vec2,
                                        vec2]) == np.array([[vec2],
                                                            [vec2]])).all())

    def test_polynomial_expansion(self):
        #Checks whether wrong input type produces error message
        self.assertRaises(TypeError, self.stat_calc._polynomial_expansion, 3.4)

        a = [np.array([0, 2]), np.array([2, 1])]
        # test cross-product part
        self.stat_calc = Identity(degree=2, cross=1)
        self.assertTrue(
            (self.stat_calc.statistics(a) == np.array([[0, 2, 0, 4, 0],
                                                       [2, 1, 4, 1,
                                                        2]])).all())
        # When a tuple
        a = [np.array([0, 2])]
        self.stat_calc = Identity(degree=2, cross=1)
        self.assertTrue(
            (self.stat_calc.statistics(a) == np.array([[0, 2, 0, 4,
                                                        0]])).all())
        self.stat_calc = Identity(degree=2, cross=0)
        self.assertTrue(
            (self.stat_calc.statistics(a) == np.array([[0, 2, 0, 4]])).all())
        a = list(np.array([2]))
        self.stat_calc = Identity(degree=2, cross=1)
        self.assertTrue(
            (self.stat_calc.statistics(a) == np.array([[2, 4]])).all())
コード例 #20
0
def infer_parameters_sabc():
    # define observation for true parameters mean=170, 65
    rng = np.random.RandomState()
    y_obs = [
        np.array(
            rng.multivariate_normal([170, 65], np.eye(2), 1).reshape(2, ))
    ]

    # define prior
    from abcpy.continuousmodels import Uniform
    mu0 = Uniform([[150], [200]], )
    mu1 = Uniform([[25], [100]], )

    # define the model
    height_weight_model = NestedBivariateGaussian([mu0, mu1])

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

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

    # define sampling scheme
    from abcpy.inferences import SABC
    sampler = SABC([height_weight_model], [distance_calculator],
                   backend,
                   seed=1)
    print('sampling')
    steps, epsilon, n_samples, n_samples_per_param, beta, delta, v = 2, np.array(
        [40000]), 10, 1, 2, 0.2, 0.3
    ar_cutoff, resample, n_update, adaptcov, full_output = 0.1, None, None, 1, 1
    #
    # # print('SABC Inferring')
    journal = sampler.sample([y_obs], steps, epsilon, n_samples,
                             n_samples_per_param, beta, delta, v, ar_cutoff,
                             resample, n_update, adaptcov, full_output)

    return journal
コード例 #21
0
def infer_model():
    # define observation for true parameters mean=170, std=15
    y_obs = [160.82499176]

    ## Create a array of models
    from abcpy.continuousmodels import Uniform, Normal, StudentT
    model_array = [None] * 2

    #Model 1: Gaussian
    mu1 = Uniform([[150], [200]], name='mu1')
    sigma1 = Uniform([[5.0], [25.0]], name='sigma1')
    model_array[0] = Normal([mu1, sigma1])

    #Model 2: Student t
    mu2 = Uniform([[150], [200]], name='mu2')
    sigma2 = Uniform([[1], [30.0]], name='sigma2')
    model_array[1] = StudentT([mu2, sigma2])

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

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

    # Initiate the Model selection scheme
    modelselection = RandomForest(model_array,
                                  statistics_calculator,
                                  backend,
                                  seed=1)

    # Choose the correct model
    model = modelselection.select_model(y_obs,
                                        n_samples=100,
                                        n_samples_per_param=1)

    # Compute the posterior probability of each of the models
    model_prob = modelselection.posterior_probability(y_obs)
コード例 #22
0
    def setUp(self):
        # define prior and model
        sigma = Uniform([[10], [20]])
        mu = Normal([0, 1])
        self.Y = Normal([mu, sigma])

        # define backend
        self.backend = Backend()

        # define statistics
        self.statistics_cal = Identity(degree=3, cross=False)

        if has_torch:
            # Initialize statistics learning
            self.statisticslearning = TripletDistanceLearning(
                [self.Y],
                self.statistics_cal,
                self.backend,
                n_samples=100,
                n_samples_per_param=1,
                seed=1,
                n_epochs=10)
コード例 #23
0
    def setUp(self):
        self.stat_calc = Identity(degree=1, cross=False)
        self.stat_calc_pipeline = Identity(degree=2,
                                           cross=False,
                                           previous_statistics=self.stat_calc)

        # try now the statistics rescaling option:
        mu = Uniform([[-5.0], [5.0]], name='mu')
        sigma = Uniform([[0.0], [10.0]], name='sigma')
        # define a Gaussian model
        self.model = Normal([mu, sigma])

        sampler = DrawFromPrior([self.model], BackendDummy(), seed=1)
        reference_parameters, reference_simulations = sampler.sample_par_sim_pairs(
            30, 1)
        reference_simulations = reference_simulations.reshape(
            reference_simulations.shape[0], reference_simulations.shape[2])
        reference_simulations_double = np.concatenate(
            [reference_simulations, reference_simulations], axis=1)

        self.stat_calc_rescaling = Identity(
            reference_simulations=reference_simulations_double)
        self.stat_calc_rescaling_2 = Identity(
            reference_simulations=reference_simulations)
コード例 #24
0
    def test_polynomial_expansion(self):
        #Checks whether wrong input type produces error message
        self.assertRaises(TypeError, self.stat_calc._polynomial_expansion, 3.4)

        a = [np.array([0, 2]), np.array([2, 1])]
        # test cross-product part
        self.stat_calc = Identity(degree=2, cross=1)
        self.assertTrue(
            (self.stat_calc.statistics(a) == np.array([[0, 2, 0, 4, 0],
                                                       [2, 1, 4, 1,
                                                        2]])).all())
        # When a tuple
        a = [np.array([0, 2])]
        self.stat_calc = Identity(degree=2, cross=1)
        self.assertTrue(
            (self.stat_calc.statistics(a) == np.array([[0, 2, 0, 4,
                                                        0]])).all())
        self.stat_calc = Identity(degree=2, cross=0)
        self.assertTrue(
            (self.stat_calc.statistics(a) == np.array([[0, 2, 0, 4]])).all())
        a = list(np.array([2]))
        self.stat_calc = Identity(degree=2, cross=1)
        self.assertTrue(
            (self.stat_calc.statistics(a) == np.array([[2, 4]])).all())
コード例 #25
0
def infer_parameters_pmcabc():
    # define observation for true parameters mean=170, 65
    rng = np.random.RandomState()
    y_obs = [
        np.array(
            rng.multivariate_normal([170, 65], np.eye(2), 1).reshape(2, ))
    ]

    # define prior
    from abcpy.continuousmodels import Uniform
    mu0 = Uniform([[150], [200]], )
    mu1 = Uniform([[25], [100]], )

    # define the model
    height_weight_model = NestedBivariateGaussian([mu0, mu1])

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

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

    # define sampling scheme
    from abcpy.inferences import PMCABC
    sampler = PMCABC([height_weight_model], [distance_calculator],
                     backend,
                     seed=1)
    # sample from scheme
    T, n_sample, n_samples_per_param = 2, 10, 1
    eps_arr = np.array([10000])
    epsilon_percentile = 95

    journal = sampler.sample([y_obs], T, eps_arr, n_sample,
                             n_samples_per_param, epsilon_percentile)

    return journal
コード例 #26
0
def infer_parameters(model_num, synthetic, T, n_sample, ar_cutoff):

    y_obs = [0]

    #prior = Uniform([[0, 0, 0, 0], [5, 5, 2.5, 2.5]])
    prior1 = Uniform([[0], [5]])
    prior2 = Uniform([[0], [5]])
    prior3 = Uniform([[0], [2.5]])
    prior4 = Uniform([[0], [2.5]])

    model = Airport([prior1, prior2, prior3, prior4],
                    name="Airport",
                    model_num=model_num,
                    synthetic=synthetic)

    from abcpy.statistics import Identity
    statistics_calculator = Identity(degree=1, cross=False)

    from abcpy.distances import Euclidean
    distance_calculator = Euclidean(statistics_calculator)

    # define sampling scheme
    from abcpy.inferences import SABC
    sampler = SABC([model], [distance_calculator], backend)

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

    # sample from scheme
    journal = sampler.sample([y_obs],
                             T,
                             1000,
                             n_sample,
                             1,
                             ar_cutoff=ar_cutoff)

    return journal
コード例 #27
0
    def test_sample(self):
        # setup backend
        dummy = BackendDummy()

        # define a uniform prior distribution
        mu = Uniform([[-5.0], [5.0]], name='mu')
        sigma = Uniform([[0.0], [10.0]], name='sigma')
        # define a Gaussian model
        self.model = Normal([mu, sigma])

        # 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 = [np.array(9.8)]

        # use the rejection sampling scheme
        sampler = RejectionABC([self.model], [dist_calc], dummy, seed=1)
        journal = sampler.sample([y_obs], 10, 1, 10)
        mu_sample = np.array(journal.get_parameters()['mu'])
        sigma_sample = np.array(journal.get_parameters()['sigma'])

        # test shape of samples
        mu_shape, sigma_shape = (len(mu_sample), mu_sample[0].shape[1]), \
                                                                    (len(sigma_sample),
                                                                     sigma_sample[0].shape[1])
        self.assertEqual(mu_shape, (10, 1))
        self.assertEqual(sigma_shape, (10, 1))

        # Compute posterior mean
        #self.assertAlmostEqual(np.average(np.asarray(samples[:,0])),1.22301,10e-2)
        self.assertLess(np.average(mu_sample) - 1.22301, 1e-2)
        self.assertLess(np.average(sigma_sample) - 6.992218, 10e-2)

        self.assertFalse(journal.number_of_simulations == 0)
コード例 #28
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)
コード例 #29
0
import numpy as np
from abcpy.statistics import Identity
from Distance import Abserror
from abcpy.inferences import APMCABC
from abcpy.continuousmodels import Uniform
from Model import TIP4PGromacsOOOH as Water

# Define Graphical model
theta1 = Uniform([[.281] , [.53]],name='theta1')
theta2 = Uniform([[0.2] , [0.9]],name='theta2')
water = Water([theta1, theta2, 2500000])

# Define distance and statistics
statistics_calculator = Identity(degree = 1, cross = False)
distance_calculator = Abserror(statistics_calculator)

# Define kernel
from abcpy.backends import BackendMPI as Backend
backend = Backend()
from abcpy.perturbationkernel import DefaultKernel
kernel = DefaultKernel([theta1, theta2])


######### Inference for simulated data ###############
water_obs = [np.load('Data/obs_data.npy')]

sampler = APMCABC([water], [distance_calculator], backend, kernel, seed = 1)
steps, n_samples, n_samples_per_param, alpha, acceptance_cutoff, covFactor, full_output, journal_file =10, 100, 1, 0.1, 0.03, 2.0, 1, None

print('TIP4P: APMCABC Inferring for simulated data')
journal_apmcabc = sampler.sample([water_obs], steps, n_samples, n_samples_per_param, alpha, acceptance_cutoff, covFactor, full_output, journal_file)
コード例 #30
0
ファイル: inferences_tests.py プロジェクト: uweschmitt/abcpy
    def test_sample(self):
        # setup backend
        backend = BackendDummy()

        # define a uniform prior distribution
        mu = Uniform([[-5.0], [5.0]], name='mu')
        sigma = Uniform([[0.0], [10.0]], name='sigma')
        # define a Gaussian model
        self.model = Normal([mu, sigma])

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

        # create fake observed data
        #y_obs = self.model.forward_simulate(1, np.random.RandomState(1))[0].tolist()
        y_obs = [np.array(9.8)]

        # Define the likelihood function
        likfun = SynLiklihood(stat_calc)

        T, n_sample, n_samples_per_param = 1, 10, 100
        sampler = PMC([self.model], [likfun], backend, seed=1)
        journal = sampler.sample([y_obs],
                                 T,
                                 n_sample,
                                 n_samples_per_param,
                                 covFactors=np.array([.1, .1]),
                                 iniPoints=None)
        mu_post_sample, sigma_post_sample, post_weights = np.array(
            journal.get_parameters()['mu']), np.array(
                journal.get_parameters()['sigma']), np.array(
                    journal.get_weights())

        # Compute posterior mean
        mu_post_mean, sigma_post_mean = np.average(mu_post_sample,
                                                   weights=post_weights,
                                                   axis=0), np.average(
                                                       sigma_post_sample,
                                                       weights=post_weights,
                                                       axis=0)

        # 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, 1))
        self.assertEqual(sigma_sample_shape, (10, 1))
        self.assertEqual(weights_sample_shape, (10, 1))
        self.assertLess(abs(mu_post_mean - (-3.55548377)), 1e-3)
        self.assertLess(abs(sigma_post_mean - 5.87147492), 1e-3)

        self.assertFalse(journal.number_of_simulations == 0)

        # use the PMC scheme for T = 2
        T, n_sample, n_samples_per_param = 2, 10, 100
        sampler = PMC([self.model], [likfun], backend, seed=1)
        journal = sampler.sample([y_obs],
                                 T,
                                 n_sample,
                                 n_samples_per_param,
                                 covFactors=np.array([.1, .1]),
                                 iniPoints=None)
        mu_post_sample, sigma_post_sample, post_weights = np.array(
            journal.get_parameters()['mu']), np.array(
                journal.get_parameters()['sigma']), np.array(
                    journal.get_weights())

        # Compute posterior mean
        mu_post_mean, sigma_post_mean = np.average(mu_post_sample,
                                                   weights=post_weights,
                                                   axis=0), np.average(
                                                       sigma_post_sample,
                                                       weights=post_weights,
                                                       axis=0)

        # 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, 1))
        self.assertEqual(sigma_sample_shape, (10, 1))
        self.assertEqual(weights_sample_shape, (10, 1))
        self.assertLess(abs(mu_post_mean - (-3.88512394)), 1e-3)
        self.assertLess(abs(sigma_post_mean - 2.90352432), 1e-3)

        self.assertFalse(journal.number_of_simulations == 0)