-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
cmdstanpy_tutorial.py
48 lines (29 loc) · 1.07 KB
/
cmdstanpy_tutorial.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python
# Python code from Jupyter notebook `cmdstanpy_tutorial.ipynb`
# ### Import CmdStanPy classes and methods
import os
from cmdstanpy import CmdStanModel, cmdstan_path
# ### Instantiate & compile the model
bernoulli_dir = os.path.join(cmdstan_path(), 'examples', 'bernoulli')
stan_file = os.path.join(bernoulli_dir, 'bernoulli.stan')
with open(stan_file, 'r') as f:
print(f.read())
model = CmdStanModel(stan_file=stan_file)
print(model)
# ### Assemble the data
data = {"N": 10, "y": [0, 1, 0, 0, 0, 0, 0, 0, 0, 1]}
# In the CmdStan `examples/bernoulli` directory, there are data files in both `JSON` and `rdump` formats.
# bern_json = os.path.join(bernoulli_dir, 'bernoulli.data.json')
# ### Do Inference
fit = model.sample(data=data)
print(fit)
# ### Access the sample: the `CmdStanMCMC` object attributes and methods
print(fit.draws().shape)
# #### Get HMC sampler tuning parameters
print(fit.step_size)
print(fit.metric_type)
print(fit.metric)
# #### Summarize the results
print(fit.summary())
# #### Run sampler diagnostics
print(fit.diagnose())