Exemple #1
0
def fit_model():
    M = MCMC(disaster_model)
    M.sample(iter=10000, burn=1000, thin=10)
    print('switchpoint: ', M.trace('switchpoint')[:])
    print('hist: ', hist(M.trace('late_mean')[:]))
    # show()
    plot(M)
Exemple #2
0
def estimate_failures(samples, #samples from noisy labelers
                      n_samples=10000, #number of samples to run MCMC for
                      burn=None, #burn-in. Defaults to n_samples/2
                      thin=10, #thinning rate. Sample every k samples from markov chain 
                      alpha_p=1, beta_p=1, #beta parameters for true positive rate
                      alpha_e=1, beta_e=10 #beta parameters for noise rates
                      ):

  if burn is None:
    burn = n_samples / 2

  S,N = samples.shape
  p = Beta('p', alpha=alpha_p, beta=beta_p) #prior on true label
  l = Bernoulli('l', p=p, size=S)
  e_pos = Beta('e_pos', alpha_e, beta_e, size=N) # error rate if label = 1
  e_neg = Beta('e_neg', alpha_e, beta_e, size=N) # error rate if label = 0

  @deterministic(plot=False)
  def noise_rate(l=l, e_pos=e_pos, e_neg=e_neg):
    #probability that a noisy labeler puts a label 1
    return np.outer(l, 1-e_pos) + np.outer(1-l, e_neg)

  noisy_label = Bernoulli('noisy_label', p=noise_rate, size=samples.shape, value=samples, observed=True)
  variables = [l, e_pos, e_neg, p, noisy_label, noise_rate]
  model = MCMC(variables, verbose=3)
  model.sample(iter=n_samples, burn=burn, thin=thin)
  model.write_csv('out.csv', ['p', 'e_pos', 'e_neg'])
  p = np.median(model.trace('p')[:])
  e_pos = np.median(model.trace('e_pos')[:],0)
  e_neg = np.median(model.trace('e_neg')[:],0)
  return p, e_pos, e_neg
Exemple #3
0
    def compute(
            self,
            observation,
            prediction,
            observation_name='observation',
            prediction_name='prediction',
            mcmc_iter=110000,
            mcmc_burn=10000,
            effect_size_type='mode',  # 'mean'
            assume_normal=False,
            **kwargs):
        if not pymc:
            raise ImportError('Module best or pymc could not be loaded!')

        data_dict = {
            observation_name: observation,
            prediction_name: prediction
        }
        best_model = self.make_model(data_dict, assume_normal)
        M = MCMC(best_model)
        M.sample(iter=mcmc_iter, burn=mcmc_burn)

        group1_data = M.get_node(observation_name).value
        group2_data = M.get_node(prediction_name).value

        N1 = len(group1_data)
        N2 = len(group2_data)

        posterior_mean1 = M.trace('group1_mean')[:]
        posterior_mean2 = M.trace('group2_mean')[:]
        diff_means = posterior_mean1 - posterior_mean2

        posterior_std1 = M.trace('group1_std')[:]
        posterior_std2 = M.trace('group2_std')[:]

        pooled_var = ((N1 - 1) * posterior_std1**2 +
                      (N2 - 1) * posterior_std2**2) / (N1 + N2 - 2)

        self.effect_size = diff_means / np.sqrt(pooled_var)

        stats = best.calculate_sample_statistics(self.effect_size)

        self.score = best_effect_size(stats[effect_size_type])
        self.score.mcmc_iter = mcmc_iter
        self.score.mcmc_burn = mcmc_burn
        self.score.data_size = [N1, N2]
        self.score.HDI = (stats['hdi_min'], stats['hdi_max'])
        self.HDI = self.score.HDI
        return self.score
def analizeMwm():
	masked_values = np.ma.masked_equal(x, value=None)
	print("m v: ", masked_values)

	print("dmwm da: ", dmwm.disasters_array)

	Mwm = MCMC(dmwm)
	Mwm.sample(iter=10000, burn=1000, thin=10)

	print("Mwm t: ", Mwm.trace('switchpoint')[:])

	hist(Mwm.trace('late_mean')[:])
	# show()

	plot(Mwm)
Exemple #5
0
    def test_simple(self):

        # Priors
        mu = Normal('mu', mu=0, tau=0.0001)
        s = Uniform('s', lower=0, upper=100, value=10)
        tau = s**-2

        # Likelihood with missing data
        x = Normal('x', mu=mu, tau=tau, value=m, observed=True)

        # Instantiate sampler
        M = MCMC([mu, s, tau, x])

        # Run sampler
        M.sample(10000, 5000, progress_bar=0)

        # Check length of value
        assert_equal(len(x.value), 100)
        # Check size of trace
        tr = M.trace('x')()
        assert_equal(shape(tr), (5000, 2))

        sd2 = [-2 < i < 2 for i in ravel(tr)]

        # Check for standard normal output
        assert_almost_equal(sum(sd2) / 10000., 0.95, decimal=1)
Exemple #6
0
 def test_nd(self):
     M = MCMC([self.NDstoch()], db=self.name, dbname=os.path.join(testdir, 'ND.'+self.name), dbmode='w')
     M.sample(10, progress_bar=0)
     a = M.trace('nd')[:]
     assert_equal(a.shape, (10,2,2))
     db = getattr(pymc.database, self.name).load(os.path.join(testdir, 'ND.'+self.name))
     assert_equal(db.trace('nd')[:], a)
    def test_simple(self):

        # Priors
        mu = Normal('mu', mu=0, tau=0.0001)
        s = Uniform('s', lower=0, upper=100, value=10)
        tau = s ** -2

        # Likelihood with missing data
        x = Normal('x', mu=mu, tau=tau, value=m, observed=True)

        # Instantiate sampler
        M = MCMC([mu, s, tau, x])

        # Run sampler
        M.sample(10000, 5000, progress_bar=0)

        # Check length of value
        assert_equal(len(x.value), 100)
        # Check size of trace
        tr = M.trace('x')()
        assert_equal(shape(tr), (5000, 2))

        sd2 = [-2 < i < 2 for i in ravel(tr)]

        # Check for standard normal output
        assert_almost_equal(sum(sd2) / 10000., 0.95, decimal=1)
Exemple #8
0
    def compute(
            self,
            observation,
            prediction,
            observation_name='observation',
            prediction_name='prediction',
            mcmc_iter=110000,
            mcmc_burn=10000,
            effect_size_type='mode',  # 'mean'
            **kwargs):
        self.mcmc_iter = mcmc_iter
        self.mcmc_burn = mcmc_burn
        data_dict = {
            observation_name: observation,
            prediction_name: prediction
        }
        best_model = self.make_model(data_dict)
        M = MCMC(best_model)
        M.sample(iter=mcmc_iter, burn=mcmc_burn)

        group1_data = M.get_node(observation_name).value
        group2_data = M.get_node(prediction_name).value

        N1 = len(group1_data)
        N2 = len(group2_data)
        self.data_size = [N1, N2]

        posterior_mean1 = M.trace('group1_mean')[:]
        posterior_mean2 = M.trace('group2_mean')[:]
        diff_means = posterior_mean1 - posterior_mean2

        posterior_std1 = M.trace('group1_std')[:]
        posterior_std2 = M.trace('group2_std')[:]

        pooled_var = ((N1 - 1) * posterior_std1**2 +
                      (N2 - 1) * posterior_std2**2) / (N1 + N2 - 2)

        self.effect_size = diff_means / np.sqrt(pooled_var)

        stats = best.calculate_sample_statistics(self.effect_size)

        self.HDI = (stats['hdi_min'], stats['hdi_max'])

        self.score = best_effect_size(stats[effect_size_type])

        return self.score
Exemple #9
0
def bimodal_gauss(data,pm,dmin=0.3):
    '''run MCMC to get regression on bimodal normal distribution'''
    size = len(data[pm])

    

    ### set up model
    p = Uniform( "p", 0.2 , 0.8) #this is the fraction that come from mean1 vs mean2
    # p = distributions.truncated_normal_like('p', mu=0.5, tau=0.001, a=0., b=1.)
    # p = Normal( 'p', mu=(1.*sum(comp0==1))/size, tau=1./0.1**2 ) # attention: wings!, tau = 1/sig^2
    # p = Normal( 'p', mu=0.5, tau=1./0.1**2 ) # attention: wings!, tau = 1/sig^2
    
    ber = Bernoulli( "ber", p = p, size = size) # produces 1 with proportion p
    precision = Gamma('precision', alpha=0.01, beta=0.01)
    
    mean1 = Uniform( "mean1", -0.5, 1.0) # if not truncated
    sig1  = Uniform( 'sig1',  0.01, 1.)
    mean2 = Uniform( "mean2", mean1 + dmin, 1.5)
    sig2  = Uniform( 'sig2',  0.01, 1.)

    pop1  = Normal( 'pop1', mean1, 1./sig1**2) # tau is 1/sig^2
    pop2  = Normal( 'pop2', mean2, 1./sig2**2)


    @deterministic
    def bimod(ber = ber, pop1 = pop1, pop2 = pop2): # value determined from parents completely
        return ber*pop1 + (1-ber)*pop2

    obs = Normal( "obs", bimod, precision, value = data[pm], observed = True)
    model = Model( {"p":p, "precision": precision, "mean1": mean1, 'sig1': sig1, "mean2":mean2, 'sig2':sig2, "obs":obs} )
    
    from pymc import MCMC, Matplot


    M = MCMC(locals(), db='pickle', dbname='metals.pickle')
    iter = 10000; burn = 9000; thin = 10
    M.sample(iter=iter, burn=burn, thin=thin)
    M.db.commit()

    mu1 = np.mean(M.trace('mean1')[:])
    sig1= np.mean(M.trace('sig1')[:])
    mu2 = np.mean(M.trace('mean2')[:])
    sig2= np.mean(M.trace('sig2')[:])
    p   = np.mean(M.trace('p')[:])
    return p, mu1, sig1, mu2, sig2, M
Exemple #10
0
def analizeM():
	M = MCMC(dm)
	print("M: ", M)

	M.sample(iter=10000, burn=1000, thin=10)
	print("M t: ", M.trace('switchpoint')[:])

	hist(M.trace('late_mean')[:])
	# show()

	plot(M)
	# show()

	print("M smd dm sp: ", M.step_method_dict[dm.switchpoint])
	print("M smd dm em: ", M.step_method_dict[dm.early_mean])
	print("M smd dm lm: ", M.step_method_dict[dm.late_mean])

	M.use_step_method(Metropolis, dm.late_mean, proposal_sd=2.)
Exemple #11
0
def bimodal_gauss(data,pm):
    '''run MCMC to get regression on bimodal normal distribution'''
    m1 = np.mean(data[pm])/2.
    m2 = np.mean(data[pm])*2.
    dm = m2 - m1
    size = len(data[pm])

    ### set up model
    p = Uniform( "p", 0.2 , 0.8) #this is the fraction that come from mean1 vs mean2
    # p = distributions.truncated_normal_like('p', mu=0.5, tau=0.001, a=0., b=1.)
    # p = Normal( 'p', mu=(1.*sum(comp0==1))/size, tau=1./0.1**2 ) # attention: wings!, tau = 1/sig^2
    # p = Normal( 'p', mu=0.5, tau=1./0.1**2 ) # attention: wings!, tau = 1/sig^2
    
    ber = Bernoulli( "ber", p = p, size = size) # produces 1 with proportion p
    precision = Gamma('precision', alpha=0.01, beta=0.01)
    
    dmu = Normal( 'dmu', dm, tau=1./0.05**2 ) # [PS] give difference between means, finite
    # dmu = Lognormal( 'dmu', 0.3, tau=1./0.1**2)
    
    mean1 = Normal( "mean1", mu = m1,          tau = 1./0.1**2 ) # better to use Normals versus Uniforms,
                                                                 # if not truncated
    mean2 = Normal( "mean2", mu = mean1 + dmu, tau = 1./0.1**2 ) # tau is 1/sig^2
    
    @deterministic
    def mean( ber = ber, mean1 = mean1, mean2 = mean2):
        return ber*mean1 + (1-ber)*mean2

    
    obs = Normal( "obs", mean, precision, value = data[pm], observed = True)
    model = Model( {"p":p, "precision": precision, "mean1": mean1, "mean2":mean2, "obs":obs} )
    
    from pymc import MCMC, Matplot



    M = MCMC(locals(), db='pickle', dbname='metals.pickle')
    iter = 3000; burn = 2000; thin = 10
    M.sample(iter=iter, burn=burn, thin=thin)
    M.db.commit()

    mu1 = np.mean(M.trace('mean1')[:])
    mu2 = np.mean(M.trace('mean2')[:])
    p   = np.mean(M.trace('p')[:])
    return p, mu1, 0.1, mu2, 0.1, M
