How to use the pystan.stan function in pystan

To help you get started, we’ve selected a few pystan examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github BayesianFreaks / scikit-stan / skstan / regression / base.py View on Github external
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
        )
github maxwshen / iap-appbml / 1.4-ans / model_1-4.py View on Github external
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
github maxwshen / iap-appbml / 1.2 / regression_1-2.py View on Github external
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
github avehtari / BDA_py_demos / demos_pystan / pystan_demo.py View on Github external
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;
}
github avehtari / BDA_py_demos / demos_pystan / pystan_demo.py View on Github external
}
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;
github IBM / yaps / yaps.py View on Github external
def __call__ (self, *args, **kwargs):
        return pystan.stan(model_code=self.compiled_model, *args, **kwargs)
github maxwshen / iap-appbml / 1.1-ans / hierarchical_model_1-1.py View on Github external
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
github maxwshen / iap-appbml / 1.2-ans / regression_1-2.py View on Github external
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