本文整理汇总了Python中pymc.rnormal函数的典型用法代码示例。如果您正苦于以下问题:Python rnormal函数的具体用法?Python rnormal怎么用?Python rnormal使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了rnormal函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: simple_hierarchical_data
def simple_hierarchical_data(n):
""" Generate data based on the simple one-way hierarchical model
given in section 3.1.1::
y[i,j] | alpha[j], sigma^2 ~ N(alpha[j], sigma^2) i = 1, ..., n_j, j = 1, ..., J;
alpha[j] | mu, tau^2 ~ N(mu, tau^2) j = 1, ..., J.
sigma^2 ~ Inv-Chi^2(5, 20)
mu ~ N(5, 5^2)
tau^2 ~ Inv-Chi^2(2, 10)
Parameters
----------
n : list, len(n) = J, n[j] = num observations in group j
"""
inv_sigma_sq = mc.rgamma(alpha=2.5, beta=50.0)
mu = mc.rnormal(mu=5.0, tau=5.0 ** -2.0)
inv_tau_sq = mc.rgamma(alpha=1.0, beta=10.0)
J = len(n)
alpha = mc.rnormal(mu=mu, tau=inv_tau_sq, size=J)
y = [mc.rnormal(mu=alpha[j], tau=inv_sigma_sq, size=n[j]) for j in range(J)]
mu_by_tau = mu * pl.sqrt(inv_tau_sq)
alpha_by_sigma = alpha * pl.sqrt(inv_sigma_sq)
alpha_bar = alpha.sum()
alpha_bar_by_sigma = alpha_bar * pl.sqrt(inv_sigma_sq)
return vars()
示例2: main
def main():
x_t = pm.rnormal(0, 1, 200)
x_t[0] = 0
y_t = np.zeros(200)
for i in range(1, 200):
y_t[i] = pm.rnormal(y_t[i - 1], 1)
plt.plot(y_t, label="$y_t$", lw=3)
plt.plot(x_t, label="$x_t$", lw=3)
plt.xlabel("time, $t$")
plt.legend()
plt.show()
colors = ["#348ABD", "#A60628", "#7A68A6"]
x = np.arange(1, 200)
plt.bar(x, autocorr(y_t)[1:], width=1, label="$y_t$",
edgecolor=colors[0], color=colors[0])
plt.bar(x, autocorr(x_t)[1:], width=1, label="$x_t$",
color=colors[1], edgecolor=colors[1])
plt.legend(title="Autocorrelation")
plt.ylabel("measured correlation \nbetween $y_t$ and $y_{t-k}$.")
plt.xlabel("k (lag)")
plt.title("Autocorrelation plot of $y_t$ and $x_t$ for differing $k$ lags.")
plt.show()
示例3: test_covariate_model_sim_no_hierarchy
def test_covariate_model_sim_no_hierarchy():
# simulate normal data
model = data.ModelData()
model.hierarchy, model.output_template = data_simulation.small_output()
X = mc.rnormal(0., 1.**2, size=(128,3))
beta_true = [-.1, .1, .2]
Y_true = pl.dot(X, beta_true)
pi_true = pl.exp(Y_true)
sigma_true = .01*pl.ones_like(pi_true)
p = mc.rnormal(pi_true, 1./sigma_true**2.)
model.input_data = pandas.DataFrame(dict(value=p, x_0=X[:,0], x_1=X[:,1], x_2=X[:,2]))
model.input_data['area'] = 'all'
model.input_data['sex'] = 'total'
model.input_data['year_start'] = 2000
model.input_data['year_end'] = 2000
# create model and priors
vars = {}
vars.update(covariate_model.mean_covariate_model('test', 1, model.input_data, {}, model, 'all', 'total', 'all'))
vars.update(rate_model.normal_model('test', vars['pi'], 0., p, sigma_true))
# fit model
m = mc.MCMC(vars)
m.sample(2)
示例4: logit_normal_draw
def logit_normal_draw(cf_mean, std, N, J):
std = pl.array(std)
if mc.__version__ == '2.0rc2': # version on Omak
X = [mc.invlogit(mc.rnormal(mu=cf_mean, tau=std**-2)) for n in range(N)]
Y = pl.array(X)
else:
X = mc.rnormal(mu=cf_mean, tau=std**-2, size=(N,J))
Y = mc.invlogit(X)
return Y
示例5: pred
def pred(a1=alpha1, mu_int=mu_int, tau_int=tau_int, mu_slope=mu_slope, tau_slope=tau_slope, tau_iq=tau_iq, values=(70,75,80,85)):
"""Estimate the probability of IQ<85 for different covariate values"""
b0 = rnormal(mu_int, tau_int, size=len(phe_pred))
a0 = rnormal(mu_slope, tau_slope, size=len(phe_pred))
b1 = a0 + a1*crit_pred
iq = rnormal(b0 + b1*phe_pred, tau_iq)
return [iq<v for v in values]
示例6: propose
def propose(self):
tau = 1./(self.adaptive_scale_factor * self.proposal_sd)**2
time = pymc.rnormal(self.stochastic.value.time, tau)
n = pymc.rnormal(len(self.stochastic.value), tau)
if n <= 0:
n = 0
times = [rand.random() for _ in range(n)]
total = float(sum(times))
times = [item*time/total for item in times]
events = [Event(time=item, censored=False) for item in times]
self.stochastic.value = MultiEvent(events)
示例7: step
def step(self):
x0 = self.value[self.n]
u = pm.rnormal(np.zeros(self.N), 1.)
dx = np.dot(u, self.value)
self.stochastic.value = x0
logp = [self.logp_plus_loglike]
x_prime = [x0]
for direction in [-1, 1]:
for i in xrange(25):
delta = direction*np.exp(.1*i)*dx
try:
self.stochastic.value = x0 + delta
logp.append(self.logp_plus_loglike)
x_prime.append(x0 + delta)
except pm.ZeroProbability:
self.stochastic.value = x0
i = pm.rcategorical(np.exp(np.array(logp) - pm.flib.logsum(logp)))
self.value[self.n] = x_prime[i]
self.stochastic.value = x_prime[i]
if i == 0:
self.rejected += 1
else:
self.accepted += 1
self.n += 1
if self.n == self.N:
self.n = 0
示例8: main
def main():
""" Demonstrating thinning of two autocorrelated inputs (representing
posterior probabilities). The key point is the thinned - every 2nd / 3rd
point - functions approach zero quicker. More thinning is better (but
expensive)
"""
# x_t = pm.rnormal(0, 1, 200)
# x_t[0] = 0
y_t = np.zeros(200)
for i in range(1, 200):
y_t[i] = pm.rnormal(y_t[i - 1], 1)
max_x = 200 / 3 + 1
x = np.arange(1, max_x)
colors = ["#348ABD", "#A60628", "#7A68A6"]
plt.bar(x, autocorr(y_t)[1:max_x], edgecolor=colors[0],
label="no thinning", color=colors[0], width=1)
plt.bar(x, autocorr(y_t[::2])[1:max_x], edgecolor=colors[1],
label="keeping every 2nd sample", color=colors[1], width=1)
plt.bar(x, autocorr(y_t[::3])[1:max_x], width=1, edgecolor=colors[2],
label="keeping every 3rd sample", color=colors[2])
plt.autoscale(tight=True)
plt.legend(title="Autocorrelation plot for $y_t$", loc="lower left")
plt.ylabel("measured correlation \nbetween $y_t$ and $y_{t-k}$.")
plt.xlabel("k (lag)")
plt.title("Autocorrelation of $y_t$ (no thinning vs. thinning) \
at differing $k$ lags.")
plt.show()
示例9: step
def step(self):
x0 = np.copy(self.stochastic.value)
dx = pymc.rnormal(np.zeros(np.shape(x0)), self.proposal_tau)
logp = [self.logp_plus_loglike]
x_prime = [x0]
for direction in [-1, 1]:
for i in xrange(25):
delta = direction*np.exp(.1*i)*dx
try:
self.stochastic.value = x0 + delta
logp.append(self.logp_plus_loglike)
x_prime.append(x0 + delta)
except pymc.ZeroProbability:
self.stochastic.value = x0
i = pymc.rcategorical(np.exp(np.array(logp) - pymc.flib.logsum(logp)))
self.stochastic.value = x_prime[i]
if i == 0:
self.rejected += 1
if self.verbose > 2:
print self._id + ' rejecting'
else:
self.accepted += 1
if self.verbose > 2:
print self._id + ' accepting'
示例10: test_random_effect_priors
def test_random_effect_priors():
model = data.ModelData()
# set prior on sex
parameters = dict(random_effects={'USA': dict(dist='TruncatedNormal', mu=.1, sigma=.5, lower=-10, upper=10)})
# simulate normal data
n = 32.
area_list = pl.array(['all', 'USA', 'CAN'])
area = area_list[mc.rcategorical([.3, .3, .4], n)]
alpha_true = dict(all=0., USA=.1, CAN=-.2)
pi_true = pl.exp([alpha_true[a] for a in area])
sigma_true = .05
p = mc.rnormal(pi_true, 1./sigma_true**2.)
model.input_data = pandas.DataFrame(dict(value=p, area=area))
model.input_data['sex'] = 'male'
model.input_data['year_start'] = 2010
model.input_data['year_end'] = 2010
model.hierarchy.add_edge('all', 'USA')
model.hierarchy.add_edge('all', 'CAN')
# create model and priors
vars = {}
vars.update(covariate_model.mean_covariate_model('test', 1, model.input_data, parameters, model,
'all', 'total', 'all'))
print vars['alpha']
print vars['alpha'][1].parents['mu']
assert vars['alpha'][1].parents['mu'] == .1
示例11: plot_funnel
def plot_funnel(pi_true, delta_str):
delta = float(delta_str)
n = pl.exp(mc.rnormal(10, 2**-2, size=10000))
p = pi_true*pl.ones_like(n)
# old way:
#delta = delta * p * n
nb = rate_model.neg_binom_model('funnel', pi_true, delta, p, n)
r = nb['p_pred'].value
pl.vlines([pi_true], .1*n.min(), 10*n.max(),
linewidth=5, linestyle='--', color='black', zorder=10)
pl.plot(r, n, 'o', color=colors[0], ms=10,
mew=0, alpha=.25)
pl.semilogy(schiz['r'], schiz['n'], 's', mew=1, mec='white', ms=15,
color=colors[1],
label='Observed Values')
pl.xlabel('Rate (Per 1000 PY)', size=32)
pl.ylabel('Study Size (PY)', size=32)
pl.axis([-.0001, .0101, 50., 15000000])
pl.title(r'$\delta = %s$'%delta_str, size=48)
pl.xticks([0, .005, .01], [0, 5, 10], size=30)
pl.yticks(size=30)
示例12: test_fixed_effect_priors
def test_fixed_effect_priors():
model = data.ModelData()
# set prior on sex
parameters = dict(fixed_effects={'x_sex': dict(dist='TruncatedNormal', mu=1., sigma=.5, lower=-10, upper=10)})
# simulate normal data
n = 32.
sex_list = pl.array(['male', 'female', 'total'])
sex = sex_list[mc.rcategorical([.3, .3, .4], n)]
beta_true = dict(male=-1., total=0., female=1.)
pi_true = pl.exp([beta_true[s] for s in sex])
sigma_true = .05
p = mc.rnormal(pi_true, 1./sigma_true**2.)
model.input_data = pandas.DataFrame(dict(value=p, sex=sex))
model.input_data['area'] = 'all'
model.input_data['year_start'] = 2010
model.input_data['year_start'] = 2010
# create model and priors
vars = {}
vars.update(covariate_model.mean_covariate_model('test', 1, model.input_data, parameters, model,
'all', 'total', 'all'))
print vars['beta']
assert vars['beta'][0].parents['mu'] == 1.
示例13: __init__
def __init__(self, stochastic, proposal_sd=None, verbose=None):
pm.Metropolis.__init__(self, stochastic, proposal_sd=proposal_sd,
verbose=verbose, tally=False)
self.proposal_tau = self.proposal_sd**-2.
self.n = 0
self.N = 11
self.value = pm.rnormal(self.stochastic.value, self.proposal_tau, size=tuple([self.N] + list(self.stochastic.value.shape)))
示例14: sim_data
def sim_data(N, true_cf=[[.3, .6, .1],
[.3, .5, .2]],
true_std=[[.2, .05, .05],
[.3, 0.1, 0.1]],
sum_to_one=True):
"""
Create an NxTxJ matrix of simulated data (T is determined by the length
of true_cf, J by the length of the elements of true_cf).
true_cf - a list of lists of true cause fractions (each must sum to one)
true_std - a list of lists of the standard deviations corresponding to the true csmf's
for each time point. Can either be a list of length J inside a list of length
1 (in this case, the same standard deviation is used for all time points) or
can be T lists of length J (in this case, the a separate standard deviation
is specified and used for each time point).
"""
if sum_to_one == True:
assert pl.allclose(pl.sum(true_cf, 1), 1), 'The sum of elements of true_cf must equal 1'
T = len(true_cf)
J = len(true_cf[0])
## if only one std provided, duplicate for all time points
if len(true_std)==1 and len(true_cf)>1:
true_std = [true_std[0] for i in range(len(true_cf))]
## transform the mean and std to logit space
transformed_std = []
for t in range(T):
pi_i = pl.array(true_cf[t])
sigma_pi_i = pl.array(true_std[t])
transformed_std.append( ((1/(pi_i*(pi_i-1)))**2 * sigma_pi_i**2)**0.5 )
## find minimum standard deviation (by cause across time) and draw from this
min = pl.array(transformed_std).min(0)
common_perturbation = [pl.ones([T,J])*mc.rnormal(mu=0, tau=min**-2) for n in range(N)]
## draw from remaining variation
tau=pl.array(transformed_std)**2 - min**2
tau[tau==0] = 0.000001
additional_perturbation = [[mc.rnormal(mu=0, tau=tau[t]**-1) for t in range(T)] for n in range(N)]
result = pl.zeros([N, T, J])
for n in range(N):
result[n, :, :] = [mc.invlogit(mc.logit(true_cf[t]) + common_perturbation[n][t] + additional_perturbation[n][t]) for t in range(T)]
return result
示例15: test_log_normal_model_sim
def test_log_normal_model_sim(N=16):
# simulate negative binomial data
pi_true = 2.
sigma_true = .1
n = pl.array(pl.exp(mc.rnormal(10, 1**-2, size=N)), dtype=int)
p = pl.exp(mc.rnormal(pl.log(pi_true), 1./(sigma_true**2 + 1./n), size=N))
# create model and priors
vars = dict(mu_age=mc.Uniform('mu_age', 0., 1000., value=.01),
sigma=mc.Uniform('sigma', 0., 10000., value=1000.))
vars['mu_interval'] = mc.Lambda('mu_interval', lambda mu=vars['mu_age']: mu*pl.ones(N))
vars.update(rate_model.log_normal_model('sim', vars['mu_interval'], vars['sigma'], p, 1./pl.sqrt(n)))
# fit model
m = mc.MCMC(vars)
m.sample(1)