Exemple #12
0
 def test_nd(self):
     M = MCMC([self.NDstoch()],
              db=self.name,
              dbname=os.path.join(testdir, 'ND.' + self.name),
              dbmode='w')
     M.sample(10, progress_bar=0)
     a = M.trace('nd')[:]
     assert_equal(a.shape, (10, 2, 2))
     db = getattr(pymc.database,
                  self.name).load(os.path.join(testdir, 'ND.' + self.name))
     assert_equal(db.trace('nd')[:], a)
Exemple #13
0
 def test_zcompression(self):
     with warnings.catch_warnings():
         warnings.simplefilter('ignore')
         db = pymc.database.hdf5.Database(dbname=os.path.join(testdir, 'disaster_modelCompressed.hdf5'),
                                          dbmode='w',
                                          dbcomplevel=5)                                 
         S = MCMC(disaster_model, db=db)
         S.sample(45,10,1, progress_bar=0)
         assert_array_equal(S.trace('early_mean')[:].shape, (35,))
         S.db.close()
         db.close()
         del S
Exemple #14
0
def estimate_failures_from_counts(counts, #samples from noisy labelers
                      n_samples=10000, #number of samples to run MCMC for
                      burn=None, #burn-in. Defaults to n_samples/2
                      thin=10, #thinning rate. Sample every k samples from markov chain 
                      alpha_p=1, beta_p=1, #beta parameters for true positive rate
                      alpha_e=1, beta_e=10 #beta parameters for noise rates
                      ):

  if burn is None:
    burn = n_samples / 2

  S = counts.sum()
  N = len(counts.shape)

  p_label = Beta('p_label', alpha=alpha_p, beta=beta_p) #prior on true label
  e_pos = Beta('e_pos', alpha_e, beta_e, size=N) # error rate if label = 1
  e_neg = Beta('e_neg', alpha_e, beta_e, size=N) # error rate if label = 0

  print counts
  @deterministic(plot=False)
  def patterns(p_label=p_label, e_pos=e_pos, e_neg=e_neg):
    #probability that the noisy labelers output pattern p
    P = np.zeros((2,)*N)
    for pat in itertools.product([0,1], repeat=N):
      P[pat] = p_label*np.product([1-e_pos[i] if pat[i]==1 else e_pos[i] for i in xrange(N)])
      P[pat] += (1-p_label)*np.product([e_neg[i] if pat[i]==1 else 1-e_neg[i] for i in xrange(N)])
    assert np.abs(P.sum() - 1) < 1e-6
    return P.ravel()
    
  pattern_counts = Multinomial('pattern_counts',n=S, p=patterns, value=counts.ravel(), observed=True)
  variables = [p_label, e_pos, e_neg, patterns]
  model = MCMC(variables, verbose=3)
  model.sample(iter=n_samples, burn=burn, thin=thin)
  model.write_csv('out.csv', ['p_label', 'e_pos', 'e_neg'])
  p = np.median(model.trace('p_label')[:])
  e_pos = np.median(model.trace('e_pos')[:],0)
  e_neg = np.median(model.trace('e_neg')[:],0)
  return p, e_pos, e_neg
Exemple #15
0
def differenceOfmeans(humanMean=4.5, sampleSize=50, variance=0.2):
    #note that tau is not sigma
    #sigma^2=1/tau
    t = 1 / variance
    #what is the probability that an analyst would give this image the same rating?
    mu = TruncatedNormal('mu', mu=humanMean, tau=t, a=1,
                         b=10)  #hypothetical ground truth
    botOutput = TruncatedNormal('botOutput', mu=mu, tau=t, a=1, b=10)
    humanOutput = TruncatedNormal('humanOutput', mu=mu, tau=t, a=1, b=10)
    #when we have data from the model we can use this here
    #like this d = pymc.Binomial(‘d’, n=n, p=theta, value=np.array([0.,1.,3.,5.]), observed=True)

    sim = MCMC([mu, botOutput, humanOutput])

    sim.sample(sampleSize, 0, 1)
    botOutput = sim.trace("botOutput")[:]
    #if humans only give ratings at the 0.5 interval, not smaller
    #        humanOutput = round_to_half(sim.trace("humanOutput")[:])
    humanOutput = sim.trace("humanOutput")[:]
    #difference of the means
    #but what we care about is the mean of the human output for each image.
    difference = botOutput - humanOutput.mean()
    return difference
Exemple #16
0
def imputeBayesian(row, dist):
    out = sys.stdout  #Save the stdout path for later, we're going to need it
    f = open('/dev/null', 'w')  #were going to use this to redirect stdout

    # filling nan with 0 so everything works
    row.fillna(0, inplace=True)

    # Masked Values
    maskedValues = np.ma.masked_equal(row.values, value=0)

    # Choose between distributions, either normal or Poisson.
    if dist == "Normal":

        # Calculate tau
        if np.std(maskedValues) == 0:
            tau = np.square(1 / (np.mean(maskedValues) / 3))
        else:
            tau = np.square((1 / (np.std(maskedValues))))

        # Uses only mean
        x = Impute('x',
                   Normal,
                   maskedValues,
                   tau=tau,
                   mu=np.mean(maskedValues))

    # For Poisson
    elif dist == "Poisson":
        x = Impute('x', Poisson, maskedValues, mu=np.mean(maskedValues))

    # Fancy test
    sys.stdout = f  # Skipin stdout
    m = MCMC(x)
    m.sample(iter=1, burn=0, thin=1)
    sys.stdout = out  # coming back

    # Getting list of missing values
    missing = [i for i in range(len(row.values)) if row.values[i] == 0]

    # Getting the imputed values from the model
    for i in range(len(missing)):
        keyString = "x[" + str(missing[i]) + "]"
        imputedValue = m.trace(keyString)[:]
        row.iloc[missing[i]] = imputedValue[0]

    # Returning to use nans
    row.replace(0, np.nan, inplace=True)
    return row
Exemple #17
0
    def test_zcompression(self):

        original_filters = warnings.filters[:]
        warnings.simplefilter("ignore")
        try:
            db = pymc.database.hdf5.Database(dbname=os.path.join(testdir, 'disaster_modelCompressed.hdf5'),
                                             dbmode='w',
                                             dbcomplevel=5)
            S = MCMC(disaster_model, db=db)
            S.sample(45,10,1, progress_bar=0)
            assert_array_equal(S.trace('early_mean')[:].shape, (35,))
            S.db.close()
            db.close()
            del S
        finally:
            warnings.filters = original_filters
Exemple #18
0
    def test_zcompression(self):

        original_filters = warnings.filters[:]
        warnings.simplefilter("ignore")
        try:
            db = pymc.database.hdf5.Database(dbname=os.path.join(
                testdir, 'disaster_modelCompressed.hdf5'),
                                             dbmode='w',
                                             dbcomplevel=5)
            S = MCMC(disaster_model, db=db)
            S.sample(45, 10, 1, progress_bar=0)
            assert_array_equal(S.trace('early_mean')[:].shape, (35, ))
            S.db.close()
            db.close()
            del S
        finally:
            warnings.filters = original_filters
Exemple #19
0
def fit_std_curve_by_pymc(i_vals, i_sds, dpx_concs):
    import pymc
    from pymc import Uniform, stochastic, deterministic, MCMC
    from pymc import Matplot
    # Define prior distributions for both Ka and Kd
    ka = Uniform('ka', lower=0, upper=1000)
    kd = Uniform('kd', lower=0, upper=1000)

    @stochastic(plot=True, observed=True)
    def quenching_model(ka=ka, kd=kd, value=i_vals):
        pred_i = quenching_func(ka, kd, dpx_concs)
        # The first concentration in dpx_concs should always be zero
        # (that is, the first point in the titration should be the
        # unquenched fluorescence), so we assert that here:
        assert dpx_concs[0] == 0
        # The reason this is necessary is that in the likelihood calculation
        # we skip the error for the first point, since (when the std. err
        # is calculated by well) the error is 0 (the I / I_0 ratio is
        # always 1 for each well, the the variance/SD across the wells is 0).
        # If we don't skip this first point, we get nan for the likelihood.
        # In addition, the model always predicts 1 for the I / I_0 ratio
        # when the DPX concentration is 0, so it contributes nothing to
        # the overall fit.
        return -np.sum((value[1:] - pred_i[1:])**2 / (2 * i_sds[1:]**2))

    pymc_model = pymc.Model([ka, kd, quenching_model])
    mcmc = MCMC(pymc_model)
    mcmc.sample(iter=155000, burn=5000, thin=150)
    Matplot.plot(mcmc)

    plt.figure()
    num_to_plot = 1000
    ka_vals = mcmc.trace('ka')[:]
    kd_vals = mcmc.trace('kd')[:]
    if num_to_plot > len(ka_vals):
        num_to_plot = len(ka_vals)
    for i in range(num_to_plot):
        plt.plot(dpx_concs, quenching_func(ka_vals[i], kd_vals[i], dpx_concs),
                 alpha=0.01, color='r')
    plt.errorbar(dpx_concs, i_vals, yerr=i_sds, linestyle='', marker='o',
            color='k', linewidth=2)

    return (ka_vals, kd_vals)
Exemple #20
0
def main():
    s1 = PoissonStudent("arnaud", 1)
    s2 = PoissonStudent("francois", 1)
    s3 = PoissonStudent("david", 0.5)

    students = [s1, s2, s3]
    env = Environment(students)
    statements = env.simulate(1000, verbose=True)

    student_names = set(s['actor'] for s in statements)
    lam = Uniform('lam', lower=0, upper=1)
    students = [PoissonStudent(name=name, lam=lam) for name in student_names]
    env = Environment(students, statements)
    params = [lam]
    for s in students:
        params.extend(s.params)
    m = MCMC(params)
    m.sample(iter=10000, burn=1000, thin=10)
    hist(m.trace('lambda_david')[:])
    show()
Exemple #21
0
 def Outliers_Krough(self):
     
     fit_dict                = OrderedDict()
     
     fit_dict['methodology'] = r'Outliers Krough'
     
     #Initial Guess for fitting
     Bces_guess              = self.bces_regression()
     m_0, n_0                = Bces_guess['m'][0], Bces_guess['n'][0]
             
     Spread_vector           = ones(len(self.x_array))
     
     #Model for outliers detection
     Outliers_dect_dict      = self.inference_outliers(self.x_array, self.y_array, m_0, n_0, Spread_vector)
     
     mcmc = MCMC(Outliers_dect_dict)
     mcmc.sample(100000, 20000)
     
     #Extract the data with the outliers coordinates
     probability_of_points           = mcmc.trace('inlier')[:].astype(float).mean(0)
     fit_dict['x_coords_outliers']   = self.x_array[probability_of_points < self.prob_threshold]
     fit_dict['y_coords_outliers']   = self.y_array[probability_of_points < self.prob_threshold]
             
     return fit_dict
    def detect(
        self,
        filtered_sig,
        fs,
        step=2,
        iter_count=4000,
        burn_count=2000,
    ):
        '''Detect and returns P wave detection results.
            Note:
                This function returns None when detection fails.
        '''
        # r_list = self.r_list
        raw_sig = filtered_sig
        sig_seg = raw_sig
        max_hermit_level = 8

        p_model = P_model_Gaussian.MakeModel(sig_seg,
                                             max_hermit_level=max_hermit_level)
        M = MCMC(p_model)

        M.sample(iter=iter_count, burn=burn_count, thin=10)

        # retrieve parameters
        hermit_coefs = list()
        for h_ind in xrange(0, P_model_Gaussian.HermitFunction_max_level):
            hermit_value = np.mean(M.trace('hc%d' % h_ind)[:])
            hermit_coefs.append(hermit_value)

        fitting_curve = np.zeros(len(sig_seg), )
        for level, coef in zip(xrange(0, max_hermit_level), hermit_coefs):
            fitting_curve += P_model_Gaussian.HermitFunction(
                level, len(sig_seg)) * coef

        plt.figure(1)
        plt.clf()
        plt.plot(sig_seg, label='ECG')
        plt.plot(fitting_curve, label='fitting curve')

        # Hermit coef vis
        plt.bar(xrange(0, len(hermit_coefs)), [
            0.12,
        ] * len(hermit_coefs),
                width=0.5,
                alpha=0.3,
                color='grey')
        plt.bar(xrange(0, len(hermit_coefs)), [
            -0.12,
        ] * len(hermit_coefs),
                width=0.5,
                alpha=0.3,
                color='grey')
        plt.bar(xrange(0, len(hermit_coefs)),
                np.array(hermit_coefs) * 0.2,
                width=0.5,
                color='r')
        plt.legend()
        plt.grid(True)
        plt.show(block=False)
        pdb.set_trace()
        # plt.savefig('./results/tmp/%d.png' % int(time.time()))

        results = dict()

        return results
