Created
December 21, 2021 23:23
-
-
Save tommylees112/91acda6127f632fb4eb558a8163cfd77 to your computer and use it in GitHub Desktop.
Unpooled Stan Model with dummy data for SO Question / Stan Forum Question
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import numpy as np | |
from cmdstanpy import cmdstan_path, CmdStanModel, CmdStanMCMC | |
def make_dummy_data(): | |
variables = [f"SM_lag_{i}" for i in range(6)] | |
variables += [f"PCP_lag_{i}" for i in range(6)] | |
variables += [f"VCI_lag_{i}" for i in range(6)] | |
N_samples = 100 | |
X_train = pd.DataFrame({ | |
var: np.random.random(N_samples) | |
for var in variables | |
}) | |
y_train = np.random.random(len(X_train)) | |
lc_idx = np.repeat([0, 1, 2], 20)[:len(X_train)] | |
K = len(X_train.columns) | |
data = dict( | |
N=X_data.shape[0], | |
K=K, | |
X_train=X_data.values, | |
y_train=y_data.values, | |
landcover_idx=lc_idx, | |
J=len(np.unique(lc_idx)) | |
) | |
return data | |
if __name__ == "__main__": | |
data = make_dummy_data() | |
# build model | |
stan_file = "unpooled.stan" | |
stan_model = CmdStanModel(stan_file=stan_file) | |
stan_model.compile() | |
# fit model | |
model_fit: CmdStanMCMC = stan_model.sample( | |
data=data, | |
chains=4, | |
parallel_chains=4, | |
seed=1111, | |
show_progress=True, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment