def setUpClass(cls): schools_code = """\ data { int<lower=0> J; // number of schools real y[J]; // estimated treatment effects real<lower=0> sigma[J]; // s.e. of effect estimates } parameters { real mu; real<lower=0> tau; real eta[J]; } transformed parameters { real theta[J]; for (j in 1:J) theta[j] = mu + tau * eta[j]; } model { eta ~ normal(0, 1); y ~ normal(theta, sigma); }""" cls.schools_dat = {'J': 8, 'y': [28, 8, -3, 7, -1, 1, 18, 12], 'sigma': [15, 10, 16, 11, 9, 11, 10, 18]} cls.model = get_model("schools_model", schools_code)
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 { int N; real y[N]; } parameters { real mu; real<lower=0> sigma; } model { y ~ normal(mu, sigma); }""" stepsize0 = 0.15 sm = get_model("normal_mu_sigma_model", code) sf = sm.sampling(data=dict(y=y, N=20), 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, N=20), control=dict(adapt_engaged=False, stepsize=stepsize0)) self.assertEqual(sf2.get_sampler_params()[0]['stepsize__'][0], stepsize0) sf3 = stan(fit=sf, iter=1, data=dict(y=y, N=20), init=0, chains=1) i_u = sf3.unconstrain_pars(sf3.get_inits()[0]) np.testing.assert_equal(i_u, [0, 0])
def setUpClass(cls): model_code = 'parameters {real y;} model {y ~ normal(0,1);}' cls.model = get_model("standard_normal_model", model_code, model_name="normal1", verbose=True, obfuscate_model_name=False) #model = pystan.StanModel(model_code=model_code) cls.fits = [cls.model.sampling(iter=4000, chains=2, seed=i) for i in range(10)]
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 { int N; real y[N]; } parameters { real mu; real<lower=0> sigma; } model { y ~ normal(mu, sigma); }''' sm = get_model("normal_mu_sigma_model", code) sf = sm.sampling(iter=10, thin=3, data=dict(y=y, N=20)) 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)
def setUpClass(cls): model_code = """ data { int<lower=2> K; int<lower=1> D; } parameters { matrix[K,D] beta; } model { for (k in 1:K) for (d in 1:D) beta[k,d] ~ normal((d==2?100:0),1); }""" cls.model = get_model("matrix_normal_model", model_code)
def setUpClass(cls): model_code = """ data { int<lower=2> K; int<lower=1> D; } parameters { matrix[K,D] beta; } model { for (k in 1:K) for (d in 1:D) beta[k,d] ~ normal(if_else(d==2,100, 0),1); }""" cls.model = get_model("matrix_normal_model", model_code)
def test_grad_log(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 { int N; real y[N]; } parameters { real mu; real<lower=0> sigma; } model { y ~ normal(mu, sigma); }''' def log_prob_fun(mu, log_sigma, adjust=True): sigma = np.exp(log_sigma) lp = -1 * np.sum((y - mu)**2) / (2 * (sigma**2)) - len(y) * np.log(sigma) if adjust: lp = lp + np.log(sigma) return lp def log_prob_grad_fun(mu, log_sigma, adjust=True): sigma = np.exp(log_sigma) g_lsigma = np.sum((y - mu)**2) * sigma**(-2) - len(y) if adjust: g_lsigma = g_lsigma + 1 g_mu = np.sum(y - mu) * sigma**(-2) return (g_mu, g_lsigma) sm = get_model("normal_mu_sigma_model", code) sf = sm.sampling(data=dict(y=y, N=20), iter=200) mu = 0.1 sigma = 2 self.assertEqual(sf.log_prob(sf.unconstrain_pars(dict(mu=mu, sigma=sigma))), log_prob_fun(mu, np.log(sigma))) self.assertEqual(sf.log_prob(sf.unconstrain_pars(dict(mu=mu, sigma=sigma)), False), log_prob_fun(mu, np.log(sigma), adjust=False)) g1 = sf.grad_log_prob(sf.unconstrain_pars(dict(mu=mu, sigma=sigma)), False) np.testing.assert_allclose(g1, log_prob_grad_fun(mu, np.log(sigma), adjust=False))
def setUpClass(cls): model_code = 'parameters {real y;} model {y ~ normal(0,1);}' model = get_model("standard_normal_model", model_code, model_name="normal1", verbose=True, obfuscate_model_name=False) fit = model.sampling() tempfolder = tempfile.mkdtemp() cls.pickle_fit = os.path.join(tempfolder, 'stanfit.pkl') cls.pickle_extract = os.path.join(tempfolder, 'stanextract.pkl') with io.open(cls.pickle_fit, mode="wb") as f: pickle.dump(fit, f) with io.open(cls.pickle_extract, mode="wb") as f: pickle.dump(fit.extract(), f) module_name = model.module.__name__ del model del sys.modules[module_name]
def setUpClass(cls): bernoulli_model_code = """ data { int<lower=0> N; int<lower=0,upper=1> y[N]; } parameters { real<lower=0,upper=1> theta; } model { for (n in 1:N) y[n] ~ bernoulli(theta); } """ cls.bernoulli_data = bernoulli_data = {'N': 10, 'y': [0, 1, 0, 0, 0, 0, 0, 0, 0, 1]} cls.model = model = get_model("bernoulli_model", bernoulli_model_code, model_name="bernoulli") #cls.model = model = pystan.StanModel(model_code=bernoulli_model_code, model_name="bernoulli") cls.fit = model.sampling(data=bernoulli_data)
def setUpClass(cls): stdnorm = """ data { int N; real y[N]; } parameters { real mu; real<lower=0> sigma; } model { y ~ normal(mu, sigma); } """ N = 30 np.random.seed(1) y = np.random.normal(size=N) cls.dat = {'N': N, 'y': y} cls.sm = get_model("normal_mu_sigma_model", stdnorm, verbose=True)
def setUpClass(cls): model_code = """ data { int<lower=2> K; int<lower=1> D; } parameters { matrix[K,D] beta; } model { for (k in 1:K) for (d in 1:D) beta[k,d] ~ normal((d==2?100:0),1); }""" cls.model = get_model("matrix_normal_model", model_code) #cls.model = StanModel(model_code=model_code) control = {"metric": "diag_e"} fit = cls.model.sampling(chains=1, data=dict(K=3, D=4), warmup=1500, iter=1501, control=control, check_hmc_diagnostics=False, seed=14) cls.pos_def_matrix_diag_e = fit.get_inv_metric()[0] cls.stepsize_diag_e = fit.get_stepsize()[0] control = {"metric": "dense_e"} fit = cls.model.sampling(chains=1, data=dict(K=3, D=4), warmup=1500, iter=1501, control=control, check_hmc_diagnostics=False, seed=14) cls.pos_def_matrix_dense_e = fit.get_inv_metric()[0] cls.stepsize_dense_e = fit.get_stepsize()[0]
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 { int N; real y[N]; } parameters { real mu; real<lower=0> sigma; } model { y ~ normal(mu, sigma); }""" stepsize0 = 0.15 sm = get_model("normal_mu_sigma_model", code) sf = sm.sampling(data=dict(y=y, N=20), 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, N=20), control=dict(adapt_engaged=False, stepsize=stepsize0)) self.assertEqual(sf2.get_sampler_params()[0]['stepsize__'][0], stepsize0) sf3 = stan(fit=sf, iter=1, data=dict(y=y, N=20), init=0, chains=1, control={"adapt_engaged": False}) i_u = sf3.unconstrain_pars(sf3.get_inits()[0]) np.testing.assert_equal(i_u, [0, 0])
def setUpClass(cls): model_code = 'parameters {real y;} model {y ~ normal(0,1);}' cls.model = get_model("standard_normal_model", model_code, model_name="normal1", verbose=True, obfuscate_model_name=False)
def setUpClass(cls): model_code = 'parameters {real x;real y;real z;} model {x ~ normal(0,1);y ~ normal(0,1);z ~ normal(0,1);}' cls.model = get_model("standard_normals_model", model_code)