예제 #1
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
        self.assertEqual(np.shape(mu_sample), (10, 1))
        self.assertEqual(np.shape(sigma_sample), (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)
예제 #2
0
    def setUp(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 a stupid uniform model now
        self.model2 = Uniform([[0], [10]])

        self.sampler = DrawFromPrior([self.model], dummy, seed=1)
        self.original_journal = self.sampler.sample(100)

        self.generate_from_journal = GenerateFromJournal([self.model],
                                                         dummy,
                                                         seed=2)
        self.generate_from_journal_2 = GenerateFromJournal([self.model2],
                                                           dummy,
                                                           seed=2)

        # expected mean values from bootstrapped samples:
        self.mu_mean = -0.2050921750330999
        self.sigma_mean = 5.178647189918053
        # expected mean values from subsampled samples:
        self.mu_mean_2 = -0.021275259024241676
        self.sigma_mean_2 = 5.672004487129107
예제 #3
0
def instantiate_MA2_with_uniform_triangular_region(size=100, **kwargs):
    R1 = Uniform([[0], [1]], name='R1')
    R2 = Uniform([[0], [1]], name='R2')

    model = MA2Reparametrized([R1, R2], size=size, **kwargs)

    return model
예제 #4
0
def infer_parameters_abcsubsim():
    # define observation for true parameters mean=170, 65
    rng = np.random.RandomState(seed=1)
    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]], name="mu0")
    mu1 = Uniform([[25], [100]], name="mu1")
    # 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 ABCsubsim
    sampler = ABCsubsim([height_weight_model], [distance_calculator], backend, seed=1)
    steps, n_samples, n_samples_per_param, chain_length = 2, 10, 1, 2
    print('ABCsubsim Inferring')
    journal = sampler.sample([y_obs], steps, n_samples, n_samples_per_param, chain_length)

    return journal
예제 #5
0
    def setUp(self):
        if has_torch:
            self.net = createDefaultNN(2, 3)()
            self.net_with_scaler = ScalerAndNet(self.net, None)
            self.net_with_discard_wrapper = DiscardLastOutputNet(self.net)
            self.stat_calc = NeuralEmbedding(self.net)
            self.stat_calc_with_scaler = NeuralEmbedding(self.net_with_scaler)
            self.stat_calc_with_discard_wrapper = NeuralEmbedding(
                self.net_with_discard_wrapper)
            # reference input and output
            torch.random.manual_seed(1)
            self.tensor = torch.randn(1, 2)
            self.out = self.net(self.tensor)
            self.out_discard = self.net_with_discard_wrapper(self.tensor)

            # 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])

            self.stat_calc_rescaling = NeuralEmbedding(
                self.net,
                reference_simulations=reference_simulations,
                previous_statistics=Identity(degree=2))

        if not has_torch:
            self.assertRaises(ImportError, NeuralEmbedding, None)
예제 #6
0
def infer_parameters_smcabc():
    # 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 SMCABC
    sampler = SMCABC([height_weight_model], [distance_calculator], backend, seed=1)
    steps, n_samples, n_samples_per_param, epsilon = 2, 10, 1, 2000
    journal = sampler.sample([y_obs], steps, n_samples, n_samples_per_param, epsilon, full_output=1)

    return journal
예제 #7
0
def infer_parameters_sabc():
    # define observation for true parameters mean=170, 65
    rng = np.random.RandomState(seed=1)
    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]], name="mu0")
    mu1 = Uniform([[25], [100]], name="mu1")

    # 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)
    steps, epsilon, n_samples, n_samples_per_param, beta, delta, v = 2, 40000, 10, 1, 2, 0.2, 0.3
    ar_cutoff, resample, n_update, full_output = 0.1, None, None, 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, full_output)

    return journal