Exemple #23
0
    mcmc = MCMC(pymc_model)
    mcmc.sample(iter=10000, burn=5000, thin=5)

    # Show the pymc histograms and autocorrelation plots
    plt.ion()
    pymc.Matplot.plot(mcmc)
    plt.show()

    # Plot the original data along with the sampled trajectories
    plt.figure()
    plt.plot(tspan, ydata_norm[:, 0], 'r')
    plt.plot(tspan, ydata_norm[:, 1], 'g')
    plt.plot(tspan, ydata_norm[:, 2], 'b')

    num_timecourses = 1000
    num_iterations_sampled = mcmc.trace('robertson_model')[:].shape[0]

    plt.plot(tspan,
             mcmc.trace('robertson_model')[num_iterations_sampled -
                                           num_timecourses:, 0::3].T,
             alpha=0.05,
             color='r')
    plt.plot(tspan,
             mcmc.trace('robertson_model')[num_iterations_sampled -
                                           num_timecourses:, 1::3].T,
             alpha=0.05,
             color='g')
    plt.plot(tspan,
             mcmc.trace('robertson_model')[num_iterations_sampled -
                                           num_timecourses:, 2::3].T,
             alpha=0.05,
                      value=ydata,
                      plot=True)

if __name__ == '__main__':
    # Build a model
    # NOTE: Be careful to avoid namespace clashes with pysb.Model!
    pymc_model = pymc.Model([k, decay_model, timecourse])

    # Initialize an MCMC object from the model
    mcmc = MCMC(pymc_model)

    # Sample
    mcmc.sample(iter=10000, burn=5000)

    # Plot the posterior distribution of the parameter k
    plt.ion()
    Matplot.plot(mcmc)

    # Plot the original data (underlying and noisy)
    # along with the sampled trajectories
    plt.figure()
    plt.plot(tspan, ysim, color='r')
    plt.plot(tspan, ydata, color='g')
    num_timecourses = 1000
    num_iterations_sampled = mcmc.trace('decay_model')[:].shape[0]
    plt.plot(tspan,
             mcmc.trace('decay_model')[num_iterations_sampled -
                                       num_timecourses:, :].T,
             alpha=0.05,
             color='b')
Exemple #25
0
class bayes_plotter():
    
    def __init__(self):
        
        self.Fig                = None
        self.Axis               = None
        self.Valid_Traces       = None
        
        self.pymc_database      = None
        self.dbMCMC             = None

        self.Traces_filter      = None
        self.pymc_stats_keys    = ['mean', '95% HPD interval', 'standard deviation',  'mc error', 'quantiles', 'n']

    def load_pymc_database(self, Database_address):
        
        #In case the database is open from a previous use
        if self.pymc_database != None:
            self.pymc_database.close()
        
        #Load the pymc output textfile database
        self.pymc_database  = database.pickle.load(Database_address)
        
        #Create a dictionary with the bases to 
        self.Traces_dict = {}
        self.traces_list = self.pymc_database.trace_names[0] #This variable contains all the traces from the MCMC (stochastic and deterministic)
        
        for trace in self.traces_list:
            self.Traces_dict[trace] = self.pymc_database.trace(trace)
    
        #Generate a MCMC object to recover all the data from the run
        self.dbMCMC      = MCMC(self.Traces_dict, self.pymc_database)
        
        return

    def extract_traces_statistics(self, traces_list = None):
        
        #     traces_yplus = bp.pymc_database.trace('He_abud')[:]
        #     print 'The y_plus trace evolution\n'
        #     print 'Mean inf',      statistics_dict['He_abud']['mean']
        #     print 'Median numpy',  median(traces_yplus)
        #     print 'Mean numpy',    mean(traces_yplus), '\n'
        # 
        #     print 'percentiles: 25, 50, 75' 
        #     print percentile(traces_yplus,25), percentile(traces_yplus,50), percentile(traces_yplus,75),'\n'
        #     print 'percentiles: 16, 84' 
        #     print percentile(traces_yplus,16), percentile(traces_yplus,84),'\n'
        #     print 'percentiles: 37.73, 68.27' 
        #     print percentile(traces_yplus,37.73), percentile(traces_yplus,68.27),'\n'
        #     print 'Standard deviation'
        #     print percentile(traces_yplus,4.55), percentile(traces_yplus,95.45)
        #     print 'HUD 95', statistics_dict['He_abud']['95% HPD interval'],'\n'
        #     
        #     print 'PYMC std', statistics_dict['He_abud']['standard deviation']
        #     print 'Numpy std', std(traces_yplus), '\n'
        
        
        self.statistics_dict = OrderedDict()
                
        #If no list input we extract all the traces from the analysis        
        if traces_list == None:
            traces_list = self.traces_list
                
        for trace in traces_list:
            self.statistics_dict[trace] = OrderedDict()
            
            for stat in self.pymc_stats_keys:
                self.statistics_dict[trace][stat] = self.dbMCMC.trace(trace).stats()[stat]                
            
            Trace_array = self.pymc_database.trace(trace)[:] 
            self.statistics_dict[trace]['16th_p'] = percentile(Trace_array, 16)
            self.statistics_dict[trace]['84th_p'] = percentile(Trace_array, 84)
        
        return self.statistics_dict
        
    def close_database(self):
        
        self.pymc_database.close()
        self.pymc_database = None
        
        return

    def Import_FigConf(self, Fig = None, Axis = None):
        
        if Fig != None:
            self.Fig = Fig
            
        if Axis != None:
            self.Axis = Axis
    
    def FigConf(self, Figtype = 'Single', FigWidth = 16, FigHeight = 9, AxisFormat = 111, fontsize = 8, PlotStyle = 'Night', n_columns = None, n_rows = None, n_colors = None, color_map = 'colorblind'):
        
        self.Triangle_Saver = False
        
        if Figtype == 'Single':
            self.Fig                = plt.figure(figsize = (FigWidth, FigHeight))  
            self.Axis1              = self.Fig.add_subplot(AxisFormat)
            #fig.subplots_adjust(hspace = .5, wspace=.001) 
        
        elif Figtype == 'Posteriors':
            self.Fig                = plt.figure(figsize = (FigWidth, FigHeight))
            AxisFormat              = int(str(n_colors)  + '11')
            self.Axis1              = self.Fig.add_subplot(AxisFormat)
            
        elif Figtype == 'Grid':
            self.Fig, self.Axis1    = plt.subplots(n_rows, n_columns, figsize=(FigWidth, FigHeight))  
            self.Axis1              = self.Axis1.ravel()
        
        elif Figtype == 'triangle':
            self.Triangle_Saver = True
            return
        
        rc('legend',    fontsize=fontsize + 3,      handlelength=3, frameon = True)
        rc('axes',      titlesize=fontsize+5)
        rc('axes',      labelsize=fontsize+5)
        rc('xtick',     labelsize=fontsize+2)
        rc('ytick',     labelsize=fontsize+2)
        rc('text',      usetex=True)
        rc('font',      size=fontsize, style = 'normal', variant='normal', stretch='normal', weight='normal')
   
#         params = {'backend': 'wxAgg', 'lines.markersize' : 2, 'axes.labelsize': fontlabel_size, 'text.fontsize': fontlabel_size, 'legend.fontsize': fontlabel_size, 'xtick.labelsize': tick_size, 'ytick.labelsize': tick_size, 'text.usetex': True, 'figure.figsize': fig_size}
#         plt.rcParams.update(params)
   
        self.define_ColorVector(PlotStyle, n_colors, color_map)
   
        return
   
    def legend_conf(self, Axis, loc = 'best', fontize = None, edgelabel = False):
        #WARNING: THIS DOES NOT WORK WITH LEGEND RAVELIN
   
        if self.Axis1.get_legend_handles_labels()[1] != None:
            Old_Handles, Old_Labels = Axis.get_legend_handles_labels()
            Handles_by_Label = OrderedDict(zip(Old_Labels, Old_Handles))
            
            Hl          = zip(Handles_by_Label.values(), Handles_by_Label.keys())

            New_Handles, New_labels = zip(*Hl)        

            myLegend    = Axis.legend(New_Handles, New_labels, loc=loc, prop={'size':12}, scatterpoints=1, numpoints=1)

            if fontize != None:

                Leg_Frame = myLegend.get_frame()
                Leg_Frame.set_facecolor(self.Color_Vector[0])
                Leg_Frame.set_edgecolor(self.Color_Vector[1])
    
                for label in myLegend.get_texts():
                    label.set_fontsize('large')
             
                for label in myLegend.get_lines():
                    label.set_linewidth(1.5)
                 
                for text in myLegend.get_texts():
                    text.set_color(self.Color_Vector[1])   
            
            if edgelabel:
                Leg_Frame = myLegend.get_frame()
                Leg_Frame.set_edgecolor('black')

        return
   
    def define_ColorVector(self, PlotStyle, n_colors, color_map):
   
        self.ColorVector = [None, None, None]
        
        self.ColorVector[0]        = 'w'
        self.ColorVector[1]        = 'b'
        
        if n_colors == None:
            self.ColorVector[2]    = cycle(sb.color_palette(color_map))
        else:
            self.ColorVector[2]    = sb.color_palette(color_map, n_colors)
        
    def Compute_SigmaLevel(self, trace1, trace2, nbins=20):
        
        # From a set of traces, bin by number of standard deviations
        L, xbins, ybins = histogram2d(trace1, trace2, nbins)
        L[L == 0]       = 1E-16
        logL            = nplog(L)
    
        shape           = L.shape
        L               = L.ravel()
    
        #Obtain the indices to sort and unsort the flattened array
        i_sort          = argsort(L)[::-1]
        i_unsort        = argsort(i_sort)
    
        L_cumsum        = L[i_sort].cumsum()
        L_cumsum        /= L_cumsum[-1]
        
        xbins           = 0.5 * (xbins[1:] + xbins[:-1])
        ybins           = 0.5 * (ybins[1:] + ybins[:-1])
    
        return xbins, ybins, L_cumsum[i_unsort].reshape(shape)
    
    def plot_SigmaLevels_MCMC(self, xdata, ydata, trace_x, trace_y, plot_Scatter = True, **kwargs):
        
        #"""Plot traces and contours"""
        xbins, ybins, sigma = self.Compute_SigmaLevel(trace_x, trace_y)
        self.Axis.contour(xbins, ybins, sigma.T, levels=[0.683, 0.955], **kwargs)
        
        if plot_Scatter:
            self.Axis.plot(trace_x, trace_y, ',k', alpha=0.1)

    def plot_posteriors_histagram(self, Traces, labels):
        
        n_traces = len(Traces)
        
        self.FigConf(Figtype = 'Posteriors', n_colors = n_traces)
        
        for i in range(len(Traces)):
            Trace           = Traces[i]
            Axis_Location   = int(str(n_traces) + '1' +  str(i + 1))
            Axis            = plt.subplot(Axis_Location) 
            HDP_coords      = self.statistics_dict[Trace]['95% HPD interval']
            
            for HDP in HDP_coords:
                Axis.axvline(x = HDP, label = 'HPD interval: ' + round_sig(HDP_coords[0], 4) + ' - ' + round_sig(HDP_coords[1], 4), color='grey', linestyle = 'dashed')
                                                
            Axis.axvline(x = self.statistics_dict[Trace]['mean'], label = 'Mean value: ' + round_sig(self.statistics_dict[Trace]['mean'], 4), color='grey', linestyle = 'solid')
            Axis.hist(self.Traces_dict[Trace][:], histtype='stepfilled', bins=35, alpha=.7, color=self.ColorVector[2][i], normed=False)
            Axis.set_ylabel(labels[i],fontsize=20)
                        
            self.legend_conf(Axis, loc='best', edgelabel=True)

