include_only = range(0, n_predictors) # default is to include all #x = x.iloc[include_only] predictor_names = x.columns n_predictors = len(predictor_names) # THE MODEL with pm.Model() as model: # define hyperpriors muB = pm.Normal('muB', 0, .100) tauB = pm.Gamma('tauB', .01, .01) udfB = pm.Uniform('udfB', 0, 1) tdfB = 1 + tdfBgain * (-pm.log(1 - udfB)) # define the priors tau = pm.Gamma('tau', 0.01, 0.01) beta0 = pm.Normal('beta0', mu=0, tau=1.0E-12) beta1 = pm.T('beta1', mu=muB, lam=tauB, nu=tdfB, shape=n_predictors) mu = beta0 + pm.dot(beta1, x.values.T) # define the likelihood #mu = beta0 + beta1[0] * x.values[:,0] + beta1[1] * x.values[:,1] yl = pm.Normal('yl', mu=mu, tau=tau, observed=y) # Generate a MCMC chain start = pm.find_MAP() step1 = pm.NUTS([beta1]) step2 = pm.Metropolis([beta0, tau, muB, tauB, udfB]) trace = pm.sample(10000, [step1, step2], start, progressbar=False) # EXAMINE THE RESULTS burnin = 2000 thin = 1 # Print summary for each trace
zx = (x - x_m) / x_sd zy = (y - y_m) / y_sd tdf_gain = 100 # 1 for low-baised tdf, 100 for high-biased tdf # THE MODEL with pm.Model() as model: # define the priors beta0 = pm.Normal('beta0', mu=0, tau=1.0E-12) beta1 = pm.Normal('beta1', mu=0, tau=1.0E-12) tau = pm.Gamma('tau', 0.001, 0.001) udf = pm.Uniform('udf', 0, 1) tdf = 1 - tdf_gain * pm.log(1 - udf) # tdf in [1,Inf). # define the likelihood mu = beta0 + beta1 * zx yl = pm.T('yl', mu=mu, lam=tau, nu=tdf, observed=zy) # Generate a MCMC chain start = pm.find_MAP() step = [pm.Metropolis([rv]) for rv in model.unobserved_RVs] trace = pm.sample(10000, step, start, progressbar=False) # EXAMINE THE RESULTS burnin = 1000 thin = 10 ## Print summary for each trace #pm.summary(trace[burnin::thin]) #pm.summary(trace) ## Check for mixing and autocorrelation #pm.autocorrplot(trace[burnin::thin], vars =[tau])
NxLvl = len(set(x)) # # Construct list of all pairwise comparisons, to compare with NHST TukeyHSD: contrast_dict = None for g1idx in range(NxLvl): for g2idx in range(g1idx + 1, NxLvl): cmpVec = np.repeat(0, NxLvl) cmpVec[g1idx] = -1 cmpVec[g2idx] = 1 contrast_dict = (contrast_dict, cmpVec) z = (y - np.mean(y)) / np.std(y) ## THE MODEL. with pm.Model() as model: # define the hyperpriors a_SD_unabs = pm.T('a_SD_unabs', mu=0, lam=0.001, nu=1) a_SD = abs(a_SD_unabs) + 0.1 atau = 1 / a_SD**2 m = pm.Gamma('m', 1, 1) d = pm.Gamma('d', 1, 1) sG = m**2 / d**2 rG = m / d**2 # define the priors sigma = pm.Uniform('sigma', 0, 10) # y values are assumed to be standardized tau = pm.Gamma('tau', sG, rG) a0 = pm.Normal('a0', mu=0, tau=0.001) # y values are assumed to be standardized a = pm.Normal('a', mu=0, tau=atau, shape=NxLvl) mu = a0 + a # define the likelihood
returns.plot(title='return of NIKKEI index close price', figsize=(30, 8)) nreturns = np.array(returns[1:])[::-1] import pymc as pm from pymc.distributions.timeseries import GaussianRandomWalk from scipy.sparse import csc_matrix from scipy import optimize with pm.Model() as model: sigma, log_sigma = model.TransformedVar( 'sigma', pm.Exponential.dist(1. / .02, testval=.1), pm.logtransform) nu = pm.Exponential('nu', 1. / 10) s = GaussianRandomWalk('s', sigma**-2, shape=len(nreturns)) r = pm.T('r', nu, lam=pm.exp(-2 * s), observed=nreturns) with model: start = pm.find_MAP(vars=[s], fmin=optimize.fmin_l_bfgs_b) step = pm.NUTS(scaling=start) trace = pm.sample(2000, step, start, progressbar=False) plt.plot(trace[s][::10].T, 'b', alpha=.03) plt.title('log volatility') with model: pm.traceplot(trace, model.vars[:2]) exps = np.exp(trace[s][::10].T) plt.plot(returns[:600][::-1]) plt.plot(exps, 'r', alpha=.03)