예제 #8
0
    def setUp(self):
        class Mockobject(Normal):
            def __init__(self, parameters):
                super(Mockobject, self).__init__(parameters)

            def pdf(self, input_values, x):
                return x

        self.N1 = Uniform([[1.3], [1.55]], name='n1')
        self.N2 = Uniform([[self.N1], [1.60]], name='n2')
        self.N3 = Uniform([[2], [4]], name='n3')
        self.graph1 = Uniform([[self.N1], [self.N2]], name='graph1')
        self.graph2 = Uniform([[.5], [self.N3]])

        self.graph = [self.graph1, self.graph2]

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

        self.sampler1 = RejectionABC([self.graph1], [distance_calculator],
                                     backend)
        self.sampler2 = RejectionABC([self.graph2], [distance_calculator],
                                     backend)
        self.sampler3 = RejectionABC(
            self.graph, [distance_calculator, distance_calculator], backend)

        self.pdf1 = self.sampler1.pdf_of_prior(self.sampler1.model,
                                               [1.32088846, 1.42945274])
        self.pdf2 = self.sampler2.pdf_of_prior(self.sampler2.model, [3])
        self.pdf3 = self.sampler3.pdf_of_prior(self.sampler3.model,
                                               [1.32088846, 1.42945274, 3])
예제 #9
0
def infer_parameters_apmcabc():
    # define observation for true parameters mean=170, 65
    rng = np.random.RandomState(seed=1)
    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]], name="mu0")
    mu1 = Uniform([[25], [100]], name="mu1")
    # 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 APMCABC
    sampler = APMCABC([height_weight_model], [distance_calculator], backend, seed=1)
    steps, n_samples, n_samples_per_param, alpha, acceptance_cutoff, covFactor, full_output, journal_file = 2, 100, 1, 0.2, 0.03, 2.0, 1, None
    print('APMCABC Inferring')
    journal = sampler.sample([y_obs], steps, n_samples, n_samples_per_param, alpha, acceptance_cutoff, covFactor,
                             full_output, journal_file)

    return journal
예제 #10
0
def infer_parameters():
    # 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]], )
    sigma = Uniform([[5], [25]], )
    # define the model
    from abcpy.continuousmodels import Normal as Gaussian
    height = Gaussian([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)

    # define kernel
    from abcpy.perturbationkernel import DefaultKernel
    kernel = DefaultKernel([mu, sigma])

    # 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([height], [distance_calculator], backend, kernel, 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([height_obs], T, eps_arr, n_sample,
                             n_samples_per_param, epsilon_percentile)

    return journal
예제 #11
0
def infer_parameters_pmc():
    # define observation for true parameters mean=170, 65
    rng = np.random.RandomState(seed=1)
    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]], name="mu0")
    mu1 = Uniform([[25], [100]], name="mu1")
    # define the model
    height_weight_model = NestedBivariateGaussian([mu0, mu1])

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

    from abcpy.approx_lhd import SynLikelihood
    approx_lhd = SynLikelihood(statistics_calculator)

    # define sampling scheme
    from abcpy.inferences import PMC
    sampler = PMC([height_weight_model], [approx_lhd], backend, seed=2)

    # sample from scheme
    T, n_sample, n_samples_per_param = 2, 10, 10
    print('PMC Inferring')
    journal = sampler.sample([y_obs], T, n_sample, n_samples_per_param)

    return journal
예제 #12
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])
예제 #13
0
def infer_parameters_pmcabc():
    # define observation for true parameters mean=170, 65
    rng = np.random.RandomState(seed=1)
    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]], name="mu0")
    mu1 = Uniform([[25], [100]], name="mu1")
    # 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
    print('PMCABC Inferring')
    journal = sampler.sample([y_obs], T, eps_arr, n_sample, n_samples_per_param, epsilon_percentile)

    return journal
예제 #14
0
    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.402868)), 1e-3)
        self.assertLess(abs(sigma_post_mean - 6.212), 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.03325763) ), 1e-3)
        self.assertLess(abs(sigma_post_mean - 6.92124735), 1e-3)

        self.assertFalse(journal.number_of_simulations == 0)