#             Axis.yaxis.set_ticks(arange(0, 250, 50))

    def plot_Ownposteriors_histagram(self, Traces, labels, sigfig_n = 4, xlim = None, ylim = None):
        
        n_traces = len(Traces)
        
        self.FigConf(Figtype = 'Posteriors', n_colors = n_traces)
        
        for i in range(len(Traces)):
            Trace           = Traces[i]
            Axis_Location   = int(str(n_traces) + '1' +  str(i + 1))
            Axis            = plt.subplot(Axis_Location) 
            HDP_coords      = [percentile(Trace, 16), percentile(Trace, 84)]
            mean_value      = median(Trace)
            
            for HDP in HDP_coords:
                Axis.axvline(x = HDP, label = 'Percentiles: 16th-84th: ' + round_sig(HDP_coords[0], n=sigfig_n, scien_notation=False) + ' - ' + round_sig(HDP_coords[1], n=sigfig_n, scien_notation=False), color='grey', linestyle = 'dashed')
            
            Median_text_legend = r'Median value: $' + round_sig(mean_value, n=sigfig_n, scien_notation=False) + '_{-' + round_sig(mean_value - HDP_coords[0], n=sigfig_n, scien_notation=False) + '}^{+' + round_sig(HDP_coords[1] - mean_value, n=sigfig_n, scien_notation=False) + '}$'                               
            Axis.axvline(x = mean_value, label = Median_text_legend, color='grey', linestyle = 'solid')
            Axis.hist(Trace, histtype='stepfilled', bins=35, alpha=.7, color=self.ColorVector[2][i], normed=False)
            Axis.set_ylabel(labels[i],fontsize=20)
                        
            self.legend_conf(Axis, loc='best', edgelabel=True)

#             Axis.yaxis.set_ticks(arange(0, 250, 50))
            
            if xlim != None:
                Axis.set_xlim(xlim[0], xlim[1])
                
            if ylim != None:
                Axis.set_ylim(ylim[0], ylim[1])

              
        return
    
    def plot_tracers(self, Traces, labels):
        
        n_traces = len(Traces)
        
        self.FigConf(Figtype = 'Posteriors', n_colors = n_traces) 
        
        for i in range(len(Traces)):
            Trace           = Traces[i]
            Axis_Location   = int(str(n_traces) + '1' +  str(i + 1))
            Axis            = plt.subplot(Axis_Location)         
            Variable_value  = self.statistics_dict[Trace]['mean']
            Variable_error  = self.statistics_dict[Trace]['standard deviation']
            label = labels[i] + ': ' + round_sig(Variable_value, 4) + r'$\pm$' + round_sig(Variable_error,4)            
            Axis.plot(self.pymc_database.trace(Trace)[:], label = label, color=self.ColorVector[2][i])
            Axis.axhline(y = self.statistics_dict[Trace]['mean'],  color=self.ColorVector[2][i], linestyle = '--' )
            Axis.set_ylabel(labels[i],fontsize=20)
            self.legend_conf(Axis, loc=1, edgelabel=True)
            
            plt.locator_params(axis = 'y', nbins = 5)
           
    def plot_acorrelation(self, Traces, labels, n_columns=4, n_rows=2):
        
        n_traces = len(Traces)
        
        self.FigConf(Figtype = 'Grid', n_colors = n_traces, n_columns=n_columns, n_rows=n_rows, FigHeight=9, FigWidth=16)         
        
        for i in range(len(Traces)):
            Trace   = Traces[i]                
            maxlags = min(len(self.pymc_database.trace(Trace)[:]) - 1, 100)
            label   = labels[i]
            if Trace != 'ChiSq':
                self.Axis1[i].acorr(x = self.pymc_database.trace(Trace)[:], color=self.ColorVector[2][i], detrend= detrend_mean, maxlags=maxlags)
            else:
                #Apano momentaneo
                chisq_adapted = reshape(self.pymc_database.trace(Trace)[:], len(self.pymc_database.trace(Trace)[:]))
                maxlags = min(len(chisq_adapted) - 1, 100)
                self.Axis1[i].acorr(x = chisq_adapted, color=self.ColorVector[2][i], detrend= detrend_mean, maxlags=maxlags)
            self.Axis1[i].set_xlim(0, maxlags)
            self.Axis1[i].set_title(label)
                
        return

    def plot_chiSq_Behaviour(self, Traces,  labels):
    
        n_traces = len(Traces)
        
        self.FigConf(Figtype = 'Grid', n_colors = n_traces, n_columns=4, n_rows=2, FigHeight=9, FigWidth=16)
        
        chisq_adapted   = reshape(self.pymc_database.trace('ChiSq')[:], len(self.pymc_database.trace('ChiSq')[:])) * -2
        y_lim           = 30
        min_chi_index   = argmin(chisq_adapted)
        
        for i in range(len(Traces)):
            Trace   = Traces[i]
            label   = labels[i]       

            if Trace != 'ChiSq':
                self.Axis1[i].scatter(x = self.pymc_database.trace(Trace)[:], y = chisq_adapted, color=self.ColorVector[2][i])
                x_min           = npmin(self.pymc_database.trace(Trace)[:]) 
                x_max           = npmax(self.pymc_database.trace(Trace)[:])

                self.Axis1[i].axvline(x = self.statistics_dict[Trace]['mean'], label = 'Inference value: ' + round_sig(self.statistics_dict[Trace]['mean'], 4,scien_notation=False), color='grey', linestyle = 'solid')
                self.Axis1[i].scatter(self.pymc_database.trace(Trace)[:][min_chi_index], chisq_adapted[min_chi_index], color='Black', label = r'$\chi^{2}_{min}$ value: ' + round_sig(self.pymc_database.trace(Trace)[:][min_chi_index],4,scien_notation=False))
                
                self.Axis1[i].set_ylabel(r'$\chi^{2}$',fontsize=20)
                self.Axis1[i].set_ylim(0, y_lim)
                self.Axis1[i].set_xlim(x_min, x_max)
                self.Axis1[i].set_title(label,fontsize=20)
                legend_i = self.Axis1[i].legend(loc='best', fontsize='x-large')
                legend_i.get_frame().set_facecolor('white')

    def plot_triangle_histContours(self, Traces, labels, true_values = None):
        
        n_traces = len(Traces)

        self.FigConf(Figtype = 'triangle')
        
        #Make plots
        List_Arrays = []
        for i in range(n_traces):
            List_Arrays.append(self.pymc_database.trace(Traces[i])[:])
        
        Samples     = array(List_Arrays).T
                    
        #Make 
        if true_values != None:
            self.figure = corner.corner(Samples[:,:], labels=labels, quantiles=[0.16, 0.5, 0.84], show_titles=True, title_args={"fontsize": 100}, truths = true_values, title_fmt='0.3f')
        else:
            self.figure = corner.corner(Samples[:,:], labels=labels, quantiles=[0.16, 0.5, 0.84], show_titles=True, title_args={"fontsize": 100}, title_fmt='0.3f')
                     
    def savefig(self, output_address, extension = '.png', reset_fig = True):
    
        if self.Triangle_Saver:
            self.figure.savefig(output_address + extension, dpi=500, bbox_inches='tight', pad_inches=0.2)
            
        else:
            plt.tight_layout()
            plt.savefig(output_address + extension, dpi=500, facecolor=self.ColorVector[0], bbox_inches='tight', pad_inches=0.2)
        
        if reset_fig:
            self.reset_fig()
    
        return
    
    def reset_fig(self):
        
        plt.cla()
        
        return
    
    
    
    
# def GetLower_ChiSquare(MCMC_Database, variable, mean = None, min_value = None, max_value = None, nbins = 100):
#     
#     print 'Treating variable', variable
#     
#     
#     ChiArray        =  Database_MCMC.trace('ChiSq')[:] * -1
#     VariableArray   = Database_MCMC.trace(variable)[:]
#         
#     if mean == None:
#         mean = average(VariableArray)
# 
#     if min_value == None:
#         min_value = 0.8
# 
#     if max_value == None:
#         max_value = 1.2
#         
#     Bins                        = linspace(mean*min_value, mean*max_value, nbins)
#     Points_Index                = digitize(VariableArray, Bins)
#          
#     Unique_Bins                 = unique(Points_Index)
#     Min_Index, Max_Index        = min(Unique_Bins), max(Unique_Bins)
#      
#     Min_values_Chi              = zeros(len(Points_Index))
#     Min_values_Variable         = zeros(len(Points_Index))
#      
#     for i in range(Min_Index, Max_Index):
#         inBinIndex              = where(Points_Index == i)[0]
#  
#         if len(ChiArray[inBinIndex]) != 0:
#             Min_values_Chi[i]       = np_min(ChiArray[inBinIndex])
#             Min_values_Variable[i]  = Bins[i]
#  
#     return Min_values_Variable, Min_values_Chi, mean
Exemple #26
0
Ibetas = np.empty((Ni,int((iters-burns)/thins)))
Iig = np.empty((Ni,int((iters-burns)/thins)))
Iil = np.empty((Ni,int((iters-burns)/thins)))
Iia = np.empty((Ni,int((iters-burns)/thins)))
Iiw = np.empty((Ni,int((iters-burns)/thins)))
Iae = np.empty((Ni,int((iters-burns)/thins)))
for i in range(0, 1):

    print('-------------')
    print('Processing class ' + str(i+1) + ' out of ' + str(Ni))
    print('-------------')
    np.save('terribleHackClass.npy',np.array([i]))
    imp.reload(decayModelAlign)
    M = MCMC(decayModelAlign)
    M.sample(iter=iters, burn=burns, thin=thins,verbose=0)
    Irhos[i,:]=M.trace('rho_s')[:]
    Ialphas[i,:]=M.trace('alpha')[:]
    Ibetas[i,:]=M.trace('beta')[:]
    Iig[i,:]=M.trace('ignore_length')[:]
    Iil[i,:]=M.trace('attract_length')[:]
    Iia[i,:]=M.trace('attract_angle')[:]
    Iiw[i,:]=M.trace('align_weight')[:]
    Iae[i,:]=M.trace('attract_exponent')[:]
    
np.save('Irhos0.npy',Irhos)
np.save('Ialphas0.npy',Ialphas)
np.save('Ibetas0.npy',Ibetas)
np.save('Iig0.npy',Iig)
np.save('Iil0.npy',Iil)
np.save('Iia0.npy',Iia)
np.save('Iiw0.npy',Iiw)
Exemple #27
0
# Define data and stochastics
switchpoint = DiscreteUniform(
    'switchpoint',
    lower=0,
    upper=110,
    doc='Switchpoint[year]')

early_mean = Exponential('early_mean', beta=1.)
late_mean = Exponential('late_mean', beta=1.)

@deterministic(plot=False)
def rate(s=switchpoint, e=early_mean, l=late_mean):
    ''' Concatenate Poisson means '''
    out = empty(len(disasters_array))
    out[:s] = e
    out[s:] = l
    return out

disasters = Poisson('disasters', mu=rate, value=disasters_array, observed=True)

