示例#1
0
class LogRegTests(unittest.TestCase):
    def setUp(self):
        self.stat_calc = Identity(degree=2, cross=False)
        self.distancefunc = LogReg(self.stat_calc, seed=1)
        self.rng = np.random.RandomState(1)

    def test_distance(self):
        d1 = 0.5 * self.rng.randn(100, 2) - 10
        d2 = 0.5 * self.rng.randn(100, 2) + 10

        d1 = d1.tolist()
        d2 = d2.tolist()

        # Checks whether wrong input type produces error message
        self.assertRaises(TypeError, self.distancefunc.distance, 3.4, d2)
        self.assertRaises(TypeError, self.distancefunc.distance, d1, 3.4)

        # completely separable datasets should have a distance of 1.0
        self.assertEqual(self.distancefunc.distance(d1, d2), 1.0)

        # equal data sets should have a distance of 0.0
        self.assertEqual(self.distancefunc.distance(d1, d1), 0.0)

    def test_dist_max(self):
        self.assertTrue(self.distancefunc.dist_max() == 1.0)
示例#2
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
示例#3
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])
示例#4
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)

        y_sim = sampler.simulate(1, rng=rng)

        self.assertTrue(isinstance(y_sim, list))

        self.assertTrue(len(y_sim) == 2)

        self.assertTrue(isinstance(y_sim[0][0], np.ndarray))
示例#5
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 = PMCABC([self.graph], [distance_calculator], backend)

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

        self.sampler.sample_from_prior(rng=self.rng)

        kernel = DefaultKernel([self.N1, self.N2, self.B1])
        self.sampler.kernel = kernel

        self.sampler.accepted_parameters_manager.update_broadcast(
            self.sampler.backend, [[3, 0.11, 0.029], [4, 0.098, 0.031]],
            accepted_cov_mats=[[[1, 0], [0, 1]]],
            accepted_weights=np.array([1, 1]))

        kernel_parameters = []
        for kernel in self.sampler.kernel.kernels:
            kernel_parameters.append(
                self.sampler.accepted_parameters_manager.
                get_accepted_parameters_bds_values(kernel.models))
        self.sampler.accepted_parameters_manager.update_kernel_values(
            self.sampler.backend, kernel_parameters)
示例#6
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
示例#7
0
class LogRegTests(unittest.TestCase):
    def setUp(self):
        self.stat_calc = Identity(degree = 1, cross = 0)
        self.distancefunc = LogReg(self.stat_calc)
        
    def test_distance(self):
        d1 = 0.5 * np.random.randn(100,2) - 10
        d2 = 0.5 * np.random.randn(100,2) + 10
        
        #Checks whether wrong input type produces error message
        self.assertRaises(TypeError, self.distancefunc.distance, 3.4, d2)
        self.assertRaises(TypeError, self.distancefunc.distance, d1, 3.4)
        
        # completely separable datasets should have a distance of 1.0
        self.assertEqual(self.distancefunc.distance(list(d1),list(d2)), 1.0)

        # equal data sets should have a distance of 0.0
        self.assertEqual(self.distancefunc.distance(list(d1),list(d1)), 0.0)
        
    def test_dist_max(self):
        self.assertTrue(self.distancefunc.dist_max() == 1.0)        
示例#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
    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)
示例#10
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)
示例#11
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)])
示例#12
0
def infer_parameters():
    # define backend
    # Note, the dummy backend does not parallelize the code!
    from abcpy.backends import BackendDummy as Backend
    backend = Backend()

    # 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
    height = Normal([mu, sigma], )

    # define statistics
    from abcpy.statistics import Identity
    statistics_calculator = Identity(degree=3, cross=True)

    # Learn the optimal summary statistics using Semiautomatic summary selection
    from abcpy.summaryselections import Semiautomatic
    summary_selection = Semiautomatic([height],
                                      statistics_calculator,
                                      backend,
                                      n_samples=1000,
                                      n_samples_per_param=1,
                                      seed=1)

    # Redefine the statistics function
    statistics_calculator.statistics = lambda x, f2=summary_selection.transformation, \
                                              f1=statistics_calculator.statistics: f2(f1(x))

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

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

    # 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
def infer_parameters(steps=3,
                     n_sample=250,
                     n_samples_per_param=10,
                     logging_level=logging.WARN):
    """Perform inference for this example.

    Parameters
    ----------
    steps : integer, optional
        Number of iterations in the sequential PMCABC algoritm ("generations"). The default value is 3
    n_samples : integer, optional
        Number of posterior samples to generate. The default value is 250.
    n_samples_per_param : integer, optional
        Number of data points in each simulated data set. The default value is 10.

    Returns
    -------
    abcpy.output.Journal
        A journal containing simulation results, metadata and optionally intermediate results.
    """
    logging.basicConfig(level=logging_level)
    # 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.continuousmodels import Uniform
    mu = Uniform([[150], [200]], name="mu")
    sigma = Uniform([[5], [25]], name="sigma")

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

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

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

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

    return journal
示例#14
0
 def setUp(self):
     self.stat_calc = Identity(degree=1, cross=0)
     self.distancefunc = LogReg(self.stat_calc)
示例#15
0
    rng = np.random.RandomState(rseed)
    n_samples = int(T / dt) + 1
    noise = rng.normal(loc=0, scale=0.05, size=(n_samples))
    obs_data = hh_simulator.forward_simulate([gbar_K_true, gbar_Na_true])

    # define statistics
    # statistics_calculator = Identity()
    t = stimulus_dict["t"]
    stim_duration = stimulus_dict["duration"]
    t_stim_on = stimulus_dict["t_stim_on"]
    statistics_calculator = Features(t, stim_duration, t_stim_on)

    # define distance
    distance_calculator = Euclidean(statistics_calculator)
    distance_calculator2 = Wasserstein(statistics_calculator)
    distance_calculator3 = LogReg(statistics_calculator)

    # checking
    sim_data = hh_simulator.forward_simulate(
        [gbar_K_true + 3, gbar_Na_true + 4])

    #obs_data = [obs_data[0] / np.max(obs_data[0]) + noise]
    #sim_data = [sim_data[0] / np.max(sim_data[0])]

    s1 = statistics_calculator.statistics(obs_data)
    s2 = statistics_calculator.statistics(sim_data)
    dist = distance_calculator.distance(obs_data, sim_data)
    dist2 = distance_calculator2.distance(obs_data, sim_data)
    dist3 = distance_calculator3.distance(obs_data, sim_data)
    print(s1)
    print(s2)
示例#16
0
 def setUp(self):
     self.stat_calc = Identity(degree=2, cross=False)
     self.distancefunc = LogReg(self.stat_calc, seed=1)
     self.rng = np.random.RandomState(1)