Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def fit(self, x: np.array, y: np.array):
return ps.stan(
model_code=self.model_code,
data=self.preprocess(
RegressionStanData(x, y, self.shrinkage)
),
**self.kwargs
)
def inference(dataset):
print '\tPerforming inference...'
NUM_ITER = 1000
WARMUP = 500
NUM_CHAINS = 4
NUM_CORES = 4
STAN_FN = 'model_1-4.stan'
# import pdb; pdb.set_trace()
fit = pystan.stan(file = STAN_FN,
data = dataset,
iter = NUM_ITER,
warmup = WARMUP,
chains = NUM_CHAINS,
n_jobs = NUM_CORES)
print(fit)
fit.plot()
plt.tight_layout()
plt.savefig('fit_pystan.png')
return
def inference(dataset):
print '\tPerforming inference...'
NUM_ITER = 2000
WARMUP = 200
NUM_CHAINS = 4
NUM_CORES = 4
STAN_FN = 'regression_1-2.stan'
# import pdb; pdb.set_trace()
fit = pystan.stan(file = STAN_FN,
data = dataset,
iter = NUM_ITER,
warmup = WARMUP,
chains = NUM_CHAINS,
n_jobs = NUM_CORES)
print(fit)
fit.plot()
plt.tight_layout()
plt.savefig('fit_pystan.png')
return
bernoulli_code = """
data {
int N;
int y[N];
}
parameters {
real theta;
}
model {
theta ~ beta(1,1);
for (n in 1:N)
y[n] ~ bernoulli(theta);
}
"""
data = dict(N=10, y=[0,1,0,0,1,1,1,0,1,0])
fit = pystan.stan(model_code=bernoulli_code, data=data)
print(fit)
samples = fit.extract(permuted=True)
plt.hist(samples['theta'], 50)
plt.show()
# ==== Vectorized Bernoulli model ==============================================
# ==============================================================================
bernoulli_code = """
data {
int N;
int y[N];
}
parameters {
real theta;
}
}
model {
theta ~ beta(1,1);
y ~ binomial(N,theta);
}
"""
data = dict(N=10, y=8)
fit = pystan.stan(model_code=binomial_code, data=data)
samples = fit.extract(permuted=True)
plt.hist(samples['theta'], 50)
plt.show()
# ==== Re-running Binomial model with new data =================================
# ==============================================================================
data = dict(N=10, y=10)
fit = pystan.stan(fit=fit, data=data)
samples = fit.extract(permuted=True)
plt.hist(samples['theta'], 50)
plt.show()
# ==== Comparison of two groups with Binomial ==================================
# ==============================================================================
binomial_code = """
data {
int N1;
int y1;
int N2;
int y2;
}
parameters {
real theta1;
def __call__ (self, *args, **kwargs):
return pystan.stan(model_code=self.compiled_model, *args, **kwargs)
def inference(dataset):
print '\tPerforming inference...'
NUM_ITER = 1000
WARMUP = 500
NUM_CHAINS = 4
NUM_CORES = 4
STAN_FN = 'hierarchical_model_1-1.stan'
fit = pystan.stan(file = STAN_FN,
data = dataset,
iter = NUM_ITER,
warmup = WARMUP,
chains = NUM_CHAINS,
n_jobs = NUM_CORES)
print(fit)
fit.plot()
plt.tight_layout()
plt.savefig('fit_pystan.png')
normal_mus = fit['normal_mus'][-1]
print normal_mus
return
def inference(dataset):
print '\tPerforming inference...'
NUM_ITER = 1000
WARMUP = 500
NUM_CHAINS = 4
NUM_CORES = 4
STAN_FN = 'regression_1-2.stan'
# import pdb; pdb.set_trace()
fit = pystan.stan(file = STAN_FN,
data = dataset,
iter = NUM_ITER,
warmup = WARMUP,
chains = NUM_CHAINS,
n_jobs = NUM_CORES)
print(fit)
fit.plot()
plt.tight_layout()
plt.savefig('fit_pystan.png')
return