# import disaster_model
from pymc import MCMC
# M = MCMC(disaster_model)
M = MCMC([switchpoint,early_mean,late_mean,rate,disasters])
M.sample(iter=10000, burn=1000, thin=10)
print switchpoint.value
print rate.value
print M.trace('switchpoint')[:]
# from pymc.Matplot import plot
# plot(M)
class bayes_plotter():
    def __init__(self):

        self.Fig = None
        self.Axis = None
        self.Valid_Traces = None

        self.pymc_database = None
        self.dbMCMC = None

        self.Traces_filter = None
        self.pymc_stats_keys = [
            'mean', '95% HPD interval', 'standard deviation', 'mc error',
            'quantiles', 'n'
        ]

    def load_pymc_database(self, Database_address):

        #In case the database is open from a previous use
        if self.pymc_database != None:
            self.pymc_database.close()

        #Load the pymc output textfile database
        self.pymc_database = database.pickle.load(Database_address)

        #Create a dictionary with the bases to
        self.Traces_dict = {}
        self.traces_list = self.pymc_database.trace_names[
            0]  #This variable contains all the traces from the MCMC (stochastic and deterministic)

        for trace in self.traces_list:
            self.Traces_dict[trace] = self.pymc_database.trace(trace)

        #Generate a MCMC object to recover all the data from the run
        self.dbMCMC = MCMC(self.Traces_dict, self.pymc_database)

        return

    def extract_traces_statistics(self, traces_list=None):

        #     traces_yplus = bp.pymc_database.trace('He_abud')[:]
        #     print 'The y_plus trace evolution\n'
        #     print 'Mean inf',      statistics_dict['He_abud']['mean']
        #     print 'Median numpy',  median(traces_yplus)
        #     print 'Mean numpy',    mean(traces_yplus), '\n'
        #
        #     print 'percentiles: 25, 50, 75'
        #     print percentile(traces_yplus,25), percentile(traces_yplus,50), percentile(traces_yplus,75),'\n'
        #     print 'percentiles: 16, 84'
        #     print percentile(traces_yplus,16), percentile(traces_yplus,84),'\n'
        #     print 'percentiles: 37.73, 68.27'
        #     print percentile(traces_yplus,37.73), percentile(traces_yplus,68.27),'\n'
        #     print 'Standard deviation'
        #     print percentile(traces_yplus,4.55), percentile(traces_yplus,95.45)
        #     print 'HUD 95', statistics_dict['He_abud']['95% HPD interval'],'\n'
        #
        #     print 'PYMC std', statistics_dict['He_abud']['standard deviation']
        #     print 'Numpy std', std(traces_yplus), '\n'

        self.statistics_dict = OrderedDict()

        #If no list input we extract all the traces from the analysis
        if traces_list == None:
            traces_list = self.traces_list

        for trace in traces_list:
            self.statistics_dict[trace] = OrderedDict()

            for stat in self.pymc_stats_keys:
                self.statistics_dict[trace][stat] = self.dbMCMC.trace(
                    trace).stats()[stat]

            Trace_array = self.pymc_database.trace(trace)[:]
            self.statistics_dict[trace]['16th_p'] = percentile(Trace_array, 16)
            self.statistics_dict[trace]['84th_p'] = percentile(Trace_array, 84)

        return self.statistics_dict

    def close_database(self):

        self.pymc_database.close()
        self.pymc_database = None

        return

    def Import_FigConf(self, Fig=None, Axis=None):

        if Fig != None:
            self.Fig = Fig

        if Axis != None:
            self.Axis = Axis

    def FigConf(self,
                Figtype='Single',
                FigWidth=16,
                FigHeight=9,
                AxisFormat=111,
                fontsize=8,
                PlotStyle='Night',
                n_columns=None,
                n_rows=None,
                n_colors=None,
                color_map='colorblind'):

        self.Triangle_Saver = False

        if Figtype == 'Single':
            self.Fig = plt.figure(figsize=(FigWidth, FigHeight))
            self.Axis1 = self.Fig.add_subplot(AxisFormat)
            #fig.subplots_adjust(hspace = .5, wspace=.001)

        elif Figtype == 'Posteriors':
            self.Fig = plt.figure(figsize=(FigWidth, FigHeight))
            AxisFormat = int(str(n_colors) + '11')
            self.Axis1 = self.Fig.add_subplot(AxisFormat)

        elif Figtype == 'Grid':
            self.Fig, self.Axis1 = plt.subplots(n_rows,
                                                n_columns,
                                                figsize=(FigWidth, FigHeight))
            self.Axis1 = self.Axis1.ravel()

        elif Figtype == 'triangle':
            self.Triangle_Saver = True
            return

        rc('legend', fontsize=fontsize + 3, handlelength=3, frameon=True)
        rc('axes', titlesize=fontsize + 5)
        rc('axes', labelsize=fontsize + 5)
        rc('xtick', labelsize=fontsize + 2)
        rc('ytick', labelsize=fontsize + 2)
        rc('text', usetex=True)
        rc('font',
           size=fontsize,
           style='normal',
           variant='normal',
           stretch='normal',
           weight='normal')

        #         params = {'backend': 'wxAgg', 'lines.markersize' : 2, 'axes.labelsize': fontlabel_size, 'text.fontsize': fontlabel_size, 'legend.fontsize': fontlabel_size, 'xtick.labelsize': tick_size, 'ytick.labelsize': tick_size, 'text.usetex': True, 'figure.figsize': fig_size}
        #         plt.rcParams.update(params)

        self.define_ColorVector(PlotStyle, n_colors, color_map)

        return

    def legend_conf(self, Axis, loc='best', fontize=None, edgelabel=False):
        #WARNING: THIS DOES NOT WORK WITH LEGEND RAVELIN

        if self.Axis1.get_legend_handles_labels()[1] != None:
            Old_Handles, Old_Labels = Axis.get_legend_handles_labels()
            Handles_by_Label = OrderedDict(zip(Old_Labels, Old_Handles))

            Hl = zip(Handles_by_Label.values(), Handles_by_Label.keys())

            New_Handles, New_labels = zip(*Hl)

            myLegend = Axis.legend(New_Handles,
                                   New_labels,
                                   loc=loc,
                                   prop={'size': 12},
                                   scatterpoints=1,
                                   numpoints=1)

            if fontize != None:

                Leg_Frame = myLegend.get_frame()
                Leg_Frame.set_facecolor(self.Color_Vector[0])
                Leg_Frame.set_edgecolor(self.Color_Vector[1])

                for label in myLegend.get_texts():
                    label.set_fontsize('large')

                for label in myLegend.get_lines():
                    label.set_linewidth(1.5)

                for text in myLegend.get_texts():
                    text.set_color(self.Color_Vector[1])

            if edgelabel:
                Leg_Frame = myLegend.get_frame()
                Leg_Frame.set_edgecolor('black')

        return

    def define_ColorVector(self, PlotStyle, n_colors, color_map):

        self.ColorVector = [None, None, None]

        self.ColorVector[0] = 'w'
        self.ColorVector[1] = 'b'

        if n_colors == None:
            self.ColorVector[2] = cycle(sb.color_palette(color_map))
        else:
            self.ColorVector[2] = sb.color_palette(color_map, n_colors)

    def Compute_SigmaLevel(self, trace1, trace2, nbins=20):

        # From a set of traces, bin by number of standard deviations
        L, xbins, ybins = histogram2d(trace1, trace2, nbins)
        L[L == 0] = 1E-16
        logL = nplog(L)

        shape = L.shape
        L = L.ravel()

        #Obtain the indices to sort and unsort the flattened array
        i_sort = argsort(L)[::-1]
        i_unsort = argsort(i_sort)

        L_cumsum = L[i_sort].cumsum()
        L_cumsum /= L_cumsum[-1]

        xbins = 0.5 * (xbins[1:] + xbins[:-1])
        ybins = 0.5 * (ybins[1:] + ybins[:-1])

        return xbins, ybins, L_cumsum[i_unsort].reshape(shape)

    def plot_SigmaLevels_MCMC(self,
                              xdata,
                              ydata,
                              trace_x,
                              trace_y,
                              plot_Scatter=True,
                              **kwargs):

        #"""Plot traces and contours"""
        xbins, ybins, sigma = self.Compute_SigmaLevel(trace_x, trace_y)
        self.Axis.contour(xbins,
                          ybins,
                          sigma.T,
                          levels=[0.683, 0.955],
                          **kwargs)

        if plot_Scatter:
            self.Axis.plot(trace_x, trace_y, ',k', alpha=0.1)

    def plot_posteriors_histagram(self, Traces, labels):

        n_traces = len(Traces)

        self.FigConf(Figtype='Posteriors', n_colors=n_traces)

        for i in range(len(Traces)):
            Trace = Traces[i]
            Axis_Location = int(str(n_traces) + '1' + str(i + 1))
            Axis = plt.subplot(Axis_Location)
            HDP_coords = self.statistics_dict[Trace]['95% HPD interval']

            for HDP in HDP_coords:
                Axis.axvline(x=HDP,
                             label='HPD interval: ' +
                             round_sig(HDP_coords[0], 4) + ' - ' +
                             round_sig(HDP_coords[1], 4),
                             color='grey',
                             linestyle='dashed')

            Axis.axvline(x=self.statistics_dict[Trace]['mean'],
                         label='Mean value: ' +
                         round_sig(self.statistics_dict[Trace]['mean'], 4),
                         color='grey',
                         linestyle='solid')
            Axis.hist(self.Traces_dict[Trace][:],
                      histtype='stepfilled',
                      bins=35,
                      alpha=.7,
                      color=self.ColorVector[2][i],
                      normed=False)
            Axis.set_ylabel(labels[i], fontsize=20)

            self.legend_conf(Axis, loc='best', edgelabel=True)

