You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"The input data consists of `N` - the number of bernoulli trials and `y` - the list of observed outcomes.\n",
62
63
"Inspection of the data shows that on average, there is a 20% chance of success for any given Bernoulli trial."
63
-
],
64
-
"metadata": {}
64
+
]
65
65
},
66
66
{
67
67
"cell_type": "code",
68
68
"execution_count": null,
69
+
"metadata": {},
70
+
"outputs": [],
69
71
"source": [
70
72
"# examine bernoulli data\n",
71
-
"import ujson\n",
73
+
"import json\n",
72
74
"import statistics\n",
73
75
"with open(data_file,'r') as fp:\n",
74
-
" data_dict = ujson.load(fp)\n",
76
+
" data_dict = json.load(fp)\n",
75
77
"print(data_dict)\n",
76
78
"print('mean of y: {}'.format(statistics.mean(data_dict['y'])))"
77
-
],
78
-
"outputs": [],
79
-
"metadata": {}
79
+
]
80
80
},
81
81
{
82
82
"cell_type": "markdown",
83
+
"metadata": {},
83
84
"source": [
84
85
"As in the \"Hello World\" tutorial, we produce a sample from the posterior of the model conditioned on the data:"
85
-
],
86
-
"metadata": {}
86
+
]
87
87
},
88
88
{
89
89
"cell_type": "code",
90
90
"execution_count": null,
91
+
"metadata": {},
92
+
"outputs": [],
91
93
"source": [
92
94
"# fit the model to the data\n",
93
95
"fit = model.sample(data=data_file)"
94
-
],
95
-
"outputs": [],
96
-
"metadata": {}
96
+
]
97
97
},
98
98
{
99
99
"cell_type": "markdown",
100
+
"metadata": {},
100
101
"source": [
101
102
"The fitted model produces an estimate of `theta` - the chance of success"
102
-
],
103
-
"metadata": {}
103
+
]
104
104
},
105
105
{
106
106
"cell_type": "code",
107
107
"execution_count": null,
108
+
"metadata": {},
109
+
"outputs": [],
108
110
"source": [
109
111
"fit.summary()"
110
-
],
111
-
"outputs": [],
112
-
"metadata": {}
112
+
]
113
113
},
114
114
{
115
115
"cell_type": "markdown",
116
+
"metadata": {},
116
117
"source": [
117
118
"To run a prior predictive check, we add a `generated quantities` block to the model, in which we generate a new data vector `y_rep` using the current estimate of theta. The resulting model is in file [bernoulli_ppc.stan](https://github.com/stan-dev/cmdstanpy/blob/master/test/data/bernoulli_ppc.stan)"
"We run the `generate_quantities` method on `bernoulli_ppc` using existing sample `fit` as input. The `generate_quantities` method takes the values of `theta` in the `fit` sample as the set of draws from the posterior used to generate the corresponsing `y_rep` quantities of interest.\n",
135
136
"\n",
136
137
"The arguments to the `generate_quantities` method are:\n",
137
138
" + `data` - the data used to fit the model\n",
138
139
" + `mcmc_sample` - either a `CmdStanMCMC` object or a list of stan-csv files\n"
"The `generate_quantities` method returns a `CmdStanGQ` object which contains the values for all variables in the generated quantitites block of the program ``bernoulli_ppc.stan``. Unlike the output from the ``sample`` method, it doesn't contain any information on the joint log probability density, sampler state, or parameters or transformed parameter values.\n",
155
156
"\n",
156
157
"In this example, each draw consists of the N-length array of replicate of the `bernoulli` model's input variable `y`, which is an N-length array of Bernoulli outcomes."
"For models as simple as the bernoulli models here, it would be trivial to re-run the sampler and generate a new sample which contains both the estimate of the parameters `theta` as well as `y_rep` values. For models which are difficult to fit, i.e., when producing a sample is computationally expensive, the `generate_quantities` method is preferred."
0 commit comments