def negative_b_mcm_model(p_df): n_mu = pm.Normal('n_mu', mu=1650, tau=0.00001) n_lam = pm.Uniform('n_uni_alpha', 0, 1) n_alpha = pm.Exponential('n_alpha', beta=n_lam) n_ob = pm.NegativeBinomial('n_observed', mu=n_mu, alpha=n_alpha, value=p_df, observed=True) n_es = pm.NegativeBinomial('n_estimated', mu=n_mu, alpha=n_alpha, observed=False) n_model = pm.Model([n_mu, n_lam, n_alpha, n_ob, n_es]) return n_mu, n_lam, n_alpha, n_ob, n_es, n_model
def getposterior(values): print(values) if numpy.min(values) != numpy.max(values): mu = pymc.Uniform('mu', lower=numpy.min(values), upper=numpy.max(values)) tau = pymc.Uniform('tau', lower=0.01, upper=10) y = pymc.NegativeBinomial('y', mu, tau, value=values, observed=True) model = pymc.MCMC([y, mu, tau]) model.sample(iter=10000, burn=5000, verbose=0) return (model) return (False)
def test_set_initval(): # Make sure the dependencies between variables are maintained when # generating initial values rng = np.random.RandomState(392) with pm.Model(rng_seeder=rng) as model: eta = pm.Uniform("eta", 1.0, 2.0, size=(1, 1)) mu = pm.Normal("mu", sd=eta, initval=[[100]]) alpha = pm.HalfNormal("alpha", initval=100) value = pm.NegativeBinomial("value", mu=mu, alpha=alpha) assert np.array_equal(model.initial_values[mu], np.array([[100.0]])) np.testing.assert_array_equal(model.initial_values[alpha], np.array(100)) assert model.initial_values[value] is None # `Flat` cannot be sampled, so let's make sure that doesn't break initial # value computations with pm.Model() as model: x = pm.Flat("x") y = pm.Normal("y", x, 1) assert y in model.initial_values
## impute zeros m = numpy.ma.masked_where(g == 0, g).min(axis=0).min(axis=0).data / 2 for i in xrange(g.shape[2]): mask = g[:, :, i] == 0 numpy.place(g[:, :, i], mask, m[i]) mu = (g[libidxs].T * libsizes_hat).T # pt I # estimate overdispersion # var == mu + t0 * mu + t1 * mu ** 2 theta0 = pymc.Exponential("theta0", 1. / 10, trace=True) theta1 = pymc.Exponential("theta1", 1. / 10, trace=True) alpha = mu / (mu * theta1 + theta0) dnb = pymc.NegativeBinomial("dnb", mu, alpha, observed=True, value=d) # manual init. theta0.value = 0.5 theta1.value = 0.5 inp = (dnb, alpha, theta0, theta1) mcmc = pymc.MCMC(input=inp) NBURN = 20000 mcmc.sample(25000 + NBURN, NBURN, thin=10) print("") #t0 = numpy.median(theta0.trace()[:]) #t1 = numpy.median(theta1.trace()[:]) t0 = scipy.stats.mstats.mquantiles(theta0.trace(), [0.667])[0] t1 = scipy.stats.mstats.mquantiles(theta1.trace(), [0.667])[0]
for i in xrange(Cm.shape[0]): for j in xrange(Cm.shape[1]): Cm[i, j] = pymc.Exponential("Cm_{%d,%d}" % (i, j), 0.001, trace=False) Cm = pymc.ArrayContainer(Cm) Cmc = numpy.empty(shape=Fmc.shape + (2, ), dtype="O") for i in xrange(Cmc.shape[0]): for j in xrange(Cmc.shape[1]): condidx = conds[i][1] cg = d[condidx, j] for k in xrange(Cmc.shape[2]): if k == 0: Cmc[i, j, k] = pymc.NegativeBinomial( "Cmc_{%d,%d,%d}" % (i, j, k), mu=Cm[i, j] * Fmc[i, j] * nf[condidx], alpha=alphamc, value=cg[:, k], observed=True, trace=False) else: assert k == 1 Cmc[i, j, k] = pymc.NegativeBinomial( "Cmc_{%d,%d,%d}" % (i, j, k), mu=Cm[i, j] * (1 - Fmc[i, j]) * nf[condidx], alpha=alphamc, value=cg[:, k], observed=True, trace=False) Cmc = pymc.ArrayContainer(Cmc) # interested in cond Cy73 vs others
bins=100, histtype='stepfilled', color=colors[1]) plt.xlabel('No of accidents') plt.ylabel('Frequency') plt.title('Distribution of observed data') plt.tight_layout() plt.show() n_mu = pm.Normal('n_mu', mu=1650, tau=0.00001) n_lam = pm.Uniform('n_uni_alpha', 0, 1) n_alpha = pm.Exponential('n_alpha', beta=n_lam) n_y_obs = pm.NegativeBinomial('n_observed', mu=mu, alpha=n_alpha, value=accidents, observed=True) n_y_pre = pm.NegativeBinomial('n_estimated', mu=mu, alpha=n_alpha, observed=False) n_model = pm.Model([n_mu, n_lam, n_alpha, n_y_obs, n_y_pre]) n_mcmc = pm.MCMC(n_model) mc_res = n_mcmc.sample(M, 100) print('MLE:', freq_results['x'], 'MCMC(Poisson):', mcmc.trace('mu')[:].mean(), 'MCMC(Negative binominal)', n_mcmc.trace('mu')[:].mean())
def make_model(recs, curve_sub, curve_params=[], pr_type='mixed', pr_hists=None, pr_samps=None, check_inflec=True): input_dict = curve_sub(*curve_params) arfun = input_dict['arfun'] fun_params = input_dict['fun_params'] # if pr_type=='unknown': # splreps = [] # for i in xrange(len(pr_hists)): # where_ok = np.where(pr_hists[i][0]>0) # pr_mesh = pr_hists[i][1][where_ok] # lp_mesh = np.log(pr_hists[i][0][where_ok]) # splreps.append(UnivariateSpline(pr_mesh, lp_mesh, bbox=[0,1])) # # @pm.stochastic(dtype=float) # def pr(value = pr_hists[:,1,10], splreps = splreps): # out=0 # for i in xrange(len(value)): # this_value = value[i] # if this_value<0 or this_value>1: # return -np.inf # else: # out += splreps[i](this_value) # return out if pr_type == 'model_exp': pr = recs.mbg_pr elif pr_type == 'data': pr = recs.pr elif pr_type == 'mixed': pr = recs.mix_pr elif pr_type == 'data_untrans': pr = recs.pfpr else: raise ValueError, 'PR type unknown' # # A deterministic that measures the change in attack rate given a certain change in PR. # delta_ar = pm.Lambda('delta_ar', lambda fp = fun_params: np.diff(arfun(diff_pts, *fp))) fboth = pm.Lambda( 'fboth', lambda fp=fun_params, pr=pr: arfun(np.hstack((pr, xplot)), *fp)) # Evaluation of trend at PR values AR_trend = pm.Lambda('AR_trend', lambda fp=fun_params, pr=pr: arfun(pr, *fp)) # The function evaluated on the display mesh fplot = pm.Lambda('fplot', lambda fp=fun_params: arfun(xplot, *fp)) pl.clf() pl.plot(xplot, fplot.value) @pm.potential def check_trend(AR=AR_trend, f=fplot): if np.any(AR <= 0) or np.any(f <= 0): return -np.Inf if check_inflec: d2 = np.diff(f, 2) d2 = d2[np.where(np.abs(d2) > 1e-6)] chgs = np.where(np.abs(np.diff(np.sign(d2))) > 1)[0] if np.diff(f[-3:], 2) > 0 or len(chgs) > 1: return -np.Inf return 0 # Negative-binomial parameters. r_int = pm.Exponential('r_int', .0001, value=.3) r_lin = pm.Uninformative('r_lin', value=1.) r_quad = pm.Uninformative('r_quad', value=.1) rplot = pm.Lambda('rplot', lambda r_int=r_int, r_lin=r_lin, r_quad=r_quad: r_int + r_lin * xplot + r_quad * xplot**2) @pm.potential def check_r(i=r_int, l=r_lin, q=r_quad): # if q>0: # xhat = -l / 2 / q # if i + l*xhat + q*xhat*xhat <= 0 and xhat>0: # return -np.Inf if l <= 0 or l + 2. * q <= 0: return -np.Inf if i + l + q <= 0 or i < 0: return -np.Inf return 0 # shape parameter of gamma process is multiplied by total survey time time_scale_fac = time_scaling(recs.pcd, recs.surv_int) tottime = (recs.yr_end - recs.yr_start + 1) scale_time = tottime / time_scale_fac pop = recs.pyor / tottime # Shape parameter of Poisson intensity is only multiplied by scaled survey time. r = pm.Lambda('r', lambda i=r_int, l=r_lin, q=r_quad, pr=pr: (i + l * pr + q * pr * pr) * scale_time) # scale parameter of Poisson intensity is multiplied by scaled survey time * number of people sampled. exp_rate = pm.Lambda('exp_rate', lambda t=AR_trend: scale_time * pop * t) # The data AR = pm.NegativeBinomial('AR', exp_rate, r, value=recs.cases, observed=True) @pm.deterministic(dtype=float) def AR_dev(AR=AR, mu=exp_rate, r=r): return np.array([ pm.negative_binomial_like(AR[i], mu[i], r[i]) for i in xrange(len(AR)) ]) out = locals() out.update(input_dict) return out