#Plot the prior pyplot.figure() pyplot.hist(x,bins=30) pyplot.title('Prior Distribution of X') pyplot.show() for i in range(10): #Create the KernelSmoothing node X = KernelSmoothing('X', x) # f(x) = x*x Y = X*X #Create the observed node with data generated from the true distribution OBS = pymc3.Normal('OBS', Y, 0.1, value=xtrue*xtrue+pymc.rnormal(0,1), observed=True) #Do the sampling model = pymc3.Model([X,Y,OBS]) mcmc = pymc3.MCMC(model) mcmc.sample(5000) #Get the posterior sample, to be passed into the KernelSmoothing node on the next iteration x = mcmc.trace('X')[:] #Display the histogram of the posterior distribution pyplot.figure() pyplot.hist(x,bins=30) pyplot.title('Posterior Distribution of X: %d Iterations' % (i+1,)) pyplot.show()
# Set the size of the halo's mass. mass_large = pm.Uniform("mass_large", 40, 180, trace=False) # Set the initial prior position of the halos; it's a 2D Uniform # distribution. halo_position = pm.Uniform("halo_position", 0, 4200, size=(1,2)) @pm.deterministic def mean(mass=mass_large, h_pos=halo_position, glx_pos=data[:,:2]): return mass/f_distance(glx_pos, h_pos, 240)* tangential_distance(glx_pos, h_pos) ellpty = pm.Normal("ellipticity", mean, 1./0.05, observed=True, value=data[:,2:]) mcmc = pm.MCMC([ellpty, mean, halo_position, mass_large]) map_ = pm.MAP([ellpty, mean, halo_position, mass_large]) map_.fit() mcmc.sample(200000, 140000, 3) # In[ ]: t = mcmc.trace("halo_position")[:].reshape (20000,2) fig = draw_sky(data) plt.title("Galaxy positions and ellipticities of sky %d."%n_sky) plt.xlabel("$x$ position") plt.ylabel("$y$ position") scatter(t[:,0], t[:,1], alpha=0.015, c="r")
plt.bar(np.arange(size), textfile, color="#348ABD") plt.xlabel("Time (days)") plt.ylabel("Text message received") plt.title("Did the user's texting habit change over time?") plt.xlim(0, size) # Create the MCMC model with its parameters (lambdas, tau) with pm.Model() as model: lambda_1 = pm.Exponential('lambda_1', alpha) # create stochastic variable lambda_2 = pm.Exponential('lambda_2', alpha) #create stochastic variable tau = pm.DiscreteUniform("tau", lower=0, upper=size) # tau has Uniform distribution print("Random output:", tau.random(), tau.random(), tau.random()) n_data_points = size idx = np.arange(n_data_points) with model: lambda_ = pm.math.switch(tau >= idx, lambda_1, lambda_2) #pm.Poisson("obs", lambda_, value = textfile, observed=True) with model: obs = pm.Poisson("obs", lambda_, observed=textfile) print(obs.tag.test_value) model = pm.Model([obs, lambda_1, lambda_2, tau]) print(model) mcmc = pm.MCMC(model) mcmc.sample(40000, 10000)
@pymc.deterministic def predictionC(c0=c0, c1=c1, c2=c2): return fC(x, c0, c1, c2) ymodelC = pymc.Normal("yC", predictionC, errC, value=y, observed=True) # construct PyMC models: modelA = pymc.Model([predictionA, a0, a1, ymodelA, errA]) modelB = pymc.Model([predictionB, b0, b1, b2, ymodelB, errB]) modelC = pymc.Model([predictionC, c0, c1, c2, ymodelC, errC]) # conduct inference separately for each model: mcmcA = pymc.MCMC(modelA) mcmcA.sample(niter * thin + burn, burn, thin, verbose=verbose, progress_bar=True) mcmcB = pymc.MCMC(modelB) mcmcB.sample(niter * thin + burn, burn, thin, verbose=verbose, progress_bar=True) mcmcC = pymc.MCMC(modelC) mcmcC.sample(niter * thin + burn,
def _pymc_bayes_method(self, exog = None, med = None, endog = None): """Estimate indirect effect and intervals with fully Bayesian model (pymc as backend). Default sampler is Slice sampling for all parameters Better to use pymc3 as backend if convergence problems appear since it uses NUTS sampler Parameters ---------- exog : 1d array-like Exogenous variable med :1d array-like Mediator variable endog : 1d array-like Endogenous variable Returns ------- indirect : dictionary Dictionary containing: (1) point estimate, (2) interval estimates """ indirect = {} # Standardize data if specified if 'standardize' in self.parameters: # Check if key exists rather than throw error if omitted if self.parameters['standardize']: exog, med, endog = self._standardize(exog = exog, med = med, endog = endog) # Priors if self.method == 'bayes-norm': # Mediator model: M ~ i_M + a*X i_M = pm.Normal('i_M', mu = 0, tau = 1e-10, value = 0) a = pm.Normal('a', mu = 0, tau = 1e-10, value = 0) # Endogenous model: Y ~ i_Y + c*X + b*M i_Y = pm.Normal('i_Y', mu = 0, tau = 1e-10, value = 0) c = pm.Normal('c', mu = 0, tau = 1e-10, value = 0) b = pm.Normal('b', mu = 0, tau = 1e-10, value = 0) else: # Mediator model: M ~ i_M + a*X i_M = pm.Cauchy('i_M', alpha = 0, beta = 10, value = 0) a = pm.Cauchy('a', alpha = 0, beta = 2.5, value = 0) # Endogenous model: Y ~ i_Y + c*X + b*M i_Y = pm.Cauchy('i_Y', alpha = 0, beta = 10, value = 0) c = pm.Cauchy('c', alpha = 0, beta = 2.5, value = 0) b = pm.Cauchy('b', alpha = 0, beta = 2.5, value = 0) # Expected values (linear combos) expected_med = i_M + a*exog expected_endog = i_Y + c*exog + b*med if self.mediator_type == 'continuous': tau_M = pm.Gamma('tau_M', alpha = .001, beta = .001, value = 1) response_M = pm.Normal('response_M', mu = expected_med, tau = tau_M, value = med, observed = True) med_model = [i_M, a, tau_M, response_M] else: p_M = pm.InvLogit('p_M', expected_med) response_M = pm.Bernoulli('response_M', value = med, p = p_M, observed = True) med_model = [i_M, a, response_M] if self.endogenous_type == 'continuous': tau_Y = pm.Gamma('tau_Y', alpha = .001, beta = .001, value = 1) response_Y = pm.Normal('response_Y', mu = expected_endog, tau = tau_Y, value = endog, observed = True) endog_model = [i_Y, b, c, tau_Y, response_Y] else: p_Y = pm.InvLogit('p_Y', expected_endog) response_Y = pm.Bernoulli('response_Y', value = endog, p = p_Y, observed = True) endog_model = [i_Y, b, c, response_Y] # Build MCMC mediator model and estimate model med_model = pm.Model(med_model) med_mcmc = pm.MCMC(med_model) # Specify samplers for mediator model (slice sampling) med_mcmc.use_step_method(pm.Slicer, a, w = 10, m = 10000, doubling = True) med_mcmc.use_step_method(pm.Slicer, i_M, w = 10, m = 10000, doubling = True) if self.mediator_type == 'continuous': med_mcmc.use_step_method(pm.Slicer, tau_M, w = 10, m = 10000, doubling = True) # Run multiple chains if specified for i in xrange(self.parameters['n_chains']): med_mcmc.sample(iter = self.parameters['iter'], burn = self.parameters['burn'], thin = self.parameters['thin'], progress_bar = False) # Build MCMC endogenous model and estimate model endog_model = pm.Model(endog_model) endog_mcmc = pm.MCMC(endog_model) # Specify samplers for mediator model (slice sampling) endog_mcmc.use_step_method(pm.Slicer, b, w = 10, m = 10000, doubling = True) endog_mcmc.use_step_method(pm.Slicer, c, w = 10, m = 10000, doubling = True) endog_mcmc.use_step_method(pm.Slicer, i_Y, w = 10, m = 10000, doubling = True) if self.mediator_type == 'continuous': endog_mcmc.use_step_method(pm.Slicer, tau_Y, w = 10, m = 10000, doubling = True) # Run multiple chains if specified for i in xrange(self.parameters['n_chains']): endog_mcmc.sample(iter = self.parameters['iter'], burn = self.parameters['burn'], thin = self.parameters['thin'], progress_bar = False) # Get posterior distribution of a and b then create indirect effect a_path = a.trace(chain = None) b_path = b.trace(chain = None) ab_estimates = a_path*b_path # If plotting distribution of ab if self.plot: self.ab_estimates = a_path*b_path # Save mcmc models if specified if 'save_models' in self.parameters: # Check if key exists rather than throw error if omitted if self.parameters['save_models']: self.med_mcmc = med_mcmc self.endog_mcmc = endog_mcmc # Point estimate if self.parameters['estimator'] == 'mean': indirect['point'] = np.mean(ab_estimates) else: indirect['point'] = np.median(ab_estimates) # Interval estimate if self.interval == 'cred': indirect['ci'] = self._boot_interval(ab_estimates = ab_estimates) else: indirect['ci'] = self._hpd_interval(ab_estimates = ab_estimates) return indirect
import pymc3 import mymodel S = pymc3.MCMC(mymodel, db='pickle') S.sample(iter=10000, burn=5000, thin=2) pymc3.Matplot.plot(S)
# A = pm.Normal('A', mu=0.0, tau=A_tau) # C = pm.Normal('C', mu=0.0, tau=C_tau) # t = pm.HalfNormal('t', tau=t_tau) @pm.Deterministic def y_mu(o=o, m=m, A=A, C=C, t=t): ret = A + t**m * xd**m + C * t**m * xd**m * np.cos(o * np.log(xd)) y_sd = pm.HalfNormal('y_sd', tau=1) y_tau = 1/y_sd**2 y_obs = pm.Normal('y_obs', mu=y_mu, tau=y_tau, value=yd, observed=True) model = pm.Model([y_obs, y_mu, y_sd, t, C, A, m, o]) mcmc = pm.MCMC(model) # MCMC not in pymc3. Use NUTS or HamiltonianMC mcmc.sample(iter=200000, burn=10000, thin=10) osamp = mcmc.trace('o')[:] msamp = mcmc.trace('m')[:] plt.figure(figsize=(10,10)) plt.plot(osamp, alpha=0.75, c='b') plt.plot(msamp, alpha=0.75, c='r') plt.figure(figsize=(10,10)) plt.scatter(xd, yd) fig = plt.figure(figsize=(10,10)) ax = fig.add_subplot(211) plt.hist(osamp, bins=25, alpha=0.5)