#             Axis.yaxis.set_ticks(arange(0, 250, 50))

    def plot_Ownposteriors_histagram(self,
                                     Traces,
                                     labels,
                                     sigfig_n=4,
                                     xlim=None,
                                     ylim=None):

        n_traces = len(Traces)

        self.FigConf(Figtype='Posteriors', n_colors=n_traces)

        for i in range(len(Traces)):
            Trace = Traces[i]
            Axis_Location = int(str(n_traces) + '1' + str(i + 1))
            Axis = plt.subplot(Axis_Location)
            HDP_coords = [percentile(Trace, 16), percentile(Trace, 84)]
            mean_value = median(Trace)

            for HDP in HDP_coords:
                Axis.axvline(
                    x=HDP,
                    label='Percentiles: 16th-84th: ' + round_sig(
                        HDP_coords[0], n=sigfig_n, scien_notation=False) +
                    ' - ' +
                    round_sig(HDP_coords[1], n=sigfig_n, scien_notation=False),
                    color='grey',
                    linestyle='dashed')

            Median_text_legend = r'Median value: $' + round_sig(
                mean_value, n=sigfig_n,
                scien_notation=False) + '_{-' + round_sig(
                    mean_value - HDP_coords[0],
                    n=sigfig_n,
                    scien_notation=False) + '}^{+' + round_sig(
                        HDP_coords[1] - mean_value,
                        n=sigfig_n,
                        scien_notation=False) + '}$'
            Axis.axvline(x=mean_value,
                         label=Median_text_legend,
                         color='grey',
                         linestyle='solid')
            Axis.hist(Trace,
                      histtype='stepfilled',
                      bins=35,
                      alpha=.7,
                      color=self.ColorVector[2][i],
                      normed=False)
            Axis.set_ylabel(labels[i], fontsize=20)

            self.legend_conf(Axis, loc='best', edgelabel=True)

            #             Axis.yaxis.set_ticks(arange(0, 250, 50))

            if xlim != None:
                Axis.set_xlim(xlim[0], xlim[1])

            if ylim != None:
                Axis.set_ylim(ylim[0], ylim[1])

        return

    def plot_tracers(self, Traces, labels):

        n_traces = len(Traces)

        self.FigConf(Figtype='Posteriors', n_colors=n_traces)

        for i in range(len(Traces)):
            Trace = Traces[i]
            Axis_Location = int(str(n_traces) + '1' + str(i + 1))
            Axis = plt.subplot(Axis_Location)
            Variable_value = self.statistics_dict[Trace]['mean']
            Variable_error = self.statistics_dict[Trace]['standard deviation']
            label = labels[i] + ': ' + round_sig(
                Variable_value, 4) + r'$\pm$' + round_sig(Variable_error, 4)
            Axis.plot(self.pymc_database.trace(Trace)[:],
                      label=label,
                      color=self.ColorVector[2][i])
            Axis.axhline(y=self.statistics_dict[Trace]['mean'],
                         color=self.ColorVector[2][i],
                         linestyle='--')
            Axis.set_ylabel(labels[i], fontsize=20)
            self.legend_conf(Axis, loc=1, edgelabel=True)

            plt.locator_params(axis='y', nbins=5)

    def plot_acorrelation(self, Traces, labels, n_columns=4, n_rows=2):

        n_traces = len(Traces)

        self.FigConf(Figtype='Grid',
                     n_colors=n_traces,
                     n_columns=n_columns,
                     n_rows=n_rows,
                     FigHeight=9,
                     FigWidth=16)

        for i in range(len(Traces)):
            Trace = Traces[i]
            maxlags = min(len(self.pymc_database.trace(Trace)[:]) - 1, 100)
            label = labels[i]
            if Trace != 'ChiSq':
                self.Axis1[i].acorr(x=self.pymc_database.trace(Trace)[:],
                                    color=self.ColorVector[2][i],
                                    detrend=detrend_mean,
                                    maxlags=maxlags)
            else:
                #Apano momentaneo
                chisq_adapted = reshape(
                    self.pymc_database.trace(Trace)[:],
                    len(self.pymc_database.trace(Trace)[:]))
                maxlags = min(len(chisq_adapted) - 1, 100)
                self.Axis1[i].acorr(x=chisq_adapted,
                                    color=self.ColorVector[2][i],
                                    detrend=detrend_mean,
                                    maxlags=maxlags)
            self.Axis1[i].set_xlim(0, maxlags)
            self.Axis1[i].set_title(label)

        return

    def plot_chiSq_Behaviour(self, Traces, labels):

        n_traces = len(Traces)

        self.FigConf(Figtype='Grid',
                     n_colors=n_traces,
                     n_columns=4,
                     n_rows=2,
                     FigHeight=9,
                     FigWidth=16)

        chisq_adapted = reshape(
            self.pymc_database.trace('ChiSq')[:],
            len(self.pymc_database.trace('ChiSq')[:])) * -2
        y_lim = 30
        min_chi_index = argmin(chisq_adapted)

        for i in range(len(Traces)):
            Trace = Traces[i]
            label = labels[i]

            if Trace != 'ChiSq':
                self.Axis1[i].scatter(x=self.pymc_database.trace(Trace)[:],
                                      y=chisq_adapted,
                                      color=self.ColorVector[2][i])
                x_min = npmin(self.pymc_database.trace(Trace)[:])
                x_max = npmax(self.pymc_database.trace(Trace)[:])

                self.Axis1[i].axvline(
                    x=self.statistics_dict[Trace]['mean'],
                    label='Inference value: ' +
                    round_sig(self.statistics_dict[Trace]['mean'],
                              4,
                              scien_notation=False),
                    color='grey',
                    linestyle='solid')
                self.Axis1[i].scatter(
                    self.pymc_database.trace(Trace)[:][min_chi_index],
                    chisq_adapted[min_chi_index],
                    color='Black',
                    label=r'$\chi^{2}_{min}$ value: ' + round_sig(
                        self.pymc_database.trace(Trace)[:][min_chi_index],
                        4,
                        scien_notation=False))

                self.Axis1[i].set_ylabel(r'$\chi^{2}$', fontsize=20)
                self.Axis1[i].set_ylim(0, y_lim)
                self.Axis1[i].set_xlim(x_min, x_max)
                self.Axis1[i].set_title(label, fontsize=20)
                legend_i = self.Axis1[i].legend(loc='best', fontsize='x-large')
                legend_i.get_frame().set_facecolor('white')

    def plot_triangle_histContours(self, Traces, labels, true_values=None):

        n_traces = len(Traces)

        self.FigConf(Figtype='triangle')

        #Make plots
        List_Arrays = []
        for i in range(n_traces):
            List_Arrays.append(self.pymc_database.trace(Traces[i])[:])

        Samples = array(List_Arrays).T

        #Make
        if true_values != None:
            self.figure = corner.corner(Samples[:, :],
                                        labels=labels,
                                        quantiles=[0.16, 0.5, 0.84],
                                        show_titles=True,
                                        title_args={"fontsize": 100},
                                        truths=true_values,
                                        title_fmt='0.3f')
        else:
            self.figure = corner.corner(Samples[:, :],
                                        labels=labels,
                                        quantiles=[0.16, 0.5, 0.84],
                                        show_titles=True,
                                        title_args={"fontsize": 100},
                                        title_fmt='0.3f')

    def savefig(self, output_address, extension='.png', reset_fig=True):

        if self.Triangle_Saver:
            self.figure.savefig(output_address + extension,
                                dpi=500,
                                bbox_inches='tight',
                                pad_inches=0.2)

        else:
            plt.tight_layout()
            plt.savefig(output_address + extension,
                        dpi=500,
                        facecolor=self.ColorVector[0],
                        bbox_inches='tight',
                        pad_inches=0.2)

        if reset_fig:
            self.reset_fig()

        return

    def reset_fig(self):

        plt.cla()

        return


# def GetLower_ChiSquare(MCMC_Database, variable, mean = None, min_value = None, max_value = None, nbins = 100):
#
#     print 'Treating variable', variable
#
#
#     ChiArray        =  Database_MCMC.trace('ChiSq')[:] * -1
#     VariableArray   = Database_MCMC.trace(variable)[:]
#
#     if mean == None:
#         mean = average(VariableArray)
#
#     if min_value == None:
#         min_value = 0.8
#
#     if max_value == None:
#         max_value = 1.2
#
#     Bins                        = linspace(mean*min_value, mean*max_value, nbins)
#     Points_Index                = digitize(VariableArray, Bins)
#
#     Unique_Bins                 = unique(Points_Index)
#     Min_Index, Max_Index        = min(Unique_Bins), max(Unique_Bins)
#
#     Min_values_Chi              = zeros(len(Points_Index))
#     Min_values_Variable         = zeros(len(Points_Index))
#
#     for i in range(Min_Index, Max_Index):
#         inBinIndex              = where(Points_Index == i)[0]
#
#         if len(ChiArray[inBinIndex]) != 0:
#             Min_values_Chi[i]       = np_min(ChiArray[inBinIndex])
#             Min_values_Variable[i]  = Bins[i]
#
#     return Min_values_Variable, Min_values_Chi, mean
print "frequentist thetas:\n", thetas_freq

# Build the model. We're modelling the tumor rate in each group as a draw from a
# Beta distribution shared across all the groups. The number of tumors in a
# group is then sampled as a Binomial(n, rate)
a = Uniform('a', lower=1, upper=15)
b = Uniform('b', lower=1, upper=15)
thetas = Beta('thetas', alpha=a, beta=b, size=n.size)
y = Binomial('y', n=n, p=thetas, value=tumors, observed=True)

# sample from the model
M = MCMC({'y':y, 'thetas': thetas, 'a':a, 'b':b})
M.sample(iter=30000, burn=10000, thin=10)

# compute posterior estimates
thetas_bayes = M.trace('thetas')[:].mean(axis=0)
a_post = M.trace('a')[:].mean()
b_post = M.trace('b')[:].mean()

print "++++++++++ Mixing spot checks ++++++++++"
print "a.trace:", M.trace('a')[:]
print "theta[4].trace:", M.trace('thetas')[:,4]

print "++++++++++ Posteriors ++++++++++"
print "a posterior:", a_post
print "b posterior:", b_post
print "thetas posterior:\n", thetas_bayes

# plot the quality of the fit to empirical distribution of thetas
plt.title('Theta goodness of fit')
plt.ylabel('density')
Exemple #30
0


#plt.savefig('repulsion_length.png')
#plt.show()
#plt.hist(M.trace('eta')[:])
#plt.xlim(0,1)

#plt.title('autocorrelation')

#plt.savefig('autocorrelation.png')
#plt.show()

#show()
plt.figure()
aa = (M.trace('alpha')[:])
bb = (M.trace('beta')[:])
mv=(1-bb)*(1-aa)
sv=aa
ev=(bb)*(1-aa)
plt.hist(mv,normed=True, label='memory')
plt.hist(sv,normed=True, label='social')
plt.hist(ev,normed=True, label='environment')

plt.legend(loc='upper center')
plt.xlim(0,1)
plt.show()
plt.savefig('heading_weights.png')

plt.figure()
plt.hist((M.trace('rho_s')[:]),normed=True, label='s')
Exemple #31
0
# M.sample(iter=400000, burn=50000, thin=10,verbose=0)
# np.save('mc_data/nm_rho_s.npy',M.trace('rho_s')[:])
# np.save('mc_data/nm_alpha.npy',M.trace('alpha')[:])
# np.save('mc_data/nm_beta.npy',M.trace('beta')[:])

# np.save('mc_data/nm_ia.npy',M.trace('interaction_angle')[:])
# np.save('mc_data/nm_il.npy',M.trace('interaction_length')[:])
# np.save('mc_data/nm_ig.npy',M.trace('ignore_length')[:])

# network model with alignment
M = MCMC(networkModelAlignMC)
M.use_step_method(
    pymc.AdaptiveMetropolis,
    [
        networkModelAlignMC.interaction_angle,
        networkModelAlignMC.interaction_length,
        networkModelAlignMC.align_weight,
        networkModelAlignMC.ignore_length,
    ],
    delay=1000,
)
M.sample(iter=400000, burn=50000, thin=10, verbose=0)
np.save("mc_data/nma_rho_s.npy", M.trace("rho_s")[:])
np.save("mc_data/nma_alpha.npy", M.trace("alpha")[:])
np.save("mc_data/nma_beta.npy", M.trace("beta")[:])

np.save("mc_data/nma_ia.npy", M.trace("interaction_angle")[:])
np.save("mc_data/nma_il.npy", M.trace("interaction_length")[:])
np.save("mc_data/nma_aw.npy", M.trace("align_weight")[:])
np.save("mc_data/nma_ig.npy", M.trace("ignore_length")[:])
iterations = 1000000
burn = 900000
thin = 10

hmod = kq1.generate_model(HISTORICAL)
cmod = kq1.generate_model(CONCURRENT)

H = MCMC(hmod, db="pickle", dbname="historical_model.pickle")

H.sample(iterations, burn, thin=thin, verbose=2)

C = MCMC(cmod, db="pickle", dbname="concurrent_model.pickle")

C.sample(iterations, burn, thin=thin, verbose=2)

conc = np.sum(C.trace("pred")[:], 0)/float((iterations-burn)/thin)
historical = np.sum(H.trace("pred")[:], 0)/float((iterations-burn)/thin)

x = np.arange(200,3200, 200)

colors = ('0.7', 'black')
markers = (False, True)

for i,p in enumerate((70,85)):
    plot(x, conc[i*-1][C.crit_pred==1], color=colors[i], marker='^'*markers[i])
    plot(x, historical[i*-1][H.crit_pred==1], color=colors[i], marker='o'*markers[i])
    plot(x, conc[i*-1][C.crit_pred==0], color=colors[i], marker='^'*markers[i], linestyle="dashed")
    plot(x, historical[i*-1][H.crit_pred==0], color=colors[i], marker='o'*markers[i], linestyle="dashed")
xlim(150, 3050)
ylim(0,1)
#!/usr/bin/env python
import two_normal_model
from pymc import MCMC
from pymc.Matplot import plot

# do posterior sampling
m = MCMC(two_normal_model)
m.sample(iter=100000, burn=1000)
print(m.stats())

import numpy
for p in ['mean1', 'mean2', 'std_dev', 'theta']:
    numpy.savetxt("%s.trace" % p, m.trace(p)[:])

# draw some pictures
plot(m)
Exemple #34
0
@deterministic(plot=False)
def r(s=s, e=e, l=l):
    """ Concatenate Poisson means """
    out = np.empty(len(disasters_array))
    out[:s] = e
    out[s:] = l
    return out


from pymc.examples import DisasterModel
from pymc import MCMC
M = MCMC(DisasterModel)

M.isample(iter=10000, burn=1000, thin=10)

M.trace('s')[:]
#array([41, 40, 40, ..., 43, 44, 44])

from pylab import hist, show
hist(M.trace('l')[:])
#(array([   8,   52,  565, 1624, 2563, 2105, 1292,  488,  258,   45]),
#array([ 0.52721865,  0.60788251,  0.68854637,  0.76921023,  0.84987409,
#   0.93053795,  1.01120181,  1.09186567,  1.17252953,  1.25319339]),
#<a list of 10 Patch objects>)
show()

from pymc.Matplot import plot
plot(M)

