├── .gitignore ├── LICENSE ├── README.md ├── python ├── _extensions │ └── quarto-ext │ │ └── include-code-files │ │ ├── _extension.yml │ │ └── include-code-files.lua ├── data │ ├── multi_data.json │ └── uni_data.json ├── demo.html ├── demo.pdf ├── demo.qmd ├── demo.tex ├── mcmc_analysis_tools_pystan3.py ├── mcmc_visualization_tools.py └── stan_programs │ ├── multi_full_model.stan │ ├── multi_prior_model.stan │ ├── uni_full_model.stan │ └── uni_prior_model.stan └── r ├── _extensions └── quarto-ext │ └── include-code-files │ ├── _extension.yml │ └── include-code-files.lua ├── data ├── multi_data.R └── uni_data.R ├── demo.html ├── demo.pdf ├── demo.qmd ├── demo.tex ├── mcmc_analysis_tools_rstan.R ├── mcmc_visualization_tools.R └── stan_programs ├── multi_full_model.stan ├── multi_prior_model.stan ├── uni_full_model.stan └── uni_prior_model.stan /.gitignore: -------------------------------------------------------------------------------- 1 | ## Core latex/pdflatex auxiliary files: 2 | *.aux 3 | *.lof 4 | *.log 5 | *.lot 6 | *.fls 7 | *.out 8 | *.toc 9 | *.fmt 10 | *.fot 11 | *.cb 12 | *.cb2 13 | .*.lb 14 | 15 | ## Intermediate documents: 16 | *.dvi 17 | *.xdv 18 | *-converted-to.* 19 | # these rules might exclude image files for figures etc. 20 | # *.ps 21 | # *.eps 22 | # *.pdf 23 | 24 | ## Generated if empty string is given at "Please type another file name for output:" 25 | .pdf 26 | 27 | ## Bibliography auxiliary files (bibtex/biblatex/biber): 28 | *.bbl 29 | *.bcf 30 | *.blg 31 | *-blx.aux 32 | *-blx.bib 33 | *.run.xml 34 | 35 | ## Build tool auxiliary files: 36 | *.fdb_latexmk 37 | *.synctex 38 | *.synctex(busy) 39 | *.synctex.gz 40 | *.synctex.gz(busy) 41 | *.pdfsync 42 | 43 | ## Build tool directories for auxiliary files 44 | # latexrun 45 | latex.out/ 46 | 47 | ## Auxiliary and intermediate files from other packages: 48 | # algorithms 49 | *.alg 50 | *.loa 51 | 52 | # achemso 53 | acs-*.bib 54 | 55 | # amsthm 56 | *.thm 57 | 58 | # beamer 59 | *.nav 60 | *.pre 61 | *.snm 62 | *.vrb 63 | 64 | # changes 65 | *.soc 66 | 67 | # comment 68 | *.cut 69 | 70 | # cprotect 71 | *.cpt 72 | 73 | # elsarticle (documentclass of Elsevier journals) 74 | *.spl 75 | 76 | # endnotes 77 | *.ent 78 | 79 | # fixme 80 | *.lox 81 | 82 | # feynmf/feynmp 83 | *.mf 84 | *.mp 85 | *.t[1-9] 86 | *.t[1-9][0-9] 87 | *.tfm 88 | 89 | #(r)(e)ledmac/(r)(e)ledpar 90 | *.end 91 | *.?end 92 | *.[1-9] 93 | *.[1-9][0-9] 94 | *.[1-9][0-9][0-9] 95 | *.[1-9]R 96 | *.[1-9][0-9]R 97 | *.[1-9][0-9][0-9]R 98 | *.eledsec[1-9] 99 | *.eledsec[1-9]R 100 | *.eledsec[1-9][0-9] 101 | *.eledsec[1-9][0-9]R 102 | *.eledsec[1-9][0-9][0-9] 103 | *.eledsec[1-9][0-9][0-9]R 104 | 105 | # glossaries 106 | *.acn 107 | *.acr 108 | *.glg 109 | *.glo 110 | *.gls 111 | *.glsdefs 112 | *.lzo 113 | *.lzs 114 | 115 | # uncomment this for glossaries-extra (will ignore makeindex's style files!) 116 | # *.ist 117 | 118 | # gnuplottex 119 | *-gnuplottex-* 120 | 121 | # gregoriotex 122 | *.gaux 123 | *.gtex 124 | 125 | # htlatex 126 | *.4ct 127 | *.4tc 128 | *.idv 129 | *.lg 130 | *.trc 131 | *.xref 132 | 133 | # hyperref 134 | *.brf 135 | 136 | # knitr 137 | *-concordance.tex 138 | # TODO Comment the next line if you want to keep your tikz graphics files 139 | *.tikz 140 | *-tikzDictionary 141 | 142 | # listings 143 | *.lol 144 | 145 | # luatexja-ruby 146 | *.ltjruby 147 | 148 | # makeidx 149 | *.idx 150 | *.ilg 151 | *.ind 152 | 153 | # minitoc 154 | *.maf 155 | *.mlf 156 | *.mlt 157 | *.mtc[0-9]* 158 | *.slf[0-9]* 159 | *.slt[0-9]* 160 | *.stc[0-9]* 161 | 162 | # minted 163 | _minted* 164 | *.pyg 165 | 166 | # morewrites 167 | *.mw 168 | 169 | # nomencl 170 | *.nlg 171 | *.nlo 172 | *.nls 173 | 174 | # pax 175 | *.pax 176 | 177 | # pdfpcnotes 178 | *.pdfpc 179 | 180 | # sagetex 181 | *.sagetex.sage 182 | *.sagetex.py 183 | *.sagetex.scmd 184 | 185 | # scrwfile 186 | *.wrt 187 | 188 | # sympy 189 | *.sout 190 | *.sympy 191 | sympy-plots-for-*.tex/ 192 | 193 | # pdfcomment 194 | *.upa 195 | *.upb 196 | 197 | # pythontex 198 | *.pytxcode 199 | pythontex-files-*/ 200 | 201 | # tcolorbox 202 | *.listing 203 | 204 | # thmtools 205 | *.loe 206 | 207 | # TikZ & PGF 208 | *.dpth 209 | *.md5 210 | *.auxlock 211 | 212 | # todonotes 213 | *.tdo 214 | 215 | # vhistory 216 | *.hst 217 | *.ver 218 | 219 | # easy-todo 220 | *.lod 221 | 222 | # xcolor 223 | *.xcp 224 | 225 | # xmpincl 226 | *.xmpi 227 | 228 | # xindy 229 | *.xdy 230 | 231 | # xypic precompiled matrices and outlines 232 | *.xyc 233 | *.xyd 234 | 235 | # endfloat 236 | *.ttt 237 | *.fff 238 | 239 | # Latexian 240 | TSWLatexianTemp* 241 | 242 | ## Editors: 243 | # WinEdt 244 | *.bak 245 | *.sav 246 | 247 | # Texpad 248 | .texpadtmp 249 | 250 | # LyX 251 | *.lyx~ 252 | 253 | # Kile 254 | *.backup 255 | 256 | # gummi 257 | .*.swp 258 | 259 | # KBibTeX 260 | *~[0-9]* 261 | 262 | # TeXnicCenter 263 | *.tps 264 | 265 | # auto folder when using emacs and auctex 266 | ./auto/* 267 | *.el 268 | 269 | # expex forward references with \gathertags 270 | *-tags.tex 271 | 272 | # standalone packages 273 | *.sta 274 | 275 | # Makeindex log files 276 | *.lpz 277 | 278 | *.rds 279 | *.DS_Store 280 | *.pkl 281 | *_files 282 | *__pycache__/ 283 | *.obj 284 | build 285 | *.Rhistory 286 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2024, Michael Betancourt 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are met: 7 | 8 | 1. Redistributions of source code must retain the above copyright notice, this 9 | list of conditions and the following disclaimer. 10 | 11 | 2. Redistributions in binary form must reproduce the above copyright notice, 12 | this list of conditions and the following disclaimer in the documentation 13 | and/or other materials provided with the distribution. 14 | 15 | 3. Neither the name of the copyright holder nor the names of its 16 | contributors may be used to endorse or promote products derived from 17 | this software without specific prior written permission. 18 | 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 22 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 23 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 26 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 27 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | A collection of functions for visualizing Markov chain Monte Carlo output in 2 | `R` with the base graphics library and `python` with `matplotlib`. While the 3 | functions are relatively general they are designed to facilitate the 4 | implementation of Bayesian inference, including visual prior checks, visual 5 | posterior retrodictive checks, and the visualization of marginal posterior 6 | inferences. 7 | 8 | Most of the visualization functions assume that Markov chain Monte Carlo output 9 | is organized into named lists or dictionaries for `R` and `python`, each 10 | consisting of two-dimensional arrays indexed by Markov chain and then iteration. 11 | This format is also used in https://github.com/betanalpha/mcmc_diagnostics. 12 | 13 | Each folder contains an extensive demonstration of the visualization functions 14 | applied to Bayesian inference. -------------------------------------------------------------------------------- /python/_extensions/quarto-ext/include-code-files/_extension.yml: -------------------------------------------------------------------------------- 1 | title: Include Code Files 2 | author: Bruno Beaufils 3 | version: 1.0.0 4 | quarto-required: ">=1.2" 5 | contributes: 6 | filters: 7 | - include-code-files.lua 8 | 9 | 10 | -------------------------------------------------------------------------------- /python/_extensions/quarto-ext/include-code-files/include-code-files.lua: -------------------------------------------------------------------------------- 1 | --- include-code-files.lua – filter to include code from source files 2 | --- 3 | --- Copyright: © 2020 Bruno BEAUFILS 4 | --- License: MIT – see LICENSE file for details 5 | 6 | --- Dedent a line 7 | local function dedent (line, n) 8 | return line:sub(1,n):gsub(" ","") .. line:sub(n+1) 9 | end 10 | 11 | --- Filter function for code blocks 12 | local function transclude (cb) 13 | if cb.attributes.include then 14 | local content = "" 15 | local fh = io.open(cb.attributes.include) 16 | if not fh then 17 | io.stderr:write("Cannot open file " .. cb.attributes.include .. " | Skipping includes\n") 18 | else 19 | local number = 1 20 | local start = 1 21 | 22 | -- change hyphenated attributes to PascalCase 23 | for i,pascal in pairs({"startLine", "endLine"}) 24 | do 25 | local hyphen = pascal:gsub("%u", "-%0"):lower() 26 | if cb.attributes[hyphen] then 27 | cb.attributes[pascal] = cb.attributes[hyphen] 28 | cb.attributes[hyphen] = nil 29 | end 30 | end 31 | 32 | if cb.attributes.startLine then 33 | cb.attributes.startFrom = cb.attributes.startLine 34 | start = tonumber(cb.attributes.startLine) 35 | end 36 | for line in fh:lines ("L") 37 | do 38 | if cb.attributes.dedent then 39 | line = dedent(line, cb.attributes.dedent) 40 | end 41 | if number >= start then 42 | if not cb.attributes.endLine or number <= tonumber(cb.attributes.endLine) then 43 | content = content .. line 44 | end 45 | end 46 | number = number + 1 47 | end 48 | fh:close() 49 | end 50 | -- remove key-value pair for used keys 51 | cb.attributes.include = nil 52 | cb.attributes.startLine = nil 53 | cb.attributes.endLine = nil 54 | cb.attributes.dedent = nil 55 | -- return final code block 56 | return pandoc.CodeBlock(content, cb.attr) 57 | end 58 | end 59 | 60 | return { 61 | { CodeBlock = transclude } 62 | } 63 | 64 | -------------------------------------------------------------------------------- /python/data/multi_data.json: -------------------------------------------------------------------------------- 1 | { 2 | "M": 3, 3 | "N": 100, 4 | "x0": [ 5 | 0, 2, -1 6 | ], 7 | "X": [ 8 | [0.608522, 4.523937, -1.213269], [-1.100340, 1.717995, 1.216851], 9 | [0.907243, 4.090955, -2.601180], [-0.459248, 5.286839, 0.967676], 10 | [-2.238569, 1.619051, 0.805592], [-0.254692, 3.912015, 0.255155], 11 | [-0.812237, 4.531217, -0.202633], [-0.198299, 4.652117, -2.816685], 12 | [-0.110434, 3.012779, 0.740889], [1.949409, 0.099542, -3.728682], 13 | [-4.256711, -1.630418, 3.118961], [4.220311, 0.943876, -4.729621], 14 | [2.169745, 0.921366, -3.978120], [-3.356227, -0.912361, 1.908738], 15 | [-4.633703, 0.575928, 4.140648], [0.309642, 5.771694, 0.913461], 16 | [2.976850, -0.638093, -2.928711], [1.039894, 3.925083, -1.904418], 17 | [-0.445340, 5.943379, -1.928092], [-1.126765, 1.762489, -1.757014], 18 | [-4.029812, 0.956855, 3.419753], [-2.054125, -2.060823, 1.851903], 19 | [1.236093, 0.701136, -2.797659], [-0.108315, 4.767620, -1.884732], 20 | [0.648976, 3.510897, -2.578833], [-1.799139, 1.463486, 0.612964], 21 | [1.584822, 1.470886, -2.515080], [-0.728573, 5.809706, 1.515089], 22 | [-0.900935, 3.927979, 0.279707], [-0.416157, 3.501460, -0.957918], 23 | [4.292351, -0.095997, -4.683472], [2.020347, 0.150212, -2.850894], 24 | [-1.451130, 1.120878, 1.231683], [-0.510480, 7.098177, -0.526673], 25 | [1.942618, 1.248688, -3.154924], [3.800572, -2.209991, -4.574729], 26 | [-1.013159, 4.117045, -0.019127], [0.696202, 3.453109, -2.013209], 27 | [2.413100, 1.038477, -2.824125], [-1.109786, 3.014773, 0.406799], 28 | [1.792731, 1.191022, -3.217938], [1.728705, 1.398100, -3.916014], 29 | [-0.635582, 6.568411, 0.715334], [0.810953, 4.477041, -0.941653], 30 | [-1.069897, 6.066880, 0.217535], [0.686819, 2.256965, -2.884042], 31 | [-2.929787, 1.339995, 0.730474], [-1.715661, 1.027075, -0.382395], 32 | [0.224044, 1.926318, -1.109231], [-0.080724, 4.148897, -1.483574], 33 | [1.254077, 2.064577, -3.328988], [-3.850603, -2.073843, 5.025120], 34 | [-3.172285, -0.319249, 2.830019], [2.401422, 0.137526, -2.180040], 35 | [-2.200748, 0.806180, 1.183484], [1.864080, 1.283475, -0.979642], 36 | [-1.253295, 6.579556, -0.928637], [-1.452363, 4.691053, -0.296871], 37 | [1.676716, 2.520880, -4.112915], [0.206466, 6.675465, 0.045659], 38 | [-0.362306, 4.427619, -1.345846], [2.299673, 0.256240, -3.795914], 39 | [0.268750, 4.977036, -0.580857], [-1.059502, 3.033086, -0.279620], 40 | [0.545579, 6.220928, -1.931763], [-2.373465, 0.224792, 0.649319], 41 | [2.328374, 1.701407, -4.367405], [0.126142, 3.442209, -2.122519], 42 | [0.119665, 5.304590, -2.787035], [-1.092645, 3.204018, 0.221535], 43 | [-3.243951, 2.843787, 2.684053], [-3.270061, -0.751946, 4.597550], 44 | [-1.186092, 2.179126, 0.300057], [1.959061, 1.715324, -3.224113], 45 | [1.235928, 4.152216, -1.974203], [-2.597972, -1.001948, 1.727635], 46 | [-0.416502, 7.237751, -0.452722], [2.278789, 0.919385, -3.941404], 47 | [3.053279, -3.053980, -2.082441], [-2.499473, -3.616267, 3.029635], 48 | [0.573277, 3.690950, -1.470774], [0.686691, 2.468837, -1.321302], 49 | [1.420916, 1.242773, -2.673637], [-0.740451, 3.702616, -0.658349], 50 | [1.867058, -0.154931, -2.447768], [0.949549, 3.794018, -1.884348], 51 | [-0.828483, 4.489934, -1.157857], [6.066433, 2.868394, -6.264665], 52 | [-1.675920, 1.210429, 0.358216], [-0.486353, 4.517126, -0.478259], 53 | [-3.148158, -1.668119, 0.873556], [1.188730, 2.854311, -2.329085], 54 | [-0.668111, 5.322376, -0.454351], [-1.640807, 1.868150, 0.930859], 55 | [-0.034015, 2.438960, -2.222721], [6.275273, 5.540400, -8.346076], 56 | [-0.454522, 4.404316, 2.158436], [1.319745, 2.591975, -3.052576], 57 | [1.088936, 3.664042, -3.144060], [1.007674, 5.317974, -3.120248] 58 | ], 59 | "y": [ 60 | 12.5979605679208, 72.4365143925198, 4.9083679377149, 43.4130074603847, 80.7997814046909, 35.1743238292875, 35.0825267272652, 8.35100836204944, 42.7840361611394, 1.36325661226419, 219.068258273232, 31.7526037550637, 4.41207981924806, 61 | 145.302059745438, 257.971570115353, 27.7855627269194, 12.1367377375291, 7.32884165965262, 17.5330062749366, 15.7742349721585, 62 | 208.585830870455, 129.020240216104, 2.43070003029122, 13.132605790459, 4.49305483844234, 70.2565653666327, 4.07253402141562, 63 | 52.7079291383856, 44.183964456752, 22.7510336510785, 31.8978208561757, 4.70185849223332, 81.325180747388, 29.1156786233861, 64 | 3.83480493303557, 22.6392404453285, 40.2838918071852, 7.39379958136533, 5.28907144737057, 51.2537805171311, 3.33552200718939, 65 | 1.1711588022725, 39.6684507251656, 12.4328457212714, 42.7777046410864, 2.48247339705992, 90.6093773242094, 46.3716911429442, 66 | 18.1714454112314, 14.9865497573223, 0.276564836728407, 302.638407265753, 171.927125340188, 9.66734982583635, 94.1784725337269, 67 | 14.5227682255717, 34.0720961755612, 41.5755710131371, 3.36917657878797, 21.0181274647858, 18.7710767326467, 4.33903887408966, 68 | 18.2804541770747, 38.0574548148327, 12.6372902988313, 82.7222013555676, 8.88978377263224, 8.91629818250989, 9.83321496658544, 69 | 47.6488953445901, 149.575638574643, 250.549232521269, 52.2598851002436, 4.19057258355584, 7.42949996991276, 126.669295135108, 70 | 27.2298693410387, 4.96223443942482, 25.9232249381075, 195.918048136755, 11.5361871859756, 12.5398964622637, 3.70800395159372, 71 | 29.1287033861767, 8.19668372337071, 7.71523283329511, 23.9821437557835, 92.3888421140611, 62.5150266586071, 28.2626505321804, 72 | 107.386755200542, 5.07079077983615, 30.2662961583862, 74.1100392925698, 7.11452794047561, 172.944151373448, 63.281526346813, 73 | 2.69303710871817, 4.81130813358664, 9.64003916227308 74 | ] 75 | } 76 | -------------------------------------------------------------------------------- /python/data/uni_data.json: -------------------------------------------------------------------------------- 1 | { 2 | "N": 1000, 3 | "x": [ 4 | -2.97348027524669, 0.388611920650857, 3.7860167184133, -2.11532505207753, -2.05086426111779, 1.23881091387601, 5 | -0.223011012267395, 2.28712697491343, -1.71278252192451, 2.65437121703705, -0.157489791480464, 1.8615372235093, 6 | 3.36182937623681, 2.42515785635999, 2.17394655624105, -1.07167815033313, -1.79246910131136, -0.673331808215533, 7 | 0.0767325927881738, -1.17522611187832, -1.54122469396116, 1.03805490088226, 1.20632113611642, -0.588043195945986, 8 | -2.51057385712628, -1.15421410027293, -0.169313272238467, -0.983795729820899, -0.112748390035786, -0.490945903936672, 9 | 1.89627259463064, -1.07178596286251, -0.743790057725642, -1.65113466114743, -2.78640384485104, -1.77321590456568, 10 | -3.79541884468883, 2.01601854433318, -0.0681191140282089, 0.827727846676397, -2.1249322628893, -1.95900227246779, 11 | -1.40534105750887, 1.74578939402713, 0.545907562283087, 1.43938493455244, -0.225016809883682, -3.0717390766596, 12 | 1.46069680629352, 1.52702527645707, 0.12467837727999, -1.26969891137703, -1.33237969647966, 0.914817999895252, 13 | -1.34551702046565, -3.86800540178274, -0.158210238584515, 4.72087028629111, -0.136793373482998, -0.917736715813687, 14 | 1.05005400485659, 0.268763684288038, 0.224301850138695, -2.43672149615835, 1.29296496701885, -2.42912244745404, 15 | -1.88057325992304, -0.7932177955986, 1.70398051790464, 0.193090627785851, 1.90121242756078, -2.62870646881024, 16 | 2.26039079216485, 1.72412208692294, 0.446946476893187, 0.265203644584037, 0.721238124794334, -0.918606618211031, 17 | 1.89657363779301, -2.09962105311812, -2.83424381933599, -3.57395272688435, 0.812342582980217, 4.66869220621487, 18 | -0.535485261287095, 1.03498824154688, 0.351051219072201, 0.60520533429147, 1.61275366972448, -0.220888010538226, 19 | -0.680350262877783, 1.00709499800659, 1.619171714507, 1.97269825276133, -2.62434739494803, 1.50718418723262, 20 | -1.38831858836681, 0.137218484961964, -1.69820235009403, -2.28548249544971, 0.0673786015455058, 1.8541110381547, 21 | -2.60441205286796, -2.30781955703601, -0.264886532467073, 0.701379331388585, 2.27808692679609, 2.02314224657252, 22 | -1.4585864704286, 0.360523358385156, 0.902795149729645, -1.83220544565982, 0.972476185372778, 2.69295021418979, 23 | 1.39665161297624, -1.31559352311261, 0.136689051357421, -1.34746228109364, 0.317388338441274, -3.54340788753439, 24 | 3.66218604731205, 1.30627999077202, -1.13912608739532, -0.0259413489532041, -0.148862813618366, 1.7928262943576, 25 | -0.676672992604949, -1.21863175954127, 0.394908336668155, -2.49951629322222, 0.0256821418503826, -0.0522355303183139, 26 | 0.225737316763876, -1.2399362606659, 1.73599978760894, -0.0443916210453394, 2.23462751484444, -0.193356261715294, 27 | 0.493783178918816, 1.66297481382755, -0.732490516837395, 3.48180928998353, 1.159157087239, 0.492780854619329, 28 | -1.79317652186067, 1.81320473993243, 0.714159728887527, 0.998004525208241, 0.664382262045555, 1.35337016263256, 29 | -1.7449119301181, 1.42332712112288, -2.51754485542597, 1.04809359767519, 1.98174076811045, -2.43321725906455, 30 | 1.40118824476981, 1.9357428604516, 2.72601574965036, 1.47089013495148, -1.78023107879238, -0.724896882345517, 31 | -0.686042305022754, 2.6386970511777, 0.658292952633693, 1.24415499259213, -0.580095562762227, -1.53502056956509, 32 | -4.71179500433612, -3.09676250716159, 1.32085518691949, -1.61748680108676, 1.74101247504765, -0.313031105594641, 33 | 1.41848947640336, -3.19243832308249, -0.411577808678408, 1.33853492732756, -1.03059423770006, -0.69646600576326, 34 | -2.76555749664639, 2.18662921422797, 0.598785293067926, 1.99934989471512, -3.36459767447556, 2.56467599621676, 35 | 3.00783539971868, -1.2059770865875, -1.28710119995976, -0.821137908451273, -1.21446209018219, -4.13437693494328, 36 | 1.0001002249913, 2.11585790361668, 3.11015304264666, 1.65723226955571, 2.38912795543375, 0.326596424476767, 37 | -1.02313273526447, 1.82773747638169, -0.373132760765079, -0.786938951780137, 2.52068007369725, 0.781238436104176, 38 | 1.19156785632883, 2.55187505625067, -1.61778741474883, 2.63764864523473, -0.0787123122117819, 0.41687136635166, 39 | -1.44926016686686, 4.02682503634428, -1.27813641144035, -1.70814100928115, -0.460929942133731, 1.35185410101978, 40 | -0.276353912054921, -0.778458081484537, 1.25768029088608, 1.74989246263504, -2.33012768529994, -3.42200071384182, 41 | -0.851764907069444, -2.60427421697768, -0.581461080733049, 1.64597872637312, 2.6903850725163, 0.276060115004754, 42 | 1.15567405600298, -2.38882188149197, 1.85634729955736, -0.00407170169503185, -0.320659364698908, -2.08846573887788, 43 | -2.81331236021652, 0.945082931586096, -1.33556817759153, 2.01851944763724, -0.0317670109811637, -0.151214294537344, 44 | 1.81540935011295, 0.24824878362639, 2.37960348973577, -0.236862893432051, -0.547583794494695, -2.67387261669133, 45 | 0.794677990676838, -0.780460382098712, 1.26951756224412, 3.20589898140238, -1.54521964117249, 0.869353553255022, 46 | -0.974314218155707, -0.307865460430178, 0.794028743191166, 1.51074681840989, 0.325791544540394, -0.54395848542403, 47 | 0.817984892696379, 1.9085543563862, -0.924555435052203, 0.642276991699371, 1.44277199503424, 0.566127037829794, 48 | -0.860891290633549, 1.14990752320774, 3.13923589429483, 0.639115770577305, -1.05609721591993, -0.536648432769677, 49 | 0.0362461147136803, 0.314824207303793, -0.329464770753349, 0.000574324584113506, 1.44539233408089, -0.018818679408215, 50 | -1.1820607129283, 2.38544593988329, -0.0388450751317342, 0.212147787927498, 0.281626905501133, 0.68041596088093, 51 | -1.14601138872579, -1.86143624806395, 1.99933190245016, 0.546101301722465, -0.652429007357909, 1.19786409869744, 52 | -0.65477338576529, -1.12073599568971, 0.272865246538829, -2.28517651362317, -0.349267033988028, -0.56526459861254, 53 | 0.396717274014399, -0.159100430539236, 0.143903439465844, -0.588614896865701, -2.47443458197223, -0.426420894271595, 54 | 2.98650845154638, -3.37748008100567, -0.57509011771876, -0.536945807298771, 0.137839300982697, -1.05585956197602, 55 | -1.24052245292213, -0.247332946908769, 0.699349103503229, 2.62561669742154, 0.659810358895002, 0.546797211824156, 56 | -3.51707517678809, -1.64009374699043, -2.01374189847369, 0.365331927772696, 0.137356434031797, 2.0797850219423, 57 | 0.548544062418155, -0.770088942413554, 0.769317038018945, 1.25704138232117, -0.344926040703071, 2.09099225019243, 58 | 0.72267553424863, 1.50807275990043, -1.70412542233797, 0.0685313171574897, -2.67787468633424, -1.63831127935215, 59 | 1.87574094373709, -4.2842779586635, -2.7245441717373, 1.15940264028257, -1.54248067327084, -0.543130876902085, 60 | -1.29911158214737, 0.490962141969492, -1.51861082181853, -4.63808637618426, -2.26721456600846, 3.12373740301105, 61 | 0.641215774695631, -0.595732115160891, 1.22896899010113, 1.01510712225012, -1.07394851627177, 2.19891104296852, 62 | 0.151102020733846, -1.83590640826489, -1.5087924896006, -1.24408733347682, 1.51925320470776, -1.99424348574826, 63 | 0.756687722293901, 1.45298220858995, -0.00632586882128499, -1.85840416190548, 1.03618322456174, -0.109381033044312, 64 | -1.24786713922668, -1.29371896475317, -2.6375818096717, -0.489402762197171, -2.15928781809171, -1.47383257272719, 65 | 1.33440278583701, 1.03599762025946, 0.420074991364103, -0.60611329807431, 0.0235931389336941, 0.558525734004912, 66 | 1.85221918743635, -0.489412979017564, -1.31178948499042, 1.36430001619883, -0.596081425042378, -2.17675301224551, 67 | 0.845612305473108, 0.781203833865423, 1.21591771567409, 0.131042505582803, -0.947730024882309, -2.58627993878295, 68 | 0.996027689338731, -0.61959459589927, 1.46288669987758, -1.92535650978575, -0.637210197125096, 0.766520852805443, 69 | -2.15614251762138, 0.586190768553997, -3.20941068711069, 2.56320558070399, -1.79962133000013, -2.33039056720364, 70 | 0.714333241478087, 1.87258120120607, -0.0462612979361543, 2.33547569173567, 0.585301987673688, 2.34028555106701, 71 | 0.292747020173378, -2.17650321407879, -0.0156095345650497, 1.86491755398342, 3.64036996412883, 2.35153309314585, 72 | 0.4557419782799, 1.41660561982284, -0.788582734417958, 0.244521987332009, -0.573685994875725, 1.32949815363488, 73 | 0.56284479174236, -0.619630429947286, 2.72427459725633, 0.299106781556197, 2.07688595628409, 1.10331165049588, 74 | 0.0994649322106696, 1.66434871748781, 1.4515416375452, 2.26935884488573, -0.624129934239117, 0.319438220928861, 75 | -0.0922798090484157, -2.36076987032651, -0.994858886325651, -0.0267039997552047, 0.355527661060961, -0.656727326503503, 76 | 0.269693271453237, -0.504681284572652, -0.0805075276665118, -0.38897685384537, 2.16808148399543, 2.65924829403847, 77 | 0.712731581125743, -2.27120955878056, -0.880673034132468, 3.1714437689298, 1.55358118096413, -0.0196840661799863, 78 | 1.36715980945078, 0.0165168476035796, -0.361266535035795, 2.42903272816492, 0.0859738876627339, 3.29357172868433, 79 | 3.9268153049159, -0.0772185685640941, 2.7395994279804, -0.621524334133572, -0.511530465682691, 0.2317516601398, 80 | -1.07753105574493, 0.323154520884174, -1.56027745968308, 1.69237607227489, -2.4928332676874, -0.51411115614987, 81 | 0.523426092901334, -0.792099839062467, -0.661671099193596, -0.963341586004282, -1.025522450437, 0.124942574157214, 82 | 0.615093202983625, 3.59497199231568, -3.07706557229721, 1.02464832228954, 0.575186618136236, 1.14317850503507, 83 | 1.72080166427526, 0.66470445686251, 0.890664328657449, 1.79176604597143, -2.84001585366204, -0.741072818095891, 84 | 0.913830411517674, -1.71263617067831, 2.64978793744459, 0.926203051531795, -3.71744668284093, 1.23754036692694, 85 | -0.952979908925408, 0.740167190624829, 0.704090922168532, -0.147646671799528, -2.22192286882542, 0.600067485892054, 86 | -5.87779189388218, 1.01239913169371, -0.943142683327459, 0.0163412614618589, 1.56428635113983, 2.77802429996028, 87 | 0.999447178649491, 0.902013378357271, -3.47174695926919, 2.4315048023544, 2.24618099610904, 1.46255643730949, 88 | -3.88668125007907, 1.20913199131384, -1.54553864413762, -3.66350515907364, 2.03567431222538, 3.84489703906902, 89 | 1.1689745267362, -2.25941764440645, -3.47538691274868, 3.13534647223833, -0.847105505235109, 0.948440790323099, 90 | -0.205749967336535, -1.5117538509673, 3.94800138129151, 0.346882278019239, 2.83384575562394, -0.409962757092534, 91 | 1.94215408977393, -0.407354155308547, 0.423603861083299, 0.684614142232429, 0.28145935462725, -0.23130455045145, 92 | -0.784331318690699, 0.96186932339141, -0.13559561028754, 0.556408997162904, 0.761343195916989, 2.17134137040358, 93 | 0.11503883186759, 0.732105367990709, -1.75288660173309, 1.83391605663121, 0.823263556771948, 0.0983012929366989, 94 | 1.34081408751966, -1.04282121420697, -1.79979831150086, -0.544538835250276, 0.222478678050219, -0.303416512266726, 95 | -2.57048530724497, 1.13563771266784, 0.91561116605342, -1.34292338373588, 2.31378125909458, -0.733179302432669, 96 | 0.661462005371547, -0.588806729024348, -0.703868502776974, -0.96232138841801, 1.42251265604481, 1.7103708883739, 97 | -4.15475205205501, -1.6735612504075, 1.42752578093264, 0.218198422445086, -0.10326838767214, 1.5445565270668, 98 | 0.28759889475142, 1.03095910641709, 0.613668730326088, 1.50387062303787, -1.40171418770066, 0.788468695457926, 99 | -2.34387100970231, 1.75437034733546, -4.70362715301781, -2.43192951319531, 0.930111579802539, -0.0199751869680365, 100 | 2.97258954401692, -1.24187830307982, -1.90019675177668, -1.87601188040025, -0.436338794020732, -2.48066767572886, 101 | 2.91611221110062, 2.20948683067298, -2.24763303351106, -1.62507757296138, 1.20410490318299, -1.41618385616596, 102 | -1.12379830796754, 1.58357727284914, -0.00653514773391728, 0.407600561443092, 1.75474513265013, -0.59679495487265, 103 | 0.832653481683181, -2.30049014273782, -0.503687069773006, 2.91172854314957, 2.91726675480919, 0.718973300717549, 104 | 2.82408291706945, -0.508369804458931, -0.811035832646499, 2.9411348066735, -1.84961397376983, 4.47714470910093, 105 | -2.4183302782753, -1.67841347960582, 1.37374436738219, -2.90183812661439, 1.28971081578799, -1.51411626239641, 106 | -1.01436482702816, -2.70539138314324, 4.66270192902194, -0.295286123102207, 0.619378956197799, -0.697822650291226, 107 | 1.36732949410492, 1.78865395713018, 1.28750753649489, -0.739113677624461, -1.79301154890639, -0.735351971910027, 108 | 1.39643556933055, 1.5612423882589, -5.78554918602247, 0.524386899748744, 0.154416786814975, -1.48054521824262, 109 | 0.0716974293441585, 1.86258330898638, -0.548051778920168, 0.344847705708142, 0.840976961989663, -1.3607120681393, 110 | -3.80485307394032, -1.02755118660012, 0.360534289424389, 2.74689596012086, 2.60764130540425, 0.767194018579478, 111 | -1.70518929237267, -0.712364629380218, 2.29493297649402, -0.605615506818826, 0.332362920998355, -0.436214033435082, 112 | -0.182310084330898, -3.44892631014521, 3.3900598777517, 0.254911691552688, -0.0789167918721656, -2.69241421027943, 113 | -0.639453364296709, -1.51817770808696, -1.41265669950169, 0.473410999589592, -0.248829654826105, 1.21798883116038, 114 | -1.81760812027381, 2.17070731877953, 1.07921826599463, -2.33222831038585, -3.46513579992463, 3.92438568104819, 115 | 0.946385510252748, -1.06414713927595, 1.04693392221918, 1.0228749923474, 0.22359945890904, -1.06834103348682, 116 | -2.0455614009012, -1.3211067170199, -1.70353706565959, 3.05061253371525, 2.73512747996355, -0.195364444214567, 117 | -0.120224910728617, -1.42701989044147, -0.0753192248329186, 1.47443042824447, -0.716946460084727, -0.413207653648445, 118 | 2.83215758008484, 3.25151687164511, -1.31265283172619, 0.782535615228212, 1.49062636072837, 2.38798954452061, 119 | -1.79354688159849, -0.517580144006596, 0.47740806699988, -1.57842363072541, 0.41905396432482, 2.02818348621818, 120 | 1.51833212610216, -0.447036496304949, -0.819897760490449, -1.01749842802848, 2.18487617606147, 3.43875551724911, 121 | -0.891000291453652, -0.581004986155109, 1.67227080386371, -1.30352198182979, -0.386842760840038, 0.116987567806281, 122 | -2.07360765641767, -0.208050361657903, 1.61096325034053, -0.58250294052089, -0.465335429284823, -1.97990465794318, 123 | -0.284620408515581, -1.5530998115555, -1.23878858786327, -0.167572563237314, -0.0568644651637708, 3.15688391103243, 124 | -1.23671644794502, 0.105670921772999, 1.48415947833025, -2.72520485107597, 1.98845265655787, -3.3044904837512, 125 | 4.14131676280283, 1.8178902554432, 1.67550336917058, -0.419381803312046, -0.112197169774325, -0.946980010534109, 126 | 4.14936428508625, 2.66425747867466, -0.543000323417032, -2.04355766398434, 3.13709487314846, 0.524577783142451, 127 | -0.138060938523938, -0.678931112315457, -0.615333675282163, -1.72591079542673, 1.22437594339284, 4.6432852750905, 128 | -3.02521735992416, 1.76639231111141, 0.0982157533346887, -1.33533903502217, -0.76373384361318, -3.12568945939733, 129 | -1.27831145824198, -2.29111777878401, -0.61495595656245, 2.31573924240428, 1.88810515720046, -0.16112299057416, 130 | 1.79669753994491, 0.539145066471411, 1.34746739716695, 1.23917811945854, 0.749041514337003, 3.50403202425053, 131 | 1.81560815637852, 1.32151743808227, 2.87244704336262, 0.992920712563419, -0.00623748195277366, 1.37655378041298, 132 | -0.214832910350545, -1.96286541312817, -2.94006760610819, -3.48493578060337, 0.344549603775324, -0.291774414574002, 133 | 0.00690337131874758, 0.956580747272851, -0.00469602645420533, -0.805415230075618, 0.884729651885267, -1.4511123180901, 134 | -1.10051192367174, -2.65724721869175, -0.131909255721811, 0.0476886729848612, 3.70437201606473, -3.15913173198366, 135 | -3.6932380567881, -1.30464783351948, -0.543402549717554, -2.75892114539611, 4.28637295067154, -2.59475222282, 136 | 0.338472648229414, 0.00560091954151038, -1.43910326821232, 3.06081655747899, 0.841545044728545, -4.2786640794453, 137 | 1.3698594208894, -1.45564547082023, 0.181439787316659, -0.969669809914332, 1.36612289760876, 1.06326303066042, 138 | -1.5000395198936, 1.97881122726553, 1.0268711681672, -1.66955310586129, -1.10489462434308, 3.43028410466813, 139 | 2.57491264014705, -1.79502652868134, -0.525536578312499, -0.665540178033214, 4.33394161311059, 1.1154086870885, 140 | -0.335063535575263, -0.43145646609064, -1.77676746507487, 0.180413218576034, -1.98406439405249, -1.64570710053155, 141 | 0.673356486515467, 0.193613031684727, 0.973528082373361, 1.5444522530589, -0.738940657600758, -2.24400141644715, 142 | -1.66637395548261, 0.672634795608524, -1.13555387456465, -0.128503692088455, -1.91327774175174, -2.38681759199364, 143 | 2.32751720143219, -2.57017251445944, 1.96033317175985, -1.04878759899483, 0.369912665685496, -0.78028807455502, 144 | 0.658437626205707, -1.19872641508666, -0.242660650149105, -3.13400437436235, 1.82319031561281, 0.255896720161721, 145 | 1.46918234251688, -1.8148651055924, -2.201265296076, -0.559086121036214, 5.87408443561656, 0.00373701758480173, 146 | -1.74721224223806, 1.38586543059863, 0.111039609262691, -0.331404109771393, -0.372158046552632, -1.41167648537365, 147 | 0.108571813324765, -0.011279638125608, -2.11839049429992, -2.20080916208066, -0.237527904692324, -1.68529945180572, 148 | 0.565485887231355, -2.88274454452724, 1.48354585573364, 0.0152616791746361, 1.52877279152779, 0.308439030421961, 149 | 1.27928414854257, 0.479453635763578, 0.459345620466327, 1.14067372099882, 1.56619294684858, -2.39997003750201, 150 | -0.256257036975002, 0.940657312506235, 1.65791195088271, -1.53995884344766, -0.623736651960553, 1.91315210879324, 151 | 2.65155143482239, 0.709098691412881, -0.635909596758476, -1.79334680775731, -0.348761777161998, -1.63540654006067, 152 | 1.1306446609338, 0.248282557674565, 1.18401159104429, 0.496501081002607, -0.50110950412365, -0.958015229794818, 153 | 1.12178323049355, -1.26885514993936, -2.55808361030992, 0.315098257904927, -0.111522233378047, 1.69788217043688, 154 | 1.11792152334535, 0.180605134886399, -1.46707911097077, 2.11934532177035, 1.64626753323918, 2.78210099659422, 155 | 2.87623659445249, -0.109878265051329, -0.745194565810166, 1.84670952453888, -2.07673249829034, 0.713565292897239, 156 | 0.92453019573433, -5.80592454814232, -2.27981346772052, 0.657831599226184, -0.438600122261934, -4.20782012210412, 157 | -0.672239103338481, 1.90023625524656, 3.37838136078018, -0.607457697236948, 0.372544310805443, 0.301215261148968, 158 | -0.615409004908731, -3.50657765641244, -0.721934951854982, -0.947563288026582, -1.36135934446017, -1.60139350813517, 159 | -1.93629142530013, -2.85799646648072, -2.47845631808278, -0.291522060769191, -0.381050554458634, -0.463277477339433, 160 | 2.21082601758247, 0.841936254706115, -1.02962916149882, -0.107923709094317, 1.79105398085333, -0.666745522829573, 161 | 1.17185466043643, -1.41122284339676, 0.938139659472795, 1.10678450092974, 0.483997374578278, -0.350568702256166, 162 | 0.336334282741112, -0.0533178408608041, 0.175246901245738, 0.384012481620346, -2.63026456013059, -1.95265321235176, 163 | -0.0369345782669561, 0.88557890102721, 2.34559476284429, 1.24070878629597, 1.4499325945478, -0.5361816475255, 164 | -0.541448768680158, -3.95638122848992, -0.603832247492927, 0.376262984237162, -3.02547874266899, 1.56250805790875, 165 | -0.728724985167747, 0.819971843185472, -1.44023976568218, -0.836717449525409, 0.254932727201205, -1.23155932301895, 166 | -1.45848921797856, 2.7872343033175, -0.305899559641865, -0.749962023301405, -1.3116298326042, -2.65197519910945, 167 | -2.12158265419702, -1.22339362622324, 0.106554681387868, 2.36839186816052, -0.914935828172229, 1.13643036216014, 168 | 0.507070809373863, 1.06969854788365, 0.362687813900935, 1.84451554586414, -0.220910809494565, 2.9734478692313, 169 | 0.198424317710522, -1.30969238884426, -1.05247365087701, 0.426727349007452, 1.93386626118967, 1.54979360647473, 170 | -1.33510729689451, -0.177014891028765, 1.2529851664247, -0.231989834649266 171 | ], 172 | "y": [ 173 | 4.09321261961032, 0.913029738879507, -1.52000643005669, 4.15034287309354, 2.89639494447778, 0.99912283540669, 174 | 1.08211367985954, -0.29859590717145, 2.88268321969094, -0.860778808032692, 1.91637401086325, 0.030021725482337, 175 | -1.31139948986484, 0.343334498956989, -0.0815208612697721, 2.59117488099716, 3.28569871027358, 1.86907954162689, 176 | 1.0409265858678, 2.59752891961111, 2.76686314446292, -0.106947238939373, 0.425460564126758, 1.82093088502804, 177 | 2.92673426595568, 2.29798039396622, 1.50852440417803, 2.07969573508695, 1.33411795966622, 1.7967545153825, 178 | 0.468450854864287, 2.47706591671285, 1.85570625458699, 3.18519475422936, 3.7082891424054, 3.20710609354119, 179 | 4.21964489862411, 0.173425096751553, 2.14628518232691, 0.510363427767432, 3.08050619414246, 2.64460735587963, 180 | 2.93970588297264, 0.365218360569378, 1.84485048094099, 0.336911451338909, 2.12029618663737, 3.56154417463365, 181 | 0.624366540482505, 0.0783482797383266, 1.57257966771474, 2.13610605336542, 2.55987993357925, 0.741797911140622, 182 | 2.90338837501739, 5.15159455321963, 1.08227598097881, -1.78619306132453, 1.38579774764371, 2.63872030390259, 183 | 0.28552447393337, 1.16039181016147, 1.27853690768405, 3.08862830165274, 0.637883614512416, 3.2763799148464, 184 | 2.8879471709822, 1.66025349792065, -0.0268340443953689, 1.16200414777632, 0.612196717752689, 3.90811343489492, 185 | -0.10026638805403, 0.873311266497441, 0.796144960954674, 1.40260939143972, 0.953702968497859, 2.41273683011668, 186 | -0.0157526911460181, 3.54470859281219, 4.17704143443671, 4.36134275370551, 1.16926487521097, -2.33898470769269, 187 | 1.86486963298553, 0.884383479610751, 1.11340035020301, 1.23828306192409, 0.300560913899301, 1.55042076425748, 188 | 2.45725097749438, 0.663883819268411, 0.289564724864882, -0.632524270762662, 3.59569124406479, -0.0583792642746095, 189 | 2.15487856693163, 1.30819678933378, 2.69011531124074, 3.5811598364605, 1.67129542716692, -0.14867987799989, 190 | 3.64607069483943, 2.6701794546118, 1.58908171690094, 1.0739998985601, -0.37017986293367, -0.428527803469043, 191 | 2.44871278475326, 0.770005877284758, 0.612180101736191, 3.27784639126792, 0.976420357171391, -0.490363732908601, 192 | 0.57978757034066, 2.65949489129565, 1.32490437211952, 2.24201267687948, 1.23312198075701, 3.97506467547112, 193 | -1.35434572507029, 0.388287320992519, 2.70427925003346, 1.72113274493045, 1.48552167204184, 0.0144335307605211, 194 | 1.517875558442, 2.07612072982722, 1.88209811370263, 3.43336274850652, 1.8209759656242, 1.92652522611024, 195 | 0.315248569768283, 2.14442231241001, -0.0396975179680036, 2.22496299887118, 0.196604200013059, 1.50966315208958, 196 | 1.37324323019842, 0.610454349359322, 2.43940059888664, -1.30336321860082, 0.759567619536034, 1.39773880269089, 197 | 2.72404513315737, -0.454470254810424, 1.24939605098303, 1.24572467146646, 0.708582768180385, 0.593227880720287, 198 | 3.41221778282807, 0.982020440933972, 2.68553868779715, 0.641700051118403, 0.161521738942025, 3.11827042235913, 199 | 0.0847845403269621, -0.358128602871222, -0.490222975212125, 0.515923811643469, 2.97800840062333, 2.61748067120503, 200 | 2.59242909901078, -0.217934305198121, 0.963408876685928, -0.179636386757038, 1.76754910759398, 2.81580057814613, 201 | 5.29847181834518, 3.82037110134064, 0.416982460555141, 2.69395540428381, 0.323714890595682, 1.73937223301695, 202 | 0.624222615376499, 4.19848537728514, 1.82128679478557, 0.404936357076049, 2.24819216393239, 1.24635702277362, 203 | 3.27068299382672, 0.0912039846526116, 1.08275900279519, -0.0433683330247073, 3.69492346239896, -0.121391594732788, 204 | -0.606542178029206, 2.71803815487704, 2.20733843938916, 2.27747249869735, 2.15574444468048, 4.93370525730897, 205 | 0.440689728221148, -0.160008725290528, -1.10179595267864, -0.0710193278754955, -0.361336978174347, 1.05337510065607, 206 | 2.39369112462763, 0.115097251083697, 1.67722626558158, 2.08132213999404, -0.367765196804355, 0.642110809420777, 207 | 0.263620428673311, -0.115399531456492, 2.96147904417185, -0.374570196283091, 1.62174524447954, 1.22043485677552, 208 | 2.68498039941255, -1.58741977558664, 1.96107985659395, 2.83178387146323, 1.99934537314918, 1.02879434437607, 209 | 2.40547115076836, 2.29812128365925, 0.362119100658346, 0.586871979130403, 3.58751297581587, 4.02907941895184, 210 | 2.37925333250761, 3.53179572637047, 1.88702419856866, 0.156499551534123, -0.269400167828235, 1.43251536144712, 211 | 0.0678944359104987, 3.96948787963822, 0.313387892867861, 1.64343360063939, 1.55194032853145, 2.73926507777634, 212 | 3.87673401155136, 0.978396423263973, 2.30147645477159, 0.161143914484639, 0.878268658164506, 1.14995421724149, 213 | 0.0344290297473015, 0.297285671390229, -0.924128361865014, 1.49773356535779, 1.42444227990827, 3.62434993258555, 214 | 1.28589866350862, 2.04630745547954, 0.720352504788885, -1.07938611200004, 3.26610591459358, 0.621428671287927, 215 | 1.60088457943365, 1.45016497068544, 0.733699504676149, 0.521107346744342, 2.21897502542792, 1.77306015197449, 216 | -0.0343457892057231, 0.310532680677379, 2.97946206979068, 1.21745067831175, 0.705929181969721, 1.35846209769936, 217 | 2.00536457357482, 0.838774480037818, -1.40941048480838, 0.995969907872874, 2.03251193660109, 1.78125150849814, 218 | 1.81073531242225, 0.740910518715359, 1.63377030166295, 1.19972276018817, 0.57215590577814, 1.65171095014138, 219 | 2.33702790890342, -0.675999940683589, 1.58474258649632, 0.936120489272363, 1.19167895398344, 1.04231487108516, 220 | 2.15102438919314, 3.44031927679386, -0.380917167522604, 0.838805045414338, 2.07965537319571, 0.637784387481786, 221 | 2.29870562579393, 1.23601838415802, 1.87044851081529, 3.08536262879412, 0.934505725309734, 1.68138322982001, 222 | 1.01526342510896, 0.941765930376399, 1.53477732278793, 1.92191884239398, 3.58297691536235, 2.00435196029219, 223 | -0.525311248039306, 4.43459383450627, 1.95528067750403, 2.09404059296556, 1.46177969811782, 2.14713144327006, 224 | 2.70240445989524, 1.57858378715265, 1.36299987197737, -0.758555194447416, 0.992921192004053, 1.00683553062598, 225 | 4.10904478744738, 2.43882976199047, 2.77775940015792, 1.76806886551308, 1.25634935102336, -0.959969117241652, 226 | 1.1640452555003, 2.00237506628269, 0.745093663114181, 0.439020630365179, 1.18890026185967, 0.629524063241482, 227 | 0.964970288203398, 0.535769790867729, 3.19540608827785, 1.23179522013014, 4.03112323967, 2.79882513723854, 228 | 0.182392738679284, 5.46031642933399, 3.84458166225927, 1.12670436959345, 2.32113628571841, 2.05231275299842, 229 | 2.42659530519935, 0.853127502905693, 2.53412620954811, 4.85970812002659, 3.20599976313356, -1.22791801894699, 230 | 0.743701287053884, 1.90857953450979, 0.993460201557671, 0.749843717470442, 2.52607038879533, 0.376173126492325, 231 | 1.78941417916714, 3.24337292164057, 2.95850106533403, 2.65897654709315, 0.503051060725511, 3.22633497681201, 232 | 1.09534001471549, -0.0757077874753784, 1.41078383687826, 2.84722167157215, 1.08877083110023, 1.96107708794485, 233 | 2.52363716324902, 2.23953954298482, 3.60653301204482, 2.11094607978909, 2.72061685628213, 2.34229158016391, 234 | 0.0589158290208395, 0.460769175325292, 0.740616573663058, 2.18449285578637, 0.968932544101162, 1.12299942117837, 235 | -0.379521658139272, 2.06441798774162, 2.42284039795967, 0.48712179517697, 1.86118972590093, 2.83322644298144, 236 | 0.784392192145685, 0.654600875244286, 0.374193824375087, 1.20334959571634, 2.94408206259358, 3.53653416888936, 237 | 0.351306184176725, 2.0223256229209, 0.367490991948104, 3.35489147967852, 1.6824878905876, 0.558160780790123, 238 | 2.86627318467496, 0.901773510087595, 3.70228708296573, -0.362422349415035, 3.06025696385339, 3.14346670487588, 239 | 0.989261662609541, -0.340265366133011, 1.42066224291654, -0.0283938962431776, 1.02633847944213, -0.0860864959259057, 240 | 0.956156760019438, 3.31225197165691, 1.43963530334162, 0.0901406213480833, -1.42698785975014, -0.302734413620216, 241 | 1.07185921018725, 0.266091263321139, 1.41512774864085, 1.22426125971676, 1.0987699926457, 0.0714350748802789, 242 | 0.703632709454185, 1.26546966512123, -0.969328403595323, 1.61689271189736, -0.516920721918932, 0.232805488816433, 243 | 1.28810541986937, -0.143644284201457, -0.122273441357518, 0.0327323993826025, 1.40998723897439, 0.711977972930796, 244 | 1.52272249297501, 3.58767962701002, 2.09328921247643, 1.78253141253804, 1.14347857306659, 2.14883085159507, 245 | 1.0782084009057, 2.05434065252423, 1.62151607373388, 2.22243721267223, -0.302587678052417, -0.806421038531469, 246 | 1.33908051054444, 2.76471130338785, 2.5340891976479, -0.482625806526023, 0.344878190386138, 2.02551161355536, 247 | 0.391417189097671, 1.6911421706425, 1.69188004841594, -0.888191200425209, 1.47687621161585, -0.918537301037894, 248 | -1.43417550786572, 1.69980066200757, -0.73752419838363, 2.18789519735777, 2.03028371922513, 0.368528338124499, 249 | 2.58628076450438, 1.02110555330262, 2.69941559134338, 0.511185986717186, 3.40375258078098, 1.88781631029485, 250 | 1.00203502752984, 1.61465792494404, 1.72586761712736, 2.56645689891174, 2.10453169129281, 0.986755411266939, 251 | 1.21017622564881, -0.574318099640658, 3.88913102147272, 1.14221063245285, 1.10457188322069, 0.740529140724361, 252 | 0.255732067097214, 1.39194108921291, 0.407888743279962, 0.293060322735106, 3.62795377453793, 1.68103595915825, 253 | 1.06918827712726, 2.98391234383537, -0.704192408363461, 1.09640857163994, 4.24028335000687, 0.697943748493506, 254 | 2.01828219055506, 0.963989512191895, 1.05631275404934, 2.48934877936732, 2.9225732606325, 1.01789911175197, 255 | 6.29767770135188, 1.07447341366547, 2.39110964861299, 1.78653041607376, 0.141634247485447, -0.726691144116602, 256 | 0.127233334090803, 0.675174213127952, 3.80920779096633, -0.609678664552043, -0.454032180108705, 0.203080433272608, 257 | 4.15132804188615, 0.494732047339845, 3.02796175324832, 3.67107160809726, -0.31953425105126, -1.37256105287655, 258 | 0.804660198509745, 3.65398718824898, 3.68567635277981, -0.636019816359457, 2.06465290427854, 0.408632979711445, 259 | 1.36794812138867, 2.44048389930693, -1.6031728850044, 0.966576495596317, -0.995792759717186, 2.52978564076606, 260 | 0.354142143293367, 1.37083368286664, 1.58924283121106, 0.860185660585792, 1.32956030611851, 1.64185054377141, 261 | 3.06416642117337, 0.0701731616758758, 1.64892393588888, 1.3173163377083, 1.15762779162927, 0.0757940554046765, 262 | 1.40286409263484, 1.14530206448492, 2.55925615325654, 0.168676145743757, 0.957817107959313, 1.21727549253246, 263 | 1.02639444543396, 2.61422906177331, 3.35056670416635, 2.21953956016956, 1.19933576660051, 1.145724648303, 264 | 3.57837465206737, 0.909847711716077, 1.23196044811668, 2.65645725810815, 0.04386121475556, 2.57485774054118, 265 | 0.772120061861759, 1.27586223492898, 2.50119595006469, 2.14551979031477, 0.401887368817353, -0.061860406809632, 266 | 4.55973820872442, 2.71841185839313, 0.572081237852014, 1.68338346672031, 2.05115175761569, -0.0889252025314778, 267 | 1.37323353226966, 0.796641273336469, 0.551928492040589, 0.194580959422487, 2.23354133073286, 0.596218119470255, 268 | 3.53011073733074, -0.0181316549960668, 4.8044298437824, 3.72043017185584, 1.35848642242917, 1.46406230142204, 269 | -1.55263099699867, 1.93101915668395, 2.76380888991147, 3.01130952877212, 2.44451407363277, 3.15705805812754, 270 | -0.528910604908715, 0.147152776862518, 3.11518815986173, 3.2620107021579, 0.942605611965076, 2.49163671181177, 271 | 2.07429444916949, -0.133787981689672, 1.73079474416316, 0.763076590801216, 0.252648919409172, 1.22777605237725, 272 | 0.848273258989574, 3.62020464891545, 1.66021463734779, -0.529624922351691, -0.901001395790005, 0.885669592949573, 273 | -1.30406191497362, 1.7406154688764, 1.6459723610058, -0.807252835531886, 3.27568467872634, -2.01080900029492, 274 | 3.37377282006872, 2.52792075223291, 0.899124204405803, 4.02646121004849, 0.351720174434834, 2.575234857468, 275 | 2.60625214453976, 3.6035708045419, -2.18263956091509, 1.58848221126227, 1.13996270196348, 1.76397418323567, 276 | 0.734055237254077, 0.350571297680903, 0.859961404694269, 1.88566859436685, 2.45957633888298, 2.45119824168142, 277 | -0.0523309764047473, 0.399798230681928, 6.3490419364817, 1.64255156385048, 1.49681792304193, 3.13595910105686, 278 | 0.8537204261838, -0.0863141135734797, 1.4592410832734, 1.63550787313118, 0.835983426334697, 2.54047880956247, 279 | 4.53513560888056, 2.26967664045368, 1.06971948918117, -0.960617543201271, -0.0760541763728872, 0.83085392344177, 280 | 3.00870443382252, 1.66162265030544, 0.226198587733247, 2.10524619222186, 1.01563493780581, 1.99527631310152, 281 | 1.51136295581848, 3.87100613042309, -0.334084747281979, 1.0236725504708, 2.17285698041277, 3.25349087212019, 282 | 2.37246031569949, 2.70570978887486, 2.27811125961249, 0.817943918001445, 1.43526341487092, 0.987408789532983, 283 | 2.61009511936405, -0.0222594354568378, 0.873306511383256, 3.37665659998833, 4.05981900953633, -1.29827632337998, 284 | 1.51122017866066, 2.17015607552156, 1.15486741111231, 0.351468493696525, 1.65937985305916, 2.62561839549727, 285 | 2.71944709951901, 2.37853031435699, 2.31316586825454, -1.20592196284984, -0.539466117784977, 1.73919494858082, 286 | 1.79293689454218, 2.71137783130984, 1.32783382836477, 0.702105801034454, 3.106352319865, 1.55586573420219, 287 | -0.026084010162573, -1.4189285353373, 1.92735913308255, 0.820207352903071, 0.602473546799985, -0.339102515821603, 288 | 2.91839699269116, 2.52295069742902, 1.66223514270921, 2.64816067674905, 1.20246675113458, -0.356943173322093, 289 | -0.0664119634618731, 1.71279356811939, 1.81562955615722, 1.84355477808967, -0.508848769208184, -0.824394598347882, 290 | 2.26907553957469, 2.36406334532383, 0.238504092743128, 2.15350250898054, 1.79971686906479, 1.38089923413865, 291 | 3.0910945104481, 1.67561118388226, 0.364420430653054, 2.1229284265312, 1.95035391216514, 3.14436646533845, 292 | 1.39669147734954, 2.98320081028047, 2.36332493937895, 1.53642567459095, 1.25382563841528, -1.35617108092112, 293 | 2.12272512886604, 1.96254501360658, 0.219832367872158, 3.46348786875236, -0.230415612808242, 3.76028640287119, 294 | -1.12271752275117, 0.351895941010165, 0.970647511478105, 2.31163411670526, 2.03645577542796, 2.83598228149118, 295 | -1.89260841087687, -0.53904502495578, 1.41976206624078, 3.29560570341916, -1.21667768712223, 0.927420412661057, 296 | 1.21409471376245, 2.25043239607045, 1.67631167819824, 3.19704551350105, 0.513823957579633, -2.19624953992097, 297 | 4.28923163551941, -0.222840003053493, 1.38896810559317, 2.62671423541001, 1.45192977403232, 4.06310800490308, 298 | 2.25807641884351, 3.38501490553395, 1.96568692569537, -0.136642591724362, 0.41683456592576, 1.95719672705817, 299 | 0.284533028777548, 0.612935311414318, 0.799877111048384, 0.279494917288065, 1.2269455649674, -0.746147105261822, 300 | 0.579008099588994, 0.303898362520712, -0.314276525777138, 0.285402257472302, 1.92383504164652, 0.157326164384764, 301 | 2.02611664012673, 2.49082979432319, 4.01757291455758, 3.43751347568533, 0.688632811315826, 1.95176017999056, 302 | 1.83980855434169, 0.556651514245863, 1.42406328434086, 2.23184513071833, 0.299930007168815, 2.48229253646995, 303 | 2.44678173449586, 3.70343587302699, 1.92149613052011, 1.56251853405422, -1.57636984532531, 3.88842968560542, 304 | 3.80570188998941, 2.46686080428244, 1.63695411291694, 3.4108971174609, -2.0116134406481, 3.37257698537968, 305 | 1.30105243678474, 1.05731086666002, 2.59021455252566, -1.00179522201112, 0.925180502253882, 3.89946211329145, 306 | 0.291001136207085, 2.31148797146298, 1.46849952508102, 2.3758500074294, 0.848691296141032, 1.18998068218256, 307 | 2.31149730612413, -0.194463884702655, 0.749236001734463, 3.48820622661055, 1.71233098443215, -1.20339631290637, 308 | -0.842159343398741, 2.84516411189009, 1.53370050152653, 2.21412797656611, -1.65051808904485, 0.670743705192673, 309 | 1.534504058987, 1.96848349425432, 2.9843918352648, 1.26129883310799, 3.11326172391896, 2.79816486023283, 310 | 0.85891056952982, 1.89546067715942, 0.656581154896421, 0.386238176935947, 2.07220863757888, 2.94126080396632, 311 | 2.99702495554191, 0.851431788224715, 2.27046268741267, 1.46243435711124, 3.03072771007654, 3.15452485097099, 312 | -0.0864231778928764, 3.35546186972655, 0.00124819913763669, 2.39079727449894, 0.865007861781265, 2.03567833084434, 313 | 1.40874924337594, 2.01454832891051, 1.39908376510079, 3.79781400915645, 0.186088868350486, 1.90250439193913, 314 | -0.0952789329526168, 2.61807996745755, 2.92302551919088, 1.76302693132054, -2.96911272182897, 1.64364992162355, 315 | 2.77450788564301, 1.02271681426266, 1.28390986857272, 1.53736645786611, 1.63881459939964, 2.35253355821583, 316 | 1.41794695147816, 1.66564742320104, 3.24887110461376, 3.2461598291219, 1.0738338428779, 2.82330163145636, 317 | 2.02582592695775, 3.17029013053227, 0.511802130402621, 1.38194487123292, 0.390532399921275, 1.33742025901059, 318 | 0.802992327958725, 1.93264667854889, 1.42918012780516, 1.09678050565504, 0.344529656365481, 3.88157559185623, 319 | 1.27357643992406, 0.627348867492911, 0.305016631247095, 3.05435191478174, 2.25947265845383, -0.206834631338269, 320 | -0.559838796700316, 1.12414463815155, 1.12667852850332, 3.01135687077758, 1.93966594054807, 3.20557893558127, 321 | 0.502699975015689, 1.2017667184532, 0.547162222736257, 1.16309615218679, 2.17342550521084, 2.22720008487568, 322 | 0.750103737668447, 2.22467235973635, 3.33895634538646, 1.17210736914905, 1.63596333397612, 0.0726244543591358, 323 | 0.0559976131950194, 1.61447317135411, 2.5377597819351, -0.149537356883677, -0.0840392465192093, -0.0778150454409202, 324 | -0.211731612422485, 1.37428405817172, 1.5087392041638, 0.161946460827746, 2.95646049010091, 0.78346799269428, 325 | 0.276799220053059, 5.65122423104723, 3.53407083442801, 1.17810096759635, 1.7115616956669, 4.38335554272312, 326 | 1.94370565349226, 0.0651692059676496, -1.30306557770104, 1.81019755681093, 1.30804622558708, 1.52590709960593, 327 | 2.02163038640456, 3.61301408176047, 1.61499483469986, 2.19074334390178, 2.93740831987617, 2.33026177200824, 328 | 3.31550271830491, 4.57329252967054, 3.77584659598598, 1.67384197943036, 2.4281793371634, 1.3498125356525, 329 | -0.0504889396121124, 0.941661369971904, 2.15945876781595, 1.66546788374136, -0.12353678567133, 2.56462278255081, 330 | 0.324319610339132, 2.89714855874152, 0.492831133323445, 0.866030275308876, 1.56203602920506, 2.13256841238061, 331 | 1.74452895927964, 0.958297420626964, 1.24193279477275, 2.01567690740236, 3.33804637512299, 2.85527672590805, 332 | 1.42583484495621, 0.707403759124224, 0.0795870019756514, 0.330711074301718, -0.0234985199962274, 1.91832474047302, 333 | 1.58142417983675, 5.02445767637512, 2.07326510107575, 0.737176925491965, 3.67903497996156, 0.308862896539812, 334 | 2.18863589630007, 0.514878286852934, 2.69487671921054, 2.10965929765648, 1.41454776325155, 2.24785234337198, 335 | 2.90924619258157, -1.12316622929532, 2.30474136633931, 2.63223757856747, 2.37158143160764, 3.3031567426752, 336 | 3.84036875615065, 2.6252789958729, 1.33058755568246, -0.0978812122484387, 1.70976088065832, 0.288670521370106, 337 | 1.3474466265222, 0.22027118522591, 1.33119560001082, -0.349447081702539, 1.47138889743383, -0.532429822287273, 338 | 1.12012143589041, 2.44632132915787, 2.02233401560028, 1.3534181227979, 0.0531170333324189, -0.00638330366437717, 339 | 2.44918645949934, 1.3445612900376, 0.908363780639696, 1.26105845093861 340 | ] 341 | } 342 | -------------------------------------------------------------------------------- /python/demo.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/betanalpha/mcmc_visualization_tools/7143752d8e6fa99f4ec63959acb45eab68d87cbd/python/demo.pdf -------------------------------------------------------------------------------- /python/demo.qmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Markov Chain Monte Carlo Visualization Functions" 3 | author: "Michael Betancourt" 4 | date: "May 2024" 5 | toc: true 6 | number-sections: true 7 | highlight: pygments 8 | crossref: 9 | lst-title: "Stan Program" 10 | filters: 11 | - include-code-files 12 | format: 13 | html: 14 | html-math-method: katex 15 | theme: 16 | - lux 17 | - custom.scss 18 | standalone: true 19 | embed-resources: true 20 | code-overflow: wrap 21 | linkcolor: "#B97C7C" 22 | pdf: 23 | keep-tex: true 24 | fig-width: 5.5 25 | fig-height: 5.5 26 | code-overflow: wrap 27 | monofontoptions: 28 | - Scale=0.5 29 | jupyter: pystan3_env 30 | format-links: false 31 | --- 32 | 33 | In this note I will review a suite of `python` functions that implement various 34 | visualizations of probabilistic behavior using the output of a Markov chain 35 | Monte Carlo algorithm. 36 | 37 | Most of these visualizations utilize nested quantile intervals to visualize 38 | one-dimensional pushforward behavior as described in 39 | [Chapter 7, Section 5](https://betanalpha.github.io/assets/chapters_html/transforming_probability_spaces.html#sec:1d-pushforward-characterizations) 40 | of my probability theory material. The individual quantiles are consistently 41 | estimated as the empirical average of the empirical quantiles derived from 42 | individual Markov chains. Because they do not communicate the quantile 43 | estimator errors these visualizations can be misleading if the Markov chains do 44 | not contain enough information. 45 | 46 | # Initial Setup 47 | 48 | First and foremost we have to set up our local `python` environment. 49 | 50 | ```{python} 51 | import matplotlib 52 | import matplotlib.pyplot as plot 53 | plot.rcParams['figure.figsize'] = [5, 2.5] 54 | plot.rcParams['figure.dpi'] = 100 55 | plot.rcParams['font.family'] = "Serif" 56 | 57 | import numpy 58 | import scipy.stats as stats 59 | import math 60 | 61 | import json 62 | ``` 63 | 64 | This includes loading up `pystan`. 65 | 66 | ```{python} 67 | # Needed to run pystan through a jupyter kernel 68 | import nest_asyncio 69 | nest_asyncio.apply() 70 | 71 | import stan 72 | ``` 73 | 74 | Finally we'll load my recommended 75 | [Markov chain Monte Carlo analysis tools](https://github.com/betanalpha/mcmc_diagnostics) 76 | and the visualization functions themselves. 77 | 78 | ```{python} 79 | import mcmc_analysis_tools_pystan3 as util 80 | ``` 81 | 82 | ```{python} 83 | import mcmc_visualization_tools as putil 84 | ``` 85 | 86 | # One-Dimensional Baseline Function 87 | 88 | Our first example is one-dimensional curve-fitting, i.e. regression, model with 89 | a linear baseline function, 90 | $$ 91 | p(y_{n} \mid x_{n}, \alpha, \beta, \sigma) 92 | = 93 | \text{normal}(y_{n} \mid \alpha + \beta \, x_{n}, \sigma). 94 | $$ 95 | 96 | ## Data Exploration 97 | 98 | What is data if not an opportunity to explore? 99 | 100 | ```{python} 101 | with open("data/uni_data.json","r") as infile: 102 | data = json.load(infile) 103 | ``` 104 | 105 | The `plot_line_hist` function constructs a histogram but then plots only its 106 | outline, without lines separating the interior histogram bins. Here we can 107 | plot histograms summarizing the observed inputs 108 | $$ 109 | \{ \tilde{x}_{1}, \ldots, \tilde{x}_{n}, \ldots, \tilde{x}_{N} \} 110 | $$ 111 | and the observed outputs, 112 | $$ 113 | \{ \tilde{y}_{1}, \ldots, \tilde{y}_{n}, \ldots, \tilde{y}_{N} \}. 114 | $$ 115 | 116 | ```{python} 117 | f, axarr = plot.subplots(1, 2, layout="constrained") 118 | putil.plot_line_hist(axarr[0], data['x'], -6, 6, 0.5, 119 | xlabel="x", title="Observed Inputs") 120 | putil.plot_line_hist(axarr[1], data['y'], -4, 7, 0.5, 121 | xlabel="y", title="Observed Outputs") 122 | plot.show() 123 | ``` 124 | 125 | This presentation is a bit cleaner than conventional histogram plots, especially 126 | when there is no ambiguity about the binning. 127 | 128 | We can also plot histograms with arbitrary bin configurations. 129 | 130 | ```{python} 131 | f, axarr = plot.subplots(1, 2, layout="constrained") 132 | putil.plot_line_hist(axarr[0], data['x'], 133 | breaks=[-6, -3, -1.5, -0.75, 0, 0.75, 1.5, 3, 6], 134 | xlabel="x", title="Observed Inputs") 135 | putil.plot_line_hist(axarr[1], data['y'], 136 | breaks=[-4, 0, 7], 137 | xlabel="y", title="Observed Outputs") 138 | plot.show() 139 | ``` 140 | 141 | A key advantage of reducing a histogram to its outline is that it is much easier 142 | to overlay multiple histograms on top of each other without compromising 143 | legibility. The `plot_line_hists` function constructs and then overlays two 144 | histograms with the same binning. 145 | 146 | ```{python} 147 | putil.plot_line_hists(plot.gca(), data['x'], data['y'], -6, 7, 0.5) 148 | plot.gca().text(-3.5, 90, "Observed\nInputs", color="black") 149 | plot.gca().text(3.5, 90, "Observed\nOutputs", color=putil.mid_teal) 150 | plot.show() 151 | ``` 152 | 153 | We can also use the `add` argument of `plot_line_hist` to overlay multiple 154 | histogram outlines onto an existing axis. 155 | 156 | ```{python} 157 | putil.plot_line_hist(plot.gca(), data['x'], -6, 6, 0.5, 158 | col="black", add=True) 159 | putil.plot_line_hist(plot.gca(), data['y'], -4, 7, 0.5, 160 | col=putil.mid_teal, add=True) 161 | 162 | plot.gca().text(-3.5, 90, "Observed\nInputs", color="black") 163 | plot.gca().text(3.5, 90, "Observed\nOutputs", color=putil.mid_teal) 164 | 165 | plot.gca().set_xlim([-6, 7]) 166 | plot.gca().set_xlabel("") 167 | plot.gca().set_ylim([0, 160]) 168 | plot.gca().set_ylabel("Counts") 169 | 170 | plot.show() 171 | ``` 172 | 173 | ## Prior Checks 174 | 175 | Here we'll be exceptionally thorough and start with an investigation of the 176 | prior model and its consequences. In addition to the individual parameters 177 | we'll look at the prior behavior of baseline function and the prior predictive 178 | distribution along a grid of inputs defined by the `x_grid` array. 179 | 180 | ```{.stan include="stan_programs/uni_prior_model.stan" filename="uni\\_prior\\_model.stan" eval=FALSE} 181 | ``` 182 | 183 | Note that I'm using a less-aggressive step size adaptation here because the 184 | half-normal prior model for $\sigma$ results in an slightly awkward tail for the 185 | unconstrained $\log(\sigma)$ values that can be a bit difficult to navigate. 186 | 187 | ```{python } 188 | #| warning: false 189 | #| message: false 190 | data['N_grid'] = 1000 191 | data['x_grid'] = numpy.arange(-6, 6, 12 / data['N_grid']) 192 | 193 | with open('stan_programs/uni_prior_model.stan', 'r') as file: 194 | stan_program = file.read() 195 | model = stan.build(stan_program, random_seed=5838299, data=data) 196 | fit = model.sample(num_samples=1024, refresh=0, delta=0.9) 197 | ``` 198 | 199 | Of course we always consult our diagnostics first to make sure that our Markov 200 | chains, and hence any visualization we derive from them, accurately characterize 201 | the exact target distribution, in this case the prior distribution of our model. 202 | 203 | ```{python} 204 | diagnostics = util.extract_hmc_diagnostics(fit) 205 | util.check_all_hmc_diagnostics(diagnostics) 206 | 207 | samples = util.extract_expectand_vals(fit) 208 | base_samples = util.filter_expectands(samples, 209 | ['alpha', 'beta', 'sigma']) 210 | util.check_all_expectand_diagnostics(base_samples) 211 | ``` 212 | 213 | We can visualize the probability distribution of baseline functions in two ways. 214 | Firstly we can plot a subset of baseline function configurations. Secondly we 215 | can plot nested quantile intervals that quantify the marginal behavior of the 216 | function output at each input. Neither of these visualizations fully 217 | characterize the probabilistic behavior but together they capture the most 218 | important features. 219 | 220 | The `plot_realizations` function plots a selection of values corresponding to 221 | the `f_names` array against `data$x_grid` while the 222 | `plot_conn_pushforward_quantiles` function plots nested quantile intervals of 223 | those values for each element of `data['x_grid']`. Here "conn" refers to 224 | "connected" as the individual marginal quantiles are connected into continuous 225 | polygons. 226 | 227 | ```{python} 228 | f, axarr = plot.subplots(1, 2, layout="constrained") 229 | 230 | f_names = [ f'f_grid[{n + 1}]' for n in range(data['N_grid']) ] 231 | putil.plot_realizations(axarr[0], samples, f_names, data['x_grid'], 232 | xlabel="x", ylabel="f") 233 | putil.plot_conn_pushforward_quantiles(axarr[1], samples, 234 | f_names, data['x_grid'], 235 | xlabel="x", ylabel="f") 236 | 237 | plot.show() 238 | ``` 239 | 240 | Finally let's use the `plot_conn_pushforward_quantiles` function to plot nested 241 | quantile intervals of the conditional prior predictive behavior at each element 242 | of `data$x_grid`. 243 | 244 | ```{python} 245 | pred_names = util.name_nested_list('y_pred_grid', [data['N_grid']]) 246 | putil.plot_conn_pushforward_quantiles(plot.gca(), samples, 247 | pred_names, data['x_grid'], 248 | xlabel="x", ylabel="f") 249 | plot.show() 250 | ``` 251 | 252 | ## Posterior Inference 253 | 254 | Having thoroughly investigated our prior model and its consequences and not 255 | found any undesired behavior we can move on to constructing posterior 256 | inferences. 257 | 258 | ```{.stan include="stan_programs/uni_full_model.stan" filename="uni\\_full\\_model.stan" eval=FALSE} 259 | ``` 260 | 261 | ```{python} 262 | #| warning: false 263 | #| message: false 264 | with open('stan_programs/uni_full_model.stan', 'r') as file: 265 | stan_program = file.read() 266 | model = stan.build(stan_program, random_seed=5838299, data=data) 267 | fit = model.sample(num_samples=1024, refresh=0) 268 | ``` 269 | 270 | There are no signs of trouble from the computational diagnostics. 271 | 272 | ```{python} 273 | diagnostics = util.extract_hmc_diagnostics(fit) 274 | util.check_all_hmc_diagnostics(diagnostics) 275 | 276 | samples = util.extract_expectand_vals(fit) 277 | base_samples = util.filter_expectands(samples, 278 | ['alpha', 'beta', 'sigma']) 279 | util.check_all_expectand_diagnostics(base_samples) 280 | ``` 281 | 282 | Before examining any posterior inferences, however, we need to validate that our 283 | model is adequately capturing the relevant features of the observed data. For 284 | this one-dimensional baseline function model we can implement an informative 285 | retrodictive check by comparing the conditional posterior predictive 286 | distributions at each input, 287 | $$ 288 | p(y \mid x, 289 | \tilde{x}_{1}, \tilde{y}_{1}, \ldots, \tilde{x}_{N}, \tilde{y}_{N}), 290 | $$ 291 | to the observed input-output pairs, $(x_{n}, y_{n})$. 292 | 293 | ```{python} 294 | pred_names = [ f'y_pred_grid[{n + 1}]' for n in range(data['N_grid']) ] 295 | putil.plot_conn_pushforward_quantiles(plot.gca(), samples, 296 | pred_names, data['x_grid'], 297 | xlabel="x", ylabel="f") 298 | plot.scatter(data['x'], data['y'], s=1, color="white", zorder=4) 299 | plot.scatter(data['x'], data['y'], s=0.8, color="black", zorder=4) 300 | 301 | plot.show() 302 | ``` 303 | 304 | Fortunately there are no signs of tension between the posterior predictive 305 | distributional behaviors and the observed behaviors. Confident in the adequacy 306 | of our model we can move onto visualizing posterior inferences. 307 | 308 | For example we can visualize the pushforward, or marginal, probability 309 | distributions for each parameter. Note that the `plot_expectand_pushforward` 310 | function is already part of my Markov chain Monte Carlo analysis tools and not 311 | one of the visualization functions being introduced here. 312 | 313 | ```{python} 314 | f, axarr = plot.subplots(1, 3, layout="constrained") 315 | 316 | util.plot_expectand_pushforward(axarr[0], samples['alpha'], 317 | 25, display_name="alpha") 318 | 319 | util.plot_expectand_pushforward(axarr[1], samples['beta'], 320 | 25, display_name="beta") 321 | 322 | util.plot_expectand_pushforward(axarr[2], samples['sigma'], 323 | 25, display_name="sigma") 324 | 325 | plot.show() 326 | ``` 327 | 328 | Communicating the posterior behavior of the baseline function, however, is 329 | facilitated with the new visualization functions. 330 | 331 | ```{python} 332 | f, axarr = plot.subplots(1, 2, layout="constrained") 333 | 334 | f_names = [ f'f_grid[{n + 1}]' for n in range(data['N_grid']) ] 335 | putil.plot_realizations(axarr[0], samples, f_names, data['x_grid'], 336 | xlabel="x", ylabel="f") 337 | putil.plot_conn_pushforward_quantiles(axarr[1], samples, 338 | f_names, data['x_grid'], 339 | xlabel="x", ylabel="f") 340 | 341 | plot.show() 342 | ``` 343 | 344 | Conveniently all of these visualization functions feature optional arguments 345 | for baseline behavior which allows us to compare our posterior inferences to 346 | the true behavior when it is known, for example in simulation studies. 347 | 348 | ```{python} 349 | true_alpha = 1.5 350 | true_beta = -0.75 351 | true_sigma = 0.33 352 | 353 | f, axarr = plot.subplots(1, 3, layout="constrained") 354 | 355 | util.plot_expectand_pushforward(axarr[0], samples['alpha'], 356 | 25, display_name="alpha", 357 | baseline=true_alpha, 358 | baseline_color=putil.mid_teal) 359 | 360 | util.plot_expectand_pushforward(axarr[1], samples['beta'], 361 | 25, display_name="beta", 362 | baseline=true_beta, 363 | baseline_color=putil.mid_teal) 364 | 365 | util.plot_expectand_pushforward(axarr[2], samples['sigma'], 366 | 25, display_name="sigma", 367 | baseline=true_sigma, 368 | baseline_color=putil.mid_teal) 369 | 370 | plot.show() 371 | ``` 372 | 373 | ```{python} 374 | true_fs = [ true_alpha + true_beta * x for x in data['x_grid'] ] 375 | 376 | f, axarr = plot.subplots(1, 2, layout="constrained") 377 | putil.plot_realizations(axarr[0], samples, 378 | f_names, data['x_grid'], 379 | baseline_values=true_fs, 380 | baseline_color=putil.mid_teal, 381 | xlabel="x", ylabel="f") 382 | putil.plot_conn_pushforward_quantiles(axarr[1], samples, 383 | f_names, data['x_grid'], 384 | baseline_values=true_fs, 385 | baseline_color=putil.mid_teal, 386 | xlabel="x", ylabel="f") 387 | 388 | plot.show() 389 | ``` 390 | 391 | The `plot_realizations` and `plot_conn_pushforward_quantiles` functions also 392 | include `residual` arguments that allow us to directly visualize how the 393 | probabilistic behavior varies around the baseline values. 394 | 395 | ```{python} 396 | f, axarr = plot.subplots(1, 2, layout="constrained") 397 | putil.plot_realizations(axarr[0], samples, 398 | f_names, data['x_grid'], 399 | baseline_values=true_fs, 400 | residual=True, 401 | xlabel="x", ylabel="f") 402 | putil.plot_conn_pushforward_quantiles(axarr[1], samples, 403 | f_names, data['x_grid'], 404 | residual=True, 405 | baseline_values=true_fs, 406 | xlabel="x", ylabel="f") 407 | 408 | plot.show() 409 | ``` 410 | 411 | # Multi-Dimensional Baseline Function 412 | 413 | Now that we're warmed up let's consider a three-dimensional curve-fitting model 414 | with a quadratic baseline function, 415 | $$ 416 | p(y_{n} \mid \mathbf{x}_{n}, \alpha, \beta, \sigma) 417 | = 418 | \text{normal}(y_{n} \mid \beta_{0} 419 | + \boldsymbol{\beta}^{T} \cdot \mathbf{x} 420 | + \mathbf{x}^{T} \cdot \mathbf{B} \cdot \mathbf{x}, \sigma), 421 | $$ 422 | where $\mathbf{B}$ is a positive-definite matrix whose three diagonal elements 423 | are organized into the vector $\boldsymbol{\beta}_{d}$ and three off-diagonal 424 | elements are organized into the vector $\boldsymbol{\beta}_{o}$. 425 | 426 | ## Plot Data 427 | 428 | The `plot_line_hist` allows us to cleanly visualize each component of the 429 | observed inputs. 430 | 431 | ```{python} 432 | with open("data/multi_data.json","r") as infile: 433 | data = json.load(infile) 434 | data['X'] = numpy.asarray(data['X']) 435 | 436 | 437 | f, axarr = plot.subplots(1, 3, layout="constrained") 438 | 439 | putil.plot_line_hist(axarr[0], data['X'][:,0], -9, 9, 1, xlabel="x1") 440 | putil.plot_line_hist(axarr[1], data['X'][:,1], -9, 9, 1, xlabel="x2") 441 | putil.plot_line_hist(axarr[2], data['X'][:,2], -9, 9, 1, xlabel="x3") 442 | 443 | plot.show() 444 | 445 | f, axarr = plot.subplots(2, 3, layout="constrained") 446 | 447 | axarr[0, 0].scatter(data['X'][:,0], data['X'][:,1], color="black", s=2) 448 | axarr[0, 0].set_xlim([-9, 9]) 449 | axarr[0, 0].set_xlabel("x1") 450 | axarr[0, 0].set_ylim([-9, 9]) 451 | axarr[0, 0].set_ylabel("x2") 452 | 453 | axarr[0, 1].scatter(data['X'][:,0], data['X'][:,2], color="black", s=2) 454 | axarr[0, 1].set_xlim([-9, 9]) 455 | axarr[0, 1].set_xlabel("x1") 456 | axarr[0, 1].set_ylim([-9, 9]) 457 | axarr[0, 1].set_ylabel("x3") 458 | 459 | axarr[0, 2].scatter(data['X'][:,1], data['X'][:,2], color="black", s=2) 460 | axarr[0, 2].set_xlim([-9, 9]) 461 | axarr[0, 2].set_xlabel("x2") 462 | axarr[0, 2].set_ylim([-9, 9]) 463 | axarr[0, 2].set_ylabel("x3") 464 | 465 | axarr[1, 0].scatter(data['X'][:,0], data['y'], color="black", s=2) 466 | axarr[1, 0].set_xlim([-9, 9]) 467 | axarr[1, 0].set_xlabel("x1") 468 | axarr[1, 0].set_ylim([-25, 325]) 469 | axarr[1, 0].set_ylabel("y") 470 | 471 | axarr[1, 1].scatter(data['X'][:,1], data['y'], color="black", s=2) 472 | axarr[1, 1].set_xlim([-9, 9]) 473 | axarr[1, 1].set_xlabel("x2") 474 | axarr[1, 1].set_ylim([-25, 325]) 475 | axarr[1, 1].set_ylabel("y") 476 | 477 | axarr[1, 2].scatter(data['X'][:,2], data['y'], color="black", s=2) 478 | axarr[1, 2].set_xlim([-9, 9]) 479 | axarr[1, 2].set_xlabel("x3") 480 | axarr[1, 2].set_ylim([-25, 325]) 481 | axarr[1, 2].set_ylabel("y") 482 | 483 | plot.show() 484 | ``` 485 | 486 | ## Prior Checks 487 | 488 | As before we'll first investigate the consequences of our prior model. 489 | 490 | For a discussion of why the quadratic baseline model is implemented in this way 491 | see Section 2.3.2 of my 492 | [Taylor regression modeling chapter](https://betanalpha.github.io/assets/case_studies/taylor_models.html#232_Higher-Order_Implementations). 493 | 494 | ```{.stan include="stan_programs/multi_prior_model.stan" filename="multi\\_prior\\_model.stan" eval=FALSE} 495 | ``` 496 | 497 | ```{python} 498 | #| warning: false 499 | #| message: false 500 | with open('stan_programs/multi_prior_model.stan', 'r') as file: 501 | stan_program = file.read() 502 | model = stan.build(stan_program, random_seed=5838299, data=data) 503 | fit = model.sample(num_samples=1024, refresh=0) 504 | ``` 505 | 506 | Higher-dimensional probability distributions are no trouble for Hamiltonian 507 | Monte Carlo. 508 | 509 | ```{python} 510 | diagnostics = util.extract_hmc_diagnostics(fit) 511 | util.check_all_hmc_diagnostics(diagnostics) 512 | 513 | samples = util.extract_expectand_vals(fit) 514 | base_samples = util.filter_expectands(samples, 515 | ['beta0', 'beta1', 516 | 'beta2_d', 'beta2_o', 517 | 'sigma'], 518 | True) 519 | util.check_all_expectand_diagnostics(base_samples) 520 | ``` 521 | 522 | With a multi-dimensional input space we can no longer visualize the baseline 523 | functional behavior nor the conditional prior predictive behavior directly. 524 | We can, however, visualize many of its features. 525 | 526 | For example we might consider the marginal behavior of the predicted outputs, 527 | regardless of the corresponding observed inputs. Here we'll summarize this 528 | marginal behavior with a histogram, and use the `plot_hist_quantiles` function 529 | to visualize the prior predictive distribution of the histogram counts. 530 | 531 | ```{python} 532 | putil.plot_hist_quantiles(plot.gca(), samples, 'y_pred') 533 | plot.show() 534 | ``` 535 | 536 | We can also set the binning by hand, either with bounds a fixed bin width 537 | or an arbitrary bin configuration. 538 | 539 | ```{python} 540 | putil.plot_hist_quantiles(plot.gca(), samples, 'y_pred', -400, 400, 50) 541 | plot.show() 542 | ``` 543 | 544 | ```{python} 545 | putil.plot_hist_quantiles(plot.gca(), samples, 'y_pred', 546 | breaks=[-400, -200, -100, -50, -25, 0, 547 | 25, 50, 100, 200, 400]) 548 | plot.show() 549 | ``` 550 | 551 | To capture the interactions between the predictive outputs and the observed 552 | input components we'll need a more sophisticated summary statistic. Here we'll 553 | use the empirical mean and medians of the predictive outputs within bins of each 554 | input component. For a detailed discussion of how this summary statistic is 555 | constructed see Section 2.5 of my 556 | [Taylor regression modeling chapter](https://betanalpha.github.io/assets/case_studies/taylor_models.html#25_Posterior_Retrodictive_Checks). 557 | 558 | Conveniently the `plot_conditional_mean_quantiles` and 559 | `plot_conditional_median_quantiles` functions visualize the prior predictive 560 | behavior of these summary statistics. 561 | 562 | ```{python} 563 | f, axarr = plot.subplots(1, 3, layout="constrained") 564 | f.suptitle("Marginal Quantiles of Conditional Means") 565 | 566 | pred_names = [ f'y_pred[{n + 1}]' for n in range(data['N']) ] 567 | putil.plot_conditional_mean_quantiles(axarr[0], samples, pred_names, 568 | data['X'][:,0], -9, 9, 1, 569 | xlabel="x1", ylabel="") 570 | putil.plot_conditional_mean_quantiles(axarr[1], samples, pred_names, 571 | data['X'][:,1], -9, 9, 1, 572 | xlabel="x2", ylabel="") 573 | putil.plot_conditional_mean_quantiles(axarr[2], samples, pred_names, 574 | data['X'][:,2], -9, 9, 1, 575 | xlabel="x3", ylabel="") 576 | plot.show() 577 | ``` 578 | 579 | ```{python} 580 | f, axarr = plot.subplots(1, 3, layout="constrained") 581 | f.suptitle("Marginal Quantiles of Conditional Medians") 582 | 583 | pred_names = [ f'y_pred[{n + 1}]' for n in range(data['N']) ] 584 | putil.plot_conditional_median_quantiles(axarr[0], samples, pred_names, 585 | data['X'][:,0], -9, 9, 1, 586 | xlabel="x1", ylabel="") 587 | putil.plot_conditional_median_quantiles(axarr[1], samples, pred_names, 588 | data['X'][:,1], -9, 9, 1, 589 | xlabel="x2", ylabel="") 590 | putil.plot_conditional_median_quantiles(axarr[2], samples, pred_names, 591 | data['X'][:,2], -9, 9, 1, 592 | xlabel="x3", ylabel="") 593 | plot.show() 594 | ``` 595 | 596 | ## Posterior Inference 597 | 598 | Now we're ready to incorporate the observed data. 599 | 600 | ```{.stan include="stan_programs/multi_full_model.stan" filename="multi\\_full\\_model.stan" eval=FALSE} 601 | ``` 602 | 603 | ```{python} 604 | #| warning: false 605 | #| message: false 606 | with open('stan_programs/multi_full_model.stan', 'r') as file: 607 | stan_program = file.read() 608 | model = stan.build(stan_program, random_seed=5838299, data=data) 609 | fit = model.sample(num_samples=1024, refresh=0) 610 | ``` 611 | 612 | Fortunately our computational fortune has persisted. 613 | 614 | ```{python} 615 | diagnostics = util.extract_hmc_diagnostics(fit) 616 | util.check_all_hmc_diagnostics(diagnostics) 617 | 618 | samples = util.extract_expectand_vals(fit) 619 | base_samples = util.filter_expectands(samples, 620 | ['beta0', 'beta1', 621 | 'beta2_d', 'beta2_o', 622 | 'sigma'], 623 | True) 624 | util.check_all_expectand_diagnostics(base_samples) 625 | ``` 626 | 627 | The summary statistics that we used above to implement our prior predictive 628 | checks are equally useful for implementing informative posterior retrodictive 629 | checks. Conveniently the visualization functions all feature `baseline_values` 630 | functions that we can use to visualize the observed behavior along with the 631 | posterior predictive behavior. 632 | 633 | ```{python} 634 | putil.plot_hist_quantiles(plot.gca(), samples, 'y_pred', 635 | baseline_values=data['y']) 636 | plot.show() 637 | ``` 638 | 639 | Additionally the `plot_conditional_mean_quantiles` and 640 | `plot_conditional_median_quantiles` functions feature a `residual` option that 641 | plots the posterior predictive behaviors relative to the baseline values. Any 642 | deviations from zero in these plots suggests retrodictive tension; here, 643 | however, there don't seem to be any problems. 644 | 645 | ```{python} 646 | f, axarr = plot.subplots(1, 3, layout="constrained") 647 | f.suptitle("Marginal Quantiles of Conditional Means") 648 | 649 | pred_names = [ f'y_pred[{n + 1}]' for n in range(data['N']) ] 650 | putil.plot_conditional_mean_quantiles(axarr[0], samples, pred_names, 651 | data['X'][:,0], -9, 9, 1, 652 | data['y'], xlabel="x1", ylabel="") 653 | putil.plot_conditional_mean_quantiles(axarr[1], samples, pred_names, 654 | data['X'][:,1], -9, 9, 1, 655 | data['y'], xlabel="x2", ylabel="") 656 | putil.plot_conditional_mean_quantiles(axarr[2], samples, pred_names, 657 | data['X'][:,2], -9, 9, 1, 658 | data['y'], xlabel="x3", ylabel="") 659 | plot.show() 660 | 661 | f, axarr = plot.subplots(1, 3, layout="constrained") 662 | f.suptitle("Marginal Quantiles of Conditional Means Minus Baselines") 663 | 664 | putil.plot_conditional_mean_quantiles(axarr[0], samples, pred_names, 665 | data['X'][:,0], -9, 9, 1, 666 | data['y'], residual=True, 667 | xlabel="x1", ylabel="") 668 | putil.plot_conditional_mean_quantiles(axarr[1], samples, pred_names, 669 | data['X'][:,1], -9, 9, 1, 670 | data['y'], residual=True, 671 | xlabel="x2", ylabel="") 672 | putil.plot_conditional_mean_quantiles(axarr[2], samples, pred_names, 673 | data['X'][:,2], -9, 9, 1, 674 | data['y'], residual=True, 675 | xlabel="x3", ylabel="") 676 | plot.show() 677 | ``` 678 | 679 | ```{python} 680 | f, axarr = plot.subplots(1, 3, layout="constrained") 681 | f.suptitle("Marginal Quantiles of Conditional Medians") 682 | 683 | pred_names = [ f'y_pred[{n + 1}]' for n in range(data['N']) ] 684 | putil.plot_conditional_median_quantiles(axarr[0], samples, pred_names, 685 | data['X'][:,0], -9, 9, 1, 686 | data['y'], xlabel="x1", ylabel="") 687 | putil.plot_conditional_median_quantiles(axarr[1], samples, pred_names, 688 | data['X'][:,1], -9, 9, 1, 689 | data['y'], xlabel="x2", ylabel="") 690 | putil.plot_conditional_median_quantiles(axarr[2], samples, pred_names, 691 | data['X'][:,2], -9, 9, 1, 692 | data['y'], xlabel="x3", ylabel="") 693 | plot.show() 694 | 695 | f, axarr = plot.subplots(1, 3, layout="constrained") 696 | f.suptitle("Marginal Quantiles of Conditional Medians Minus Baselines") 697 | 698 | putil.plot_conditional_median_quantiles(axarr[0], samples, pred_names, 699 | data['X'][:,0], -9, 9, 1, 700 | data['y'], residual=True, 701 | xlabel="x1", ylabel="") 702 | putil.plot_conditional_median_quantiles(axarr[1], samples, pred_names, 703 | data['X'][:,1], -9, 9, 1, 704 | data['y'], residual=True, 705 | xlabel="x2", ylabel="") 706 | putil.plot_conditional_median_quantiles(axarr[2], samples, pred_names, 707 | data['X'][:,2], -9, 9, 1, 708 | data['y'], residual=True, 709 | xlabel="x3", ylabel="") 710 | plot.show() 711 | ``` 712 | 713 | With no indications of model inadequacy we can move onto our posterior 714 | inferences. As before we can visualize the pushforward posterior distributions 715 | for each individual, one-dimensional parameter. 716 | 717 | ```{python} 718 | f, axarr = plot.subplots(4, 3, layout="constrained") 719 | 720 | util.plot_expectand_pushforward(axarr[0, 0], samples['beta0'], 721 | 25, display_name="beta0") 722 | 723 | axarr[0, 1].axis('off') 724 | 725 | util.plot_expectand_pushforward(axarr[0, 2], samples['sigma'], 726 | 25, display_name="sigma") 727 | 728 | for m in range(data['M']): 729 | name = f'beta1[{m + 1}]' 730 | util.plot_expectand_pushforward(axarr[1, m], samples[name], 731 | 25, display_name=name) 732 | 733 | for m in range(data['M']): 734 | name = f'beta2_d[{m + 1}]' 735 | util.plot_expectand_pushforward(axarr[2, m], samples[name], 736 | 25, display_name=name) 737 | 738 | for m in range(data['M']): 739 | name = f'beta2_o[{m + 1}]' 740 | util.plot_expectand_pushforward(axarr[3, m], samples[name], 741 | 25, display_name=name) 742 | 743 | plot.show() 744 | ``` 745 | 746 | The `plot_disc_pushforward_quantiles` function plots disconnected, marginal 747 | nested quantile intervals for a collection of one-dimensional variables. This 748 | allows for a more compact visualization of the marginal posterior distributions. 749 | 750 | ```{python} 751 | f, axarr = plot.subplots(1, 3, layout="constrained") 752 | 753 | names = [ f'beta1[{m + 1}]' for m in range(data['M']) ] 754 | putil.plot_disc_pushforward_quantiles(axarr[0], samples, names, 755 | xlabel="beta1", 756 | ylabel="Marginal Posterior Quantiles") 757 | 758 | names = [ f'beta2_o[{m + 1}]' for m in range(data['M']) ] 759 | putil.plot_disc_pushforward_quantiles(axarr[1], samples, names, 760 | xlabel="beta2_d", 761 | ylabel="Marginal Posterior Quantiles") 762 | 763 | names = [ f'beta2_d[{m + 1}]' for m in range(data['M']) ] 764 | putil.plot_disc_pushforward_quantiles(axarr[2], samples, names, 765 | xlabel="beta2_o", 766 | ylabel="Marginal Posterior Quantiles") 767 | 768 | plot.show() 769 | ``` 770 | 771 | ```{python} 772 | names = [ f'beta1[{m + 1}]' for m in range(data['M']) ] + \ 773 | [ f'beta2_d[{m + 1}]' for m in range(data['M']) ] + \ 774 | [ f'beta2_o[{m + 1}]' for m in range(data['M']) ] 775 | putil.plot_disc_pushforward_quantiles(plot.gca(), samples, names, 776 | xlabel="All Slopes", 777 | ylabel="Marginal Posterior Quantiles") 778 | plot.show() 779 | ``` 780 | 781 | This function also includes an optional `baseline_values` argument and 782 | `residual` configuration which we can use to compare the probabilistic to the 783 | point values, for example our marginal posterior inferences to the true values 784 | when analyzing simulated data. 785 | 786 | ```{python} 787 | true_slopes = [-6.00, -1.50, 13.00, 0.50, 0.25, 788 | 1.00, -0.50, -2.00, -1.00] 789 | 790 | f, axarr = plot.subplots(2, 1, layout="constrained") 791 | 792 | putil.plot_disc_pushforward_quantiles(axarr[0], samples, names, 793 | baseline_values=true_slopes, 794 | baseline_color=putil.mid_teal, 795 | xlabel="All Slopes", ylabel="") 796 | 797 | putil.plot_disc_pushforward_quantiles(axarr[1], samples, names, 798 | baseline_values=true_slopes, 799 | residual=True, 800 | xlabel="All Slopes", ylabel="") 801 | 802 | plot.show() 803 | ``` 804 | 805 | # License {-} 806 | 807 | The code in this case study is copyrighted by Michael Betancourt and licensed 808 | under the new BSD (3-clause) license: 809 | 810 | https://opensource.org/licenses/BSD-3-Clause 811 | 812 | The text and figures in this case study are copyrighted by Michael Betancourt 813 | and licensed under the CC BY-NC 4.0 license: 814 | 815 | https://creativecommons.org/licenses/by-nc/4.0/ 816 | 817 | # Original Computing Environment {-} 818 | 819 | ```{python} 820 | from watermark import watermark 821 | print(watermark()) 822 | ``` 823 | 824 | ```{python} 825 | print(watermark(packages="matplotlib, numpy, json, stan")) 826 | ``` 827 | -------------------------------------------------------------------------------- /python/stan_programs/multi_full_model.stan: -------------------------------------------------------------------------------- 1 | data { 2 | int M; // Number of covariates 3 | int N; // Number of observations 4 | 5 | vector[M] x0; // Covariate baselines 6 | matrix[N, M] X; // Covariate design matrix 7 | 8 | array[N] real y; // Variates 9 | } 10 | 11 | transformed data { 12 | matrix[N, M * (M + 3) / 2 + 1] deltaX; 13 | for (n in 1:N) { 14 | deltaX[n, 1] = 1; 15 | 16 | for (m1 in 1:M) { 17 | // Linear perturbations 18 | deltaX[n, m1 + 1] = X[n, m1] - x0[m1]; 19 | } 20 | 21 | for (m1 in 1:M) { 22 | // On-diagonal quadratic perturbations 23 | deltaX[n, M + m1 + 1] 24 | = deltaX[n, m1 + 1] * deltaX[n, m1 + 1]; 25 | 26 | for (m2 in (m1 + 1):M) { 27 | int m3 = (2 * M - m1) * (m1 - 1) / 2 + m2 - m1; 28 | 29 | // Off-diagonal quadratic perturbations 30 | // Factor of 2 ensures that beta parameters have the 31 | // same interpretation as the expanded implementation 32 | deltaX[n, 2 * M + m3 + 1] 33 | = 2 * deltaX[n, m1 + 1] * deltaX[n, m2 + 1]; 34 | } 35 | } 36 | } 37 | } 38 | 39 | parameters { 40 | real beta0; // Intercept 41 | vector[M] beta1; // Linear slopes 42 | vector[M] beta2_d; // On-diagonal quadratic slopes 43 | vector[M * (M - 1) / 2] beta2_o; // Off-diagonal quadratic slopes 44 | real sigma; // Measurement Variability 45 | } 46 | 47 | model { 48 | vector[M * (M + 3) / 2 + 1] beta 49 | = append_row( 50 | append_row( 51 | append_row(beta0, beta1), 52 | beta2_d), 53 | beta2_o); 54 | 55 | // Prior model 56 | beta0 ~ normal(0, 10); 57 | beta1 ~ normal(0, 10); 58 | beta2_d ~ normal(0, 2); 59 | beta2_o ~ normal(0, 1); 60 | sigma ~ normal(0, 5); 61 | 62 | // Observation model 63 | y ~ normal(deltaX * beta, sigma); 64 | } 65 | 66 | generated quantities { 67 | // Posterior predictions 68 | array[N] real y_pred; 69 | { 70 | vector[M * (M + 3) / 2 + 1] beta 71 | = append_row( 72 | append_row( 73 | append_row(beta0, beta1), 74 | beta2_d), 75 | beta2_o); 76 | y_pred = normal_rng(deltaX * beta, sigma); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /python/stan_programs/multi_prior_model.stan: -------------------------------------------------------------------------------- 1 | data { 2 | int M; // Number of covariates 3 | int N; // Number of observations 4 | 5 | vector[M] x0; // Covariate baselines 6 | matrix[N, M] X; // Covariate design matrix 7 | } 8 | 9 | transformed data { 10 | matrix[N, M * (M + 3) / 2 + 1] deltaX; 11 | for (n in 1:N) { 12 | deltaX[n, 1] = 1; 13 | 14 | for (m1 in 1:M) { 15 | // Linear perturbations 16 | deltaX[n, m1 + 1] = X[n, m1] - x0[m1]; 17 | } 18 | 19 | for (m1 in 1:M) { 20 | // On-diagonal quadratic perturbations 21 | deltaX[n, M + m1 + 1] 22 | = deltaX[n, m1 + 1] * deltaX[n, m1 + 1]; 23 | 24 | for (m2 in (m1 + 1):M) { 25 | int m3 = (2 * M - m1) * (m1 - 1) / 2 + m2 - m1; 26 | 27 | // Off-diagonal quadratic perturbations 28 | // Factor of 2 ensures that beta parameters have the 29 | // same interpretation as the expanded implementation 30 | deltaX[n, 2 * M + m3 + 1] 31 | = 2 * deltaX[n, m1 + 1] * deltaX[n, m2 + 1]; 32 | } 33 | } 34 | } 35 | } 36 | 37 | parameters { 38 | real beta0; // Intercept 39 | vector[M] beta1; // Linear slopes 40 | vector[M] beta2_d; // On-diagonal quadratic slopes 41 | vector[M * (M - 1) / 2] beta2_o; // Off-diagonal quadratic slopes 42 | real sigma; // Measurement Variability 43 | } 44 | 45 | model { 46 | vector[M * (M + 3) / 2 + 1] beta 47 | = append_row( 48 | append_row( 49 | append_row(beta0, beta1), 50 | beta2_d), 51 | beta2_o); 52 | 53 | // Prior model 54 | beta0 ~ normal(0, 10); 55 | beta1 ~ normal(0, 10); 56 | beta2_d ~ normal(0, 2); 57 | beta2_o ~ normal(0, 1); 58 | sigma ~ normal(0, 5); 59 | } 60 | 61 | generated quantities { 62 | // Posterior predictions 63 | array[N] real y_pred; 64 | { 65 | vector[M * (M + 3) / 2 + 1] beta 66 | = append_row( 67 | append_row( 68 | append_row(beta0, beta1), 69 | beta2_d), 70 | beta2_o); 71 | y_pred = normal_rng(deltaX * beta, sigma); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /python/stan_programs/uni_full_model.stan: -------------------------------------------------------------------------------- 1 | data { 2 | int N; 3 | vector[N] x; // Observed inputs 4 | vector[N] y; // Observed outputs 5 | 6 | int N_grid; // Number of grid points for quantifying functional behavior 7 | vector[N_grid] x_grid; // Grid points for quantifying functional behavior 8 | } 9 | 10 | parameters { 11 | real alpha; 12 | real beta; 13 | real sigma; 14 | } 15 | 16 | model { 17 | alpha ~ normal(0, 3 / 2.32); 18 | beta ~ normal(0, 3 / 2.32); 19 | sigma ~ normal(0, 1 / 2.57); 20 | 21 | y ~ normal(alpha + beta * x, sigma); 22 | } 23 | 24 | generated quantities { 25 | vector[N_grid] f_grid = alpha + beta * x_grid; 26 | array[N_grid] real y_pred_grid = normal_rng(f_grid, sigma); 27 | } 28 | -------------------------------------------------------------------------------- /python/stan_programs/uni_prior_model.stan: -------------------------------------------------------------------------------- 1 | data { 2 | int N; 3 | vector[N] x; // Observed inputs 4 | vector[N] y; // Observed outputs 5 | 6 | int N_grid; // Number of grid points for quantifying functional behavior 7 | vector[N_grid] x_grid; // Grid points for quantifying functional behavior 8 | } 9 | 10 | parameters { 11 | real alpha; 12 | real beta; 13 | real sigma; 14 | } 15 | 16 | model { 17 | alpha ~ normal(0, 3 / 2.32); 18 | beta ~ normal(0, 3 / 2.32); 19 | sigma ~ normal(0, 1 / 2.57); 20 | } 21 | 22 | generated quantities { 23 | vector[N_grid] f_grid = alpha + beta * x_grid; 24 | array[N_grid] real y_pred_grid = normal_rng(f_grid, sigma); 25 | } 26 | -------------------------------------------------------------------------------- /r/_extensions/quarto-ext/include-code-files/_extension.yml: -------------------------------------------------------------------------------- 1 | title: Include Code Files 2 | author: Bruno Beaufils 3 | version: 1.0.0 4 | quarto-required: ">=1.2" 5 | contributes: 6 | filters: 7 | - include-code-files.lua 8 | 9 | 10 | -------------------------------------------------------------------------------- /r/_extensions/quarto-ext/include-code-files/include-code-files.lua: -------------------------------------------------------------------------------- 1 | --- include-code-files.lua – filter to include code from source files 2 | --- 3 | --- Copyright: © 2020 Bruno BEAUFILS 4 | --- License: MIT – see LICENSE file for details 5 | 6 | --- Dedent a line 7 | local function dedent (line, n) 8 | return line:sub(1,n):gsub(" ","") .. line:sub(n+1) 9 | end 10 | 11 | --- Filter function for code blocks 12 | local function transclude (cb) 13 | if cb.attributes.include then 14 | local content = "" 15 | local fh = io.open(cb.attributes.include) 16 | if not fh then 17 | io.stderr:write("Cannot open file " .. cb.attributes.include .. " | Skipping includes\n") 18 | else 19 | local number = 1 20 | local start = 1 21 | 22 | -- change hyphenated attributes to PascalCase 23 | for i,pascal in pairs({"startLine", "endLine"}) 24 | do 25 | local hyphen = pascal:gsub("%u", "-%0"):lower() 26 | if cb.attributes[hyphen] then 27 | cb.attributes[pascal] = cb.attributes[hyphen] 28 | cb.attributes[hyphen] = nil 29 | end 30 | end 31 | 32 | if cb.attributes.startLine then 33 | cb.attributes.startFrom = cb.attributes.startLine 34 | start = tonumber(cb.attributes.startLine) 35 | end 36 | for line in fh:lines ("L") 37 | do 38 | if cb.attributes.dedent then 39 | line = dedent(line, cb.attributes.dedent) 40 | end 41 | if number >= start then 42 | if not cb.attributes.endLine or number <= tonumber(cb.attributes.endLine) then 43 | content = content .. line 44 | end 45 | end 46 | number = number + 1 47 | end 48 | fh:close() 49 | end 50 | -- remove key-value pair for used keys 51 | cb.attributes.include = nil 52 | cb.attributes.startLine = nil 53 | cb.attributes.endLine = nil 54 | cb.attributes.dedent = nil 55 | -- return final code block 56 | return pandoc.CodeBlock(content, cb.attr) 57 | end 58 | end 59 | 60 | return { 61 | { CodeBlock = transclude } 62 | } 63 | 64 | -------------------------------------------------------------------------------- /r/data/multi_data.R: -------------------------------------------------------------------------------- 1 | M <- 3 2 | N <- 100 3 | x0 <- 4 | c(0, 2, -1) 5 | X <- 6 | structure(c(0.60852223354037, -1.10033970547018, 0.907242653750291, -0.459247830102321, -2.2385689797001, -0.254692413254836, 7 | -0.812236756137696, -0.198299012437181, -0.110434410522669, 1.9494088249124, -4.25671101251423, 4.22031147175022, 8 | 2.1697446307094, -3.35622657985497, -4.63370325003654, 0.309641682696826, 2.9768503024376, 1.03989430497363, 9 | -0.445339823573345, -1.12676523165663, -4.02981196797131, -2.05412505879869, 1.23609335017028, -0.108314636707815, 10 | 0.648976442504881, -1.79913856014403, 1.58482218732048, -0.728572763582346, -0.900934957146994, -0.416157077409824, 11 | 4.29235131435656, 2.02034655809335, -1.45113000862758, -0.510480459563565, 1.9426182669456, 3.80057227887428, 12 | -1.01315908992137, 0.696201870062755, 2.41310049432604, -1.10978648592927, 1.79273092527482, 1.7287054380964, 13 | -0.63558162664676, 0.810952533091813, -1.06989707304084, 0.68681913428841, -2.92978701701776, -1.71566105319856, 14 | 0.224043589534039, -0.0807242883920974, 1.25407667822961, -3.85060313447261, -3.17228539208567, 2.40142225331849, 15 | -2.20074833231336, 1.86408041013266, -1.25329516872653, -1.45236250117659, 1.67671600529286, 0.206466337271312, 16 | -0.362305836665703, 2.29967338456621, 0.268750293724062, -1.0595021338914, 0.545578856456281, -2.37346524187507, 17 | 2.32837444307425, 0.126142399308887, 0.119665334997685, -1.0926450122945, -3.24395085870205, -3.27006147935717, 18 | -1.18609219386581, 1.95906144820667, 1.23592790981444, -2.59797244019552, -0.416501920219419, 2.27878850838137, 19 | 3.05327863273689, -2.49947255277097, 0.573276716930885, 0.686691121419873, 1.42091600379166, -0.740451452939531, 20 | 1.86705784739047, 0.949548881047256, -0.828483182411306, 6.06643342792335, -1.67592026985863, -0.486353136087173, 21 | -3.14815834178592, 1.1887303502459, -0.668111207877824, -1.64080652925362, -0.0340146278900818, 6.27527271848059, 22 | -0.454521552327839, 1.31974539125959, 1.08893637250158, 1.00767380094198, 4.5239374956804, 1.71799455001116, 4.09095549383841, 23 | 5.28683876700261, 1.61905065479526, 3.91201512294887, 4.53121680993971, 4.65211667293192, 3.01277859062311, 24 | 0.0995419235803519, -1.63041823923586, 0.943876479497746, 0.921366411263757, -0.912361013564168, 0.575928144737539, 25 | 5.77169368917076, -0.638092669010536, 3.9250825096469, 5.94337868670784, 1.76248896883973, 0.956855022788201, 26 | -2.06082268799082, 0.701136072366006, 4.76762010724061, 3.51089747045802, 1.46348568305127, 1.47088557246714, 27 | 5.80970560399961, 3.92797900516666, 3.50145988422488, -0.0959974321658519, 0.150211796122815, 1.12087806075335, 28 | 7.0981766561561, 1.24868759787003, -2.209991421419, 4.11704467044952, 3.45310858163722, 1.03847747638272, 3.01477337841951, 29 | 1.19102153925432, 1.39810047840372, 6.5684110663747, 4.47704100375534, 6.06687974335252, 2.25696512355485, 1.33999526765744, 30 | 1.02707464901912, 1.92631827722852, 4.14889737806973, 2.06457729571516, -2.07384284704506, -0.319248849929581, 31 | 0.137525642748123, 0.806180390407782, 1.28347467745689, 6.57955611539694, 4.69105269701785, 2.52088041061643, 32 | 6.67546499139029, 4.42761902271023, 0.256239533532929, 4.97703648212084, 3.03308621264093, 6.22092765753456, 33 | 0.224792190707618, 1.70140651677365, 3.44220914071332, 5.30458976005535, 3.20401756107991, 2.84378682720262, -0.7519460515653, 34 | 2.17912570845433, 1.71532427020659, 4.15221582250183, -1.00194839977667, 7.23775098649842, 0.919385057820201, 35 | -3.05397960659817, -3.61626706297899, 3.69094955977179, 2.46883712646022, 1.24277314112081, 3.70261571411084, 36 | -0.154931158957028, 3.79401757766105, 4.48993364803398, 2.86839403594931, 1.21042937417978, 4.51712617143636, 37 | -1.66811901715516, 2.85431106643675, 5.32237643224789, 1.86815019540443, 2.43896019265515, 5.54040030080425, 4.4043159676981, 38 | 2.59197520853726, 3.66404235409235, 5.31797373522147, -1.2132689864963, 1.21685064031047, -2.60117980614775, 39 | 0.967676453209636, 0.805591908273759, 0.255155135037622, -0.202633237683323, -2.816685318907, 0.740888891228586, 40 | -3.72868158735532, 3.11896095448416, -4.72962131122218, -3.97812042496476, 1.90873841027682, 4.14064822777055, 41 | 0.913461499275111, -2.92871118470123, -1.90441758982535, -1.92809205119279, -1.75701367522665, 3.41975345143302, 42 | 1.85190275368232, -2.7976592346579, -1.88473187596501, -2.57883254294816, 0.612963764920528, -2.51508013137446, 43 | 1.51508877793648, 0.279706643280706, -0.957917597818251, -4.68347224196425, -2.85089387313961, 1.23168276533512, 44 | -0.526673373746309, -3.15492446654596, -4.57472890726648, -0.019126695552358, -2.01320879464209, -2.82412533335634, 45 | 0.406799187885663, -3.21793760938613, -3.91601404099444, 0.715333676687507, -0.941652948187541, 0.217534757111784, 46 | -2.88404184075981, 0.730474248943419, -0.382394775527607, -1.1092311433632, -1.48357359922731, -3.32898799537423, 47 | 5.02512010597287, 2.83001852750945, -2.18004031526115, 1.18348392118618, -0.97964232053828, -0.928636579785787, 48 | -0.296871088665963, -4.11291492528879, 0.0456585919589192, -1.34584582335414, -3.79591357113468, -0.580856899525596, 49 | -0.279620232827926, -1.93176307680076, 0.649318722142874, -4.36740469156254, -2.12251892789795, -2.78703535424733, 50 | 0.221534990165079, 2.68405292235432, 4.59754951087401, 0.300056861497666, -3.22411288692602, -1.97420296197727, 51 | 1.72763463241995, -0.45272221374985, -3.94140423700058, -2.08244097014888, 3.02963469289758, -1.47077370772774, 52 | -1.32130220094393, -2.67363746582516, -0.658349327725383, -2.4477678494855, -1.88434840341596, -1.15785740301101, 53 | -6.26466463972363, 0.358215614473561, -0.478258555062276, 0.873555596038979, -2.32908543148128, -0.454351363710231, 54 | 0.930858958582136, -2.22272103292096, -8.34607561456481, 2.15843647560531, -3.05257594451974, -3.14406014789865, 55 | -3.12024848682692), 56 | .Dim = c(100, 3)) 57 | y <- 58 | c(12.5979605679208, 72.4365143925198, 4.9083679377149, 43.4130074603847, 80.7997814046909, 35.1743238292875, 59 | 35.0825267272652, 8.35100836204944, 42.7840361611394, 1.36325661226419, 219.068258273232, 31.7526037550637, 4.41207981924806, 60 | 145.302059745438, 257.971570115353, 27.7855627269194, 12.1367377375291, 7.32884165965262, 17.5330062749366, 15.7742349721585, 61 | 208.585830870455, 129.020240216104, 2.43070003029122, 13.132605790459, 4.49305483844234, 70.2565653666327, 4.07253402141562, 62 | 52.7079291383856, 44.183964456752, 22.7510336510785, 31.8978208561757, 4.70185849223332, 81.325180747388, 29.1156786233861, 63 | 3.83480493303557, 22.6392404453285, 40.2838918071852, 7.39379958136533, 5.28907144737057, 51.2537805171311, 3.33552200718939, 64 | 1.1711588022725, 39.6684507251656, 12.4328457212714, 42.7777046410864, 2.48247339705992, 90.6093773242094, 46.3716911429442, 65 | 18.1714454112314, 14.9865497573223, 0.276564836728407, 302.638407265753, 171.927125340188, 9.66734982583635, 94.1784725337269, 66 | 14.5227682255717, 34.0720961755612, 41.5755710131371, 3.36917657878797, 21.0181274647858, 18.7710767326467, 4.33903887408966, 67 | 18.2804541770747, 38.0574548148327, 12.6372902988313, 82.7222013555676, 8.88978377263224, 8.91629818250989, 9.83321496658544, 68 | 47.6488953445901, 149.575638574643, 250.549232521269, 52.2598851002436, 4.19057258355584, 7.42949996991276, 126.669295135108, 69 | 27.2298693410387, 4.96223443942482, 25.9232249381075, 195.918048136755, 11.5361871859756, 12.5398964622637, 3.70800395159372, 70 | 29.1287033861767, 8.19668372337071, 7.71523283329511, 23.9821437557835, 92.3888421140611, 62.5150266586071, 28.2626505321804, 71 | 107.386755200542, 5.07079077983615, 30.2662961583862, 74.1100392925698, 7.11452794047561, 172.944151373448, 63.281526346813, 72 | 2.69303710871817, 4.81130813358664, 9.64003916227308) 73 | -------------------------------------------------------------------------------- /r/data/uni_data.R: -------------------------------------------------------------------------------- 1 | N <- 1000 2 | x <- 3 | c(-2.97348027524669, 0.388611920650857, 3.7860167184133, -2.11532505207753, -2.05086426111779, 1.23881091387601, 4 | -0.223011012267395, 2.28712697491343, -1.71278252192451, 2.65437121703705, -0.157489791480464, 1.8615372235093, 5 | 3.36182937623681, 2.42515785635999, 2.17394655624105, -1.07167815033313, -1.79246910131136, -0.673331808215533, 6 | 0.0767325927881738, -1.17522611187832, -1.54122469396116, 1.03805490088226, 1.20632113611642, -0.588043195945986, 7 | -2.51057385712628, -1.15421410027293, -0.169313272238467, -0.983795729820899, -0.112748390035786, -0.490945903936672, 8 | 1.89627259463064, -1.07178596286251, -0.743790057725642, -1.65113466114743, -2.78640384485104, -1.77321590456568, 9 | -3.79541884468883, 2.01601854433318, -0.0681191140282089, 0.827727846676397, -2.1249322628893, -1.95900227246779, 10 | -1.40534105750887, 1.74578939402713, 0.545907562283087, 1.43938493455244, -0.225016809883682, -3.0717390766596, 11 | 1.46069680629352, 1.52702527645707, 0.12467837727999, -1.26969891137703, -1.33237969647966, 0.914817999895252, 12 | -1.34551702046565, -3.86800540178274, -0.158210238584515, 4.72087028629111, -0.136793373482998, -0.917736715813687, 13 | 1.05005400485659, 0.268763684288038, 0.224301850138695, -2.43672149615835, 1.29296496701885, -2.42912244745404, 14 | -1.88057325992304, -0.7932177955986, 1.70398051790464, 0.193090627785851, 1.90121242756078, -2.62870646881024, 15 | 2.26039079216485, 1.72412208692294, 0.446946476893187, 0.265203644584037, 0.721238124794334, -0.918606618211031, 16 | 1.89657363779301, -2.09962105311812, -2.83424381933599, -3.57395272688435, 0.812342582980217, 4.66869220621487, 17 | -0.535485261287095, 1.03498824154688, 0.351051219072201, 0.60520533429147, 1.61275366972448, -0.220888010538226, 18 | -0.680350262877783, 1.00709499800659, 1.619171714507, 1.97269825276133, -2.62434739494803, 1.50718418723262, 19 | -1.38831858836681, 0.137218484961964, -1.69820235009403, -2.28548249544971, 0.0673786015455058, 1.8541110381547, 20 | -2.60441205286796, -2.30781955703601, -0.264886532467073, 0.701379331388585, 2.27808692679609, 2.02314224657252, 21 | -1.4585864704286, 0.360523358385156, 0.902795149729645, -1.83220544565982, 0.972476185372778, 2.69295021418979, 22 | 1.39665161297624, -1.31559352311261, 0.136689051357421, -1.34746228109364, 0.317388338441274, -3.54340788753439, 23 | 3.66218604731205, 1.30627999077202, -1.13912608739532, -0.0259413489532041, -0.148862813618366, 1.7928262943576, 24 | -0.676672992604949, -1.21863175954127, 0.394908336668155, -2.49951629322222, 0.0256821418503826, -0.0522355303183139, 25 | 0.225737316763876, -1.2399362606659, 1.73599978760894, -0.0443916210453394, 2.23462751484444, -0.193356261715294, 26 | 0.493783178918816, 1.66297481382755, -0.732490516837395, 3.48180928998353, 1.159157087239, 0.492780854619329, 27 | -1.79317652186067, 1.81320473993243, 0.714159728887527, 0.998004525208241, 0.664382262045555, 1.35337016263256, 28 | -1.7449119301181, 1.42332712112288, -2.51754485542597, 1.04809359767519, 1.98174076811045, -2.43321725906455, 29 | 1.40118824476981, 1.9357428604516, 2.72601574965036, 1.47089013495148, -1.78023107879238, -0.724896882345517, 30 | -0.686042305022754, 2.6386970511777, 0.658292952633693, 1.24415499259213, -0.580095562762227, -1.53502056956509, 31 | -4.71179500433612, -3.09676250716159, 1.32085518691949, -1.61748680108676, 1.74101247504765, -0.313031105594641, 32 | 1.41848947640336, -3.19243832308249, -0.411577808678408, 1.33853492732756, -1.03059423770006, -0.69646600576326, 33 | -2.76555749664639, 2.18662921422797, 0.598785293067926, 1.99934989471512, -3.36459767447556, 2.56467599621676, 34 | 3.00783539971868, -1.2059770865875, -1.28710119995976, -0.821137908451273, -1.21446209018219, -4.13437693494328, 35 | 1.0001002249913, 2.11585790361668, 3.11015304264666, 1.65723226955571, 2.38912795543375, 0.326596424476767, 36 | -1.02313273526447, 1.82773747638169, -0.373132760765079, -0.786938951780137, 2.52068007369725, 0.781238436104176, 37 | 1.19156785632883, 2.55187505625067, -1.61778741474883, 2.63764864523473, -0.0787123122117819, 0.41687136635166, 38 | -1.44926016686686, 4.02682503634428, -1.27813641144035, -1.70814100928115, -0.460929942133731, 1.35185410101978, 39 | -0.276353912054921, -0.778458081484537, 1.25768029088608, 1.74989246263504, -2.33012768529994, -3.42200071384182, 40 | -0.851764907069444, -2.60427421697768, -0.581461080733049, 1.64597872637312, 2.6903850725163, 0.276060115004754, 41 | 1.15567405600298, -2.38882188149197, 1.85634729955736, -0.00407170169503185, -0.320659364698908, -2.08846573887788, 42 | -2.81331236021652, 0.945082931586096, -1.33556817759153, 2.01851944763724, -0.0317670109811637, -0.151214294537344, 43 | 1.81540935011295, 0.24824878362639, 2.37960348973577, -0.236862893432051, -0.547583794494695, -2.67387261669133, 44 | 0.794677990676838, -0.780460382098712, 1.26951756224412, 3.20589898140238, -1.54521964117249, 0.869353553255022, 45 | -0.974314218155707, -0.307865460430178, 0.794028743191166, 1.51074681840989, 0.325791544540394, -0.54395848542403, 46 | 0.817984892696379, 1.9085543563862, -0.924555435052203, 0.642276991699371, 1.44277199503424, 0.566127037829794, 47 | -0.860891290633549, 1.14990752320774, 3.13923589429483, 0.639115770577305, -1.05609721591993, -0.536648432769677, 48 | 0.0362461147136803, 0.314824207303793, -0.329464770753349, 0.000574324584113506, 1.44539233408089, -0.018818679408215, 49 | -1.1820607129283, 2.38544593988329, -0.0388450751317342, 0.212147787927498, 0.281626905501133, 0.68041596088093, 50 | -1.14601138872579, -1.86143624806395, 1.99933190245016, 0.546101301722465, -0.652429007357909, 1.19786409869744, 51 | -0.65477338576529, -1.12073599568971, 0.272865246538829, -2.28517651362317, -0.349267033988028, -0.56526459861254, 52 | 0.396717274014399, -0.159100430539236, 0.143903439465844, -0.588614896865701, -2.47443458197223, -0.426420894271595, 53 | 2.98650845154638, -3.37748008100567, -0.57509011771876, -0.536945807298771, 0.137839300982697, -1.05585956197602, 54 | -1.24052245292213, -0.247332946908769, 0.699349103503229, 2.62561669742154, 0.659810358895002, 0.546797211824156, 55 | -3.51707517678809, -1.64009374699043, -2.01374189847369, 0.365331927772696, 0.137356434031797, 2.0797850219423, 56 | 0.548544062418155, -0.770088942413554, 0.769317038018945, 1.25704138232117, -0.344926040703071, 2.09099225019243, 57 | 0.72267553424863, 1.50807275990043, -1.70412542233797, 0.0685313171574897, -2.67787468633424, -1.63831127935215, 58 | 1.87574094373709, -4.2842779586635, -2.7245441717373, 1.15940264028257, -1.54248067327084, -0.543130876902085, 59 | -1.29911158214737, 0.490962141969492, -1.51861082181853, -4.63808637618426, -2.26721456600846, 3.12373740301105, 60 | 0.641215774695631, -0.595732115160891, 1.22896899010113, 1.01510712225012, -1.07394851627177, 2.19891104296852, 61 | 0.151102020733846, -1.83590640826489, -1.5087924896006, -1.24408733347682, 1.51925320470776, -1.99424348574826, 62 | 0.756687722293901, 1.45298220858995, -0.00632586882128499, -1.85840416190548, 1.03618322456174, -0.109381033044312, 63 | -1.24786713922668, -1.29371896475317, -2.6375818096717, -0.489402762197171, -2.15928781809171, -1.47383257272719, 64 | 1.33440278583701, 1.03599762025946, 0.420074991364103, -0.60611329807431, 0.0235931389336941, 0.558525734004912, 65 | 1.85221918743635, -0.489412979017564, -1.31178948499042, 1.36430001619883, -0.596081425042378, -2.17675301224551, 66 | 0.845612305473108, 0.781203833865423, 1.21591771567409, 0.131042505582803, -0.947730024882309, -2.58627993878295, 67 | 0.996027689338731, -0.61959459589927, 1.46288669987758, -1.92535650978575, -0.637210197125096, 0.766520852805443, 68 | -2.15614251762138, 0.586190768553997, -3.20941068711069, 2.56320558070399, -1.79962133000013, -2.33039056720364, 69 | 0.714333241478087, 1.87258120120607, -0.0462612979361543, 2.33547569173567, 0.585301987673688, 2.34028555106701, 70 | 0.292747020173378, -2.17650321407879, -0.0156095345650497, 1.86491755398342, 3.64036996412883, 2.35153309314585, 71 | 0.4557419782799, 1.41660561982284, -0.788582734417958, 0.244521987332009, -0.573685994875725, 1.32949815363488, 72 | 0.56284479174236, -0.619630429947286, 2.72427459725633, 0.299106781556197, 2.07688595628409, 1.10331165049588, 73 | 0.0994649322106696, 1.66434871748781, 1.4515416375452, 2.26935884488573, -0.624129934239117, 0.319438220928861, 74 | -0.0922798090484157, -2.36076987032651, -0.994858886325651, -0.0267039997552047, 0.355527661060961, -0.656727326503503, 75 | 0.269693271453237, -0.504681284572652, -0.0805075276665118, -0.38897685384537, 2.16808148399543, 2.65924829403847, 76 | 0.712731581125743, -2.27120955878056, -0.880673034132468, 3.1714437689298, 1.55358118096413, -0.0196840661799863, 77 | 1.36715980945078, 0.0165168476035796, -0.361266535035795, 2.42903272816492, 0.0859738876627339, 3.29357172868433, 78 | 3.9268153049159, -0.0772185685640941, 2.7395994279804, -0.621524334133572, -0.511530465682691, 0.2317516601398, 79 | -1.07753105574493, 0.323154520884174, -1.56027745968308, 1.69237607227489, -2.4928332676874, -0.51411115614987, 80 | 0.523426092901334, -0.792099839062467, -0.661671099193596, -0.963341586004282, -1.025522450437, 0.124942574157214, 81 | 0.615093202983625, 3.59497199231568, -3.07706557229721, 1.02464832228954, 0.575186618136236, 1.14317850503507, 82 | 1.72080166427526, 0.66470445686251, 0.890664328657449, 1.79176604597143, -2.84001585366204, -0.741072818095891, 83 | 0.913830411517674, -1.71263617067831, 2.64978793744459, 0.926203051531795, -3.71744668284093, 1.23754036692694, 84 | -0.952979908925408, 0.740167190624829, 0.704090922168532, -0.147646671799528, -2.22192286882542, 0.600067485892054, 85 | -5.87779189388218, 1.01239913169371, -0.943142683327459, 0.0163412614618589, 1.56428635113983, 2.77802429996028, 86 | 0.999447178649491, 0.902013378357271, -3.47174695926919, 2.4315048023544, 2.24618099610904, 1.46255643730949, 87 | -3.88668125007907, 1.20913199131384, -1.54553864413762, -3.66350515907364, 2.03567431222538, 3.84489703906902, 88 | 1.1689745267362, -2.25941764440645, -3.47538691274868, 3.13534647223833, -0.847105505235109, 0.948440790323099, 89 | -0.205749967336535, -1.5117538509673, 3.94800138129151, 0.346882278019239, 2.83384575562394, -0.409962757092534, 90 | 1.94215408977393, -0.407354155308547, 0.423603861083299, 0.684614142232429, 0.28145935462725, -0.23130455045145, 91 | -0.784331318690699, 0.96186932339141, -0.13559561028754, 0.556408997162904, 0.761343195916989, 2.17134137040358, 92 | 0.11503883186759, 0.732105367990709, -1.75288660173309, 1.83391605663121, 0.823263556771948, 0.0983012929366989, 93 | 1.34081408751966, -1.04282121420697, -1.79979831150086, -0.544538835250276, 0.222478678050219, -0.303416512266726, 94 | -2.57048530724497, 1.13563771266784, 0.91561116605342, -1.34292338373588, 2.31378125909458, -0.733179302432669, 95 | 0.661462005371547, -0.588806729024348, -0.703868502776974, -0.96232138841801, 1.42251265604481, 1.7103708883739, 96 | -4.15475205205501, -1.6735612504075, 1.42752578093264, 0.218198422445086, -0.10326838767214, 1.5445565270668, 97 | 0.28759889475142, 1.03095910641709, 0.613668730326088, 1.50387062303787, -1.40171418770066, 0.788468695457926, 98 | -2.34387100970231, 1.75437034733546, -4.70362715301781, -2.43192951319531, 0.930111579802539, -0.0199751869680365, 99 | 2.97258954401692, -1.24187830307982, -1.90019675177668, -1.87601188040025, -0.436338794020732, -2.48066767572886, 100 | 2.91611221110062, 2.20948683067298, -2.24763303351106, -1.62507757296138, 1.20410490318299, -1.41618385616596, 101 | -1.12379830796754, 1.58357727284914, -0.00653514773391728, 0.407600561443092, 1.75474513265013, -0.59679495487265, 102 | 0.832653481683181, -2.30049014273782, -0.503687069773006, 2.91172854314957, 2.91726675480919, 0.718973300717549, 103 | 2.82408291706945, -0.508369804458931, -0.811035832646499, 2.9411348066735, -1.84961397376983, 4.47714470910093, 104 | -2.4183302782753, -1.67841347960582, 1.37374436738219, -2.90183812661439, 1.28971081578799, -1.51411626239641, 105 | -1.01436482702816, -2.70539138314324, 4.66270192902194, -0.295286123102207, 0.619378956197799, -0.697822650291226, 106 | 1.36732949410492, 1.78865395713018, 1.28750753649489, -0.739113677624461, -1.79301154890639, -0.735351971910027, 107 | 1.39643556933055, 1.5612423882589, -5.78554918602247, 0.524386899748744, 0.154416786814975, -1.48054521824262, 108 | 0.0716974293441585, 1.86258330898638, -0.548051778920168, 0.344847705708142, 0.840976961989663, -1.3607120681393, 109 | -3.80485307394032, -1.02755118660012, 0.360534289424389, 2.74689596012086, 2.60764130540425, 0.767194018579478, 110 | -1.70518929237267, -0.712364629380218, 2.29493297649402, -0.605615506818826, 0.332362920998355, -0.436214033435082, 111 | -0.182310084330898, -3.44892631014521, 3.3900598777517, 0.254911691552688, -0.0789167918721656, -2.69241421027943, 112 | -0.639453364296709, -1.51817770808696, -1.41265669950169, 0.473410999589592, -0.248829654826105, 1.21798883116038, 113 | -1.81760812027381, 2.17070731877953, 1.07921826599463, -2.33222831038585, -3.46513579992463, 3.92438568104819, 114 | 0.946385510252748, -1.06414713927595, 1.04693392221918, 1.0228749923474, 0.22359945890904, -1.06834103348682, 115 | -2.0455614009012, -1.3211067170199, -1.70353706565959, 3.05061253371525, 2.73512747996355, -0.195364444214567, 116 | -0.120224910728617, -1.42701989044147, -0.0753192248329186, 1.47443042824447, -0.716946460084727, -0.413207653648445, 117 | 2.83215758008484, 3.25151687164511, -1.31265283172619, 0.782535615228212, 1.49062636072837, 2.38798954452061, 118 | -1.79354688159849, -0.517580144006596, 0.47740806699988, -1.57842363072541, 0.41905396432482, 2.02818348621818, 119 | 1.51833212610216, -0.447036496304949, -0.819897760490449, -1.01749842802848, 2.18487617606147, 3.43875551724911, 120 | -0.891000291453652, -0.581004986155109, 1.67227080386371, -1.30352198182979, -0.386842760840038, 0.116987567806281, 121 | -2.07360765641767, -0.208050361657903, 1.61096325034053, -0.58250294052089, -0.465335429284823, -1.97990465794318, 122 | -0.284620408515581, -1.5530998115555, -1.23878858786327, -0.167572563237314, -0.0568644651637708, 3.15688391103243, 123 | -1.23671644794502, 0.105670921772999, 1.48415947833025, -2.72520485107597, 1.98845265655787, -3.3044904837512, 124 | 4.14131676280283, 1.8178902554432, 1.67550336917058, -0.419381803312046, -0.112197169774325, -0.946980010534109, 125 | 4.14936428508625, 2.66425747867466, -0.543000323417032, -2.04355766398434, 3.13709487314846, 0.524577783142451, 126 | -0.138060938523938, -0.678931112315457, -0.615333675282163, -1.72591079542673, 1.22437594339284, 4.6432852750905, 127 | -3.02521735992416, 1.76639231111141, 0.0982157533346887, -1.33533903502217, -0.76373384361318, -3.12568945939733, 128 | -1.27831145824198, -2.29111777878401, -0.61495595656245, 2.31573924240428, 1.88810515720046, -0.16112299057416, 129 | 1.79669753994491, 0.539145066471411, 1.34746739716695, 1.23917811945854, 0.749041514337003, 3.50403202425053, 130 | 1.81560815637852, 1.32151743808227, 2.87244704336262, 0.992920712563419, -0.00623748195277366, 1.37655378041298, 131 | -0.214832910350545, -1.96286541312817, -2.94006760610819, -3.48493578060337, 0.344549603775324, -0.291774414574002, 132 | 0.00690337131874758, 0.956580747272851, -0.00469602645420533, -0.805415230075618, 0.884729651885267, -1.4511123180901, 133 | -1.10051192367174, -2.65724721869175, -0.131909255721811, 0.0476886729848612, 3.70437201606473, -3.15913173198366, 134 | -3.6932380567881, -1.30464783351948, -0.543402549717554, -2.75892114539611, 4.28637295067154, -2.59475222282, 135 | 0.338472648229414, 0.00560091954151038, -1.43910326821232, 3.06081655747899, 0.841545044728545, -4.2786640794453, 136 | 1.3698594208894, -1.45564547082023, 0.181439787316659, -0.969669809914332, 1.36612289760876, 1.06326303066042, 137 | -1.5000395198936, 1.97881122726553, 1.0268711681672, -1.66955310586129, -1.10489462434308, 3.43028410466813, 138 | 2.57491264014705, -1.79502652868134, -0.525536578312499, -0.665540178033214, 4.33394161311059, 1.1154086870885, 139 | -0.335063535575263, -0.43145646609064, -1.77676746507487, 0.180413218576034, -1.98406439405249, -1.64570710053155, 140 | 0.673356486515467, 0.193613031684727, 0.973528082373361, 1.5444522530589, -0.738940657600758, -2.24400141644715, 141 | -1.66637395548261, 0.672634795608524, -1.13555387456465, -0.128503692088455, -1.91327774175174, -2.38681759199364, 142 | 2.32751720143219, -2.57017251445944, 1.96033317175985, -1.04878759899483, 0.369912665685496, -0.78028807455502, 143 | 0.658437626205707, -1.19872641508666, -0.242660650149105, -3.13400437436235, 1.82319031561281, 0.255896720161721, 144 | 1.46918234251688, -1.8148651055924, -2.201265296076, -0.559086121036214, 5.87408443561656, 0.00373701758480173, 145 | -1.74721224223806, 1.38586543059863, 0.111039609262691, -0.331404109771393, -0.372158046552632, -1.41167648537365, 146 | 0.108571813324765, -0.011279638125608, -2.11839049429992, -2.20080916208066, -0.237527904692324, -1.68529945180572, 147 | 0.565485887231355, -2.88274454452724, 1.48354585573364, 0.0152616791746361, 1.52877279152779, 0.308439030421961, 148 | 1.27928414854257, 0.479453635763578, 0.459345620466327, 1.14067372099882, 1.56619294684858, -2.39997003750201, 149 | -0.256257036975002, 0.940657312506235, 1.65791195088271, -1.53995884344766, -0.623736651960553, 1.91315210879324, 150 | 2.65155143482239, 0.709098691412881, -0.635909596758476, -1.79334680775731, -0.348761777161998, -1.63540654006067, 151 | 1.1306446609338, 0.248282557674565, 1.18401159104429, 0.496501081002607, -0.50110950412365, -0.958015229794818, 152 | 1.12178323049355, -1.26885514993936, -2.55808361030992, 0.315098257904927, -0.111522233378047, 1.69788217043688, 153 | 1.11792152334535, 0.180605134886399, -1.46707911097077, 2.11934532177035, 1.64626753323918, 2.78210099659422, 154 | 2.87623659445249, -0.109878265051329, -0.745194565810166, 1.84670952453888, -2.07673249829034, 0.713565292897239, 155 | 0.92453019573433, -5.80592454814232, -2.27981346772052, 0.657831599226184, -0.438600122261934, -4.20782012210412, 156 | -0.672239103338481, 1.90023625524656, 3.37838136078018, -0.607457697236948, 0.372544310805443, 0.301215261148968, 157 | -0.615409004908731, -3.50657765641244, -0.721934951854982, -0.947563288026582, -1.36135934446017, -1.60139350813517, 158 | -1.93629142530013, -2.85799646648072, -2.47845631808278, -0.291522060769191, -0.381050554458634, -0.463277477339433, 159 | 2.21082601758247, 0.841936254706115, -1.02962916149882, -0.107923709094317, 1.79105398085333, -0.666745522829573, 160 | 1.17185466043643, -1.41122284339676, 0.938139659472795, 1.10678450092974, 0.483997374578278, -0.350568702256166, 161 | 0.336334282741112, -0.0533178408608041, 0.175246901245738, 0.384012481620346, -2.63026456013059, -1.95265321235176, 162 | -0.0369345782669561, 0.88557890102721, 2.34559476284429, 1.24070878629597, 1.4499325945478, -0.5361816475255, 163 | -0.541448768680158, -3.95638122848992, -0.603832247492927, 0.376262984237162, -3.02547874266899, 1.56250805790875, 164 | -0.728724985167747, 0.819971843185472, -1.44023976568218, -0.836717449525409, 0.254932727201205, -1.23155932301895, 165 | -1.45848921797856, 2.7872343033175, -0.305899559641865, -0.749962023301405, -1.3116298326042, -2.65197519910945, 166 | -2.12158265419702, -1.22339362622324, 0.106554681387868, 2.36839186816052, -0.914935828172229, 1.13643036216014, 167 | 0.507070809373863, 1.06969854788365, 0.362687813900935, 1.84451554586414, -0.220910809494565, 2.9734478692313, 168 | 0.198424317710522, -1.30969238884426, -1.05247365087701, 0.426727349007452, 1.93386626118967, 1.54979360647473, 169 | -1.33510729689451, -0.177014891028765, 1.2529851664247, -0.231989834649266) 170 | y <- 171 | c(4.09321261961032, 0.913029738879507, -1.52000643005669, 4.15034287309354, 2.89639494447778, 0.99912283540669, 172 | 1.08211367985954, -0.29859590717145, 2.88268321969094, -0.860778808032692, 1.91637401086325, 0.030021725482337, 173 | -1.31139948986484, 0.343334498956989, -0.0815208612697721, 2.59117488099716, 3.28569871027358, 1.86907954162689, 174 | 1.0409265858678, 2.59752891961111, 2.76686314446292, -0.106947238939373, 0.425460564126758, 1.82093088502804, 175 | 2.92673426595568, 2.29798039396622, 1.50852440417803, 2.07969573508695, 1.33411795966622, 1.7967545153825, 176 | 0.468450854864287, 2.47706591671285, 1.85570625458699, 3.18519475422936, 3.7082891424054, 3.20710609354119, 177 | 4.21964489862411, 0.173425096751553, 2.14628518232691, 0.510363427767432, 3.08050619414246, 2.64460735587963, 178 | 2.93970588297264, 0.365218360569378, 1.84485048094099, 0.336911451338909, 2.12029618663737, 3.56154417463365, 179 | 0.624366540482505, 0.0783482797383266, 1.57257966771474, 2.13610605336542, 2.55987993357925, 0.741797911140622, 180 | 2.90338837501739, 5.15159455321963, 1.08227598097881, -1.78619306132453, 1.38579774764371, 2.63872030390259, 181 | 0.28552447393337, 1.16039181016147, 1.27853690768405, 3.08862830165274, 0.637883614512416, 3.2763799148464, 182 | 2.8879471709822, 1.66025349792065, -0.0268340443953689, 1.16200414777632, 0.612196717752689, 3.90811343489492, 183 | -0.10026638805403, 0.873311266497441, 0.796144960954674, 1.40260939143972, 0.953702968497859, 2.41273683011668, 184 | -0.0157526911460181, 3.54470859281219, 4.17704143443671, 4.36134275370551, 1.16926487521097, -2.33898470769269, 185 | 1.86486963298553, 0.884383479610751, 1.11340035020301, 1.23828306192409, 0.300560913899301, 1.55042076425748, 186 | 2.45725097749438, 0.663883819268411, 0.289564724864882, -0.632524270762662, 3.59569124406479, -0.0583792642746095, 187 | 2.15487856693163, 1.30819678933378, 2.69011531124074, 3.5811598364605, 1.67129542716692, -0.14867987799989, 188 | 3.64607069483943, 2.6701794546118, 1.58908171690094, 1.0739998985601, -0.37017986293367, -0.428527803469043, 189 | 2.44871278475326, 0.770005877284758, 0.612180101736191, 3.27784639126792, 0.976420357171391, -0.490363732908601, 190 | 0.57978757034066, 2.65949489129565, 1.32490437211952, 2.24201267687948, 1.23312198075701, 3.97506467547112, 191 | -1.35434572507029, 0.388287320992519, 2.70427925003346, 1.72113274493045, 1.48552167204184, 0.0144335307605211, 192 | 1.517875558442, 2.07612072982722, 1.88209811370263, 3.43336274850652, 1.8209759656242, 1.92652522611024, 193 | 0.315248569768283, 2.14442231241001, -0.0396975179680036, 2.22496299887118, 0.196604200013059, 1.50966315208958, 194 | 1.37324323019842, 0.610454349359322, 2.43940059888664, -1.30336321860082, 0.759567619536034, 1.39773880269089, 195 | 2.72404513315737, -0.454470254810424, 1.24939605098303, 1.24572467146646, 0.708582768180385, 0.593227880720287, 196 | 3.41221778282807, 0.982020440933972, 2.68553868779715, 0.641700051118403, 0.161521738942025, 3.11827042235913, 197 | 0.0847845403269621, -0.358128602871222, -0.490222975212125, 0.515923811643469, 2.97800840062333, 2.61748067120503, 198 | 2.59242909901078, -0.217934305198121, 0.963408876685928, -0.179636386757038, 1.76754910759398, 2.81580057814613, 199 | 5.29847181834518, 3.82037110134064, 0.416982460555141, 2.69395540428381, 0.323714890595682, 1.73937223301695, 200 | 0.624222615376499, 4.19848537728514, 1.82128679478557, 0.404936357076049, 2.24819216393239, 1.24635702277362, 201 | 3.27068299382672, 0.0912039846526116, 1.08275900279519, -0.0433683330247073, 3.69492346239896, -0.121391594732788, 202 | -0.606542178029206, 2.71803815487704, 2.20733843938916, 2.27747249869735, 2.15574444468048, 4.93370525730897, 203 | 0.440689728221148, -0.160008725290528, -1.10179595267864, -0.0710193278754955, -0.361336978174347, 1.05337510065607, 204 | 2.39369112462763, 0.115097251083697, 1.67722626558158, 2.08132213999404, -0.367765196804355, 0.642110809420777, 205 | 0.263620428673311, -0.115399531456492, 2.96147904417185, -0.374570196283091, 1.62174524447954, 1.22043485677552, 206 | 2.68498039941255, -1.58741977558664, 1.96107985659395, 2.83178387146323, 1.99934537314918, 1.02879434437607, 207 | 2.40547115076836, 2.29812128365925, 0.362119100658346, 0.586871979130403, 3.58751297581587, 4.02907941895184, 208 | 2.37925333250761, 3.53179572637047, 1.88702419856866, 0.156499551534123, -0.269400167828235, 1.43251536144712, 209 | 0.0678944359104987, 3.96948787963822, 0.313387892867861, 1.64343360063939, 1.55194032853145, 2.73926507777634, 210 | 3.87673401155136, 0.978396423263973, 2.30147645477159, 0.161143914484639, 0.878268658164506, 1.14995421724149, 211 | 0.0344290297473015, 0.297285671390229, -0.924128361865014, 1.49773356535779, 1.42444227990827, 3.62434993258555, 212 | 1.28589866350862, 2.04630745547954, 0.720352504788885, -1.07938611200004, 3.26610591459358, 0.621428671287927, 213 | 1.60088457943365, 1.45016497068544, 0.733699504676149, 0.521107346744342, 2.21897502542792, 1.77306015197449, 214 | -0.0343457892057231, 0.310532680677379, 2.97946206979068, 1.21745067831175, 0.705929181969721, 1.35846209769936, 215 | 2.00536457357482, 0.838774480037818, -1.40941048480838, 0.995969907872874, 2.03251193660109, 1.78125150849814, 216 | 1.81073531242225, 0.740910518715359, 1.63377030166295, 1.19972276018817, 0.57215590577814, 1.65171095014138, 217 | 2.33702790890342, -0.675999940683589, 1.58474258649632, 0.936120489272363, 1.19167895398344, 1.04231487108516, 218 | 2.15102438919314, 3.44031927679386, -0.380917167522604, 0.838805045414338, 2.07965537319571, 0.637784387481786, 219 | 2.29870562579393, 1.23601838415802, 1.87044851081529, 3.08536262879412, 0.934505725309734, 1.68138322982001, 220 | 1.01526342510896, 0.941765930376399, 1.53477732278793, 1.92191884239398, 3.58297691536235, 2.00435196029219, 221 | -0.525311248039306, 4.43459383450627, 1.95528067750403, 2.09404059296556, 1.46177969811782, 2.14713144327006, 222 | 2.70240445989524, 1.57858378715265, 1.36299987197737, -0.758555194447416, 0.992921192004053, 1.00683553062598, 223 | 4.10904478744738, 2.43882976199047, 2.77775940015792, 1.76806886551308, 1.25634935102336, -0.959969117241652, 224 | 1.1640452555003, 2.00237506628269, 0.745093663114181, 0.439020630365179, 1.18890026185967, 0.629524063241482, 225 | 0.964970288203398, 0.535769790867729, 3.19540608827785, 1.23179522013014, 4.03112323967, 2.79882513723854, 226 | 0.182392738679284, 5.46031642933399, 3.84458166225927, 1.12670436959345, 2.32113628571841, 2.05231275299842, 227 | 2.42659530519935, 0.853127502905693, 2.53412620954811, 4.85970812002659, 3.20599976313356, -1.22791801894699, 228 | 0.743701287053884, 1.90857953450979, 0.993460201557671, 0.749843717470442, 2.52607038879533, 0.376173126492325, 229 | 1.78941417916714, 3.24337292164057, 2.95850106533403, 2.65897654709315, 0.503051060725511, 3.22633497681201, 230 | 1.09534001471549, -0.0757077874753784, 1.41078383687826, 2.84722167157215, 1.08877083110023, 1.96107708794485, 231 | 2.52363716324902, 2.23953954298482, 3.60653301204482, 2.11094607978909, 2.72061685628213, 2.34229158016391, 232 | 0.0589158290208395, 0.460769175325292, 0.740616573663058, 2.18449285578637, 0.968932544101162, 1.12299942117837, 233 | -0.379521658139272, 2.06441798774162, 2.42284039795967, 0.48712179517697, 1.86118972590093, 2.83322644298144, 234 | 0.784392192145685, 0.654600875244286, 0.374193824375087, 1.20334959571634, 2.94408206259358, 3.53653416888936, 235 | 0.351306184176725, 2.0223256229209, 0.367490991948104, 3.35489147967852, 1.6824878905876, 0.558160780790123, 236 | 2.86627318467496, 0.901773510087595, 3.70228708296573, -0.362422349415035, 3.06025696385339, 3.14346670487588, 237 | 0.989261662609541, -0.340265366133011, 1.42066224291654, -0.0283938962431776, 1.02633847944213, -0.0860864959259057, 238 | 0.956156760019438, 3.31225197165691, 1.43963530334162, 0.0901406213480833, -1.42698785975014, -0.302734413620216, 239 | 1.07185921018725, 0.266091263321139, 1.41512774864085, 1.22426125971676, 1.0987699926457, 0.0714350748802789, 240 | 0.703632709454185, 1.26546966512123, -0.969328403595323, 1.61689271189736, -0.516920721918932, 0.232805488816433, 241 | 1.28810541986937, -0.143644284201457, -0.122273441357518, 0.0327323993826025, 1.40998723897439, 0.711977972930796, 242 | 1.52272249297501, 3.58767962701002, 2.09328921247643, 1.78253141253804, 1.14347857306659, 2.14883085159507, 243 | 1.0782084009057, 2.05434065252423, 1.62151607373388, 2.22243721267223, -0.302587678052417, -0.806421038531469, 244 | 1.33908051054444, 2.76471130338785, 2.5340891976479, -0.482625806526023, 0.344878190386138, 2.02551161355536, 245 | 0.391417189097671, 1.6911421706425, 1.69188004841594, -0.888191200425209, 1.47687621161585, -0.918537301037894, 246 | -1.43417550786572, 1.69980066200757, -0.73752419838363, 2.18789519735777, 2.03028371922513, 0.368528338124499, 247 | 2.58628076450438, 1.02110555330262, 2.69941559134338, 0.511185986717186, 3.40375258078098, 1.88781631029485, 248 | 1.00203502752984, 1.61465792494404, 1.72586761712736, 2.56645689891174, 2.10453169129281, 0.986755411266939, 249 | 1.21017622564881, -0.574318099640658, 3.88913102147272, 1.14221063245285, 1.10457188322069, 0.740529140724361, 250 | 0.255732067097214, 1.39194108921291, 0.407888743279962, 0.293060322735106, 3.62795377453793, 1.68103595915825, 251 | 1.06918827712726, 2.98391234383537, -0.704192408363461, 1.09640857163994, 4.24028335000687, 0.697943748493506, 252 | 2.01828219055506, 0.963989512191895, 1.05631275404934, 2.48934877936732, 2.9225732606325, 1.01789911175197, 253 | 6.29767770135188, 1.07447341366547, 2.39110964861299, 1.78653041607376, 0.141634247485447, -0.726691144116602, 254 | 0.127233334090803, 0.675174213127952, 3.80920779096633, -0.609678664552043, -0.454032180108705, 0.203080433272608, 255 | 4.15132804188615, 0.494732047339845, 3.02796175324832, 3.67107160809726, -0.31953425105126, -1.37256105287655, 256 | 0.804660198509745, 3.65398718824898, 3.68567635277981, -0.636019816359457, 2.06465290427854, 0.408632979711445, 257 | 1.36794812138867, 2.44048389930693, -1.6031728850044, 0.966576495596317, -0.995792759717186, 2.52978564076606, 258 | 0.354142143293367, 1.37083368286664, 1.58924283121106, 0.860185660585792, 1.32956030611851, 1.64185054377141, 259 | 3.06416642117337, 0.0701731616758758, 1.64892393588888, 1.3173163377083, 1.15762779162927, 0.0757940554046765, 260 | 1.40286409263484, 1.14530206448492, 2.55925615325654, 0.168676145743757, 0.957817107959313, 1.21727549253246, 261 | 1.02639444543396, 2.61422906177331, 3.35056670416635, 2.21953956016956, 1.19933576660051, 1.145724648303, 262 | 3.57837465206737, 0.909847711716077, 1.23196044811668, 2.65645725810815, 0.04386121475556, 2.57485774054118, 263 | 0.772120061861759, 1.27586223492898, 2.50119595006469, 2.14551979031477, 0.401887368817353, -0.061860406809632, 264 | 4.55973820872442, 2.71841185839313, 0.572081237852014, 1.68338346672031, 2.05115175761569, -0.0889252025314778, 265 | 1.37323353226966, 0.796641273336469, 0.551928492040589, 0.194580959422487, 2.23354133073286, 0.596218119470255, 266 | 3.53011073733074, -0.0181316549960668, 4.8044298437824, 3.72043017185584, 1.35848642242917, 1.46406230142204, 267 | -1.55263099699867, 1.93101915668395, 2.76380888991147, 3.01130952877212, 2.44451407363277, 3.15705805812754, 268 | -0.528910604908715, 0.147152776862518, 3.11518815986173, 3.2620107021579, 0.942605611965076, 2.49163671181177, 269 | 2.07429444916949, -0.133787981689672, 1.73079474416316, 0.763076590801216, 0.252648919409172, 1.22777605237725, 270 | 0.848273258989574, 3.62020464891545, 1.66021463734779, -0.529624922351691, -0.901001395790005, 0.885669592949573, 271 | -1.30406191497362, 1.7406154688764, 1.6459723610058, -0.807252835531886, 3.27568467872634, -2.01080900029492, 272 | 3.37377282006872, 2.52792075223291, 0.899124204405803, 4.02646121004849, 0.351720174434834, 2.575234857468, 273 | 2.60625214453976, 3.6035708045419, -2.18263956091509, 1.58848221126227, 1.13996270196348, 1.76397418323567, 274 | 0.734055237254077, 0.350571297680903, 0.859961404694269, 1.88566859436685, 2.45957633888298, 2.45119824168142, 275 | -0.0523309764047473, 0.399798230681928, 6.3490419364817, 1.64255156385048, 1.49681792304193, 3.13595910105686, 276 | 0.8537204261838, -0.0863141135734797, 1.4592410832734, 1.63550787313118, 0.835983426334697, 2.54047880956247, 277 | 4.53513560888056, 2.26967664045368, 1.06971948918117, -0.960617543201271, -0.0760541763728872, 0.83085392344177, 278 | 3.00870443382252, 1.66162265030544, 0.226198587733247, 2.10524619222186, 1.01563493780581, 1.99527631310152, 279 | 1.51136295581848, 3.87100613042309, -0.334084747281979, 1.0236725504708, 2.17285698041277, 3.25349087212019, 280 | 2.37246031569949, 2.70570978887486, 2.27811125961249, 0.817943918001445, 1.43526341487092, 0.987408789532983, 281 | 2.61009511936405, -0.0222594354568378, 0.873306511383256, 3.37665659998833, 4.05981900953633, -1.29827632337998, 282 | 1.51122017866066, 2.17015607552156, 1.15486741111231, 0.351468493696525, 1.65937985305916, 2.62561839549727, 283 | 2.71944709951901, 2.37853031435699, 2.31316586825454, -1.20592196284984, -0.539466117784977, 1.73919494858082, 284 | 1.79293689454218, 2.71137783130984, 1.32783382836477, 0.702105801034454, 3.106352319865, 1.55586573420219, 285 | -0.026084010162573, -1.4189285353373, 1.92735913308255, 0.820207352903071, 0.602473546799985, -0.339102515821603, 286 | 2.91839699269116, 2.52295069742902, 1.66223514270921, 2.64816067674905, 1.20246675113458, -0.356943173322093, 287 | -0.0664119634618731, 1.71279356811939, 1.81562955615722, 1.84355477808967, -0.508848769208184, -0.824394598347882, 288 | 2.26907553957469, 2.36406334532383, 0.238504092743128, 2.15350250898054, 1.79971686906479, 1.38089923413865, 289 | 3.0910945104481, 1.67561118388226, 0.364420430653054, 2.1229284265312, 1.95035391216514, 3.14436646533845, 290 | 1.39669147734954, 2.98320081028047, 2.36332493937895, 1.53642567459095, 1.25382563841528, -1.35617108092112, 291 | 2.12272512886604, 1.96254501360658, 0.219832367872158, 3.46348786875236, -0.230415612808242, 3.76028640287119, 292 | -1.12271752275117, 0.351895941010165, 0.970647511478105, 2.31163411670526, 2.03645577542796, 2.83598228149118, 293 | -1.89260841087687, -0.53904502495578, 1.41976206624078, 3.29560570341916, -1.21667768712223, 0.927420412661057, 294 | 1.21409471376245, 2.25043239607045, 1.67631167819824, 3.19704551350105, 0.513823957579633, -2.19624953992097, 295 | 4.28923163551941, -0.222840003053493, 1.38896810559317, 2.62671423541001, 1.45192977403232, 4.06310800490308, 296 | 2.25807641884351, 3.38501490553395, 1.96568692569537, -0.136642591724362, 0.41683456592576, 1.95719672705817, 297 | 0.284533028777548, 0.612935311414318, 0.799877111048384, 0.279494917288065, 1.2269455649674, -0.746147105261822, 298 | 0.579008099588994, 0.303898362520712, -0.314276525777138, 0.285402257472302, 1.92383504164652, 0.157326164384764, 299 | 2.02611664012673, 2.49082979432319, 4.01757291455758, 3.43751347568533, 0.688632811315826, 1.95176017999056, 300 | 1.83980855434169, 0.556651514245863, 1.42406328434086, 2.23184513071833, 0.299930007168815, 2.48229253646995, 301 | 2.44678173449586, 3.70343587302699, 1.92149613052011, 1.56251853405422, -1.57636984532531, 3.88842968560542, 302 | 3.80570188998941, 2.46686080428244, 1.63695411291694, 3.4108971174609, -2.0116134406481, 3.37257698537968, 303 | 1.30105243678474, 1.05731086666002, 2.59021455252566, -1.00179522201112, 0.925180502253882, 3.89946211329145, 304 | 0.291001136207085, 2.31148797146298, 1.46849952508102, 2.3758500074294, 0.848691296141032, 1.18998068218256, 305 | 2.31149730612413, -0.194463884702655, 0.749236001734463, 3.48820622661055, 1.71233098443215, -1.20339631290637, 306 | -0.842159343398741, 2.84516411189009, 1.53370050152653, 2.21412797656611, -1.65051808904485, 0.670743705192673, 307 | 1.534504058987, 1.96848349425432, 2.9843918352648, 1.26129883310799, 3.11326172391896, 2.79816486023283, 308 | 0.85891056952982, 1.89546067715942, 0.656581154896421, 0.386238176935947, 2.07220863757888, 2.94126080396632, 309 | 2.99702495554191, 0.851431788224715, 2.27046268741267, 1.46243435711124, 3.03072771007654, 3.15452485097099, 310 | -0.0864231778928764, 3.35546186972655, 0.00124819913763669, 2.39079727449894, 0.865007861781265, 2.03567833084434, 311 | 1.40874924337594, 2.01454832891051, 1.39908376510079, 3.79781400915645, 0.186088868350486, 1.90250439193913, 312 | -0.0952789329526168, 2.61807996745755, 2.92302551919088, 1.76302693132054, -2.96911272182897, 1.64364992162355, 313 | 2.77450788564301, 1.02271681426266, 1.28390986857272, 1.53736645786611, 1.63881459939964, 2.35253355821583, 314 | 1.41794695147816, 1.66564742320104, 3.24887110461376, 3.2461598291219, 1.0738338428779, 2.82330163145636, 315 | 2.02582592695775, 3.17029013053227, 0.511802130402621, 1.38194487123292, 0.390532399921275, 1.33742025901059, 316 | 0.802992327958725, 1.93264667854889, 1.42918012780516, 1.09678050565504, 0.344529656365481, 3.88157559185623, 317 | 1.27357643992406, 0.627348867492911, 0.305016631247095, 3.05435191478174, 2.25947265845383, -0.206834631338269, 318 | -0.559838796700316, 1.12414463815155, 1.12667852850332, 3.01135687077758, 1.93966594054807, 3.20557893558127, 319 | 0.502699975015689, 1.2017667184532, 0.547162222736257, 1.16309615218679, 2.17342550521084, 2.22720008487568, 320 | 0.750103737668447, 2.22467235973635, 3.33895634538646, 1.17210736914905, 1.63596333397612, 0.0726244543591358, 321 | 0.0559976131950194, 1.61447317135411, 2.5377597819351, -0.149537356883677, -0.0840392465192093, -0.0778150454409202, 322 | -0.211731612422485, 1.37428405817172, 1.5087392041638, 0.161946460827746, 2.95646049010091, 0.78346799269428, 323 | 0.276799220053059, 5.65122423104723, 3.53407083442801, 1.17810096759635, 1.7115616956669, 4.38335554272312, 324 | 1.94370565349226, 0.0651692059676496, -1.30306557770104, 1.81019755681093, 1.30804622558708, 1.52590709960593, 325 | 2.02163038640456, 3.61301408176047, 1.61499483469986, 2.19074334390178, 2.93740831987617, 2.33026177200824, 326 | 3.31550271830491, 4.57329252967054, 3.77584659598598, 1.67384197943036, 2.4281793371634, 1.3498125356525, 327 | -0.0504889396121124, 0.941661369971904, 2.15945876781595, 1.66546788374136, -0.12353678567133, 2.56462278255081, 328 | 0.324319610339132, 2.89714855874152, 0.492831133323445, 0.866030275308876, 1.56203602920506, 2.13256841238061, 329 | 1.74452895927964, 0.958297420626964, 1.24193279477275, 2.01567690740236, 3.33804637512299, 2.85527672590805, 330 | 1.42583484495621, 0.707403759124224, 0.0795870019756514, 0.330711074301718, -0.0234985199962274, 1.91832474047302, 331 | 1.58142417983675, 5.02445767637512, 2.07326510107575, 0.737176925491965, 3.67903497996156, 0.308862896539812, 332 | 2.18863589630007, 0.514878286852934, 2.69487671921054, 2.10965929765648, 1.41454776325155, 2.24785234337198, 333 | 2.90924619258157, -1.12316622929532, 2.30474136633931, 2.63223757856747, 2.37158143160764, 3.3031567426752, 334 | 3.84036875615065, 2.6252789958729, 1.33058755568246, -0.0978812122484387, 1.70976088065832, 0.288670521370106, 335 | 1.3474466265222, 0.22027118522591, 1.33119560001082, -0.349447081702539, 1.47138889743383, -0.532429822287273, 336 | 1.12012143589041, 2.44632132915787, 2.02233401560028, 1.3534181227979, 0.0531170333324189, -0.00638330366437717, 337 | 2.44918645949934, 1.3445612900376, 0.908363780639696, 1.26105845093861) 338 | -------------------------------------------------------------------------------- /r/demo.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/betanalpha/mcmc_visualization_tools/7143752d8e6fa99f4ec63959acb45eab68d87cbd/r/demo.pdf -------------------------------------------------------------------------------- /r/demo.qmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Markov Chain Monte Carlo Visualization Functions" 3 | author: "Michael Betancourt" 4 | date: "May 2024" 5 | toc: true 6 | number-sections: true 7 | highlight: pygments 8 | crossref: 9 | lst-title: "Stan Program" 10 | filters: 11 | - include-code-files 12 | format: 13 | html: 14 | html-math-method: katex 15 | theme: 16 | - lux 17 | - custom.scss 18 | standalone: true 19 | embed-resources: true 20 | code-overflow: wrap 21 | linkcolor: "#B97C7C" 22 | pdf: 23 | keep-tex: true 24 | fig-width: 5.5 25 | fig-height: 5.5 26 | code-overflow: wrap 27 | monofontoptions: 28 | - Scale=0.5 29 | knitr: 30 | opts_chunk: 31 | comment: '' 32 | opts_knit: 33 | global.par: TRUE 34 | format-links: false 35 | --- 36 | 37 | In this note I will review a suite of `R` functions that implement various 38 | visualizations of probabilistic behavior using the output of a Markov chain 39 | Monte Carlo algorithm. 40 | 41 | Most of these visualizations utilize nested quantile intervals to visualize 42 | one-dimensional pushforward behavior as described in 43 | [Chapter 7, Section 5](https://betanalpha.github.io/assets/chapters_html/transforming_probability_spaces.html#sec:1d-pushforward-characterizations) 44 | of my probability theory material. The individual quantiles are consistently 45 | estimated as the empirical average of the empirical quantiles derived from 46 | individual Markov chains. Because they do not communicate the quantile 47 | estimator errors these visualizations can be misleading if the Markov chains do 48 | not contain enough information. 49 | 50 | # Initial Setup 51 | 52 | First and foremost we have to set up our local `R` environment. 53 | 54 | ```{r} 55 | #| echo: !expr knitr::is_html_output() 56 | #| eval: !expr knitr::is_html_output() 57 | par(family="sans", las=1, bty="l", 58 | cex.axis=1, cex.lab=1, cex.main=1, 59 | xaxs="i", yaxs="i", mar = c(5, 5, 3, 1)) 60 | 61 | ``` 62 | 63 | ```{r} 64 | #| echo: !expr knitr::is_latex_output() 65 | #| eval: !expr knitr::is_latex_output() 66 | par(family="serif", las=1, bty="l", cex.axis=1, cex.lab=1, cex.main=1, 67 | xaxs="i", yaxs="i", mar = c(5, 5, 3, 5)) 68 | ``` 69 | 70 | This includes loading up `RStan`. 71 | 72 | ```{r} 73 | #| warning: false 74 | #| message: false 75 | 76 | library(rstan) 77 | rstan_options(auto_write = TRUE) # Cache compiled Stan programs 78 | options(mc.cores = parallel::detectCores()) # Parallelize chains 79 | parallel:::setDefaultClusterOptions(setup_strategy = "sequential") 80 | ``` 81 | 82 | Finally we'll load my recommended 83 | [Markov chain Monte Carlo analysis tools](https://github.com/betanalpha/mcmc_diagnostics) 84 | and the visualization functions themselves. 85 | 86 | ```{r} 87 | util <- new.env() 88 | source('mcmc_analysis_tools_rstan.R', local=util) 89 | ``` 90 | 91 | ```{r} 92 | source('mcmc_visualization_tools.R', local=util) 93 | ``` 94 | 95 | # One-Dimensional Baseline Function 96 | 97 | Our first example is one-dimensional curve-fitting, i.e. regression, model with 98 | a linear baseline function, 99 | $$ 100 | p(y_{n} \mid x_{n}, \alpha, \beta, \sigma) 101 | = 102 | \text{normal}(y_{n} \mid \alpha + \beta \, x_{n}, \sigma). 103 | $$ 104 | 105 | ## Data Exploration 106 | 107 | What is data if not an opportunity to explore? 108 | 109 | ```{r} 110 | data <- read_rdump("data/uni_data.R") 111 | ``` 112 | 113 | The `plot_line_hist` function constructs a histogram but then plots only its 114 | outline, without lines separating the interior histogram bins. Here we can 115 | plot histograms summarizing the observed inputs 116 | $$ 117 | \{ \tilde{x}_{1}, \ldots, \tilde{x}_{n}, \ldots, \tilde{x}_{N} \} 118 | $$ 119 | and the observed outputs, 120 | $$ 121 | \{ \tilde{y}_{1}, \ldots, \tilde{y}_{n}, \ldots, \tilde{y}_{N} \}. 122 | $$ 123 | 124 | ```{r} 125 | par(mfrow=c(1, 2)) 126 | 127 | util$plot_line_hist(data$x, -6, 6, 0.5, 128 | xlab="x", main="Observed Inputs") 129 | 130 | util$plot_line_hist(data$y, -4, 7, 0.5, 131 | xlab="y", main="Observed Outputs") 132 | ``` 133 | 134 | This presentation is a bit cleaner than conventional histogram plots, especially 135 | when there is no ambiguity about the binning. 136 | 137 | We can also plot histograms with arbitrary bin configurations. 138 | 139 | ```{r} 140 | par(mfrow=c(1, 2)) 141 | 142 | util$plot_line_hist(data$x, 143 | breaks=c(-6, -3, -1.5, -0.75, 0, 0.75, 1.5, 3, 6), 144 | xlab="x", main="Observed Inputs") 145 | 146 | util$plot_line_hist(data$y, 147 | breaks=c(-4, 0, 7), 148 | xlab="y", main="Observed Outputs") 149 | ``` 150 | 151 | A key advantage of reducing a histogram to its outline is that it is much easier 152 | to overlay multiple histograms on top of each other without compromising 153 | legibility. The `plot_line_hists` function constructs and then overlays two 154 | histograms with the same binning. 155 | 156 | ```{r} 157 | par(mfrow=c(1, 1)) 158 | 159 | util$plot_line_hists(data$x, data$y, -6, 7, 0.5, xlab="") 160 | text(-3.5, 90, cex=1.25, label="Observed\nInputs", 161 | pos=4, col="black") 162 | text(3.5, 90, cex=1.25, label="Observed\nOutputs", 163 | pos=4, col=util$c_mid_teal) 164 | ``` 165 | 166 | We can also use the `add` argument of `plot_line_hist` to overlay multiple 167 | histogram outlines onto an existing axis. 168 | 169 | ```{r} 170 | plot(0, type="n", 171 | xlim=c(-6, 7), xlab="", 172 | ylim=c(0, 160), ylab="Counts") 173 | 174 | util$plot_line_hist(data$x, -6, 6, 0.5, col="black", add=TRUE) 175 | util$plot_line_hist(data$y, -4, 7, 0.5, col=util$c_mid_teal, add=TRUE) 176 | 177 | text(-3.5, 90, cex=1.25, label="Observed\nInputs", 178 | pos=4, col="black") 179 | text(3.5, 90, cex=1.25, label="Observed\nOutputs", 180 | pos=4, col=util$c_mid_teal) 181 | ``` 182 | 183 | ## Prior Checks 184 | 185 | Here we'll be exceptionally thorough and start with an investigation of the 186 | prior model and its consequences. In addition to the individual parameters 187 | we'll look at the prior behavior of baseline function and the prior predictive 188 | distribution along a grid of inputs defined by the `x_grid` array. 189 | 190 | ```{.stan include="stan_programs/uni_prior_model.stan" filename="uni\\_prior\\_model.stan" eval=FALSE} 191 | ``` 192 | 193 | Note that I'm using a less-aggressive step size adaptation here because the 194 | half-normal prior model for $\sigma$ results in an slightly awkward tail for the 195 | unconstrained $\log(\sigma)$ values that can be a bit difficult to navigate. 196 | 197 | ```{r} 198 | #| warning: false 199 | #| message: false 200 | data$N_grid <- 1000 201 | data$x_grid <- seq(-6, 6, 12 / (data$N_grid - 1)) 202 | 203 | fit <- stan(file='stan_programs/uni_prior_model.stan', 204 | data=data, seed=5838299, 205 | warmup=1000, iter=2024, refresh=0, 206 | control=list('adapt_delta' = 0.9)) 207 | ``` 208 | 209 | Of course we always consult our diagnostics first to make sure that our Markov 210 | chains, and hence any visualization we derive from them, accurately characterize 211 | the exact target distribution, in this case the prior distribution of our model. 212 | 213 | ```{r} 214 | diagnostics <- util$extract_hmc_diagnostics(fit) 215 | util$check_all_hmc_diagnostics(diagnostics) 216 | 217 | samples <- util$extract_expectand_vals(fit) 218 | base_samples <- util$filter_expectands(samples, 219 | c('alpha', 'beta', 'sigma')) 220 | util$check_all_expectand_diagnostics(base_samples) 221 | ``` 222 | 223 | We can visualize the probability distribution of baseline functions in two ways. 224 | Firstly we can plot a subset of baseline function configurations. Secondly we 225 | can plot nested quantile intervals that quantify the marginal behavior of the 226 | function output at each input. Neither of these visualizations fully 227 | characterize the probabilistic behavior but together they capture the most 228 | important features. 229 | 230 | The `plot_realizations` function plots a selection of values corresponding to 231 | the `f_names` array against `data$x_grid` while the 232 | `plot_conn_pushforward_quantiles` function plots nested quantile intervals of 233 | those values for each element of `data$x_grid`. Here "conn" refers to 234 | "connected" as the individual marginal quantiles are connected into continuous 235 | polygons. 236 | 237 | ```{r} 238 | par(mfrow=c(2, 1)) 239 | 240 | f_names <- sapply(1:data$N_grid, 241 | function(n) paste0('f_grid[', n, ']')) 242 | util$plot_realizations(samples, f_names, data$x_grid, 243 | xlab="x", ylab="f") 244 | util$plot_conn_pushforward_quantiles(samples, f_names, data$x_grid, 245 | xlab="x", ylab="f") 246 | ``` 247 | 248 | Finally let's use the `plot_conn_pushforward_quantiles` function to plot nested 249 | quantile intervals of the conditional prior predictive behavior at each element 250 | of `data$x_grid`. 251 | 252 | ```{r} 253 | par(mfrow=c(1, 1)) 254 | 255 | pred_names <- sapply(1:data$N_grid, 256 | function(n) paste0('y_pred_grid[', n, ']')) 257 | util$plot_conn_pushforward_quantiles(samples, pred_names, data$x_grid, 258 | xlab="x", ylab="y") 259 | ``` 260 | 261 | ## Posterior Inference 262 | 263 | Having thoroughly investigated our prior model and its consequences and not 264 | found any undesired behavior we can move on to constructing posterior 265 | inferences. 266 | 267 | ```{.stan include="stan_programs/uni_full_model.stan" filename="uni\\_full\\_model.stan" eval=FALSE} 268 | ``` 269 | 270 | ```{r} 271 | #| warning: false 272 | #| message: false 273 | fit <- stan(file='stan_programs/uni_full_model.stan', 274 | data=data, seed=5838299, 275 | warmup=1000, iter=2024, refresh=0) 276 | ``` 277 | 278 | There are no signs of trouble from the computational diagnostics. 279 | 280 | ```{r} 281 | diagnostics <- util$extract_hmc_diagnostics(fit) 282 | util$check_all_hmc_diagnostics(diagnostics) 283 | 284 | samples <- util$extract_expectand_vals(fit) 285 | base_samples <- util$filter_expectands(samples, 286 | c('alpha', 'beta', 'sigma')) 287 | util$check_all_expectand_diagnostics(base_samples) 288 | ``` 289 | 290 | Before examining any posterior inferences, however, we need to validate that our 291 | model is adequately capturing the relevant features of the observed data. For 292 | this one-dimensional baseline function model we can implement an informative 293 | retrodictive check by comparing the conditional posterior predictive 294 | distributions at each input, 295 | $$ 296 | p(y \mid x, 297 | \tilde{x}_{1}, \tilde{y}_{1}, \ldots, \tilde{x}_{N}, \tilde{y}_{N}), 298 | $$ 299 | to the observed input-output pairs, $(x_{n}, y_{n})$. 300 | 301 | ```{r} 302 | par(mfrow=c(1, 1)) 303 | 304 | pred_names <- sapply(1:data$N_grid, 305 | function(n) paste0('y_pred_grid[', n, ']')) 306 | util$plot_conn_pushforward_quantiles(samples, pred_names, data$x_grid, 307 | xlab="x", ylab="y") 308 | points(data$x, data$y, pch=16, cex=1.0, col="white") 309 | points(data$x, data$y, pch=16, cex=0.8, col="black") 310 | ``` 311 | 312 | Fortunately there are no signs of tension between the posterior predictive 313 | distributional behaviors and the observed behaviors. Confident in the adequacy 314 | of our model we can move onto visualizing posterior inferences. 315 | 316 | For example we can visualize the pushforward, or marginal, probability 317 | distributions for each parameter. Note that the `plot_expectand_pushforward` 318 | function is already part of my Markov chain Monte Carlo analysis tools and not 319 | one of the visualization functions being introduced here. 320 | 321 | ```{r} 322 | par(mfrow=c(1, 3)) 323 | 324 | util$plot_expectand_pushforward(samples[["alpha"]], 25, 325 | display_name="alpha") 326 | 327 | util$plot_expectand_pushforward(samples[["beta"]], 25, 328 | display_name="beta") 329 | 330 | util$plot_expectand_pushforward(samples[["sigma"]], 25, 331 | display_name="sigma") 332 | 333 | ``` 334 | 335 | Communicating the posterior behavior of the baseline function, however, is 336 | facilitated with the new visualization functions. 337 | 338 | ```{r} 339 | par(mfrow=c(2, 1)) 340 | 341 | f_names <- sapply(1:data$N_grid, 342 | function(n) paste0('f_grid[', n, ']')) 343 | util$plot_realizations(samples, f_names, data$x_grid, 344 | xlab="x", ylab="f") 345 | util$plot_conn_pushforward_quantiles(samples, f_names, data$x_grid, 346 | xlab="x", ylab="f") 347 | ``` 348 | 349 | Conveniently all of these visualization functions feature optional arguments 350 | for baseline behavior which allows us to compare our posterior inferences to 351 | the true behavior when it is known, for example in simulation studies. 352 | 353 | ```{r} 354 | true_alpha <- 1.5 355 | true_beta <- -0.75 356 | true_sigma <- 0.33 357 | 358 | par(mfrow=c(1, 3)) 359 | 360 | util$plot_expectand_pushforward(samples[["alpha"]], 25, 361 | display_name="alpha", 362 | baseline=true_alpha, 363 | baseline_col=util$c_mid_teal) 364 | 365 | util$plot_expectand_pushforward(samples[["beta"]], 25, 366 | display_name="beta", 367 | baseline=true_beta, 368 | baseline_col=util$c_mid_teal) 369 | 370 | util$plot_expectand_pushforward(samples[["sigma"]], 25, 371 | display_name="sigma", 372 | baseline=true_sigma, 373 | baseline_col=util$c_mid_teal) 374 | ``` 375 | 376 | ```{r} 377 | true_fs <- true_alpha + true_beta * data$x_grid 378 | 379 | par(mfrow=c(2, 1)) 380 | 381 | util$plot_realizations(samples, f_names, data$x_grid, 382 | baseline_values=true_fs, 383 | baseline_col=util$c_mid_teal, 384 | xlab="x", ylab="f") 385 | util$plot_conn_pushforward_quantiles(samples, f_names, data$x_grid, 386 | baseline_values=true_fs, 387 | baseline_col=util$c_mid_teal, 388 | xlab="x", ylab="f") 389 | ``` 390 | 391 | The `plot_realizations` and `plot_conn_pushforward_quantiles` functions also 392 | include `residual` arguments that allow us to directly visualize how the 393 | probabilistic behavior varies around the baseline values. 394 | 395 | ```{r} 396 | par(mfrow=c(2, 1)) 397 | 398 | util$plot_realizations(samples, f_names, data$x_grid, 399 | baseline_values=true_fs, 400 | residual=TRUE, 401 | xlab="x", ylab="f") 402 | util$plot_conn_pushforward_quantiles(samples, f_names, data$x_grid, 403 | baseline_values=true_fs, 404 | residual=TRUE, 405 | xlab="x", ylab="f") 406 | ``` 407 | 408 | # Multi-Dimensional Baseline Function 409 | 410 | Now that we're warmed up let's consider a three-dimensional curve-fitting model 411 | with a quadratic baseline function, 412 | $$ 413 | p(y_{n} \mid \mathbf{x}_{n}, \alpha, \beta, \sigma) 414 | = 415 | \text{normal}(y_{n} \mid \beta_{0} 416 | + \boldsymbol{\beta}^{T} \cdot \mathbf{x} 417 | + \mathbf{x}^{T} \cdot \mathbf{B} \cdot \mathbf{x}, \sigma), 418 | $$ 419 | where $\mathbf{B}$ is a positive-definite matrix whose three diagonal elements 420 | are organized into the vector $\boldsymbol{\beta}_{d}$ and three off-diagonal 421 | elements are organized into the vector $\boldsymbol{\beta}_{o}$. 422 | 423 | ## Plot Data 424 | 425 | The `plot_line_hist` allows us to cleanly visualize each component of the 426 | observed inputs. 427 | 428 | ```{r} 429 | data <- read_rdump("data/multi_data.R") 430 | 431 | par(mfrow=c(3, 3), mar = c(5, 5, 2, 1)) 432 | 433 | util$plot_line_hist(data$X[,1], -9, 9, 1, xlab="x1") 434 | util$plot_line_hist(data$X[,2], -9, 9, 1, xlab="x1") 435 | util$plot_line_hist(data$X[,3], -9, 9, 1, xlab="x1") 436 | 437 | plot(data$X[,1], data$X[,2], pch=16, cex=1.0, col="black", 438 | main="", xlim=c(-9, 9), xlab="x1", ylim=c(-9, 9), ylab="x2") 439 | 440 | plot(data$X[,1], data$X[,3], pch=16, cex=1.0, col="black", 441 | main="", xlim=c(-9, 9), xlab="x1", ylim=c(-9, 9), ylab="x3") 442 | 443 | plot(data$X[,2], data$X[,3], pch=16, cex=1.0, col="black", 444 | main="", xlim=c(-9, 9), xlab="x2", ylim=c(-9, 9), ylab="x3") 445 | 446 | plot(data$X[,1], data$y, pch=16, cex=1.0, col="black", 447 | main="", xlim=c(-9, 9), xlab="x1", ylim=c(-25, 325), ylab="y") 448 | 449 | plot(data$X[,2], data$y, pch=16, cex=1.0, col="black", 450 | main="", xlim=c(-9, 9), xlab="x2", ylim=c(-25, 325), ylab="y") 451 | 452 | plot(data$X[,3], data$y, pch=16, cex=1.0, col="black", 453 | main="", xlim=c(-9, 9), xlab="x3", ylim=c(-25, 325), ylab="y") 454 | ``` 455 | 456 | ## Prior Checks 457 | 458 | As before we'll first investigate the consequences of our prior model. 459 | 460 | For a discussion of why the quadratic baseline model is implemented in this way 461 | see Section 2.3.2 of my 462 | [Taylor regression modeling chapter](https://betanalpha.github.io/assets/case_studies/taylor_models.html#232_Higher-Order_Implementations). 463 | 464 | ```{.stan include="stan_programs/multi_prior_model.stan" filename="multi\\_prior\\_model.stan" eval=FALSE} 465 | ``` 466 | 467 | ```{r} 468 | #| warning: false 469 | #| message: false 470 | fit <- stan(file='stan_programs/multi_prior_model.stan', 471 | data=data, seed=5838299, 472 | warmup=1000, iter=2024, refresh=0) 473 | ``` 474 | 475 | Higher-dimensional probability distributions are no trouble for Hamiltonian 476 | Monte Carlo. 477 | 478 | ```{r} 479 | diagnostics <- util$extract_hmc_diagnostics(fit) 480 | util$check_all_hmc_diagnostics(diagnostics) 481 | 482 | samples <- util$extract_expectand_vals(fit) 483 | base_samples <- util$filter_expectands(samples, 484 | c('beta0', 'beta1', 485 | 'beta2_d', 'beta2_o', 486 | 'sigma'), 487 | TRUE) 488 | util$check_all_expectand_diagnostics(base_samples) 489 | ``` 490 | 491 | With a multi-dimensional input space we can no longer visualize the baseline 492 | functional behavior nor the conditional prior predictive behavior directly. 493 | We can, however, visualize many of its features. 494 | 495 | For example we might consider the marginal behavior of the predicted outputs, 496 | regardless of the corresponding observed inputs. Here we'll summarize this 497 | marginal behavior with a histogram, and use the `plot_hist_quantiles` function 498 | to visualize the prior predictive distribution of the histogram counts. 499 | 500 | ```{r} 501 | par(mfrow=c(1, 1)) 502 | 503 | util$plot_hist_quantiles(samples, 'y_pred') 504 | ``` 505 | 506 | We can also set the binning by hand, either with bounds a fixed bin width 507 | or an arbitrary bin configuration. 508 | 509 | ```{r} 510 | par(mfrow=c(1, 1)) 511 | 512 | util$plot_hist_quantiles(samples, 'y_pred', -400, 400, 50) 513 | ``` 514 | 515 | ```{r} 516 | par(mfrow=c(1, 1)) 517 | 518 | util$plot_hist_quantiles(samples, 'y_pred', 519 | breaks=c(-400, -200, -100, -50, -25, 0, 520 | 25, 50, 100, 200, 400)) 521 | ``` 522 | 523 | To capture the interactions between the predictive outputs and the observed 524 | input components we'll need a more sophisticated summary statistic. Here we'll 525 | use the empirical mean and medians of the predictive outputs within bins of each 526 | input component. For a detailed discussion of how this summary statistic is 527 | constructed see Section 2.5 of my 528 | [Taylor regression modeling chapter](https://betanalpha.github.io/assets/case_studies/taylor_models.html#25_Posterior_Retrodictive_Checks). 529 | 530 | Conveniently the `plot_conditional_mean_quantiles` and 531 | `plot_conditional_median_quantiles` functions visualize the prior predictive 532 | behavior of these summary statistics. 533 | 534 | ```{r} 535 | par(mfrow=c(1, 3)) 536 | 537 | pred_names <- sapply(1:data$N, function(n) paste0('y_pred[', n, ']')) 538 | util$plot_conditional_mean_quantiles(samples, pred_names, data$X[,1], 539 | -9, 9, 1, xlab="x1") 540 | util$plot_conditional_mean_quantiles(samples, pred_names, data$X[,2], 541 | -9, 9, 1,xlab="x2") 542 | util$plot_conditional_mean_quantiles(samples, pred_names, data$X[,3], 543 | -9, 9, 1, xlab="x3") 544 | ``` 545 | 546 | ```{r} 547 | par(mfrow=c(1, 3)) 548 | 549 | pred_names <- sapply(1:data$N, function(n) paste0('y_pred[', n, ']')) 550 | util$plot_conditional_median_quantiles(samples, pred_names, data$X[,1], 551 | -9, 9, 1, xlab="x1") 552 | util$plot_conditional_median_quantiles(samples, pred_names, data$X[,2], 553 | -9, 9, 1, xlab="x2") 554 | util$plot_conditional_median_quantiles(samples, pred_names, data$X[,3], 555 | -9, 9, 1, xlab="x3") 556 | ``` 557 | 558 | ## Posterior Inference 559 | 560 | Now we're ready to incorporate the observed data. 561 | 562 | ```{.stan include="stan_programs/multi_full_model.stan" filename="multi\\_full\\_model.stan" eval=FALSE} 563 | ``` 564 | 565 | ```{r} 566 | #| warning: false 567 | #| message: false 568 | fit <- stan(file='stan_programs/multi_full_model.stan', 569 | data=data, seed=5838299, 570 | warmup=1000, iter=2024, refresh=0) 571 | ``` 572 | 573 | Fortunately our computational fortune has persisted. 574 | 575 | ```{r} 576 | diagnostics <- util$extract_hmc_diagnostics(fit) 577 | util$check_all_hmc_diagnostics(diagnostics) 578 | 579 | samples <- util$extract_expectand_vals(fit) 580 | base_samples <- util$filter_expectands(samples, 581 | c('beta0', 'beta1', 582 | 'beta2_d', 'beta2_o', 583 | 'sigma'), 584 | TRUE) 585 | util$check_all_expectand_diagnostics(base_samples) 586 | ``` 587 | 588 | The summary statistics that we used above to implement our prior predictive 589 | checks are equally useful for implementing informative posterior retrodictive 590 | checks. Conveniently the visualization functions all feature `baseline_values` 591 | functions that we can use to visualize the observed behavior along with the 592 | posterior predictive behavior. 593 | 594 | ```{r} 595 | par(mfrow=c(1, 1)) 596 | 597 | util$plot_hist_quantiles(samples, 'y_pred', baseline_values=data$y) 598 | ``` 599 | 600 | Additionally the `plot_conditional_mean_quantiles` and 601 | `plot_conditional_median_quantiles` functions feature a `residual` option that 602 | plots the posterior predictive behaviors relative to the baseline values. Any 603 | deviations from zero in these plots suggests retrodictive tension; here, 604 | however, there don't seem to be any problems. 605 | 606 | ```{r} 607 | par(mfrow=c(2, 3)) 608 | 609 | pred_names <- sapply(1:data$N, function(n) paste0('y_pred[', n, ']')) 610 | util$plot_conditional_mean_quantiles(samples, pred_names, data$X[,1], 611 | -9, 9, 1, data$y, xlab="x1") 612 | util$plot_conditional_mean_quantiles(samples, pred_names, data$X[,2], 613 | -9, 9, 1, data$y, xlab="x2") 614 | util$plot_conditional_mean_quantiles(samples, pred_names, data$X[,3], 615 | -9, 9, 1, data$y, xlab="x3") 616 | util$plot_conditional_mean_quantiles(samples, pred_names, data$X[,1], 617 | -9, 9, 1, data$y, residual=TRUE, 618 | xlab="x1") 619 | util$plot_conditional_mean_quantiles(samples, pred_names, data$X[,2], 620 | -9, 9, 1, data$y, residual=TRUE, 621 | xlab="x2") 622 | util$plot_conditional_mean_quantiles(samples, pred_names, data$X[,3], 623 | -9, 9, 1, data$y, residual=TRUE, 624 | xlab="x3") 625 | ``` 626 | 627 | ```{r} 628 | par(mfrow=c(2, 3)) 629 | 630 | pred_names <- sapply(1:data$N, function(n) paste0('y_pred[', n, ']')) 631 | util$plot_conditional_median_quantiles(samples, pred_names, data$X[,1], 632 | -9, 9, 1, data$y, xlab="x1") 633 | util$plot_conditional_median_quantiles(samples, pred_names, data$X[,2], 634 | -9, 9, 1, data$y, xlab="x2") 635 | util$plot_conditional_median_quantiles(samples, pred_names, data$X[,3], 636 | -9, 9, 1, data$y, xlab="x3") 637 | util$plot_conditional_median_quantiles(samples, pred_names, data$X[,1], 638 | -9, 9, 1, data$y, residual=TRUE, 639 | xlab="x1") 640 | util$plot_conditional_median_quantiles(samples, pred_names, data$X[,2], 641 | -9, 9, 1, data$y, residual=TRUE, 642 | xlab="x2") 643 | util$plot_conditional_median_quantiles(samples, pred_names, data$X[,3], 644 | -9, 9, 1, data$y, residual=TRUE, 645 | xlab="x3") 646 | ``` 647 | 648 | With no indications of model inadequacy we can move onto our posterior 649 | inferences. As before we can visualize the pushforward posterior distributions 650 | for each individual, one-dimensional parameter. 651 | 652 | ```{r} 653 | par(mfrow=c(4, 3)) 654 | 655 | util$plot_expectand_pushforward(samples[["beta0"]], 25, 656 | display_name="beta0") 657 | 658 | plot.new() 659 | 660 | util$plot_expectand_pushforward(samples[["sigma"]], 25, 661 | display_name="sigma") 662 | 663 | for (m in 1:data$M) { 664 | name <- paste0('beta1[', m, ']') 665 | util$plot_expectand_pushforward(samples[[name]], 25, 666 | display_name=name) 667 | } 668 | 669 | for (m in 1:data$M) { 670 | name <- paste0('beta2_d[', m, ']') 671 | util$plot_expectand_pushforward(samples[[name]], 25, 672 | display_name=name) 673 | } 674 | 675 | for (m in 1:data$M) { 676 | name <- paste0('beta2_o[', m, ']') 677 | util$plot_expectand_pushforward(samples[[name]], 25, 678 | display_name=name) 679 | } 680 | ``` 681 | 682 | The `plot_disc_pushforward_quantiles` function plots disconnected, marginal 683 | nested quantile intervals for a collection of one-dimensional variables. This 684 | allows for a more compact visualization of the marginal posterior distributions. 685 | 686 | ```{r} 687 | par(mfrow=c(1, 3)) 688 | 689 | names <- sapply(1:data$M, function(m) paste0('beta1[', m, ']')) 690 | util$plot_disc_pushforward_quantiles(samples, names, 691 | xlab="beta1", 692 | ylab="Marginal Posterior Quantiles") 693 | 694 | names <- sapply(1:data$M, function(m) paste0('beta2_d[', m, ']')) 695 | util$plot_disc_pushforward_quantiles(samples, names, 696 | xlab="beta2_d", 697 | ylab="Marginal Posterior Quantiles") 698 | 699 | names <- sapply(1:data$M, function(m) paste0('beta2_o[', m, ']')) 700 | util$plot_disc_pushforward_quantiles(samples, names, 701 | xlab="beta2_o", 702 | ylab="Marginal Posterior Quantiles") 703 | ``` 704 | 705 | ```{r} 706 | par(mfrow=c(1, 1)) 707 | 708 | names <- c(sapply(1:data$M, function(m) paste0('beta1[', m, ']')), 709 | sapply(1:data$M, function(m) paste0('beta2_d[', m, ']')), 710 | sapply(1:data$M, function(m) paste0('beta2_o[', m, ']'))) 711 | util$plot_disc_pushforward_quantiles(samples, names, 712 | xlab="All Slopes", 713 | ylab="Marginal Posterior Quantiles") 714 | ``` 715 | 716 | This function also includes an optional `baseline_values` argument and 717 | `residual` configuration which we can use to compare the probabilistic to the 718 | point values, for example our marginal posterior inferences to the true values 719 | when analyzing simulated data. 720 | 721 | ```{r} 722 | true_slopes <- c(-6.00, -1.50, 13.00, 0.50, 0.25, 723 | 1.00, -0.50, -2.00, -1.00) 724 | 725 | par(mfrow=c(2, 1)) 726 | 727 | util$plot_disc_pushforward_quantiles(samples, names, 728 | baseline_values=true_slopes, 729 | baseline_col=util$c_mid_teal, 730 | xlab="All Slopes", 731 | ylab="Marginal Posterior\nQuantiles") 732 | 733 | util$plot_disc_pushforward_quantiles(samples, names, 734 | baseline_values=true_slopes, 735 | residual=TRUE, 736 | xlab="All Slopes", 737 | ylab="Marginal Posterior\nQuantile Residuals") 738 | ``` 739 | 740 | # License {-} 741 | 742 | The code in this case study is copyrighted by Michael Betancourt and licensed 743 | under the new BSD (3-clause) license: 744 | 745 | https://opensource.org/licenses/BSD-3-Clause 746 | 747 | The text and figures in this case study are copyrighted by Michael Betancourt 748 | and licensed under the CC BY-NC 4.0 license: 749 | 750 | https://creativecommons.org/licenses/by-nc/4.0/ 751 | 752 | # Original Computing Environment {-} 753 | 754 | ```{r, comment=NA} 755 | writeLines(readLines(file.path(Sys.getenv("HOME"), ".R/Makevars"))) 756 | ``` 757 | 758 | ```{r, comment=NA} 759 | sessionInfo() 760 | ``` 761 | -------------------------------------------------------------------------------- /r/mcmc_visualization_tools.R: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # 3 | # The code is copyright 2024 Michael Betancourt and licensed under the 4 | # new BSD (3-clause) license: 5 | # https://opensource.org/licenses/BSD-3-Clause 6 | # 7 | # For more information see 8 | # https://github.com/betanalpha/mcmc_visualization_tools. 9 | # 10 | # Requires https://github.com/betanalpha/mcmc_diagnostics. 11 | # 12 | ################################################################################ 13 | 14 | # Load required libraries 15 | library(colormap) 16 | 17 | # Graphic configuration 18 | c_light <- c("#DCBCBC") 19 | c_light_highlight <- c("#C79999") 20 | c_mid <- c("#B97C7C") 21 | c_mid_highlight <- c("#A25050") 22 | c_dark <- c("#8F2727") 23 | c_dark_highlight <- c("#7C0000") 24 | 25 | c_light_teal <- c("#6B8E8E") 26 | c_mid_teal <- c("#487575") 27 | c_dark_teal <- c("#1D4F4F") 28 | 29 | util <- new.env() 30 | if (file.exists('mcmc_analysis_tools_rstan.R')) { 31 | source('mcmc_analysis_tools_rstan.R', local=util) 32 | } else if (file.exists('mcmc_analysis_tools_other.R')) { 33 | source('mcmc_analysis_tools_other.R', local=util) 34 | } else { 35 | stop(print0('mcmc_visualization_tools.R requires that ', 36 | 'mcmc_analysis_tools_[rstan/other].R from ', 37 | 'https://github.com/betanalpha/mcmc_diagnostics ', 38 | 'is available.')) 39 | } 40 | 41 | ################################################################################ 42 | # Utility Functions 43 | ################################################################################ 44 | 45 | # Emit stop if the two input arrays are not the same length. 46 | # @param a First list 47 | # @param a_name Name of first list 48 | # @param b Second list 49 | # @param b_name Name of second list 50 | check_dimensions <- function(a, a_name, b, b_name) { 51 | if (length(a) != length(b)) { 52 | stop(sprintf('The arguments `%s` and `%s` are not the same length!', 53 | a_name, b_name)) 54 | } 55 | } 56 | 57 | # Check if the given expectand names exist in the samples object, removing any 58 | # that do not. Emit warning describing any missing names and emit stop if no 59 | # expectand names are found. 60 | # @param names A one-dimensional array of strings 61 | # @param samples A named list 62 | # @return One-dimensional array of valid expectand names 63 | check_expectand_names <- function(names, samples) { 64 | all_names <- names(samples) 65 | 66 | bad_names <- setdiff(names, all_names) 67 | B <- length(bad_names) 68 | if (B > 0) { 69 | if (B == 1) 70 | warning(paste0(sprintf('The expectand name %s is not in the', 71 | bad_names[1]), 72 | ' `samples` object and will be ignored.')) 73 | else 74 | warning(paste0(sprintf('The expectand names %s are not in the', 75 | paste(bad_names, collapse=", ")), 76 | ' `samples` object and will be ignored.')) 77 | } 78 | 79 | good_names <- intersect(names, all_names) 80 | if (length(good_names) == 0) { 81 | stop('There are no valid expectand names.') 82 | } 83 | good_names 84 | } 85 | 86 | # Check how many values fall below bin_min and above bin_max and emit 87 | # appropriate warming message. 88 | # @param bin_min Lower threshold 89 | # @param bin_max Upper threshold 90 | # @param values A one-dimensional array of values to check 91 | # @param name Value description 92 | check_bin_containment <- function(bin_min, bin_max, values, name="value") { 93 | N <- length(values) 94 | 95 | N_low <- sum(values < bin_min) 96 | if (N_low > 0) 97 | if (N_low == 1) 98 | warning(sprintf('%i %s (%.1f%%) fell below the binning.', 99 | N_low, name, 100 * N_low / N)) 100 | else 101 | warning(sprintf('%i %ss (%.1f%%) fell below the binning.', 102 | N_low, name, 100 * N_low / N)) 103 | 104 | N_high <- sum(bin_max < values) 105 | if (N_high > 0) 106 | if (N_high == 1) 107 | warning(sprintf('%i %s (%.1f%%) fell above the binning.', 108 | N_high, name, 100 * N_high / N)) 109 | else 110 | warning(sprintf('%i %ss (%.1f%%) fell above the binning.', 111 | N_high, name, 100 * N_high / N)) 112 | } 113 | 114 | # Configure binning. Any null arguments are automatically configured to match 115 | # the behavior of the `values1` and `values2` arguments. Additionally if the 116 | # difference `bin_max - bin_min` does not divide `bin_delta` then `bin_min` and 117 | # `bin_max` are tweaked to respectively smaller and larger values as needed. 118 | # @param bin_min Lower threshold 119 | # @param bin_max Upper threshold 120 | # @param bin_delta Bin width 121 | # @param values1 An array of values 122 | # @param values1 An auxiliary array of values 123 | # @return One-dimensional array of updated bin_min, bin_max, and bin_delta 124 | # values 125 | configure_bins <- function(bin_min, bin_max, bin_delta, 126 | values1, values2=NULL) { 127 | if (is.null(values2)) { 128 | # Adapt bin configuration to `values1` 129 | if (is.null(bin_min)) 130 | bin_min <- min(values1) 131 | if (is.null(bin_max)) 132 | bin_max <- max(values1) 133 | } else { 134 | # Adapt bin configuration to `values1` and `values2` 135 | if (is.null(bin_min)) 136 | bin_min <- min(min(values1), min(values2)) 137 | if (is.null(bin_max)) 138 | bin_max <- max(max(values1), min(values2)) 139 | } 140 | 141 | if (is.null(bin_delta)) 142 | bin_delta <- (bin_max - bin_min) / 25 143 | 144 | # Tweak bin configuration so that `bin_delta` 145 | # evenly divides `bin_max - bin_min` 146 | N <- (bin_max - bin_min) / bin_delta 147 | excess <- N - floor(N) 148 | if (excess > 1e-15) { 149 | bin_min <- bin_min - 0.5 * bin_delta * excess 150 | bin_max <- bin_max + 0.5 * bin_delta * excess 151 | } 152 | 153 | c(bin_min, bin_max, bin_delta) 154 | } 155 | 156 | # Compute bin plotting. 157 | # @param breaks Bin edges 158 | # @return List of plotting indices and positions 159 | configure_bin_plotting <- function(breaks) { 160 | B <- length(breaks) - 1 161 | idxs <- rep(1:B, each=2) 162 | xs <- sapply(1:length(idxs), 163 | function(b) if(b %% 2 == 1) breaks[idxs[b]] 164 | else breaks[idxs[b] + 1]) 165 | list(idxs, xs) 166 | } 167 | 168 | ################################################################################ 169 | # Data Visualizations 170 | ################################################################################ 171 | 172 | # Plot histogram outline. 173 | # @param values Values that comprise the histogram 174 | # @param bin_min Lower threshold 175 | # @param bin_max Upper threshold 176 | # @param bin_delta Bin width 177 | # @param breaks Full binning; supercedes bin_min, bin_max, and bin_delta 178 | # when not NULL 179 | # @param prob Boolean determining whether bin contents should be normalized so 180 | # that the histogram approximates a probability density function; 181 | # defaults to FALSE 182 | # @param col Color of histogram; defaults to "black" 183 | # @param add Boolean determining whether to add histogram outline to existing 184 | # plot or to create new axes; defaults to FALSE 185 | # @param xlab Label for x-axis; defaults to empty string. 186 | # @param main Plot title; defaults to empty string. 187 | plot_line_hist <- function(values, 188 | bin_min=NULL, bin_max=NULL, bin_delta=NULL, 189 | breaks=NULL, prob=FALSE, 190 | col="black", add=FALSE, 191 | xlab="", main="") { 192 | # Remove any NA values 193 | values <- values[!is.na(values)] 194 | 195 | if(is.null(breaks)) { 196 | # Construct binning configuration 197 | bin_config <- configure_bins(bin_min, bin_max, bin_delta, values) 198 | bin_min <- bin_config[1] 199 | bin_max <- bin_config[2] 200 | bin_delta <- bin_config[3] 201 | 202 | # Construct bins 203 | breaks <- seq(bin_min, bin_max, bin_delta) 204 | } else { 205 | if (!is.null(bin_min)) { 206 | warning(paste('Argument `bin_min` is being superceded', 207 | 'by argument `breaks`')) 208 | } 209 | if (!is.null(bin_max)) { 210 | warning(paste('Argument `bin_max` is being superceded', 211 | 'by argument `breaks`')) 212 | } 213 | if (!is.null(bin_delta)) { 214 | warning(paste('Argument `bin_delta` is being superceded', 215 | 'by argument `breaks`')) 216 | } 217 | 218 | if (!(all(diff(breaks) > 0))) { 219 | stop('The argument `breaks` does not define a valid binning.') 220 | } 221 | 222 | bin_min <- min(breaks) 223 | bin_max <- max(breaks) 224 | bin_delta <- diff(breaks) 225 | } 226 | 227 | plot_config <- configure_bin_plotting(breaks) 228 | plot_idxs <- plot_config[[1]] 229 | plot_xs <- plot_config[[2]] 230 | 231 | # Check bin containment 232 | check_bin_containment(bin_min, bin_max, values) 233 | 234 | # Compute bin contents 235 | counts <- hist(values[bin_min <= values & values <= bin_max], 236 | breaks=breaks, plot=FALSE)$counts 237 | 238 | ylab <- "Counts" 239 | if (prob) { 240 | ylab <- "Empirical Bin Probability / Bin Width" 241 | counts <- counts / (bin_delta * sum(counts)) 242 | } 243 | 244 | if (add) { 245 | lines(plot_xs, counts[plot_idxs], col=col, lwd=2) 246 | } else { 247 | # Plot 248 | plot(plot_xs, counts[plot_idxs], main=main, 249 | type="l", col=col, lwd=2, 250 | xlab=xlab, xlim=c(bin_min, bin_max), 251 | ylab=ylab, ylim=c(0, 1.1 * max(counts))) 252 | } 253 | } 254 | 255 | # Plot the overlay of two line histograms. 256 | # @param values1 Values that comprise the first histogram 257 | # @param values2 Values that comprise the second histogram 258 | # @param bin_min Lower threshold 259 | # @param bin_max Upper threshold 260 | # @param bin_delta Bin width 261 | # @param breaks Full binning; supercedes bin_min, bin_max, and bin_delta 262 | # when not NULL 263 | # @param prob Boolean determining whether bin contents should be normalized so 264 | # that the histogram approximates a probability density function; 265 | # defaults to FALSE 266 | # @param xlab Label for x-axis; defaults to empty string. 267 | # @param main Plot title; defaults to empty string. 268 | # @param col1 Color of first histogram; defaults to "black" 269 | # @param col2 Color of second histogram; defaults to c_mid_teal 270 | plot_line_hists <- function(values1, values2, 271 | bin_min=NULL, bin_max=NULL, bin_delta=NULL, 272 | breaks=NULL, prob=FALSE, 273 | xlab="y", main="", 274 | col1="black", col2=c_mid_teal) { 275 | # Remove any NA values 276 | values1 <- values1[!is.na(values1)] 277 | values2 <- values2[!is.na(values2)] 278 | 279 | if(is.null(breaks)) { 280 | # Construct binning configuration 281 | bin_config <- configure_bins(bin_min, bin_max, bin_delta, 282 | values1, values2) 283 | bin_min <- bin_config[1] 284 | bin_max <- bin_config[2] 285 | bin_delta <- bin_config[3] 286 | 287 | # Construct bins 288 | breaks <- seq(bin_min, bin_max, bin_delta) 289 | } else { 290 | if (!is.null(bin_min)) { 291 | warning(paste('Argument `bin_min` is being superceded', 292 | 'by argument `breaks`')) 293 | } 294 | if (!is.null(bin_max)) { 295 | warning(paste('Argument `bin_max` is being superceded', 296 | 'by argument `breaks`')) 297 | } 298 | if (!is.null(bin_delta)) { 299 | warning(paste('Argument `bin_delta` is being superceded', 300 | 'by argument `breaks`')) 301 | } 302 | 303 | if (!(all(diff(breaks) > 0))) { 304 | stop('The argument `breaks` does not define a valid binning.') 305 | } 306 | 307 | bin_min <- min(breaks) 308 | bin_max <- max(breaks) 309 | bin_delta <- diff(breaks) 310 | } 311 | 312 | plot_config <- configure_bin_plotting(breaks) 313 | plot_idxs <- plot_config[[1]] 314 | plot_xs <- plot_config[[2]] 315 | 316 | # Check bin containment 317 | check_bin_containment(bin_min, bin_max, values1) 318 | check_bin_containment(bin_min, bin_max, values2) 319 | 320 | # Compute bin contents 321 | counts1 <- hist(values1[bin_min <= values1 & values1 <= bin_max], 322 | breaks=breaks, plot=FALSE)$counts 323 | counts2 <- hist(values2[bin_min <= values2 & values2 <= bin_max], 324 | breaks=breaks, plot=FALSE)$counts 325 | 326 | ylab <- "Counts" 327 | if (prob) { 328 | ylab <- "Empirical Bin Probability / Bin Width" 329 | counts1 <- counts1 / (bin_delta * sum(counts1)) 330 | counts2 <- counts2 / (bin_delta * sum(counts2)) 331 | } 332 | 333 | # Plot 334 | ymax <- 1.1 * max(max(counts1), max(counts2)) 335 | 336 | plot(plot_xs, counts1[plot_idxs], main=main, 337 | type="l", col=col1, lwd=2, 338 | xlab=xlab, xlim=c(bin_min, bin_max), 339 | ylab=ylab, ylim=c(0, ymax)) 340 | lines(plot_xs, counts2[plot_idxs], col="white", lwd=4) 341 | lines(plot_xs, counts2[plot_idxs], col=col2, lwd=2) 342 | } 343 | 344 | ################################################################################ 345 | # Pushforward Visualizations 346 | ################################################################################ 347 | 348 | # Overlay nested quantile intervals to visualize an ensemble of histograms. 349 | # Individual quantiles are estimated as the average of the empirical quantiles 350 | # across each Markov chain, a consistent quantile estimator for Markov chain 351 | # Monte Carlo. 352 | # @param samples A named list of two-dimensional arrays for 353 | # each expectand. The first dimension of each element 354 | # indexes the Markov chains and the second dimension 355 | # indexes the sequential states within each Markov chain. 356 | # @param val_name_prefix Prefix for the relevant variable names 357 | # @param bin_min Lower threshold 358 | # @param bin_max Upper threshold 359 | # @param bin_delta Bin width 360 | # @param breaks Full binning; supercedes bin_min, bin_max, and bin_delta 361 | # when not NULL 362 | # @param baseline_values Baseline values for constructing a baseline histogram; 363 | # defaults to NULL 364 | # @param baseline_col Color for plotting baseline value; defaults to "black" 365 | # @param xlab Label for x-axis; defaults to empty string 366 | # @param display_ylim Plot limits for y-axis; defaults to NULL 367 | # @param main Plot title; defaults to empty string 368 | plot_hist_quantiles <- function(samples, val_name_prefix, 369 | bin_min=NULL, bin_max=NULL, bin_delta=NULL, 370 | breaks=NULL, 371 | baseline_values=NULL, 372 | baseline_col="black", 373 | xlab="", display_ylim=NULL, main="") { 374 | # Construct relevant variable names and format corresponding values. 375 | # Order of the variables does not affect the shape of the histogram. 376 | names <- grep(paste0('^', val_name_prefix, '\\['), 377 | names(samples), value=TRUE) 378 | collapsed_values <- c(sapply(names, function(name) c(t(samples[[name]]), 379 | recursive=TRUE))) 380 | 381 | 382 | if(is.null(breaks)) { 383 | # Construct binning configuration 384 | if (is.null(baseline_values)) 385 | bin_config <- configure_bins(bin_min, bin_max, bin_delta, 386 | collapsed_values) 387 | else 388 | bin_config <- configure_bins(bin_min, bin_max, bin_delta, 389 | collapsed_values, baseline_values) 390 | bin_min <- bin_config[1] 391 | bin_max <- bin_config[2] 392 | bin_delta <- bin_config[3] 393 | 394 | # Construct bins 395 | breaks <- seq(bin_min, bin_max, bin_delta) 396 | } else { 397 | if (!is.null(bin_min)) { 398 | warning(paste('Argument `bin_min` is being superceded', 399 | 'by argument `breaks`')) 400 | } 401 | if (!is.null(bin_max)) { 402 | warning(paste('Argument `bin_max` is being superceded', 403 | 'by argument `breaks`')) 404 | } 405 | if (!is.null(bin_delta)) { 406 | warning(paste('Argument `bin_delta` is being superceded', 407 | 'by argument `breaks`')) 408 | } 409 | 410 | if (!(all(diff(breaks) > 0))) { 411 | stop('The argument `breaks` does not define a valid binning.') 412 | } 413 | 414 | bin_min <- min(breaks) 415 | bin_max <- max(breaks) 416 | bin_delta <- diff(breaks) 417 | } 418 | 419 | plot_config <- configure_bin_plotting(breaks) 420 | plot_idxs <- plot_config[[1]] 421 | plot_xs <- plot_config[[2]] 422 | 423 | # Check bin containment 424 | check_bin_containment(bin_min, bin_max, collapsed_values, 425 | "predictive value") 426 | if (!is.null(baseline_values)) 427 | check_bin_containment(bin_min, bin_max, baseline_values, 428 | "observed value") 429 | 430 | # Construct quantiles for bin contents 431 | B <- length(breaks) - 1 432 | probs <- c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9) 433 | 434 | counts <- rep(NA, B) 435 | 436 | bin_count <- function(x, b_low, b_high) { 437 | sum(b_low <= x & x < b_high) 438 | } 439 | 440 | bin_counters <- list() 441 | for (b in 1:B) { 442 | bin_counters[[b]] <- 443 | local({ 444 | b_low <- breaks[b]; 445 | b_high <- breaks[b + 1]; 446 | function(x) bin_count(x, b_low, b_high) 447 | }) 448 | 449 | if (!is.null(baseline_values)) 450 | counts[b] = bin_counters[[b]](baseline_values) 451 | } 452 | 453 | bin_count_samples <- 454 | util$eval_expectand_pushforwards(samples, 455 | bin_counters, 456 | list('x'=array(names))) 457 | 458 | probs <- c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9) 459 | quantiles <- sapply(bin_count_samples, 460 | function(s) 461 | util$ensemble_mcmc_quantile_est(s, probs)) 462 | 463 | plot_quantiles <- do.call(cbind, lapply(plot_idxs, 464 | function(n) quantiles[1:9, n])) 465 | 466 | # Plot 467 | if (is.null(display_ylim)) { 468 | if (is.null(baseline_values)) { 469 | display_ylim <- c(0, max(quantiles[9,])) 470 | } 471 | else { 472 | display_ylim <- c(0, max(max(quantiles[9,]), max(counts))) 473 | } 474 | } 475 | 476 | plot(1, type="n", main=main, 477 | xlim=c(bin_min, bin_max), xlab=xlab, 478 | ylim=display_ylim, ylab="Counts") 479 | 480 | polygon(c(plot_xs, rev(plot_xs)), 481 | c(plot_quantiles[1,], rev(plot_quantiles[9,])), 482 | col = c_light, border = NA) 483 | polygon(c(plot_xs, rev(plot_xs)), 484 | c(plot_quantiles[2,], rev(plot_quantiles[8,])), 485 | col = c_light_highlight, border = NA) 486 | polygon(c(plot_xs, rev(plot_xs)), 487 | c(plot_quantiles[3,], rev(plot_quantiles[7,])), 488 | col = c_mid, border = NA) 489 | polygon(c(plot_xs, rev(plot_xs)), 490 | c(plot_quantiles[4,], rev(plot_quantiles[6,])), 491 | col = c_mid_highlight, border = NA) 492 | for (b in 1:B) { 493 | idx1 <- 2 * b - 1 494 | idx2 <- 2 * b 495 | lines(plot_xs[idx1:idx2], plot_quantiles[5,idx1:idx2], 496 | col=c_dark, lwd=2) 497 | } 498 | 499 | if (!is.null(baseline_values)) { 500 | plot_counts <- do.call(cbind, 501 | lapply(plot_idxs, function(n) counts[n])) 502 | 503 | lines(plot_xs, plot_counts, col="white", lty=1, lw=4) 504 | lines(plot_xs, plot_counts, col=baseline_col, lty=1, lw=2) 505 | } 506 | } 507 | 508 | # Overlay disconnected nested quantile intervals to visualize an ensemble of 509 | # one-dimensional pushforward distributions. 510 | # Individual quantiles are estimated as the average of the empirical quantiles 511 | # across each Markov chain, a consistent quantile estimator for Markov chain 512 | # Monte Carlo. 513 | # @param samples A named list of two-dimensional arrays for 514 | # each expectand. The first dimension of each element 515 | # indexes the Markov chains and the second dimension 516 | # indexes the sequential states within each Markov chain. 517 | # @param names List of relevant variable names 518 | # @param baseline_values Baseline values; defaults to NULL 519 | # @param baseline_col Color for plotting baseline value; defaults to "black" 520 | # @params residual Boolean value indicating whether to overlay quantiles and 521 | # baseline values or plot their differences 522 | # @param xlab Label for x-axis; defaults to empty string 523 | # @param xticklabs Labels for x-axis tics; defaults to NULL 524 | # @param ylab Label for y-axis; defaults to NULL 525 | # @param display_ylim Plot limits for y-axis; defaults to NULL 526 | # @param main Plot title; defaults to empty string 527 | plot_disc_pushforward_quantiles <- function(samples, names, 528 | baseline_values=NULL, 529 | baseline_col="black", 530 | residual=FALSE, 531 | xlab="", xticklabs=NULL, 532 | ylab=NULL, display_ylim=NULL, 533 | main="") { 534 | # Check that baseline values are well-defined 535 | if (!is.null(baseline_values)) { 536 | if (length(baseline_values) != length(names)) { 537 | warning(paste0('The list of baseline values has the wrong', 538 | ' dimension. Baselines will not be plotted.')) 539 | baseline_values <- NULL 540 | } 541 | } 542 | 543 | # Check that names are in samples 544 | names <- check_expectand_names(names, samples) 545 | 546 | # Construct bins 547 | N <- length(names) 548 | bin_min <- 0.5 549 | bin_max <- N + 0.5 550 | bin_delta <- 1 551 | breaks <- seq(bin_min, bin_max, bin_delta) 552 | 553 | plot_config <- configure_bin_plotting(breaks) 554 | plot_idxs <- plot_config[[1]] 555 | plot_xs <- plot_config[[2]] 556 | 557 | # Construct marginal quantiles 558 | probs <- c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9) 559 | 560 | if(!is.null(baseline_values) & residual) { 561 | calc <- function(n) { 562 | util$ensemble_mcmc_quantile_est(samples[[names[n]]] - 563 | baseline_values[n], 564 | probs) 565 | } 566 | quantiles <- sapply(1:N, calc) 567 | } else { 568 | calc <- function(n) { 569 | util$ensemble_mcmc_quantile_est(samples[[names[n]]], probs) 570 | } 571 | quantiles <- sapply(1:N, calc) 572 | } 573 | 574 | plot_quantiles <- do.call(cbind, lapply(plot_idxs, 575 | function(n) quantiles[1:9, n])) 576 | 577 | # Plot 578 | if (is.null(display_ylim)) { 579 | if (is.null(baseline_values)) { 580 | display_ylim <- c(min(quantiles[1,]), 581 | max(quantiles[9,])) 582 | } 583 | else { 584 | if (residual) { 585 | display_ylim <- c(min(c(0, quantiles[1,])), 586 | max(c(0, quantiles[9,]))) 587 | } else { 588 | display_ylim <- c(min(min(quantiles[1,]), 589 | min(baseline_values)), 590 | max(max(quantiles[9,]), 591 | max(baseline_values))) 592 | } 593 | } 594 | delta <- 0.05 * (display_ylim[2] - display_ylim[1]) 595 | display_ylim[1] <- display_ylim[1] - delta 596 | display_ylim[2] <- display_ylim[2] + delta 597 | } 598 | 599 | if (is.null(ylab)) { 600 | if (is.null(baseline_values) | !residual) 601 | ylab <- "Marginal Quantiles" 602 | else 603 | ylab <- "Marginal Quantiles - Baselines" 604 | } 605 | 606 | if (is.null(xticklabs)) { 607 | plot(1, type="n", main=main, 608 | xlim=c(bin_min, bin_max), xlab=xlab, 609 | ylim=display_ylim, ylab=ylab) 610 | } else { 611 | if (length(xticklabs) == N) { 612 | plot(1, type="n", main=main, 613 | xlim=c(bin_min, bin_max), xlab=xlab, xaxt="n", 614 | ylim=display_ylim, ylab=ylab) 615 | axis(1, at=1:N, labels=xticklabs) 616 | } else { 617 | warning(paste0('The list of x labels tick has the wrong', 618 | ' dimension and baselines will not be plotted.')) 619 | plot(1, type="n", main=main, 620 | xlim=c(bin_min, bin_max), xlab=xlab, 621 | ylim=display_ylim, ylab=ylab) 622 | } 623 | } 624 | 625 | polygon(c(plot_xs, rev(plot_xs)), 626 | c(plot_quantiles[1,], rev(plot_quantiles[9,])), 627 | col = c_light, border = NA) 628 | polygon(c(plot_xs, rev(plot_xs)), 629 | c(plot_quantiles[2,], rev(plot_quantiles[8,])), 630 | col = c_light_highlight, border = NA) 631 | polygon(c(plot_xs, rev(plot_xs)), 632 | c(plot_quantiles[3,], rev(plot_quantiles[7,])), 633 | col = c_mid, border = NA) 634 | polygon(c(plot_xs, rev(plot_xs)), 635 | c(plot_quantiles[4,], rev(plot_quantiles[6,])), 636 | col = c_mid_highlight, border = NA) 637 | for (n in 1:N) { 638 | idx1 <- 2 * n - 1 639 | idx2 <- 2 * n 640 | lines(plot_xs[idx1:idx2], plot_quantiles[5, idx1:idx2], 641 | col=c_dark, lwd=2) 642 | } 643 | 644 | if (!is.null(baseline_values)) { 645 | if (residual) { 646 | abline(h=0, col="#DDDDDD", lwd=2, lty=3) 647 | } else { 648 | for (n in 1:N) { 649 | idx1 <- 2 * n - 1 650 | idx2 <- 2 * n 651 | lines(plot_xs[idx1:idx2], rep(baseline_values[n], 2), 652 | col="white", lwd=4) 653 | lines(plot_xs[idx1:idx2], rep(baseline_values[n], 2), 654 | col=baseline_col, lwd=2) 655 | } 656 | } 657 | } 658 | } 659 | 660 | # Overlay connected nested quantile intervals to visualize an ensemble of 661 | # one-dimensional pushforward distributions. 662 | # Individual quantiles are estimated as the average of the empirical quantiles 663 | # across each Markov chain, a consistent quantile estimator for Markov chain 664 | # Monte Carlo. 665 | # @param samples A named list of two-dimensional arrays for 666 | # each expectand. The first dimension of each element 667 | # indexes the Markov chains and the second dimension 668 | # indexes the sequential states within each Markov chain. 669 | # @param names List of relevant variable names 670 | # @param plot_xs One-dimensional array of x-axis values 671 | # associated with each variable. 672 | # @param baseline_values Baseline values; defaults to NULL 673 | # @param baseline_col Color for plotting baseline value; defaults to "black" 674 | # @params residual Boolean value indicating whether to overlay quantiles and 675 | # baseline values or plot their differences 676 | # @param xlab Label for x-axis; defaults to empty string 677 | # @param display_xlim Plot limits for x-axis; defaults to NULL 678 | # @param ylab Label for y-axis; defaults to NULL 679 | # @param display_ylim Plot limits for y-axis; defaults to NULL 680 | # @param main Plot title; defaults to empty string 681 | plot_conn_pushforward_quantiles <- function(samples, names, plot_xs, 682 | baseline_values=NULL, 683 | baseline_col="black", 684 | residual=FALSE, 685 | xlab="", display_xlim=NULL, 686 | ylab=NULL, display_ylim=NULL, 687 | main="") { 688 | # Check dimensions 689 | check_dimensions(plot_xs, 'plot_xs', names, 'names') 690 | 691 | # Check that baseline values are well-defined 692 | if (!is.null(baseline_values)) { 693 | if (length(baseline_values) != length(names)) { 694 | warning(paste0('The list of baseline values has the wrong', 695 | ' dimension. Baselines will not be plotted.')) 696 | baseline_values <- NULL 697 | } 698 | } 699 | 700 | # Check that names are in samples 701 | names <- check_expectand_names(names, samples) 702 | 703 | # Construct quantiles for bin contents 704 | N <- length(names) 705 | probs <- c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9) 706 | 707 | if(!is.null(baseline_values) & residual) { 708 | calc <- function(n) { 709 | util$ensemble_mcmc_quantile_est(samples[[names[n]]] - 710 | baseline_values[n], 711 | probs) 712 | } 713 | plot_quantiles <- sapply(1:N, calc) 714 | } else { 715 | calc <- function(n) { 716 | util$ensemble_mcmc_quantile_est(samples[[names[n]]], probs) 717 | } 718 | plot_quantiles <- sapply(1:N, calc) 719 | } 720 | 721 | # Plot 722 | if (is.null(display_xlim)) 723 | display_xlim <- range(plot_xs) 724 | 725 | if (is.null(display_ylim)) { 726 | if (is.null(baseline_values)) { 727 | display_ylim <- c(min(plot_quantiles[1,]), 728 | max(plot_quantiles[9,])) 729 | } 730 | else { 731 | if (residual) { 732 | display_ylim <- c(min(c(0, plot_quantiles[1,])), 733 | max(c(0, plot_quantiles[9,]))) 734 | } else { 735 | display_ylim <- c(min(min(plot_quantiles[1,]), 736 | min(baseline_values)), 737 | max(max(plot_quantiles[9,]), 738 | max(baseline_values))) 739 | } 740 | } 741 | delta <- 0.05 * (display_ylim[2] - display_ylim[1]) 742 | display_ylim[1] <- display_ylim[1] - delta 743 | display_ylim[2] <- display_ylim[2] + delta 744 | } 745 | 746 | if (is.null(ylab)) { 747 | if (is.null(baseline_values) | !residual) 748 | ylab <- "Marginal Quantiles" 749 | else 750 | ylab <- "Marginal Quantiles - Baselines" 751 | } 752 | 753 | plot(1, type="n", main=main, 754 | xlim=display_xlim, xlab=xlab, 755 | ylim=display_ylim, ylab=ylab) 756 | 757 | polygon(c(plot_xs, rev(plot_xs)), 758 | c(plot_quantiles[1,], rev(plot_quantiles[9,])), 759 | col = c_light, border = NA) 760 | polygon(c(plot_xs, rev(plot_xs)), 761 | c(plot_quantiles[2,], rev(plot_quantiles[8,])), 762 | col = c_light_highlight, border = NA) 763 | polygon(c(plot_xs, rev(plot_xs)), 764 | c(plot_quantiles[3,], rev(plot_quantiles[7,])), 765 | col = c_mid, border = NA) 766 | polygon(c(plot_xs, rev(plot_xs)), 767 | c(plot_quantiles[4,], rev(plot_quantiles[6,])), 768 | col = c_mid_highlight, border = NA) 769 | lines(plot_xs, plot_quantiles[5,], col=c_dark, lwd=2) 770 | 771 | if (!is.null(baseline_values)) { 772 | if (residual) { 773 | abline(h=0, col="#DDDDDD", lwd=2, lty=3) 774 | } else { 775 | lines(plot_xs, baseline_values, col="white", lwd=4) 776 | lines(plot_xs, baseline_values, col=baseline_col, lwd=2) 777 | } 778 | } 779 | } 780 | 781 | # Overlay an ensemble of function realizations to visualize a probability 782 | # distribution over a space of one-dimensional functions. 783 | # @param samples A named list of two-dimensional arrays for 784 | # each expectand. The first dimension of each element 785 | # indexes the Markov chains and the second dimension 786 | # indexes the sequential states within each Markov chain. 787 | # @param names List of relevant variable names 788 | # @param plot_xs One-dimensional array of x-axis values 789 | # associated with each variable. 790 | # @param N_plots Number of realizations to plot 791 | # @param baseline_values Baseline values; defaults to NULL 792 | # @param baseline_col Color for plotting baseline value; defaults to "black" 793 | # @params residual Boolean value indicating whether to overlay quantiles and 794 | # baseline values or plot their differences 795 | # @param xlab Label for x-axis; defaults to empty string 796 | # @param display_xlim Plot limits for x-axis; defaults to NULL 797 | # @param ylab Label for y-axis; defaults to NULL 798 | # @param display_ylim Plot limits for y-axis; defaults to NULL 799 | # @param main Plot title; defaults to empty string 800 | plot_realizations <- function(samples, names, plot_xs, N_plots=50, 801 | baseline_values=NULL, 802 | baseline_col="black", 803 | residual=FALSE, 804 | xlab="", display_xlim=NULL, 805 | ylab=NULL, display_ylim=NULL, 806 | main="") { 807 | # Check dimensions 808 | check_dimensions(plot_xs, 'plot_xs', names, 'names') 809 | 810 | # Check that baseline values are well-defined 811 | if (!is.null(baseline_values)) { 812 | if (length(baseline_values) != length(names)) { 813 | warning(paste0('The list of baseline values has the wrong', 814 | ' dimension. Baselines will not be plotted.')) 815 | baseline_values <- NULL 816 | } 817 | } 818 | 819 | # Check that names are in samples 820 | names <- check_expectand_names(names, samples) 821 | 822 | # Extract function values 823 | fs <- t(sapply(names, function(name) 824 | c(samples[[name]], recursive=TRUE))) 825 | 826 | N <- dim(fs)[1] 827 | I <- dim(fs)[2] 828 | 829 | if (!is.null(baseline_values) & residual) 830 | for (i in 1:I) 831 | fs[,i] <- fs[,i] - baseline_values 832 | 833 | # Configure ensemble of function realizations 834 | J <- min(N_plots, I) 835 | plot_idx <- sapply(1:J, function(j) (I %/% J) * (j - 1) + 1) 836 | 837 | nom_colors <- c("#DCBCBC", "#C79999", "#B97C7C", 838 | "#A25050", "#8F2727", "#7C0000") 839 | line_colors <- colormap(colormap=nom_colors, nshades=J) 840 | 841 | # Plot 842 | if (is.null(display_xlim)) 843 | display_xlim <- range(plot_xs) 844 | 845 | if (is.null(display_ylim)) { 846 | if (is.null(baseline_values)) { 847 | display_ylim <- range(fs) 848 | } 849 | else { 850 | if (residual) { 851 | display_ylim <- range(c(0, fs)) 852 | } else { 853 | display_ylim <- c(min(min(fs), min(baseline_values)), 854 | max(max(fs), max(baseline_values))) 855 | } 856 | } 857 | delta <- 0.05 * (display_ylim[2] - display_ylim[1]) 858 | display_ylim[1] <- display_ylim[1] - delta 859 | display_ylim[2] <- display_ylim[2] + delta 860 | } 861 | 862 | if (is.null(ylab)) { 863 | if (is.null(baseline_values) | !residual) 864 | ylab <- "Function Outputs" 865 | else 866 | ylab <- "Function Outputs - Baselines" 867 | } 868 | 869 | plot(1, type="n", main=main, 870 | xlim=display_xlim, xlab=xlab, 871 | ylim=display_ylim, ylab=ylab) 872 | 873 | for (j in 1:J) { 874 | f <- fs[, plot_idx[j]] 875 | lines(plot_xs, f, col=line_colors[j], lwd=3) 876 | } 877 | 878 | if (!is.null(baseline_values)) { 879 | if (residual) { 880 | abline(h=0, col="#DDDDDD", lwd=2, lty=3) 881 | } else { 882 | lines(plot_xs, baseline_values, col="white", lwd=4) 883 | lines(plot_xs, baseline_values, col=baseline_col, lwd=2) 884 | } 885 | } 886 | } 887 | 888 | # Overlay nested quantile intervals to visualize an ensemble of empirical means 889 | # within the computed bins. 890 | # Individual quantiles are estimated as the average of the empirical quantiles 891 | # across each Markov chain, a consistent quantile estimator for Markov chain 892 | # Monte Carlo. 893 | # @param samples A named list of two-dimensional arrays for 894 | # each expectand. The first dimension of each element 895 | # indexes the Markov chains and the second dimension 896 | # indexes the sequential states within each Markov chain. 897 | # @param names List of relevant variable names 898 | # @param obs_xs One-dimensional array of observed x-values on which to condition 899 | # @param bin_min Lower threshold for conditioning 900 | # @param bin_max Upper threshold for conditioning 901 | # @param bin_delta Bin width for conditioning 902 | # @param baseline_values Baseline values; defaults to NULL 903 | # @param baseline_col Color for plotting baseline value; defaults to "black" 904 | # @params residual Boolean value indicating whether to overlay quantiles and 905 | # baseline values or plot their differences 906 | # @param xlab Label for x-axis; defaults to empty string 907 | # @param ylab Label for y-axis; defaults to NULL 908 | # @param display_ylim Plot limits for y-axis; defaults to NULL 909 | # @param main Plot title; defaults to empty string 910 | plot_conditional_mean_quantiles <- function(samples, names, obs_xs, 911 | bin_min=NULL, bin_max=NULL, 912 | bin_delta=NULL, 913 | baseline_values=NULL, 914 | baseline_col="black", 915 | residual=FALSE, 916 | xlab="", 917 | ylab=NULL, display_ylim=NULL, 918 | main="") { 919 | # Check dimensions 920 | check_dimensions(obs_xs, 'obs_xs', names, 'names') 921 | 922 | # Check that baseline values are well-defined 923 | if (!is.null(baseline_values)) { 924 | if (length(baseline_values) != length(names)) { 925 | warning(paste0('The list of baseline values has the wrong', 926 | ' dimension. Baselines will not be plotted.')) 927 | baseline_values <- NULL 928 | } 929 | } 930 | 931 | # Check that names are in samples 932 | names <- check_expectand_names(names, samples) 933 | 934 | # Construct binning configuration 935 | bin_config <- configure_bins(bin_min, bin_max, bin_delta, obs_xs) 936 | bin_min <- bin_config[1] 937 | bin_max <- bin_config[2] 938 | bin_delta <- bin_config[3] 939 | 940 | # Construct bins 941 | breaks <- seq(bin_min, bin_max, bin_delta) 942 | plot_config <- configure_bin_plotting(breaks) 943 | plot_idxs <- plot_config[[1]] 944 | plot_xs <- plot_config[[2]] 945 | 946 | # Check bin containment 947 | check_bin_containment(bin_min, bin_max, obs_xs, 948 | "conditioning value") 949 | 950 | # Construct quantiles for each conditional mean 951 | B <- length(breaks) - 1 952 | nonempty_bins <- sapply(1:B, 953 | function(b) 954 | sum(breaks[b] <= obs_xs & obs_xs < breaks[b + 1]) > 0) 955 | 956 | baseline_cond_means <- rep(NA, B) 957 | 958 | cond_mean <- function(y, x, b_low, b_high) { 959 | bin_idx <- which(b_low <= x & x < b_high) 960 | if (length(bin_idx)) 961 | return(mean(y[bin_idx])) 962 | else 963 | return(mean(y)) 964 | } 965 | 966 | if (!is.null(baseline_values)) { 967 | for (b in 1:B) { 968 | baseline_cond_means[b] <- cond_mean(baseline_values, obs_xs, 969 | breaks[b], breaks[b + 1]) 970 | } 971 | } 972 | 973 | if (is.null(baseline_values) | !residual) { 974 | expectands <- list() 975 | for (b in 1:B) { 976 | expectands[[b]] <- 977 | local({ 978 | x <- obs_xs; 979 | b_low <- breaks[b]; 980 | b_high <- breaks[b + 1]; 981 | function(y) cond_mean(y, x, b_low, b_high) 982 | }) 983 | } 984 | } else { 985 | expectands <- list() 986 | for (b in 1:B) { 987 | expectands[[b]] <- 988 | local({ 989 | x <- obs_xs; 990 | b_low <- breaks[b]; 991 | b_high <- breaks[b + 1]; 992 | baseline <- baseline_cond_means[b]; 993 | function(y) cond_mean(y, x, b_low, b_high) - baseline 994 | }) 995 | } 996 | } 997 | 998 | cond_mean_samples <- 999 | util$eval_expectand_pushforwards(samples, 1000 | expectands, 1001 | list('y'=array(names))) 1002 | 1003 | probs <- c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9) 1004 | mean_quantiles <- sapply(cond_mean_samples, 1005 | function(s) 1006 | util$ensemble_mcmc_quantile_est(s, probs)) 1007 | 1008 | plot_quantiles <- do.call(cbind, lapply(plot_idxs, 1009 | function(n) mean_quantiles[1:9, n])) 1010 | 1011 | # Plot 1012 | if (is.null(display_ylim)) { 1013 | if (is.null(baseline_values)) { 1014 | display_ylim <- c(min(mean_quantiles[1, nonempty_bins]), 1015 | max(mean_quantiles[9, nonempty_bins])) 1016 | } 1017 | else { 1018 | if (residual) { 1019 | display_ylim <- c(min(c(0, mean_quantiles[1, nonempty_bins])), 1020 | max(c(0, mean_quantiles[9, nonempty_bins]))) 1021 | } else { 1022 | display_ylim <- c(min(min(mean_quantiles[1, nonempty_bins]), 1023 | min(baseline_cond_means[nonempty_bins])), 1024 | max(max(mean_quantiles[9, nonempty_bins]), 1025 | max(baseline_cond_means[nonempty_bins]))) 1026 | } 1027 | } 1028 | delta <- 0.05 * (display_ylim[2] - display_ylim[1]) 1029 | display_ylim[1] <- display_ylim[1] - delta 1030 | display_ylim[2] <- display_ylim[2] + delta 1031 | } 1032 | 1033 | if (is.null(ylab)) { 1034 | if (is.null(baseline_values) | !residual) 1035 | ylab <- "Marginal Quantiles of Conditional Means" 1036 | else 1037 | ylab <- "Marginal Quantiles of Conditional Means - Baselines" 1038 | } 1039 | 1040 | plot(1, type="n", main=main, 1041 | xlim=c(bin_min, bin_max), xlab=xlab, 1042 | ylim=display_ylim, ylab=ylab) 1043 | 1044 | polygon(c(plot_xs, rev(plot_xs)), 1045 | c(plot_quantiles[1,], rev(plot_quantiles[9,])), 1046 | col = c_light, border = NA) 1047 | polygon(c(plot_xs, rev(plot_xs)), 1048 | c(plot_quantiles[2,], rev(plot_quantiles[8,])), 1049 | col = c_light_highlight, border = NA) 1050 | polygon(c(plot_xs, rev(plot_xs)), 1051 | c(plot_quantiles[3,], rev(plot_quantiles[7,])), 1052 | col = c_mid, border = NA) 1053 | polygon(c(plot_xs, rev(plot_xs)), 1054 | c(plot_quantiles[4,], rev(plot_quantiles[6,])), 1055 | col = c_mid_highlight, border = NA) 1056 | for (b in 1:B) { 1057 | idx1 <- 2 * b - 1 1058 | idx2 <- 2 * b 1059 | if (nonempty_bins[b]) { 1060 | lines(plot_xs[idx1:idx2], plot_quantiles[5, idx1:idx2], 1061 | col=c_dark, lwd=2) 1062 | } else { 1063 | polygon(c(plot_xs[idx1:idx2], rev(plot_xs[idx1:idx2])), 1064 | c(display_ylim[1], display_ylim[1], 1065 | display_ylim[2], display_ylim[2]), 1066 | col="#EEEEEE", border = NA) 1067 | } 1068 | } 1069 | 1070 | if (!is.null(baseline_values)) { 1071 | if (residual) { 1072 | abline(h=0, col="#DDDDDD", lwd=2, lty=3) 1073 | } else { 1074 | for (b in 1:B) { 1075 | if (nonempty_bins[b]) { 1076 | idx1 <- 2 * b - 1 1077 | idx2 <- 2 * b 1078 | lines(plot_xs[idx1:idx2], rep(baseline_cond_means[b], 2), 1079 | col="white", lwd=4) 1080 | lines(plot_xs[idx1:idx2], rep(baseline_cond_means[b], 2), 1081 | col=baseline_col, lwd=2) 1082 | } 1083 | } 1084 | } 1085 | } 1086 | } 1087 | 1088 | 1089 | # Overlay nested quantile intervals to visualize an ensemble of empirical 1090 | # medians within the computed bins. 1091 | # Individual quantiles are estimated as the average of the empirical quantiles 1092 | # across each Markov chain, a consistent quantile estimator for Markov chain 1093 | # Monte Carlo. 1094 | # @param samples A named list of two-dimensional arrays for 1095 | # each expectand. The first dimension of each element 1096 | # indexes the Markov chains and the second dimension 1097 | # indexes the sequential states within each Markov chain. 1098 | # @param names List of relevant variable names 1099 | # @param obs_xs One-dimensional array of observed x-values on which to condition 1100 | # @param bin_min Lower threshold for conditioning 1101 | # @param bin_max Upper threshold for conditioning 1102 | # @param bin_delta Bin width for conditioning 1103 | # @param baseline_values Baseline values; defaults to NULL 1104 | # @param baseline_col Color for plotting baseline value; defaults to "black" 1105 | # @params residual Boolean value indicating whether to overlay quantiles and 1106 | # baseline values or plot their differences 1107 | # @param xlab Label for x-axis; defaults to empty string 1108 | # @param ylab Label for y-axis; defaults to NULL 1109 | # @param display_ylim Plot limits for y-axis; defaults to NULL 1110 | # @param main Plot title; defaults to empty string 1111 | plot_conditional_median_quantiles <- function(samples, names, obs_xs, 1112 | bin_min=NULL, bin_max=NULL, 1113 | bin_delta=NULL, 1114 | baseline_values=NULL, 1115 | baseline_col="black", 1116 | residual=FALSE, 1117 | xlab="", 1118 | ylab=NULL, display_ylim=NULL, 1119 | main="") { 1120 | # Check dimensions 1121 | check_dimensions(obs_xs, 'obs_xs', names, 'names') 1122 | 1123 | # Check that baseline values are well-defined 1124 | if (!is.null(baseline_values)) { 1125 | if (length(baseline_values) != length(names)) { 1126 | warning(paste0('The list of baseline values has the wrong', 1127 | ' dimension. Baselines will not be plotted.')) 1128 | baseline_values <- NULL 1129 | } 1130 | } 1131 | 1132 | # Check that names are in samples 1133 | names <- check_expectand_names(names, samples) 1134 | 1135 | # Construct binning configuration 1136 | bin_config <- configure_bins(bin_min, bin_max, bin_delta, obs_xs) 1137 | bin_min <- bin_config[1] 1138 | bin_max <- bin_config[2] 1139 | bin_delta <- bin_config[3] 1140 | 1141 | # Construct bins 1142 | breaks <- seq(bin_min, bin_max, bin_delta) 1143 | plot_config <- configure_bin_plotting(breaks) 1144 | plot_idxs <- plot_config[[1]] 1145 | plot_xs <- plot_config[[2]] 1146 | 1147 | # Check bin containment 1148 | check_bin_containment(bin_min, bin_max, obs_xs, 1149 | "conditioning value") 1150 | 1151 | # Construct quantiles for each conditional mean 1152 | B <- length(breaks) - 1 1153 | nonempty_bins <- sapply(1:B, 1154 | function(b) 1155 | sum(breaks[b] <= obs_xs & obs_xs < breaks[b + 1]) > 0) 1156 | 1157 | baseline_cond_medians <- rep(NA, B) 1158 | 1159 | cond_median <- function(y, x, b_low, b_high) { 1160 | bin_idx <- which(b_low <= x & x < b_high) 1161 | if (length(bin_idx)) 1162 | return(median(y[bin_idx])) 1163 | else 1164 | return(median(y)) 1165 | } 1166 | 1167 | if (!is.null(baseline_values)) { 1168 | for (b in 1:B) { 1169 | baseline_cond_medians[b] <- cond_median(baseline_values, obs_xs, 1170 | breaks[b], breaks[b + 1]) 1171 | } 1172 | } 1173 | 1174 | if (is.null(baseline_values) | !residual) { 1175 | expectands <- list() 1176 | for (b in 1:B) { 1177 | expectands[[b]] <- 1178 | local({ 1179 | x <- obs_xs; 1180 | b_low <- breaks[b]; 1181 | b_high <- breaks[b + 1]; 1182 | function(y) cond_median(y, x, b_low, b_high) 1183 | }) 1184 | } 1185 | } else { 1186 | expectands <- list() 1187 | for (b in 1:B) { 1188 | expectands[[b]] <- 1189 | local({ 1190 | x <- obs_xs; 1191 | b_low <- breaks[b]; 1192 | b_high <- breaks[b + 1]; 1193 | baseline <- baseline_cond_medians[b]; 1194 | function(y) cond_median(y, x, b_low, b_high) - baseline 1195 | }) 1196 | } 1197 | } 1198 | 1199 | cond_median_samples <- 1200 | util$eval_expectand_pushforwards(samples, 1201 | expectands, 1202 | list('y'=array(names))) 1203 | 1204 | probs <- c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9) 1205 | median_quantiles <- sapply(cond_median_samples, 1206 | function(s) 1207 | util$ensemble_mcmc_quantile_est(s, probs)) 1208 | 1209 | plot_quantiles <- do.call(cbind, lapply(plot_idxs, 1210 | function(n) median_quantiles[1:9, n])) 1211 | 1212 | # Plot 1213 | if (is.null(display_ylim)) { 1214 | if (is.null(baseline_values)) { 1215 | display_ylim <- c(min(median_quantiles[1, nonempty_bins]), 1216 | max(median_quantiles[9, nonempty_bins])) 1217 | } 1218 | else { 1219 | if (residual) { 1220 | display_ylim <- c(min(c(0, median_quantiles[1, nonempty_bins])), 1221 | max(c(0, median_quantiles[9, nonempty_bins]))) 1222 | } else { 1223 | display_ylim <- c(min(min(median_quantiles[1, nonempty_bins]), 1224 | min(baseline_cond_medians[nonempty_bins])), 1225 | max(max(median_quantiles[9, nonempty_bins]), 1226 | max(baseline_cond_medians[nonempty_bins]))) 1227 | } 1228 | } 1229 | delta <- 0.05 * (display_ylim[2] - display_ylim[1]) 1230 | display_ylim[1] <- display_ylim[1] - delta 1231 | display_ylim[2] <- display_ylim[2] + delta 1232 | } 1233 | 1234 | if (is.null(ylab)) { 1235 | if (is.null(baseline_values) | !residual) 1236 | ylab <- "Marginal Quantiles of Conditional Medians" 1237 | else 1238 | ylab <- "Marginal Quantiles of Conditional Medians - Baselines" 1239 | } 1240 | 1241 | plot(1, type="n", main=main, 1242 | xlim=c(bin_min, bin_max), xlab=xlab, 1243 | ylim=display_ylim, ylab=ylab) 1244 | 1245 | polygon(c(plot_xs, rev(plot_xs)), 1246 | c(plot_quantiles[1,], rev(plot_quantiles[9,])), 1247 | col = c_light, border = NA) 1248 | polygon(c(plot_xs, rev(plot_xs)), 1249 | c(plot_quantiles[2,], rev(plot_quantiles[8,])), 1250 | col = c_light_highlight, border = NA) 1251 | polygon(c(plot_xs, rev(plot_xs)), 1252 | c(plot_quantiles[3,], rev(plot_quantiles[7,])), 1253 | col = c_mid, border = NA) 1254 | polygon(c(plot_xs, rev(plot_xs)), 1255 | c(plot_quantiles[4,], rev(plot_quantiles[6,])), 1256 | col = c_mid_highlight, border = NA) 1257 | for (b in 1:B) { 1258 | idx1 <- 2 * b - 1 1259 | idx2 <- 2 * b 1260 | if (nonempty_bins[b]) { 1261 | lines(plot_xs[idx1:idx2], plot_quantiles[5, idx1:idx2], 1262 | col=c_dark, lwd=2) 1263 | } else { 1264 | polygon(c(plot_xs[idx1:idx2], rev(plot_xs[idx1:idx2])), 1265 | c(display_ylim[1], display_ylim[1], 1266 | display_ylim[2], display_ylim[2]), 1267 | col="#EEEEEE", border = NA) 1268 | } 1269 | } 1270 | 1271 | if (!is.null(baseline_values)) { 1272 | if (residual) { 1273 | abline(h=0, col="#DDDDDD", lwd=2, lty=3) 1274 | } else { 1275 | for (b in 1:B) { 1276 | if (nonempty_bins[b]) { 1277 | idx1 <- 2 * b - 1 1278 | idx2 <- 2 * b 1279 | lines(plot_xs[idx1:idx2], rep(baseline_cond_medians[b], 2), 1280 | col="white", lwd=4) 1281 | lines(plot_xs[idx1:idx2], rep(baseline_cond_medians[b], 2), 1282 | col=baseline_col, lwd=2) 1283 | } 1284 | } 1285 | } 1286 | } 1287 | } 1288 | -------------------------------------------------------------------------------- /r/stan_programs/multi_full_model.stan: -------------------------------------------------------------------------------- 1 | data { 2 | int M; // Number of covariates 3 | int N; // Number of observations 4 | 5 | vector[M] x0; // Covariate baselines 6 | matrix[N, M] X; // Covariate design matrix 7 | 8 | array[N] real y; // Variates 9 | } 10 | 11 | transformed data { 12 | matrix[N, M * (M + 3) / 2 + 1] deltaX; 13 | for (n in 1:N) { 14 | deltaX[n, 1] = 1; 15 | 16 | for (m1 in 1:M) { 17 | // Linear perturbations 18 | deltaX[n, m1 + 1] = X[n, m1] - x0[m1]; 19 | } 20 | 21 | for (m1 in 1:M) { 22 | // On-diagonal quadratic perturbations 23 | deltaX[n, M + m1 + 1] 24 | = deltaX[n, m1 + 1] * deltaX[n, m1 + 1]; 25 | 26 | for (m2 in (m1 + 1):M) { 27 | int m3 = (2 * M - m1) * (m1 - 1) / 2 + m2 - m1; 28 | 29 | // Off-diagonal quadratic perturbations 30 | // Factor of 2 ensures that beta parameters have the 31 | // same interpretation as the expanded implementation 32 | deltaX[n, 2 * M + m3 + 1] 33 | = 2 * deltaX[n, m1 + 1] * deltaX[n, m2 + 1]; 34 | } 35 | } 36 | } 37 | } 38 | 39 | parameters { 40 | real beta0; // Intercept 41 | vector[M] beta1; // Linear slopes 42 | vector[M] beta2_d; // On-diagonal quadratic slopes 43 | vector[M * (M - 1) / 2] beta2_o; // Off-diagonal quadratic slopes 44 | real sigma; // Measurement Variability 45 | } 46 | 47 | model { 48 | vector[M * (M + 3) / 2 + 1] beta 49 | = append_row( 50 | append_row( 51 | append_row(beta0, beta1), 52 | beta2_d), 53 | beta2_o); 54 | 55 | // Prior model 56 | beta0 ~ normal(0, 10); 57 | beta1 ~ normal(0, 10); 58 | beta2_d ~ normal(0, 2); 59 | beta2_o ~ normal(0, 1); 60 | sigma ~ normal(0, 5); 61 | 62 | // Observation model 63 | y ~ normal(deltaX * beta, sigma); 64 | } 65 | 66 | generated quantities { 67 | // Posterior predictions 68 | array[N] real y_pred; 69 | { 70 | vector[M * (M + 3) / 2 + 1] beta 71 | = append_row( 72 | append_row( 73 | append_row(beta0, beta1), 74 | beta2_d), 75 | beta2_o); 76 | y_pred = normal_rng(deltaX * beta, sigma); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /r/stan_programs/multi_prior_model.stan: -------------------------------------------------------------------------------- 1 | data { 2 | int M; // Number of covariates 3 | int N; // Number of observations 4 | 5 | vector[M] x0; // Covariate baselines 6 | matrix[N, M] X; // Covariate design matrix 7 | } 8 | 9 | transformed data { 10 | matrix[N, M * (M + 3) / 2 + 1] deltaX; 11 | for (n in 1:N) { 12 | deltaX[n, 1] = 1; 13 | 14 | for (m1 in 1:M) { 15 | // Linear perturbations 16 | deltaX[n, m1 + 1] = X[n, m1] - x0[m1]; 17 | } 18 | 19 | for (m1 in 1:M) { 20 | // On-diagonal quadratic perturbations 21 | deltaX[n, M + m1 + 1] 22 | = deltaX[n, m1 + 1] * deltaX[n, m1 + 1]; 23 | 24 | for (m2 in (m1 + 1):M) { 25 | int m3 = (2 * M - m1) * (m1 - 1) / 2 + m2 - m1; 26 | 27 | // Off-diagonal quadratic perturbations 28 | // Factor of 2 ensures that beta parameters have the 29 | // same interpretation as the expanded implementation 30 | deltaX[n, 2 * M + m3 + 1] 31 | = 2 * deltaX[n, m1 + 1] * deltaX[n, m2 + 1]; 32 | } 33 | } 34 | } 35 | } 36 | 37 | parameters { 38 | real beta0; // Intercept 39 | vector[M] beta1; // Linear slopes 40 | vector[M] beta2_d; // On-diagonal quadratic slopes 41 | vector[M * (M - 1) / 2] beta2_o; // Off-diagonal quadratic slopes 42 | real sigma; // Measurement Variability 43 | } 44 | 45 | model { 46 | vector[M * (M + 3) / 2 + 1] beta 47 | = append_row( 48 | append_row( 49 | append_row(beta0, beta1), 50 | beta2_d), 51 | beta2_o); 52 | 53 | // Prior model 54 | beta0 ~ normal(0, 10); 55 | beta1 ~ normal(0, 10); 56 | beta2_d ~ normal(0, 2); 57 | beta2_o ~ normal(0, 1); 58 | sigma ~ normal(0, 5); 59 | } 60 | 61 | generated quantities { 62 | // Posterior predictions 63 | array[N] real y_pred; 64 | { 65 | vector[M * (M + 3) / 2 + 1] beta 66 | = append_row( 67 | append_row( 68 | append_row(beta0, beta1), 69 | beta2_d), 70 | beta2_o); 71 | y_pred = normal_rng(deltaX * beta, sigma); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /r/stan_programs/uni_full_model.stan: -------------------------------------------------------------------------------- 1 | data { 2 | int N; 3 | vector[N] x; // Observed inputs 4 | vector[N] y; // Observed outputs 5 | 6 | int N_grid; // Number of grid points for quantifying functional behavior 7 | vector[N_grid] x_grid; // Grid points for quantifying functional behavior 8 | } 9 | 10 | parameters { 11 | real alpha; 12 | real beta; 13 | real sigma; 14 | } 15 | 16 | model { 17 | alpha ~ normal(0, 3 / 2.32); 18 | beta ~ normal(0, 3 / 2.32); 19 | sigma ~ normal(0, 1 / 2.57); 20 | 21 | y ~ normal(alpha + beta * x, sigma); 22 | } 23 | 24 | generated quantities { 25 | vector[N_grid] f_grid = alpha + beta * x_grid; 26 | array[N_grid] real y_pred_grid = normal_rng(f_grid, sigma); 27 | } 28 | -------------------------------------------------------------------------------- /r/stan_programs/uni_prior_model.stan: -------------------------------------------------------------------------------- 1 | data { 2 | int N; 3 | vector[N] x; // Observed inputs 4 | vector[N] y; // Observed outputs 5 | 6 | int N_grid; // Number of grid points for quantifying functional behavior 7 | vector[N_grid] x_grid; // Grid points for quantifying functional behavior 8 | } 9 | 10 | parameters { 11 | real alpha; 12 | real beta; 13 | real sigma; 14 | } 15 | 16 | model { 17 | alpha ~ normal(0, 3 / 2.32); 18 | beta ~ normal(0, 3 / 2.32); 19 | sigma ~ normal(0, 1 / 2.57); 20 | } 21 | 22 | generated quantities { 23 | vector[N_grid] f_grid = alpha + beta * x_grid; 24 | array[N_grid] real y_pred_grid = normal_rng(f_grid, sigma); 25 | } 26 | --------------------------------------------------------------------------------