예제 #15
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))
예제 #16
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=0)
     self.likfun = PenLogReg(self.stat_calc, [self.model],
                             n_simulate=100,
                             n_folds=10,
                             max_iter=100000,
                             seed=1)
예제 #17
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_per_param=1,
                                                      seed=1,
                                                      n_epochs=10,
                                                      scale_samples=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=10,
                scale_samples=True)
예제 #18
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))
예제 #19
0
    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)]
예제 #20
0
    def setUp(self):
        self.stat_calc1 = Identity(degree = 1, cross = 0)
        self.stat_calc2 = Identity(degree= 1, cross = 0)
        self.likfun1 = SynLikelihood(self.stat_calc1)
        self.likfun2 = SynLikelihood(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])
예제 #21
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()
예제 #22
0
    def setUp(self):
        self.coeff = np.array([[3, 4], [5, 6]])
        self.stat_calc = LinearTransformation(self.coeff,
                                              degree=1,
                                              cross=False)

        # 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 = LinearTransformation(
            self.coeff, reference_simulations=reference_simulations_double)
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)
예제 #24
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
예제 #25
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)
예제 #26
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 summaryselection
        self.summaryselection = Semiautomatic([Y], self.statistics_cal, self.backend, n_samples = 1000, n_samples_per_param = 1, seed = 1)
예제 #27
0
class PenLogRegTests(unittest.TestCase):
    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))

    def test_likelihood(self):
        # Checks whether wrong input type produces error message
        self.assertRaises(TypeError, self.likfun.loglikelihood, 3.4, [2, 1])
        self.assertRaises(TypeError, self.likfun.loglikelihood, [2, 4], 3.4)

        # create observed data
        comp_likelihood = self.likfun.loglikelihood(self.y_obs, self.y_sim)
        expected_likelihood = 9.77317308598673e-08
        # This checks whether it computes a correct value and dimension is right. Not correct as it does not check the
        # absolute value:
        # self.assertLess(comp_likelihood - expected_likelihood, 10e-2)
        self.assertAlmostEqual(comp_likelihood, np.log(expected_likelihood))

        # check if it returns the correct error when n_samples does not match:
        self.assertRaises(RuntimeError, self.likfun_wrong_n_sim.loglikelihood, self.y_obs, self.y_sim)

        # try now with the bivariate uniform model:
        comp_likelihood_biv = self.likfun_bivariate.loglikelihood(self.y_obs_bivariate, self.y_sim_bivariate)
        expected_likelihood_biv = 0.999999999999999
        self.assertAlmostEqual(comp_likelihood_biv, np.log(expected_likelihood_biv))

    def test_likelihood_multiple_observations(self):
        comp_likelihood = self.likfun.loglikelihood(self.y_obs, self.y_sim)
        expected_likelihood = 9.77317308598673e-08
        self.assertAlmostEqual(comp_likelihood, np.log(expected_likelihood))

        expected_likelihood_biv = 0.9999999999999979
        comp_likelihood_biv = self.likfun_bivariate.loglikelihood(self.y_obs_bivariate, self.y_sim_bivariate)
        self.assertAlmostEqual(comp_likelihood_biv, np.log(expected_likelihood_biv))
예제 #28
0
    def formatted_prior(self):

        param_prior_sampler = []

        for prior in self.priors_over_hood:

            prior_type = prior[1]
            param_name = prior[0]
            if prior_type == 'u':
                low, high = prior[2][0], prior[2][1]
                new_prior = Uniform([[low], [high]], name=param_name)
            elif prior_type == 'g':
                mean, sigma = prior[2][0], prior[2][1]
                new_prior = Normal([[mean], [sigma]], name=param_name)
            else:
                raise Exception('prior type ' + str(prior_type) + ' not valid')

            param_prior_sampler.append(new_prior)

        return param_prior_sampler
예제 #29
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)
예제 #30
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)