├── .Rbuildignore ├── .Rprofile ├── .gitignore ├── DESCRIPTION ├── LICENSE ├── NAMESPACE ├── README.md ├── _site.yml ├── docs ├── index.html ├── lesson1-tasks.html ├── lesson1.html ├── lesson2-tasks.html ├── lesson2.html ├── lesson2_files │ └── figure-html │ │ ├── density-underdetermined-1.png │ │ └── density-underdetermined-2.png ├── lesson3-tasks.html ├── lesson3.html ├── lesson4-tasks.html ├── lesson4.html ├── lesson5.html ├── lesson6.html ├── site_libs │ ├── bootstrap-3.3.5 │ │ ├── css │ │ │ ├── bootstrap-theme.css │ │ │ ├── bootstrap-theme.css.map │ │ │ ├── bootstrap-theme.min.css │ │ │ ├── bootstrap.css │ │ │ ├── bootstrap.css.map │ │ │ ├── bootstrap.min.css │ │ │ ├── cerulean.min.css │ │ │ ├── cosmo.min.css │ │ │ ├── darkly.min.css │ │ │ ├── flatly.min.css │ │ │ ├── fonts │ │ │ │ ├── Lato.ttf │ │ │ │ ├── LatoBold.ttf │ │ │ │ ├── LatoItalic.ttf │ │ │ │ ├── NewsCycle.ttf │ │ │ │ ├── NewsCycleBold.ttf │ │ │ │ ├── OpenSans.ttf │ │ │ │ ├── OpenSansBold.ttf │ │ │ │ ├── OpenSansBoldItalic.ttf │ │ │ │ ├── OpenSansItalic.ttf │ │ │ │ ├── OpenSansLight.ttf │ │ │ │ ├── OpenSansLightItalic.ttf │ │ │ │ ├── Raleway.ttf │ │ │ │ ├── RalewayBold.ttf │ │ │ │ ├── Roboto.ttf │ │ │ │ ├── RobotoBold.ttf │ │ │ │ ├── RobotoLight.ttf │ │ │ │ ├── RobotoMedium.ttf │ │ │ │ ├── SourceSansPro.ttf │ │ │ │ ├── SourceSansProBold.ttf │ │ │ │ ├── SourceSansProItalic.ttf │ │ │ │ ├── SourceSansProLight.ttf │ │ │ │ └── Ubuntu.ttf │ │ │ ├── journal.min.css │ │ │ ├── lumen.min.css │ │ │ ├── paper.min.css │ │ │ ├── readable.min.css │ │ │ ├── sandstone.min.css │ │ │ ├── simplex.min.css │ │ │ ├── spacelab.min.css │ │ │ ├── united.min.css │ │ │ └── yeti.min.css │ │ ├── fonts │ │ │ ├── glyphicons-halflings-regular.eot │ │ │ ├── glyphicons-halflings-regular.svg │ │ │ ├── glyphicons-halflings-regular.ttf │ │ │ ├── glyphicons-halflings-regular.woff │ │ │ └── glyphicons-halflings-regular.woff2 │ │ ├── js │ │ │ ├── bootstrap.js │ │ │ ├── bootstrap.min.js │ │ │ └── npm.js │ │ └── shim │ │ │ ├── html5shiv.min.js │ │ │ └── respond.min.js │ ├── header-attrs-2.25 │ │ └── header-attrs.js │ ├── highlightjs-9.12.0 │ │ ├── default.css │ │ ├── highlight.js │ │ └── textmate.css │ ├── jquery-3.6.0 │ │ ├── jquery-3.6.0.js │ │ ├── jquery-3.6.0.min.js │ │ └── jquery-3.6.0.min.map │ └── navigation-1.1 │ │ ├── codefolding-lua.css │ │ ├── codefolding.js │ │ ├── sourceembed.js │ │ └── tabsets.js └── stan │ ├── lesson1 │ ├── initial.stan │ └── with_sigma_prior.stan │ └── lesson2 │ └── neg_binom.stan ├── index.Rmd ├── lesson1-tasks.Rmd ├── lesson1.Rmd ├── lesson2-tasks.Rmd ├── lesson2.Rmd ├── lesson3-tasks.Rmd ├── lesson3.Rmd ├── lesson4-tasks.Rmd ├── lesson4.Rmd ├── lesson5.Rmd ├── lesson6.Rmd ├── modelling-in-stan-2024.Rproj ├── renv.lock ├── renv ├── .gitignore ├── activate.R └── settings.json ├── solutions ├── lesson1-solutions.Rmd └── lesson2-solutions.Rmd └── stan ├── lesson1 ├── initial.stan └── with_sigma_prior.stan └── lesson2 └── neg_binom.stan /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^renv$ 2 | ^renv\.lock$ 3 | ^sbc_extensions_paper\.Rproj$ 4 | ^\.Rproj\.user$ 5 | -------------------------------------------------------------------------------- /.Rprofile: -------------------------------------------------------------------------------- 1 | source("renv/activate.R") 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # History files 2 | .Rhistory 3 | .Rapp.history 4 | 5 | # Session Data files 6 | .RData 7 | 8 | # User-specific files 9 | .Ruserdata 10 | 11 | # Example code in package build process 12 | *-Ex.R 13 | 14 | # Output files from R CMD build 15 | /*.tar.gz 16 | 17 | # Output files from R CMD check 18 | /*.Rcheck/ 19 | 20 | # RStudio files 21 | .Rproj.user/ 22 | 23 | # produced vignettes 24 | vignettes/*.html 25 | vignettes/*.pdf 26 | 27 | # OAuth2 token, see https://github.com/hadley/httr/releases/tag/v0.3 28 | .httr-oauth 29 | 30 | # knitr and R markdown default cache directories 31 | *_cache/ 32 | /cache/ 33 | /_SBC_cache* 34 | 35 | # Temporary files created by R markdown 36 | *.utf8.md 37 | *.knit.md 38 | 39 | # R Environment Variables 40 | .Renviron 41 | .Rproj.user 42 | 43 | _figs/ 44 | /*_files/ 45 | /*.html/ 46 | 47 | *.nb.html 48 | *.exe 49 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: sbc_extensions_paper 2 | Title: What the Package Does (One Line, Title Case) 3 | Version: 0.0.0.9000 4 | Authors@R: 5 | person("First", "Last", , "first.last@example.com", role = c("aut", "cre"), 6 | comment = c(ORCID = "YOUR-ORCID-ID")) 7 | Description: What the package does (one paragraph). 8 | License: `use_mit_license()`, `use_gpl3_license()` or friends to pick a 9 | license 10 | Encoding: UTF-8 11 | Roxygen: list(markdown = TRUE) 12 | RoxygenNote: 7.0.0 13 | Suggests: 14 | testthat (>= 3.0.0) 15 | Config/testthat/edition: 3 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SBC and various test quantities 2 | 3 | Simulations for the paper "Simulation-Based Calibration Checking for Bayesian Computation: The Choice of Test Quantities Shapes Sensitivity". Written in 4 | R markdown, using the [SBC](https://hyunjimoon.github.io/SBC/) R package. 5 | Code is written in R markdown (`.Rmd`), rendered HTML can be found at 6 | https://martinmodrak.github.io/sbc_test_quantities_paper. 7 | 8 | Contents: 9 | 10 | - `mvn.Rmd` - the multivariate normal examples for the main body of the paper (Section 4 - Numerical Experiments) 11 | - `ordered_simplex.Rmd` - the implementations of ordered simplex for the main body of the paper (Section 5 - Real-world case study) 12 | - `bernoulli.Rmd` - Examples in the Appendix B 13 | 14 | The code uses `renv` to recreate the same environment as we used to run the examples. 15 | Run `renv::restore()` after loading the project. 16 | 17 | The rendered site can be rebuilt with `rmarkdown::render_site()`. 18 | -------------------------------------------------------------------------------- /_site.yml: -------------------------------------------------------------------------------- 1 | name: "modelling-in-stan-2024" 2 | navbar: 3 | title: "Bayesian probabilistic programming in Stan - 2024 course" 4 | left: 5 | - text: "Home" 6 | href: index.html 7 | - text: "Lesson 1 - Tasks" 8 | href: lesson1-tasks.html 9 | - text: "Lesson 2 - Tasks" 10 | href: lesson2-tasks.html 11 | - text: "Lesson 3 - Tasks" 12 | href: lesson3-tasks.html 13 | output_dir: docs 14 | include: ["*.html", "*.png"] 15 | exclude: ["R", "*.R", "*.Rmd", "DESCRIPTION", "NAMESPACE", "renv.lock", "LICENSE", "tests", "solutions", "*.exe"] 16 | -------------------------------------------------------------------------------- /docs/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 |
6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |Last updated: 2024-05-06 14:00:15.738025.
276 | 289 | 290 | 291 | 292 | 293 |Note: Windows users in both Stan and Python might need RTools 277 | installed. Martin has a flash drive with RTools installers so that you 278 | don’t have to wait for the large download.
279 |We will use the cmdstanr
package, follow https://mc-stan.org/cmdstanr/articles/cmdstanr.html
We will use the CmdStanPy
package, follow https://mc-stan.org/cmdstanpy/installation.html
Create a file called initial.stan
and paste the
295 | following simple model.
data {
297 | int<lower=0> N;
298 | vector[N] y;
299 | }
300 |
301 | parameters {
302 | real mu;
303 | }
304 |
305 | model {
306 | // likelihood
307 | target += normal_lpdf(y | mu, 1);
308 | // prior
309 | target += normal_lpdf(mu | 0, 1);
310 | }
311 | Note that we express all contributions to the target density
312 | explicitly with target +=
.
The corresponding statistical model is
314 |\[ 315 | y_i \sim N(\mu, 1) \\ 316 | \mu \sim N(0,1) 317 | \]
318 |i.e. we are estimating the mean of a normal distribution with known 319 | standard deviation and a somewhat informative prior on the mean.
320 |Note: You’ll notice many Stan models use statements like
321 | y ~ normal(mu, 1)
instead of
322 | target += normal(y | mu, 1)
. We will discuss this
323 | alternative syntax and the differences in next lesson.
Simulate data for the model with 10 data points, using the same
325 | structure as the model assumes, i.e. draw \(\mu\) from its prior, then draw \(y\) (using rnorm
in R,
328 | numpy.random.Generator.normal
in Python)
Compile the model, prepare the data in a format for the model, fit 330 | the model and display a summary:
331 |options(mc.cores = 4)
to compute
337 | chains in parallel.InferenceData
via arviz.from_cmdstanpy
345 | and then use arviz.summaryInspect the summary - how big is the uncertainty? Are the “true” 349 | values we used in simulation recovered?
350 |sigma
Add a sigma
parameter of type real
to model
354 | the standard deviation, with a half-Normal prior i.e. the new
355 | mathematical model is:
\[ 357 | y_i \sim N(\mu, \sigma) \\ 358 | \mu \sim N(0,1) \\ 359 | \sigma \sim HalfN(0, 2) 360 | \]
361 |Since standard deviation cannot be negative, add a
362 | <lower=0>
constraint to sigma
.
HMC needs the parameter space to be Euclidean (i.e. \(\mathbb{R}^K\) for some \(K\)), so when something is constrained,
366 | there is an implicit transformation, so Stan will use
367 | log(sigma)
internally.
There is no builtin half-normal distribution in Stan. But it turns 369 | out, using a normal prior on a variable constrained to be positive is 370 | the same as using a half-normal distribution. We will go into the 371 | reasons for this and some nuances next time.
372 |Generate data according to this model, i.e. first draw \(\mu\) and \(\sigma\) from their priors, then draw \(y\). You can draw from the half-normal 376 | distribution simply by taking the absolute value of a draw from the 377 | normal distribution.
378 |Explore the summary, do you recover the true parameters?
379 |Use the model from the previous task with dataset of size 1. What 383 | happens?
384 |Is there a reason the model may not work well with just a single 385 | datapoint?
386 |Does the model from Task 2 also have issues with a dataset of size 1? 387 | Why?
388 |Bonus: Try this if you feel it won’t take you too
389 | much time. Assume that the single data point is \(y = 1\), make a contour and/or heatmap plot
391 | of the posterior density as a function \(\mu\) and \(\sigma\) (in R use
394 | dnorm( log = TRUE)
, in Python use
395 | scipy.stats.norm
and the logpdf
method).
Generally, to get a basic understanding of warnings about convergence 397 | from Stan, the guide at https://mc-stan.org/misc/warnings is IMHO quite good (I 399 | wrote most of it :-D).
400 |Add two new data elements of type real
-
404 | mu_prior_mean
and mu_prior_sd
to define the
405 | location and scale of the normal prior for \(\mu\)
Should those data have some constraints attached? If yes, add them as 408 | well.
409 |Test that the model now works with some of the datasets you generated 410 | previously.
411 |Generate a dataset of size 10 with the prior \(\mu \sim N(0,1), \sigma \sim HalfN(0, 2)\).
416 | Find the least extreme values of mu_prior_mean
and
417 | mu_prior_sd
that make the posterior generated by Stan
418 | exclude the true value of \(\mu\) from
419 | your simulation.
Try the same task with a dataset of size 3 and a dataset of size 421 | 100.
422 |Expand the model so that you now have two sets of data (possibly of 426 | unequal size), each belonging to a different group and estimate the mean 427 | in the first group and the difference in means. More specifically, the 428 | model should look like this:
429 |\[ 430 | y_A \sim N(\mu, \sigma_A) \\ 431 | y_B \sim N(\mu + \delta, \sigma_B) \\ 432 | \mu \sim N(0, 1)\\ 433 | \delta \sim N\left(0, \frac{1}{2}\right) \\ 434 | \sigma_A, \sigma_B \sim HalfN(0, 1) 435 | \]
436 |(you can keep the location and scale of the prior as data, but you 437 | can also hardcode it)
438 |Repeatedly simulate data according to the model and fit it. How big 439 | do the groups need to be, so that your posterior interval for \(\delta\) excludes 0 most of the time?
441 |\[\begin{gather*} 284 | \pi_\text{joint}(y, \theta) = \pi_\text{obs}(y | \theta) 285 | \pi_\text{prior}(\theta)\\ 286 | \pi_\text{marg}\left(y \right) = \int_\Theta \mathrm{d} \theta \: 287 | \pi_{\text{obs}}(y | \theta) \pi_\text{prior}(\theta)\\ 288 | \pi_\text{post}(\theta | y) = \frac{\pi_\text{obs}(y | \theta) 289 | \pi_\text{prior}(\theta)}{\pi_\text{marg}\left(y \right)}. 290 | \end{gather*}\]
291 |\[ 310 | y \sim N(\mu,\sigma) \\ 311 | \mu \sim N(0, 1) \\ 312 | \sigma \sim HalfN(0, 2) 313 | \]
314 |Modelled vs. unmodelled data
Great intro to the underlying philosophical ideas: Chapters 1 - 2 317 | of McElreath’s “Statistical Rethinking” - available freely online at https://xcelab.net/rmpubs/sr2/statisticalrethinking2_chapters1and2.pdf
320 |Extracting information from probability distributions via 326 | expectation values (see Mike 328 | Betancourt’s writing for a very detailed coverage)
MCMC algorithms allow us to sample posterior
330 |HMC - show example, requires gradient
335 |What is needed to be proficient at probabilistic programming?
344 |rstan
package,
359 | while we will use the newer cmdstanr
package.data
(both modelled and non-modelled)parameters
model
target +=
statements_lpdf
Simulate 30 values with neg. binomial with \(\phi = \frac{1}{2}\) (size
in
307 | R’s rnbinom
), fit with the model from Task 2. Do you
308 | recover the simulated mean of the neg. binomial?
Try to detect the model-data mismatch with a posterior predictive 310 | check.
R: - Use fit$draws(format = "draws_matrix")
to get a
313 | matrix of draws, then use
314 | rpois`` to generate the predictions as the Stan model assumes - use either
bayesplot::ppc_statwith a stat measuring the dispersion (e.g. variance, sd, ...) or
bayesplot::ppc_dens_overlayPython: use either
arviz.plot_bpv()with
kind=“t_stat”and a stat measuring the dispersion (e.g. variance, sd, ...) or
arviz.plot_ppc`
What is the smallest number of observations we need to reliably 317 | detect the problem?
Implement a negative binomial model and see how the check behaves 319 | now.
320 |neg_binomial_2_lpmf
)exponential(1)
prior on the overdispersion
324 | parameter.Given the following data:
332 |y : 2, 0, 11, 25, 9, 4, 17, 11, 8, 4, 5, 2, 6, 4, 8, 24, 0, 3, 6, 4, 12, 9, 5, 6, 2, 10, 4, 15, 0, 1, 87, 19, 2, 1, 38, 16, 5, 7, 18, 11, 1, 0, 7, 15, 5, 0, 6, 1, 0, 6, 34, 29, 7, 11, 5, 10, 8, 3, 12, 18, 7, 1, 18, 18, 6, 3, 37, 5, 1, 0, 22, 13, 1, 0, 26, 19, 6, 7, 21, 45
333 | group : "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B"
334 | type : "X", "X", "Y", "Y", "X", "X", "Y", "Y", "X", "X", "Y", "Y", "X", "X", "Y", "Y", "X", "X", "Y", "Y", "X", "X", "Y", "Y", "X", "X", "Y", "Y", "X", "X", "Y", "Y", "X", "X", "Y", "Y", "X", "X", "Y", "Y", "X", "X", "Y", "Y", "X", "X", "Y", "Y", "X", "X", "Y", "Y", "X", "X", "Y", "Y", "X", "X", "Y", "Y", "X", "X", "Y", "Y", "X", "X", "Y", "Y", "X", "X", "Y", "Y", "X", "X", "Y", "Y", "X", "X", "Y", "Y"
335 | Try to fit the neg. binomial model from task 3 with a shared mean and
336 | overdispersion parameter to the y
column of the data.
The model does not represent the data well. Try to figure out what it 338 | is and fix it!
339 |Hint: many PPCs can be performed per group, in R this is the
340 | ppc_xxx_grouped
functions.
Given the following data:
345 |y : 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1
346 | group : "B", "C", "D", "B", "C", "B", "D", "C", "A", "D", "C", "C", "C", "D", "C", "C", "B", "B", "D", "C", "D", "C", "C", "A", "B", "D", "C", "D", "D", "D", "C", "D", "A", "A", "C", "D", "D", "C", "C", "B", "B", "C", "D", "D", "A", "D", "A", "B", "C", "A"
347 | Once again use the model from Task 3. Use posterior predictive check 348 | to determine what is wrong.
349 |Can you fix it?
350 |library(dplyr)
275 | library(magrittr)
276 | library(ggplot2)
277 | library(tidyr)
278 | theme_set(theme_minimal())
279 | plot_densities <- function(n_data_vals, mu_range = c(-3,3), log_sigma_range = c(-2,2)) {
299 | set.seed(566522)
300 | y = c(-0.6906332, 1.2873201, 2.0089285, 0.1347772, -0.8905993, -0.8171846, rnorm(max(1, max(n_data_vals))))
301 | underdetermined_theory <- crossing(mu = seq(mu_range[1], mu_range[2], length.out = 200), log_sigma = seq(log_sigma_range[1], log_sigma_range[2], length.out = 200),
302 | data.frame(y_id = 1:length(y), y = y), n_data = n_data_vals
303 | ) %>%
304 | filter(y_id <= n_data) %>%
305 | mutate(log_density_point = dnorm(y, mu, exp(log_sigma), log = TRUE)) %>%
306 | group_by(mu, log_sigma, n_data) %>%
307 | summarise(log_density = sum(log_density_point), .groups = "drop") %>%
308 | group_by(n_data) %>%
309 | mutate(rel_density = exp(log_density - max(log_density))) %>%
310 | ggplot(aes(x = mu, y = log_sigma, z = rel_density)) + geom_contour() + #geom_raster() +
311 | facet_wrap(~n_data, nrow = 1, labeller = label_bquote(cols = paste(N, " = ", .(n_data)) )) + scale_y_continuous("log(sigma)")
312 |
313 | underdetermined_theory
314 | }
315 |
316 | plot_densities(c(1,2,8))
317 | plot_densities(c(15, 30, 50), mu_range = c(-1,1), log_sigma_range = c(-0.5,1))
319 | xx_lupdf
, xx_lupmf
array[size] base_type;
(arrays
342 | in Stan manual)
343 | N
ints is array[N] int;
rnbinom(mu = mean, size = overdispersion)
This is a copy of Task 7 from Lesson 1
277 |Expand the model from lesson 1 so that you now have two sets of data 278 | (possibly of unequal size), each belonging to a different group and 279 | estimate the mean in the first group and the difference in means. More 280 | specifically, the model should look like this:
281 |\[ 282 | y_A \sim N(\mu, \sigma_A) \\ 283 | y_B \sim N(\mu + \delta, \sigma_B) \\ 284 | \mu \sim N(0, 1)\\ 285 | \delta \sim N\left(0, \frac{1}{2}\right) \\ 286 | \sigma_A, \sigma_B \sim HalfN(0, 1) 287 | \]
288 |(you can keep the location and scale of the prior as data, but you 289 | can also hardcode it)
290 |Repeatedly simulate data according to the model and fit it.
291 |How big do the groups need to be, so that your posterior interval for 292 | \(\delta\) excludes 0 most of the time? 293 | No need for a very precise answer, just a ballpark estimate, so running 294 | at most 5 simulations per sample size setting should be enough to get a 295 | good grasp.
296 |Adapt the model from the previous task so that a) the standard
300 | deviation (\(\sigma\)) is the same for
301 | all data and b) the observed \(y\) are
302 | all stored in a single vector of length \(N\) and another vector of length \(N\) encodes group membership. I.e. your
305 | data
section should look something like:
data {
307 | int<lower=0> N;
308 | vector[N] y;
309 | array[N] int<lower=0,upper=1> isB;
310 | }
311 | Hint: after converting isB
into a vector
312 | (with to_vector
) you can obtain a vector of length
313 | N
of means for all observations by addition and
314 | multiplication.
You can then directly pass the vector to normal_lpdf
,
316 | i.e. have
target += normal_lpdf(y | __COMPUTATION_HERE__, sigma);
318 | If stuck, see https://mc-stan.org/docs/stan-users-guide/regression.html
321 |For extra credit, you may try to figure out how to keep the distinct 322 | standard deviations following a similar computation.
323 |Add a new continuous predictor (a vector of real numbers) for each
327 | data point and add an extra coefficient (a new variable in the
328 | parameters
block) to model its influence.
Simulate data and check parameter recovery.
330 |Make a copy of the model and convert it to matrix multiplication
334 | format. Make the number of columns in the design matrix variable. I.e.
335 | your data
section should look something like:
data {
337 | int<lower=0> N; // number of data items
338 | int<lower=0> K; // number of predictors
339 | matrix[N, K] x; // predictor matrix
340 | vector[N] y; // outcome vector
341 | }
342 | Test that with the same data, you get the same results as in the 343 | previous version (since Hamiltonian Monte Carlo is stochastic you won’t 344 | get exactly the same results, but they should be numerically very 345 | close)
346 |Compare what you’ve built with the example linear regression models 350 | in Stan’s User’s guide: https://mc-stan.org/docs/stan-users-guide/regression.html#linear-regression
353 |Generally, don’t be afraid to use the User’s guide as a starting 354 | point for your models, it is a great resource!
355 |Use dummy 360 | coding to extend the model to have 3 groups instead of 2 in the 361 | categorical predictor. Your Stan code should not need to change.
362 |Simulate data, fit, check parameter recovery.
363 |Starting with the previous model, create a negative binomial 367 | regression model with a log link.
368 |Simulate data, fit, check parameter recovery (you can use a simpler 369 | set of predictors if you prefer).
370 |library(dplyr)
275 | library(magrittr)
276 | library(ggplot2)
277 | library(tidyr)
278 | theme_set(theme_minimal())
279 | Try to complete Task 283 | 3 and 4 from Lesson 2 (and possibly others from the previous 284 | lessons)
285 |Pick projects!
286 |model.matrix
in R
does the coding for
311 | youNow, let’s do some tasks. Note that they match workflow (simple stuff 338 | first, …)
339 |Start with the code for Lesson 278 | 3 - Task 6 (dummy coding) and/or the regression 280 | code from Stan User’s guide.
281 |Convert the 3 categorical groups into a random intercept model, 282 | i.e.:
283 |data
elements for the model to represent
286 | the number of categories and the category each observation belongs
287 | toarray[N] int
as a type for the cateogry input<lower = ..., upper = ...>
292 | bounds for the category input - what would that be?parameter
elements:vector
by an array
308 | to obtain a vector
the same length as the arraynormal(0, ...)
prior on the category random interceptsHow does the fit change from the fixed effect
315 |Convert the above model to non-centered parametrization, i.e.:
319 |normal(0, 1)
prior on the random intercept
321 | vectortransformed parameters
transformed parameters
blockordered_logistic
285 | function, see also the user’s
287 | guide section