x = np.array([
    4, 5, 4, 0, 1, 4, 3, 4, 0, 6, 3, 3, 4, 0, 2, 6, 3, 3, 5, 4, 5, 3, 1, 4, 4,
from pymc import MCMC
import numpy as np
from pythonMCMC import pymcCrater
from pymc.Matplot import plot
from pylab import hist, show,draw

M = MCMC(pymcCrater)
M.sample(iter=10000, burn=700, thin=5)

print M.trace('lnlike')[:]
print M.stats()
plot(M)
show()
    return sapflow_modeled


'''data likelihoods'''
np.random.seed(1)
Y_obs = Normal('Y_obs', mu=muf, tau=sigma, value=vn, observed=True)
''' posterior sampling '''
M = MCMC([alpha, c, g1, kxmax, Lamp, Lave, LTf, p50, Z, sigma])
M.use_step_method(AdaptiveMetropolis,
                  [alpha, c, g1, kxmax, Lamp, Lave, LTf, p50, Z, sigma])
M.sample(iter=1000000, burn=500000, thin=40)

# Save trace
ensure_dir(species)
traces = {
    'alpha': M.trace('alpha')[:],
    'c': M.trace('c')[:],
    'g1': M.trace('g1')[:],
    'kxmax': M.trace('kxmax')[:],
    'Lamp': M.trace('Lamp')[:],
    'Lave': M.trace('Lave')[:],
    'LTf': M.trace('LTf')[:],
    'p50': M.trace('p50')[:],
    'Z': M.trace('Z')[:],
    'sigma': M.trace('sigma')[:]
}
pickle_out = open("MCMC.pickle", "wb")
pickle.dump(traces, pickle_out)
pickle_out.close()

# Trace
from OOH import tube_model
from pymc import MCMC
from pylab import hist, show, bar, plot
import numpy as np
#from pymc.Matplot import plot

M = MCMC(tube_model)
M.sample(iter=100000, burn=1000, thin=10)
# hist(M.trace("after_mean")[:])
# hist(M.trace("before_mean")[:])
# show()

#plot(M)
#show()

before_mean_samples = M.trace("before_mean")[:]
after_mean_samples = M.trace("after_mean")[:]
start_time_samples = M.trace("start_t")[:]

N = start_time_samples.shape[0]

expected_conversions_per_day = np.zeros(16)

for day in range(0, 16):
    ix = day < start_time_samples
    expected_conversions_per_day[day] = ((before_mean_samples[ix].sum()) +
                                         (after_mean_samples[~ix].sum())) / N
print(expected_conversions_per_day)
plot(list(range(16)), expected_conversions_per_day)
show()
Exemple #38
0
x_weight = Normal("weight", 0, 1, value=xs, observed=True)

@deterministic(plot=False)
def pred(b00=b00, b01=b01, b10=b10, b11=b11, s=switchpoint):
    out = np.empty(len(xs))
    breakk = s
    out[:breakk] = b00 + b01*xs[:breakk]
    out[breakk:] = b10 + b11*xs[breakk:]
    return out

y = Normal("y", mu=pred, tau=err, value=ys, observed=True)

model = Model([pred, b00, b01, b10, b11, switchpoint, y, err])

# g = graph.graph(model)
# g.write_png('graph.png')

m = MCMC(model)
m.sample(burn=1000, iter=10000)

bb00 = np.mean(m.trace('b00')[:])
bb01 = np.mean(m.trace('b01')[:])
bb10 = np.mean(m.trace('b10')[:])
bb11 = np.mean(m.trace('b11')[:])
switch = mode(m.trace('switch')[:])[0][0]

plot(xs, ys, 'ro')
plot([min(xs), switch], [bb00 + min(xs)*bb01, bb00 + switch*bb01])
plot([switch, max(xs)], [bb10 + switch*bb11, bb10 + max(xs)*bb11])
show()
from OOH import tube_model
from pymc import MCMC
from pylab import hist, show, bar, plot
import numpy as np
#from pymc.Matplot import plot

M = MCMC(tube_model)
M.sample(iter = 100000, burn = 1000, thin = 10)
# hist(M.trace("after_mean")[:])
# hist(M.trace("before_mean")[:])
# show()

#plot(M)
#show()

before_mean_samples = M.trace("before_mean")[:]
after_mean_samples = M.trace("after_mean")[:]
start_time_samples = M.trace("start_t")[:]

N = start_time_samples.shape[0]

expected_conversions_per_day = np.zeros(16)

for day in range(0, 16):
    ix = day < start_time_samples
    expected_conversions_per_day[day] = ((before_mean_samples[ix].sum()) + (after_mean_samples[~ix].sum())) / N
print(expected_conversions_per_day)
plot(list(range(16)), expected_conversions_per_day)
show()
Exemple #40
0
import numpy as np
from matplotlib import pylab as plt
import dive_model
#import noba_model
import pymc
from pymc import MCMC
from pymc.Matplot import plot as mcplot

M = MCMC(dive_model)

M.sample(iter=2000000, burn=0, thin=10, verbose=0)
mcplot(M)

plt.hist([M.trace('intrinsic_rate')[:]], 500, label='intrinsic')
plt.hist([M.trace('social_rate')[:]], 500, label='social')
plt.legend(loc='upper left')
plt.xlim(0, 0.2)
plt.show()

d1 = M.trace('blind_angle')[:]
bc = d1 * 180 / 3.142
plt.hist(bc, 20)
plt.xlim(0, 380)
plt.show()

plt.hist([M.trace('lag')[:]])
plt.legend(loc='upper left')
plt.xlim(0, 5)
plt.show()

plt.hist([M.trace('dist')[:]], 100)
Exemple #41
0
                         b=10)  #hypothetical ground truth
    botOutput = TruncatedNormal('botOutput', mu=mu, tau=t, a=1, b=10)
    humanOutput = TruncatedNormal('humanOutput', mu=mu, tau=t, a=1, b=10)
    #when we have data from the model we can use this here
    #like this d = pymc.Binomial(‘d’, n=n, p=theta, value=np.array([0.,1.,3.,5.]), observed=True)

    sim = MCMC([mu, botOutput, humanOutput])

    for s in sampleSize:
        #get s number of samples, no burn in for this- not optimizing anything
        print 'sample size: {}  | variance: {} '.format(s, t)
        sim.sample(s, 0, 1)
        #assume no bias at this point but we can add bias later
        b = 0
        #computer is now varying
        botOutput = sim.trace("botOutput")[:]
        #vary the human output for this image
        #humans probably only give ratings at the 0.5 interval, not smaller
        #        humanOutput = round_to_half(sim.trace("humanOutput")[:])
        humanOutput = sim.trace("humanOutput")[:]
        #difference of the means
        #but what we care about is the mean of the human output for each image.
        difference = botOutput - humanOutput.mean()
        #HighestDensityInterval: where 95% of the differences are? are they close to 0?
        HDI = HDIofMCMC(differenceOfMeans=difference, credMass=credibleMass)
        #ROPE
        if strictly == True:
            ROPE = difference[(difference <= ROPESize)
                              & (difference >= (0 - ROPESize))]
            probabilityInROPE = np.round(len(ROPE) * 1.0 / len(difference) *
                                         1.0,
Exemple #42
0
if __name__ == '__main__':
    # Create the MCMC object and start sampling
    pymc_model = pymc.Model([k1, k2, k3, robertson_model, output])
    mcmc = MCMC(pymc_model)
    mcmc.sample(iter=10000, burn=5000, thin=5)

    # Show the pymc histograms and autocorrelation plots
    plt.ion()
    pymc.Matplot.plot(mcmc)
    plt.show()

    # Plot the original data along with the sampled trajectories
    plt.figure()
    plt.plot(tspan, ydata_norm[:,0], 'r')
    plt.plot(tspan, ydata_norm[:,1], 'g')
    plt.plot(tspan, ydata_norm[:,2], 'b')

    num_timecourses = 1000
    num_iterations_sampled = mcmc.trace('robertson_model')[:].shape[0]

    plt.plot(tspan, mcmc.trace('robertson_model')[num_iterations_sampled -
               num_timecourses:,0::3].T, alpha=0.05, color='r')
    plt.plot(tspan, mcmc.trace('robertson_model')[num_iterations_sampled -
               num_timecourses:,1::3].T, alpha=0.05, color='g')
    plt.plot(tspan, mcmc.trace('robertson_model')[num_iterations_sampled -
               num_timecourses:,2::3].T, alpha=0.05, color='b')
    
    # Show k1/k3 scatter plot
    plt.figure()
    plt.scatter(mcmc.trace('k1')[:], mcmc.trace('k3')[:])
Exemple #43
0
if __name__ == '__main__':
    # Build a model
    # NOTE: Be careful to avoid namespace clashes with pysb.Model!
    pymc_model = pymc.Model([k, decay_model])

    # Initialize an MCMC object from the model
    mcmc = MCMC(pymc_model)

    # Sample
    mcmc.sample(iter=15000, burn=5000, thin=10)

    # Plot the posterior distribution of the parameter k
    plt.ion()
    Matplot.plot(mcmc)

    # Plot the original data (underlying and noisy)
    # along with the sampled trajectories
    plt.figure()
    plt.plot(tspan, ysim, color='r')
    plt.plot(tspan, ydata, color='g')
    num_to_plot = 1000
    k_vals = mcmc.trace('k')[:]
    if num_to_plot > len(k_vals):
        num_to_plot = len(k_vals)

    for i in range(num_to_plot):
        solver.run(np.array([A_0.value, k_vals[i], 1.0]))
        plt.plot(tspan, solver.yobs['A_obs'], alpha=0.05, color='b')

Exemple #44
0
        #Averaged experiments
        @stochastic
        def vars(x_plus_gen=x_plus_gen,
                 y_plus_gen=y_plus_gen,
                 x_minus_gen=x_minus_gen,
                 y_minus_gen=y_minus_gen,
                 value=0):
            return pymc.mv_normal_like(
                [x_plus_gen, y_plus_gen, x_minus_gen, y_minus_gen],
                experiment['y_exp'], experiment['y_covar_inv'])

        # Model
        if load_last_model:
            mcmc = pymc.database.pickle.load('mcmc-{}.pickle'.format(name))
        else:
            mcmc = MCMC([vars, gamma, deltaB, rB],
                        db='pickle',
                        dbmode='w',
                        dbname='mcmc-{}.pickle'.format(name))
            mcmc.sample(iter=N, burn=min(5000, int(N / 10)), thin=1)

        for v in var_list:
            commons.plot(
                mcmc.trace(v.__name__)[:], v.__doc__,
                "pics/{}_{}.png".format(name, v.__name__))

        for x, y in combinations(var_list, 2):
            commons.plot_2d_hist(
                mcmc.trace(x.__name__)[:],
                mcmc.trace(y.__name__)[:],
                "pics/{}_{}-{}_hist.png".format(name, x.__name__, y.__name__))
print 'R_effective (bulge) / R_effective (disk) =', \
       M.stats()['r_e_B']['mean'] / M.stats()['r_e_D']['mean']
print 'Effective surface brightness of the bulge: \n', \
       '    Best-fit value:', M.stats()['M_e_B']['mean'], \
       '\n    95% Confidence interval:', M.stats()['M_e_B']['quantiles'][2.5], \
        'to', M.stats()['M_e_B']['quantiles'][97.5]


# ### Visualizing specific realizations of our model ###
# The *trace()* method presents the values of a variable for all of the saved  
# Markov Chain steps. Let's plot up several of these traces, and see how  
# the model changes with different parameter values.

for i in range(50):
    plt.plot(M.r.value, M.trace('SB')[i], c='gray', alpha=.25)

plt.scatter(M.r.value, M.mags.value, c='r') 
plt.gca().invert_yaxis()
plt.xlabel('radius (")')
plt.ylabel('Surface Brightness (mag)')
plt.show()


# ### Visualizing the best-fit model and its components ###
# Now that we have our best-fit, let's see how it decomposes into the components,  
# the bulge and the disk. In other words, we can finally take our results and  
# explore what they imply about the physical system we observed!

# our best-fit parameters
r_e_B = M.stats()['r_e_B']['mean']
    def run(self, debug_info=dict()):
        '''Run delineation process for each R-R interval.'''

        r_list = self.r_list
        result_dict = self.result_dict
        fs = self.fs
        raw_sig = self.raw_sig
        p_wave_length = self.p_wave_length
        r_detector = DPI_QRS_Detector()

        for ind in xrange(0, len(r_list) - 1):
            print 'Progress: %d R-R intervals left.' % (len(r_list) - 1 - ind)
            if ind > 1:
                print 'Debug break.'
                break
            region_left = r_list[ind]
            region_right = r_list[ind + 1]
            QR_length = fs / 46.0
            PR_length = 0.5 * (region_right - region_left)

            cut_x_list = [
                int(region_right - PR_length),
                int(region_right - QR_length)
            ]

            sig_seg = raw_sig[cut_x_list[0]:cut_x_list[1]]
            sig_seg = r_detector.HPF(sig_seg, fs=fs, fc=3.0)
            sig_seg = sig_seg[:cut_x_list[1] - cut_x_list[0]]
            if len(sig_seg) <= 75:
                print 'R-R interval %d is too short!' % len(sig_seg)
                continue

            p_model = P_model_Gaussian.MakeModel(sig_seg, p_wave_length, fs=fs)
            M = MCMC(p_model)

            M.sample(iter=2000, burn=1000, thin=10)

            # retrieve parameters
            hermit_coefs = list()
            for h_ind in xrange(0, P_model_Gaussian.HermitFunction_max_level):
                hermit_value = np.mean(M.trace('hc%d' % h_ind)[:])
                hermit_coefs.append(hermit_value)
            # P wave shape parameters
            gpos = np.mean(M.trace('gaussian_start_position')[:])
            gsigma = np.mean(M.trace('gaussian_sigma')[:])
            gamp = np.mean(M.trace('gaussian_amplitude')[:])

            print 'Results:'
            print 'gpos = ', gpos
            print 'gsigma = ', gsigma
            print 'gamp = ', gamp

            len_sig = cut_x_list[1] - cut_x_list[0]
            fitting_curve = P_model_Gaussian.GetFittingCurve(
                len_sig, gpos, gsigma, gamp, hermit_coefs)
            baseline_curve = P_model_Gaussian.GetFittingCurve(
                len_sig, gpos, gsigma, 0, hermit_coefs)
            plt.figure(1)
            plt.clf()
            plt.plot(sig_seg, label='ECG')
            plt.plot(fitting_curve, label='fitting curve')
            plt.plot(baseline_curve,
                     linestyle='--',
                     alpha=0.3,
                     lw=2,
                     label='baseline curve')
            plt.plot(gpos, fitting_curve[gpos], 'r^', markersize=12)
            plt.legend()
            plt.grid(True)
            if 'plot_title' in debug_info:
                plt.title(debug_info['plot_title'], fontproperties=zn_font)
            if 'plot_xlabel' in debug_info:
                plt.xlabel(debug_info['plot_xlabel'])
            plt.show(block=False)
            plt.savefig('./results/tmp/%d.png' % int(time.time()))

            peak_pos = int(gpos + gsigma / 2.0)
            peak_global_pos = peak_pos + cut_x_list[0]

            # Save to result dict
            result_dict['gamp'].append(gamp)
            result_dict['gsigma'].append(gsigma)
            result_dict['gpos'].append(gpos)
            result_dict['hermit_coefs'].append(hermit_coefs)
            result_dict['segment_range'].append(cut_x_list)
            result_dict['peak_global_pos'].append(peak_global_pos)

            continue
        return result_dict
Exemple #47
0
    out[:s] = e
    out[s:] = l
    return out
    
datapoints = Normal('datapoints', mu=rate, tau=.1, value=dataSet, observed=True)

vars = [
        changepoint,
        early_mean,
        late_mean,
        datapoints]

M = MCMC(vars)
M.sample(iter=100000, burn=1000, thin=10)
#%%
hist(M.trace('late_mean')[:],100)
hist(M.trace('early_mean')[:],100)
hist(M.trace('changepoint')[:],100)



        
#        
##%%
#                   
#switchpoint = DiscreteUniform('switchpoint', lower=0, upper=110, doc='Switchpoint[year]')
#
#early_mean = Exponential('early_mean', beta=1.)
#late_mean = Exponential('late_mean', beta=1.)
#
#@deterministic(plot=False)
Exemple #48
0
converted_data = [(float(x[0]), float(x[1]), float(x[2])) for x in data]
xs = [x[1] for x in converted_data]
ys = [x[2] for x in converted_data]
    

b0 = Normal("b0", 0, 0.0003)
b1 = Normal("b1", 0, 0.0003)

err = Uniform("err", 0, 500)

x_weight = Normal("weight", 0, 1, value=xs, observed=True)

@deterministic(plot=False)
def pred(b0=b0, b1=b1, x=x_weight):
    return b0 + b1*x

y = Normal("y", mu=pred, tau=err, value=ys, observed=True)

model = Model([pred, b0, b1, y, err, x_weight])

m = MCMC(model)
m.sample(burn=2000, iter=10000)

bb0 = sum(m.trace('b0')[:])/len(m.trace('b0')[:])
bb1 = sum(m.trace('b1')[:])/len(m.trace('b1')[:])
err = sum(m.trace('err')[:])/len(m.trace('err')[:])

plot(xs, ys)
plot([min(xs), max(xs)], [bb0 + bb1*min(xs), bb0 + bb1*max(xs)])
show()
Exemple #49
0
import lh
from pymc import MCMC
from pymc.Matplot import plot
from matplotlib.pyplot import hist2d
import matplotlib.pyplot as plt

M = MCMC(lh)
M.sample(iter=50000, burn=1000, thin=5)
print
plot(M)
M.alpha.summary()
M.beta.summary()

alpha_arr = M.trace('alpha')[:]
beta_arr = M.trace('beta')[:]

H, xedges, yedges, img = hist2d(alpha_arr, beta_arr, 250)
extent = [yedges[0], yedges[-1], xedges[0], xedges[-1]]
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
im = ax.imshow(H, extent=extent, cmap=plt.cm.hot)
fig.colorbar(im, ax=ax)
plt.axes().set_aspect(0.67)
plt.tight_layout(pad=0.4)
plt.savefig('lighthouse_cm_uniform.png')
plt.close()
Exemple #50
0
        @deterministic
        def y_plus_gen(gamma=gamma, deltaB=deltaB, rB=rB):
            return commons.y_plus(gamma, deltaB, rB)
        @deterministic
        def x_minus_gen(gamma=gamma, deltaB=deltaB, rB=rB):
            return commons.x_minus(gamma, deltaB, rB)
        @deterministic
        def y_minus_gen(gamma=gamma, deltaB=deltaB, rB=rB):
            return commons.y_minus(gamma, deltaB, rB)

        #Averaged experiments
        @stochastic
        def vars(x_plus_gen=x_plus_gen, y_plus_gen=y_plus_gen, x_minus_gen=x_minus_gen, y_minus_gen=y_minus_gen, value=0):
            return pymc.mv_normal_like([x_plus_gen, y_plus_gen, x_minus_gen, y_minus_gen], experiment['y_exp'], experiment['y_covar_inv'])

        # Model
        if load_last_model:
            mcmc = pymc.database.pickle.load('mcmc-{}.pickle'.format(name))
        else:
            mcmc = MCMC([vars, gamma, deltaB, rB],
                        db='pickle',
                        dbmode='w',
                        dbname='mcmc-{}.pickle'.format(name))
            mcmc.sample(iter = N, burn = min(5000, int(N/10)), thin = 1)

        for v in var_list:
            commons.plot(mcmc.trace(v.__name__)[:], v.__doc__, "pics/{}_{}.png".format(name, v.__name__))

        for x,y in combinations(var_list, 2):
           commons.plot_2d_hist(mcmc.trace(x.__name__)[:], mcmc.trace(y.__name__)[:],
                         "pics/{}_{}-{}_hist.png".format(name, x.__name__, y.__name__))
Exemple #51
0
import constantModelAlign
import decayModel
import decayModelAlign
import networkModelMC
import networkModelAlignMC


import pymc
from pymc import MCMC
from pymc.Matplot import plot as mcplot


### random walk model
M = MCMC(corRandomWalk)
M.sample(iter=50000, burn=25000, thin=10,verbose=0)
np.save('mc_data/crw_rho_m.npy',M.trace('rho_m')[:])

## environment model
M = MCMC(environment)
M.sample(iter=50000, burn=25000, thin=10,verbose=0)
np.save('mc_data/env_rho_m.npy',M.trace('rho_m')[:])
np.save('mc_data/env_rho_e.npy',M.trace('rho_e')[:])
np.save('mc_data/env_beta.npy',M.trace('beta')[:])

## constant model
M = MCMC(constantModel)
M.sample(iter=50000, burn=25000, thin=10,verbose=0)
np.save('mc_data/cm_rho_s.npy',M.trace('rho_s')[:])
np.save('mc_data/cm_alpha.npy',M.trace('alpha')[:])

np.save('mc_data/cm_ia.npy',M.trace('interaction_angle')[:])
Exemple #52
0
# do the following
# (has mean around 1e2, with a variance of 9 logs in base 10)
mean_b10 = 2
var_b10 = 9

print "Setting mean (base 10) to %f, variance (base 10) to %f" % (mean_b10, var_b10)

# The lognormal variable
k = Lognormal('k', mu=np.log(10 ** mean_b10),
                   tau=1./(np.log(10) * np.log(10 ** var_b10)))

# Sample it
m = MCMC(Model([k]))
m.sample(iter=50000)

ion()

# Plot the distribution in base e
figure()
y = log(m.trace('k')[:])
y10 = log10(m.trace('k')[:])
hist(y, bins=100)
print
print "Mean, base e: %f; Variance, base e: %f" % (mean(y), var(y))

# Plot the distribution in base 10
figure()
hist(y10, bins=100)
print "Mean, base 10: %f; Variance, base 10: %f" % (mean(y10), var(y10))

Exemple #53
0
    out[s:] = l
    return out


datapoints = Normal('datapoints',
                    mu=rate,
                    tau=.1,
                    value=dataSet,
                    observed=True)

vars = [changepoint, early_mean, late_mean, datapoints]

M = MCMC(vars)
M.sample(iter=100000, burn=1000, thin=10)
#%%
hist(M.trace('late_mean')[:], 100)
hist(M.trace('early_mean')[:], 100)
hist(M.trace('changepoint')[:], 100)

#
##%%
#
#switchpoint = DiscreteUniform('switchpoint', lower=0, upper=110, doc='Switchpoint[year]')
#
#early_mean = Exponential('early_mean', beta=1.)
#late_mean = Exponential('late_mean', beta=1.)
#
#@deterministic(plot=False)
#def rate(s=switchpoint, e=early_mean, l=late_mean):
#    ''' Concatenate Poisson means '''
#    out = np.empty(len(disasters_array))
Exemple #54
0
#                self.reject()
                
            
            
            
    
    return locals()
    
if __name__ == '__main__':
    
    model = make_bma()
    M = MCMC(model)
    M.use_step_method(model['ModelMetropolis'], model['regression_model'])
    M.sample(iter=5000, burn=1000, thin=1)
    
    model_chain = M.trace("regression_model")[:]
    
    from collections import Counter
    
    counts = Counter(model_chain).items()
    counts.sort(reverse=True, key=lambda x: x[1])
    
    for f in counts[:10]:
        columns = unpack(f[0])
        print('Visits:', f[1])
        print(np.array([1. if i in columns else 0 for i in range(0,M.rank)]))
        print(M.coefficients.flatten())
        X = sm.add_constant(M.X[:, columns], prepend=True)
        corr = np.corrcoef(X[:,1:], rowvar=0)
        prior = np.linalg.det(corr)
        fit = sm.OLS(model['y'],X).fit()
Exemple #55
0
    out[s:] = l
    return out



from pymc.examples import DisasterModel
from pymc import MCMC
M = MCMC(DisasterModel)



M.isample(iter=10000, burn=1000, thin=10)



M.trace('s')[:]
#array([41, 40, 40, ..., 43, 44, 44])



from pylab import hist, show
hist(M.trace('l')[:])
#(array([   8,   52,  565, 1624, 2563, 2105, 1292,  488,  258,   45]),
#array([ 0.52721865,  0.60788251,  0.68854637,  0.76921023,  0.84987409,
#   0.93053795,  1.01120181,  1.09186567,  1.17252953,  1.25319339]),
#<a list of 10 Patch objects>)
show()



from pymc.Matplot import plot