Ejemplo n.º 1
0
class SemiautomaticTests(unittest.TestCase):
    def setUp(self):
        self.stat_calc = Identity(degree = 1, cross = 0)
        
        # define prior and model
        prior = Uniform([150, 5],[200, 25])
        self.model = Gaussian(prior, seed = 1)

        # define backend
        self.backend = Backend()

        # define statistics
        self.statistics_cal = Identity(degree = 2, cross = False)
        
        #Initialize summaryselection
        self.summaryselection = Semiautomatic(self.model, self.statistics_cal, self.backend, n_samples = 1000, seed = 1)

        
    def test_transformation(self):
        #Transform statistics extraction
        self.statistics_cal.statistics = lambda x, f2=self.summaryselection.transformation, f1=self.statistics_cal.statistics: f2(f1(x))
        y_obs = self.model.simulate(10)
        extracted_statistics_10 = self.statistics_cal.statistics(y_obs)
        self.assertEqual(np.shape(extracted_statistics_10), (10,2))
        y_obs = self.model.simulate(1)
        extracted_statistics_1 = self.statistics_cal.statistics(y_obs)
        self.assertLess(extracted_statistics_1[0,0] - 111.012664458, 10e-2)
        self.assertLess(extracted_statistics_1[0,1] - (-63.224510811), 10e-2)
Ejemplo n.º 2
0
class SemiautomaticTests(unittest.TestCase):
    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)
        
    def test_transformation(self):
        # Transform statistics extraction
        self.statistics_cal.statistics = lambda x, f2=self.summaryselection.transformation, f1=self.statistics_cal.statistics: f2(f1(x))
        # Simulate observed data
        Obs = Normal([2, 4] )
        y_obs = Obs.forward_simulate(Obs.get_input_values(), 1)[0].tolist()

        extracted_statistics = self.statistics_cal.statistics(y_obs)
        self.assertEqual(np.shape(extracted_statistics), (1,2))
Ejemplo n.º 3
0
class IdentityTests(unittest.TestCase):
    def setUp(self):
        self.stat_calc = Identity(degree=1, cross=0)
        self.stat_calc_pipeline = Identity(degree=2,
                                           cross=False,
                                           previous_statistics=self.stat_calc)

    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())

    def test_pipeline(self):
        vec1 = np.array([1, 2])
        self.stat_calc_pipeline.statistics([vec1])
Ejemplo n.º 4
0
    #Dialysis vs COPD
    p = [1.87e-01, 1.16e-02, 9.1e-01, 2.14e-01, 5.95e-02, 9.73e-02, 6.66e-03]
    import statsmodels.stats.multitest as multitest
    p_corrected = multitest.multipletests(p,
                                          alpha=alpha_value,
                                          method=correction_method)
    print(p_corrected)

if clustering:
    # #### Joint: Post-processing (Clustering) ################
    mode = np.load('posterior_mode_multiply.npz')['joint_posterior_mode']
    mode = mode[renewed, :]
    print(mode.shape)
    from abcpy.statistics import Identity
    stat = Identity(degree=4, cross=True)
    mode = stat.statistics([[mode[i, :]] for i in range(mode.shape[0])])
    print(mode.shape)
    label = [1 for i in range(16)] + [2 for i in range(16)
                                      ] + [3 for i in range(16)]
    print(label)
    from metric_learn import LMNN
    metric = LMNN(init='auto',
                  k=6,
                  min_iter=10000,
                  max_iter=50000,
                  convergence_tol=1e-6,
                  learn_rate=1e-10,
                  regularization=.5,
                  n_components=2)
    metric.fit(mode, label)
    L = metric.components_
Ejemplo n.º 5
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
Ejemplo n.º 6
0
class IdentityTests(unittest.TestCase):
    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)

    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())

        self.assertTrue((self.stat_calc_rescaling.statistics([vec1]) !=
                         self.stat_calc.statistics([vec1])).all())
        self.assertTrue((self.stat_calc_rescaling_2.statistics([vec2]) !=
                         self.stat_calc.statistics([vec2])).all())

        self.assertRaises(RuntimeError, self.stat_calc_rescaling.statistics,
                          [vec2])

    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=True)
        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=True)
        self.assertTrue(
            (self.stat_calc.statistics(a) == np.array([[0, 2, 0, 4,
                                                        0]])).all())
        self.stat_calc = Identity(degree=2, cross=False)
        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=True)
        self.assertTrue(
            (self.stat_calc.statistics(a) == np.array([[2, 4]])).all())

    def test_pipeline(self):
        vec1 = np.array([1, 2])
        self.stat_calc_pipeline.statistics([vec1])
Ejemplo n.º 7
0
                                my_data[:, 1:21][whichobs, :].reshape(4, 5).transpose())).flatten().reshape(1, -1)][0]
        alldata.append(XObserved[0])
        obsdata = [np.array(XObserved).reshape(1, -1)]
        # Define the indices used as features to remove timestamp etc. and values with NA
        AllIndices = list(np.arange(0, 25, 1))
        NANTimeIndices = list(np.argwhere(np.isnan(XObserved[0, :])).reshape(-1, )) + [0, 5, 10, 15, 20] + list(np.arange(0, 5, 1)) + [4, 9, 14, 19, 24]
        InformativeIndices = [item for item in AllIndices if item not in NANTimeIndices]
        infoindi.append(InformativeIndices)
        noAP, noNAP, SR_x = int(my_data[whichobs, 16]), int(my_data[whichobs, 11]), float(my_data[whichobs, 21])
        label.append(my_data[whichobs, 23])
    result = set(infoindi[0]).intersection(*infoindi[1:])
    print(result)
    alldata_cleaned = np.array(alldata)[:,list(result)]
    from abcpy.statistics import Identity
    stat = Identity(degree=3, cross=True)
    XChosen = stat.statistics([[alldata_cleaned[i,:]] for i in range(alldata_cleaned.shape[0])])
    print(XChosen.shape)
    from metric_learn import LMNN
    metric = LMNN(init='auto',k=6, min_iter=10000, max_iter=50000, convergence_tol=1e-6, learn_rate=1e-10, regularization=.5, n_components = 2)
    metric.fit(XChosen, label)
    L = metric.components_
    np.savez('Data/L_all_3_cross.npz', L=L)

    L = np.load('Data/L_all_3_cross.npz')['L']
    X_lmnn = XChosen.dot(L.T)
    print(X_lmnn.shape)
    import pylab as plt
    plt.figure()
    plt.plot(X_lmnn[:16,0], X_lmnn[:16,1], 'k*', label='Patients with COPD')
    plt.plot(X_lmnn[16:32,0], X_lmnn[16:32,1], 'r*', label='Patients having dialysis')
    plt.plot(X_lmnn[32:,0], X_lmnn[32:,1], 'b*', label ='Healthy volunteers')