本文整理汇总了Python中pystan.stan函数的典型用法代码示例。如果您正苦于以下问题:Python stan函数的具体用法?Python stan怎么用?Python stan使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了stan函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_specify_args
def test_specify_args(self):
y = (0.70, -0.16, 0.77, -1.37, -1.99, 1.35, 0.08,
0.02, -1.48, -0.08, 0.34, 0.03, -0.42, 0.87,
-1.36, 1.43, 0.80, -0.48, -1.61, -1.27)
code = """
data {
real y[20];
}
parameters {
real mu;
real<lower=0> sigma;
}
model {
y ~ normal(mu, sigma);
}"""
stepsize0 = 0.15
sf = stan(model_code=code, data=dict(y=y), iter=200,
control=dict(adapt_engaged=False, stepsize=stepsize0))
self.assertEqual(sf.get_sampler_params()[0]['stepsize__'][0], stepsize0)
sf2 = stan(fit=sf, iter=20, algorithm='HMC', data=dict(y=y),
control=dict(adapt_engaged=False, stepsize=stepsize0))
self.assertEqual(sf2.get_sampler_params()[0]['stepsize__'][0], stepsize0)
sf3 = stan(fit=sf, iter=1, data=list(y=y), init=0, chains=1)
i_u = sf3.unconstrain_pars(sf3.get_inits[0])
self.assertEqual(i_u, [0, 0])
示例2: test_user_init
def test_user_init(self):
model_code = self.model_code
data = self.data
beta = np.ones((data['K'], data['D']))
fit1 = pystan.stan(model_code=model_code, iter=10, chains=1, seed=2,
data=data, init=[dict(beta=beta)], warmup=0)
np.testing.assert_equal(fit1.get_inits()[0]['beta'], beta)
beta = 5 * np.ones((data['K'], data['D']))
fit2 = pystan.stan(model_code=model_code, iter=10, chains=1, seed=2,
data=data, init=[dict(beta=beta)], warmup=0)
np.testing.assert_equal(fit2.get_inits()[0]['beta'], beta)
示例3: _initialize
def _initialize(self, xs):
print("The following message exists as Stan instantiates the model.")
if hasattr(self, 'file'):
self.model = pystan.stan(file=self.file,
data=xs, iter=1, chains=1)
else:
self.model = pystan.stan(model_code=self.model_code,
data=xs, iter=1, chains=1)
self.num_vars = sum([sum(dim) if sum(dim) != 0 else 1
for dim in self.model.par_dims])
self.flag_init = True
示例4: setUpClass
def setUpClass(cls):
np.random.seed(1)
n = 10000
p = 3
cls.beta_true = beta_true = (1, 3, 5)
X = np.random.normal(size=(n, p))
X = (X - np.mean(X, axis=0)) / np.std(X, ddof=1, axis=0, keepdims=True)
y = np.dot(X, beta_true) + np.random.normal(size=n)
model_code = """
data {
int<lower=0> N;
int<lower=0> p;
matrix[N,p] x;
vector[N] y;
}
parameters {
vector[p] beta;
real<lower=0> sigma;
}
model {
y ~ normal(x * beta, sigma);
}
"""
data = {'N': n, 'p': p, 'x': X, 'y': y}
cls.fit = pystan.stan(model_code=model_code, data=data, iter=500)
示例5: test_stan_file
def test_stan_file(self):
schools_code = self.schools_code
schools_dat = self.schools_dat
with tempfile.NamedTemporaryFile(delete=False) as f:
f.write(schools_code.encode('utf-8'))
fit = pystan.stan(file=f.name, data=schools_dat, iter=1000, chains=4)
validate_data(fit)
示例6: fit
def fit():
data = make_data()
model = pystan.stan(model_code=stan_code, data=data,
iter=1000, chains=5)
#model.plot()
#plt.show()
return model
示例7: test_stan_args_basic
def test_stan_args_basic(self):
y = np.array([0.70, -0.16, 0.77, -1.37, -1.99, 1.35, 0.08,
0.02, -1.48, -0.08, 0.34, 0.03, -0.42, 0.87,
-1.36, 1.43, 0.80, -0.48, -1.61, -1.27])
code = '''
data {
real y[20];
}
parameters {
real mu;
real<lower=0> sigma;
}
model {
y ~ normal(mu, sigma);
}'''
sf = pystan.stan(model_code=code, iter=10, thin=3, data=dict(y=y))
args = sf.stan_args[0]
self.assertEqual(args['iter'], 10)
self.assertEqual(args['thin'], 3)
self.assertEqual(args['init'], b'random')
sampling = args['ctrl']['sampling']
self.assertEqual(sampling['adapt_engaged'], True)
self.assertEqual(sampling['adapt_window'], 25)
self.assertEqual(sampling['adapt_init_buffer'], 75)
self.assertEqual(sampling['adapt_gamma'], 0.05)
self.assertEqual(sampling['adapt_delta'], 0.8)
self.assertEqual(sampling['adapt_kappa'], 0.75)
self.assertEqual(sampling['adapt_t0'], 10)
示例8: fit
def fit(model_code, *args, **kwargs):
"""
Fit a Stan model. Caches the compiled model.
*args and **kwargs are passed to the pystan.stan function.
Arguments you most likely want to pass: data, init, iter, chains.
Unlike pystan.stan, if the n_jobs kwarg is not specified, it defaults to
-1.
Parameters
-------------------
model_code : string
Stan model
Returns
-------------------
pystan StanFit4Model instance : the fit model
"""
kwargs = dict(kwargs)
kwargs['model_code'] = model_code
if 'n_jobs' not in kwargs:
kwargs['n_jobs'] = -1
if model_code in FIT_CACHE:
print("Reusing model.")
kwargs['fit'] = FIT_CACHE[model_code]
else:
print("NOT reusing model.")
start = time.time()
FIT_CACHE[model_code] = pystan.stan(*args, **kwargs)
print("Ran in %0.3f sec." % (time.time() - start))
return FIT_CACHE[model_code]
示例9: run_stan
def run_stan(stan_data, target):
project_dir = os.path.join(os.environ['HOME'], 'Projects', 'Kaggle', 'otto')
data_dir = os.path.join(project_dir, 'data')
iguesses = get_initial_values(stan_data['counts'])
stan_file = os.path.join(project_dir, 'stan', 'single_component.stan')
fit = pystan.stan(file=stan_file, model_name='single_component_' + target, data=stan_data,
chains=4, iter=400, warmup=200, init=iguesses)
# dump the MCMC samples to an HDF file
samples = fit.extract()
cnames = ['concentration', 'negbin_nfailures']
nbins = stan_data['counts'].shape[1]
cnames += ['bin_prob_' + str(i + 1) for i in range(nbins)]
raw_samples = np.column_stack((samples['concentration'], samples['negbin_nfailures'], samples['bin_probs']))
samples = pd.DataFrame(data=raw_samples, columns=cnames)
samples.to_hdf(os.path.join(data_dir, 'single_component_' + target + '_samples.h5'), 'df')
# dump the stan results to a text file
with open(os.path.join(data_dir, 'single_component_' + target + '_stan_summary.txt'), 'w') as f:
print >> f, fit
# make plots of the stan results
plot_dir = os.path.join(project_dir, 'plots')
fit.plot()
plt.savefig(os.path.join(plot_dir, 'single_component_' + target + '_trace.png'))
return
示例10: main
def main():
# data
J = 8
data_y = np.array([28, 8, -3, 7, -1, 1, 18, 12])
data_sigma = np.array([15, 10, 16, 11, 9, 11, 10, 18])
standata = dict(J=J, y=data_y, sigma=data_sigma)
fit = pystan.stan('eight_schools.stan', data=standata, iter=100000)
print(fit)
示例11: gamma_tutorial
def gamma_tutorial():
'''
Basic stan gamma GLM with log link function
Taken from http://seananderson.ca/2014/04/08/gamma-glms.html
:return:
'''
# define stan data
N = 100
x = np.random.uniform(-1, 1, N)
a = 0.5
b = 1.2
y_true = np.exp(a + b * x)
shape = 10.0
# Passing a vector for scale generates one sample per value in vector; |y_true| = 100 => |y| = 100
# note that scale is generated in odd fashion (w/ shape parameter), hence odd shape parameter in stan model
y = np.random.gamma(shape, scale=y_true/shape)
# Now we put data into a dictionary so we can give to stan
this_data = {
'N': N,
'x': x,
'y': y
}
# define stan model
stan_code = """
data {
int<lower=0> N;
vector[N] x;
vector[N] y;
}
parameters {
real a;
real b;
real<lower=0.001,upper=100> shape;
}
model {
a ~ normal(0,100);
b ~ normal(0,100);
for(i in 1:N)
y[i] ~ gamma(shape, (shape / exp(a + b * x[i])));
}
"""
# fit model
fit = pystan.stan(model_code = stan_code, data=this_data, iter=1000, chains=4, thin=3)
'''
Note that the following two statements are equivalent:
y ~ normal(alpha + beta * x, sigma)
for (n in 1:N):
y[n] ~ normal(alpha + beta * x[n], sigma)
...meaning that stan automatically vectorizes
'''
"""
示例12: main
def main():
# Load the data for the toy model
ids, x, y, xerr, yerr, pxy = np.loadtxt("../data/hogg-toy-model.data", unpack=True)
with open("models/mixture-model-with-uncertainties.stan", "r") as fp:
model_code = fp.read()
# Fit the model
fit = pystan.stan(model_code=model_code, iter=10000, chains=8,
data={
"x_measured": x, "x_uncertainty": xerr,
"y_measured": y, "y_uncertainty": yerr,
"N": len(x)
})
print(fit)
fit.traceplot()
samples = fit.extract(permuted=True)
parameters = pd.DataFrame({"m": samples["m"], "b": samples["b"]})
# Predictive model
pred_x = np.arange(0, 300)
model = lambda theta: pd.Series({"fitted": theta[0] + theta[1] * pred_x})
median_parameters = parameters.median()
yhat = model(median_parameters)
# get the predicted values for each chain
chain_predictions = parameters.apply(model, axis=1)
fig = plt.figure()
ax = fig.add_subplot(111)
num_chains = 50
indices = np.random.choice(300, num_chains)
for i, index in enumerate(indices):
ax.plot(pred_x, chain_predictions.iloc[index, 0], color="lightgrey")
# data
ax.errorbar(x, y, xerr=xerr, yerr=yerr, fmt=None, facecolor="k", ecolor="k", zorder=10)
ax.plot(x, y, 'ko', zorder=10)
# fitted values
ax.plot(pred_x, yhat["fitted"], "k", lw=2)
# supplementals
ax.set_xlim(0, 300)
ax.set_ylim(0, 750)
ax.set_xlabel('X')
ax.set_ylabel('Y')
plt.show()
示例13: gamma_glm
def gamma_glm(data):
'''
Gamma GLM for use in bayesian class project
input: data - dictionary of data for stan model
:return:
'''
# if you want to make this better/run more expts, dynamically generate some of this model.
# presently, assume flat priors for all parameters
stan_data = data[0]
stan_model = """
data {
int<lower=0> N; // number of samples
int<lower=0> N_test;
int<lower=0> K; // length of each X vector
matrix[N,K] x;
vector[N] y;
matrix[N_test,K] x_test; // samples for testing
}
parameters {
real<lower=.001> beta_0;
vector<lower=.001>[K] beta;
real<lower=.001> phi;
real<lower=.001> alpha;
}
model {
alpha ~ normal(20,10);
for (k in 1:K)
beta[k] ~ normal(0,1000);
beta_0 ~ normal(10000,1000);
phi ~ normal(0,2);
for (k in 1:K)
y ~ gamma(alpha, inv(x[k] * beta + beta_0) + phi); // inverse-link
}
generated quantities { // predictions!
vector[N_test] y_test;
for (n in 1:N_test)
y_test[n] <- gamma_rng(alpha, inv(x_test[n] * beta + beta_0) + phi);
}
"""
# fit model
fit = pystan.stan(model_code=stan_model, data=stan_data, iter=2000, chains=4, thin=1)
print "Model:"
print fit
eval_acc(fit, data)
示例14: test_user_initfun_chainid
def test_user_initfun_chainid(self):
model_code = self.model_code
data = self.data
def make_inits(chain_id):
return dict(mu=chain_id)
chain_id = [9, 10, 11, 12]
fit1 = pystan.stan(model_code=model_code, iter=10, chains=4, seed=2,
data=data, init=make_inits, warmup=0, chain_id=chain_id)
for i, inits in zip(chain_id, fit1.get_inits()):
self.assertEqual(inits['mu'], i)
示例15: get_basic_model_fit
def get_basic_model_fit(prior_params, num_iters, num_chains, init_f, seed, the_type, data):
"""
convention is to return by permuted and unpermuted traces
"""
import problem_props.constants as constants
import pystan
stan_file = constants.basic_model_stan_file
stan_data = data_to_basic_model_pystan_input(data)
other_inputs = {\
'n_types': max(stan_data['types']),\
'n_props': len(data),\
'n_events': len(stan_data['types']),\
'the_type': the_type + 1\
}
all_stan_data = dict(prior_params.items() + stan_data.items() + other_inputs.items())
if init_f == None:
fit = pystan.stan(file = stan_file, data = all_stan_data, seed = seed, iter = num_iters, chains = num_chains, verbose = True)
else:
init_d = [init_f(data, i) for i in range(num_chains)]
fit = pystan.stan(file = stan_file, data = all_stan_data, seed = seed, iter = num_iters, chains = num_chains, verbose = True, init = init_d)
return fit.extract(permuted=True), fit.extract(permuted=False)