The adventures of Bayesian programming in Stan Samuel Hudec Matej Bel University Faculty of Natural Sciences Department of Mathematics 9. 2. 2018 Samuel Hudec DM seminar Topics Introduction Bayesian approach MCMC Stan enviroment First model in Stan Regression model in Stan Basic Hierarchical model in Stan Conclusion Samuel Hudec DM seminar What you can get? full Bayesian statistical inference with MCMC sampling approximate Bayesian inference with variational inference penalized maximum likelihood estimation with optimization Introduction http://mc-stan.org/ Stan is a state-of-the-art platform for statistical modeling and high-performance statistical computation. Thousands of users rely on Stan for statistical modeling, data analysis, and prediction in the social, biological, and physical sciences, engineering, and business. Samuel Hudec DM seminar Introduction http://mc-stan.org/ Stan is a state-of-the-art platform for statistical modeling and high-performance statistical computation. Thousands of users rely on Stan for statistical modeling, data analysis, and prediction in the social, biological, and physical sciences, engineering, and business. What you can get? full Bayesian statistical inference with MCMC sampling approximate Bayesian inference with variational inference penalized maximum likelihood estimation with optimization Samuel Hudec DM seminar Named by Stanislaw Ulam. Who was a scientist in the fields of mathematics and nuclear physics, also he invated the Monte Carlo method to computation, ... Introduction (Why Stan?) Samuel Hudec DM seminar Introduction (Why Stan?) Named by Stanislaw Ulam. Who was a scientist in the fields of mathematics and nuclear physics, also he invated the Monte Carlo method to computation, ... Samuel Hudec DM seminar Introduction (Stanislaw Ulam) Samuel Hudec DM seminar Higher-Level Interface RStanArm provides an R formula interface for Bayesian regression modeling. etc. Introduction (Stan interfaces) RStan (R) PyStan (Phyton) CmdStan (shell, command-line terminal) MatlabStan (MATLAB) StataStan (Stata) MathematicaStan (Mathemtatica) Samuel Hudec DM seminar Introduction (Stan interfaces) RStan (R) PyStan (Phyton) CmdStan (shell, command-line terminal) MatlabStan (MATLAB) StataStan (Stata) MathematicaStan (Mathemtatica) Higher-Level Interface RStanArm provides an R formula interface for Bayesian regression modeling. etc. Samuel Hudec DM seminar p(θ D) p(D θ)p(θ) j / j Posterior predictive density (prediction, validation,...) p(D~ D) = p(D~ θ)p(θ D) d θ j j j ZΘ Bayesian approach Based on Bayesian theorem (1763) p(D θ)p(θ) p(θ D)= j j p(D θ)p(θ) d θ Θ j R Model (likelihood) = p(D θ) Prior = p(θ) (conjugate, noninformative...)j evidence = Θ p(D θ)p(θ) d θ , p(D) Posterior = p(θ D)j R j Samuel Hudec DM seminar Posterior predictive density (prediction, validation,...) p(D~ D) = p(D~ θ)p(θ D) d θ j j j ZΘ Bayesian approach Based on Bayesian theorem (1763) p(D θ)p(θ) p(θ D)= j j p(D θ)p(θ) d θ Θ j R Model (likelihood) = p(D θ) Prior = p(θ) (conjugate, noninformative...)j evidence = Θ p(D θ)p(θ) d θ , p(D) Posterior = p(θ D)j R j p(θ D) p(D θ)p(θ) j / j Samuel Hudec DM seminar Bayesian approach Based on Bayesian theorem (1763) p(D θ)p(θ) p(θ D)= j j p(D θ)p(θ) d θ Θ j R Model (likelihood) = p(D θ) Prior = p(θ) (conjugate, noninformative...)j evidence = Θ p(D θ)p(θ) d θ , p(D) Posterior = p(θ D)j R j p(θ D) p(D θ)p(θ) j / j Posterior predictive density (prediction, validation,...) p(D~ D) = p(D~ θ)p(θ D) d θ j j j ZΘ Samuel Hudec DM seminar Bayesian approach source: McElreath, R., Statistical Rethinking CRC Press 2016. Samuel Hudec DM seminar Bayesian approach Bayesian approachBayesian approach Based on Bayesian theorem (1763) Based on Bayesian theorem (1763) p(D ✓)p(✓) ??? p(D ✓)p(✓) p(✓ D)= | p(✓ D)= | | p(D ✓)p(✓)d✓ | p(D ✓)p(✓)d✓ ⇥ | ⇥ | R R Model (likelihood) = p(D ✓) Model (likelihood) = p(D ✓) | Prior = p(✓) (conjugate, noninformative...)| PriorSamuel = Hudecp(✓) DM(conjugate, seminar noninformative...) evidence = ⇥ p(D ✓)p(✓)d✓ , p(D) evidence = ⇥ p(D ✓)p(✓)d✓ , p(D) | Posterior = p(✓ D)| Posterior = p(✓ D) R | R | p(✓ D) p(D ✓)p(✓) p(✓ D) p(D ✓)p(✓) | / | | / | Posterior predictive density (prediction, validation,...) Posterior predictive density (prediction, validation,...) p(D˜ D)= p(D˜ ✓)p(✓ D)d✓ p(D˜ D)= p(D˜ ✓)p(✓ D)d✓ | ⇥ | | | | | Z Z⇥ Samuel Hudec DM seminar Samuel Hudec DM seminar Bayesian approach Bayesian approachBayesian approach Based on Bayesian theorem (1763) Based on Bayesian theorem (1763) p(D ✓)p(✓) p(D ✓)p(✓) p(✓ D)= | p(✓ D)= | | p(D ✓)p(✓)d✓ | p(D ✓)p(✓)d✓ ⇥ | ⇥ | R R Model (likelihood) = p(D ✓) Model (likelihood) = p(D ✓) | Prior = p(✓) (conjugate, noninformative...)| PriorSamuel = Hudecp(✓) DM(conjugate, seminar noninformative...) evidence = ⇥ p(D ✓)p(✓)d✓ , p(D) evidence = ⇥ p(D ✓)p(✓)d✓ , p(D) | Posterior = p(✓ D)| Posterior = p(✓ D) R | R | p(✓ D) p(D ✓)p(✓) p(✓ D) p(D ✓)p(✓) | / | | / | Posterior predictive density (prediction, validation,...) Posterior predictive density (prediction, validation,...) p(D˜ D)= p(D˜ ✓)p(✓ D)d✓ p(D˜ D)= p(D˜ ✓)p(✓ D)d✓ | ⇥ | | | | | Z Z⇥ Samuel Hudec DM seminar Samuel Hudec DM seminar How many samples do you need? How many chains we need? for reggresion motto "four short chains to check, one long chain for inference" MCMC "Markov Chain Monte Carlo methods are a class of algorithms for sampling from a probability distribution based on constructing a Markov chain that has the desired distribution as its equilibrium distribution. The state of the chain after a number of steps is then used as a sample of the desired distribution. The quality of the sample improves as a function of the number of steps." Samuel Hudec DM seminar for reggresion motto "four short chains to check, one long chain for inference" MCMC "Markov Chain Monte Carlo methods are a class of algorithms for sampling from a probability distribution based on constructing a Markov chain that has the desired distribution as its equilibrium distribution. The state of the chain after a number of steps is then used as a sample of the desired distribution. The quality of the sample improves as a function of the number of steps." How many samples do you need? How many chains we need? Samuel Hudec DM seminar MCMC "Markov Chain Monte Carlo methods are a class of algorithms for sampling from a probability distribution based on constructing a Markov chain that has the desired distribution as its equilibrium distribution. The state of the chain after a number of steps is then used as a sample of the desired distribution. The quality of the sample improves as a function of the number of steps." How many samples do you need? How many chains we need? for reggresion motto "four short chains to check, one long chain for inference" Samuel Hudec DM seminar MCMC sourse: http://heattransfer.asmedigitalcollection.asme.org/ Samuel Hudec DM seminar MCMC Samuel Hudec DM seminar Common problems with unstacionarity or "Timing a wild chain" broad, tlat regions of the posterior density non-identifiable parameters MCMC (diagnostic) The default diadnostic output from Stan includes two metrics n eff measure of the effective number of samples Rhat R^ Gelman-Rubin convergence diagnostic (above 1 usually indicates that the chain has not yet converged) Samuel Hudec DM seminar MCMC (diagnostic) The default diadnostic output from Stan includes two metrics n eff measure of the effective number of samples Rhat R^ Gelman-Rubin convergence diagnostic (above 1 usually indicates that the chain has not yet converged) Common problems with unstacionarity or "Timing a wild chain" broad, tlat regions of the posterior density non-identifiable parameters Samuel Hudec DM seminar MCMC (diagnostic) source: McElreath, R., Statistical Rethinking CRC Press 2016. Samuel Hudec DM seminar strongly recommend installing RStudio which has basic support for .stan file types and syntax highlighting for Stan Stan enviroment (before we start) build model in Stan file mymodel.stan use R (or another enviroment) for load data set, runnig Stan model, print fit, diagnostics, plotting, .... Samuel Hudec DM seminar Stan enviroment (before we start) build model in Stan file mymodel.stan use R (or another enviroment) for load data set, runnig Stan model, print fit, diagnostics, plotting, .... strongly recommend installing RStudio which has basic support for .stan file types and syntax highlighting for Stan Samuel Hudec DM seminar Stan enviroment source: Kruschke, J., K., Doing Bayesian Data Analysis Elsevier Inc. 2015 Samuel Hudec DM seminar transformed data ... declarations ...f statements ... parametersg ... declarationsf ... transformedg parameters ... declarations ... statementsf ... modelg ... declarationsf ... statements ... generatedg quantities ... declarations ...f statements ... g Stan enviroment (General structure of Stan model specification) data ... declarationsf ... g Samuel Hudec DM seminar parameters ... declarationsf ... transformedg parameters ... declarations ... statementsf ... modelg ... declarationsf ... statements ... generatedg quantities ... declarations ...f statements ... g Stan enviroment (General structure of Stan model specification) data ... declarationsf ... transformedg data ... declarations ...f statements ... g Samuel Hudec DM seminar transformed parameters ... declarations ... statementsf ... modelg ... declarationsf ... statements ... generatedg quantities ... declarations ...f statements ... g Stan enviroment (General structure of Stan model specification) data ... declarationsf ... transformedg data ... declarations ...f
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages73 Page
-
File Size-