Skip to content

Commit ec2db44

Browse files
committed
update trace plots.
1 parent 2098a5d commit ec2db44

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed
85 KB
Binary file not shown.

knitr/planetary_motion/planetary_motion.rmd

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ The specific concepts we encounter in this article include why our inference fai
2323

2424
Given the recorded position of a planet over time, we want to estimate the physical properties of a star-planet system.
2525
This includes position, momentum, and gravitational interaction.
26-
We fit the model with Stan [@Carpenter:2017], using Hamiltonian Monte Carlo (HMC), a gradient-based Markov chain Monte Carlo (MCMC) algorithm;
26+
We fit the model with Stan [@Carpenter:2017], using a Hamiltonian Monte Carlo (HMC) sampler, a gradient-based Markov chain Monte Carlo (MCMC) algorithm;
2727
for a thorough introduction to HMC, we recommend the article by @Betancourt:2018.
2828
Initially, we meant the planetary motion problem to be a simple textbook example for ODE-based models; but it turns out many interesting challenges arise when we do a Bayesian analysis on this model.
2929
We discuss how to diagnose and fix these issues.
@@ -44,21 +44,23 @@ In our presentation, we try to distinguish generalizable methods, problem-specif
4444
```{r include=FALSE}
4545
# Adjust to your setting
4646
.libPaths("~/Rlib/")
47-
setwd("~/Code/example-models/knitr/planetary_motion")
47+
# setwd("~/Code/example-models/knitr/planetary_motion")
4848
```
4949

5050
```{r message = FALSE}
51-
library(dplyr)
5251
library(cmdstanr)
5352
library(posterior)
5453
library(ggplot2)
54+
library(dplyr)
5555
library(plyr)
5656
library(tidyr)
5757
5858
library(boot)
5959
library(latex2exp)
6060
source("tools.r")
6161
62+
bayesplot::color_scheme_set("viridisC")
63+
6264
set.seed(1954)
6365
```
6466

@@ -233,7 +235,8 @@ We extend the trace plots to include the warmup phase.
233235

234236
```{r fig.height=3}
235237
# TODO: added shaded area for warm-up phase.
236-
bayesplot::mcmc_trace(r_fit1$draws(inc_warmup = TRUE), pars = c("lp__", "k"))
238+
bayesplot::mcmc_trace(r_fit1$draws(inc_warmup = TRUE), pars = c("lp__", "k"),
239+
n_warmup = 500)
237240
```
238241

239242
It is now clear that the chain's final position is mostly driven by its initial point.
@@ -253,7 +256,7 @@ Inevitably, some of the chains start in the far tails of $p(k \mid y)$ and canno
253256
Because the parameter space is one-dimensional, we can "cheat" a bit
254257
-- well, we've earned it by setting up a simplified problem --
255258
and compute the log likelihood across a grid of values for $k$ to check that the modes indeed exist.
256-
```{r }
259+
```{r fig.height=3}
257260
ks <- seq(from = 0.2, to = 9, by = 0.01)
258261
q0 <- c(1.0, 0)
259262
p0 <- c(0, 1.0)
@@ -297,7 +300,7 @@ $$
297300
where $C$ is a constant which doesn't depend on $k$.
298301

299302
Let us now simulate trajectories for various values of $k$.
300-
```{r }
303+
```{r fig.height=4}
301304
k <- 0.5
302305
q_050 <- solve_trajectory(q0, p0, dt, k, m, n_obs, ts)
303306
k <- 1.6
@@ -575,13 +578,14 @@ print(r_fit2$time(), digits = 3)
575578
As commented before, we would've been wise not to run the algorithm for so many iterations... Stan returns several warning messages, including divergent transitions (for 343 out of 8,000 samples) and exceeded maximum treedepths (for 28 samples).
576579
We can check the warning message report using `fit$cmdstan_diagnose()`.
577580
As before, let's examine a few diagnostics:
578-
```{r, message=FALSE}
581+
```{r, message=FALSE, fig.height=3}
579582
pars <- c("lp__", "k", "q0", "p0", "star")
580583
r_fit2$summary(pars)[, c(1, 2, 4, 8, 9)]
581584
582585
pars <- c("lp__", "k", "q0[1]", "q0[2]", "p0[1]", "p0[2]",
583586
"star[1]", "star[2]")
584-
bayesplot::mcmc_trace(r_fit2$draws(), pars = pars)
587+
bayesplot::mcmc_trace(r_fit2$draws(inc_warmup = TRUE), pars = pars,
588+
n_warmup = 500)
585589
```
586590

587591
We have five well-behaved chains, which return consistent estimates, and three other chains which have with great effort ventured into other regions of the parameter space.
@@ -611,7 +615,7 @@ Our proposition is to look at _conditional likelihoods_, that is fix some of the
611615
This, from a certain point of view, very much amounts to studying a simplification of our model.
612616
To begin, we fix all parameters (based on the correct values or equivalently estimates from the well-behaving Markov chains), except for $q_*^x$.
613617

614-
```{r, fig.height=5}
618+
```{r, fig.height=4}
615619
star_x <- seq(from = -0.5, to = 0.8, by = 0.01)
616620
star_s <- array(NA, dim = c(length(star_x), 2))
617621
star_s[, 1] <- star_x
@@ -753,14 +757,14 @@ The possibility of such ill-fitting modes implies we should always run multiple
753757
This case study also raises the question of what role starting points may play.
754758
Ideally a Markov chain forgets its initial value but in a non-asymptotic regime this may not be the case.
755759
Just as there is no universal default prior, there is no universal default initial point.
756-
Modelers often need to depart from defaults to insure a numerically stable evaluation of the joint density and improve MCMC computation.
760+
Modelers often need to depart from defaults to ensure a numerically stable evaluation of the joint density and improve MCMC computation.
757761
At the same time we want dispersed initial points in order to have reliable convergence diagnostics and to potentially explore all the relevant modes.
758-
Like for other tuning parameters of an inference algorithm, picking starting points can be an iterative process, with adjustments made after a first attempt at fitting the model.
762+
As with other tuning parameters of an inference algorithm, picking starting points can be an iterative process, with adjustments made after a first attempt at fitting the model.
759763

760764
We do not advocate mindlessly discarding misbehaving chains. It is important to analyze where this poor behavior comes from, and whether it hints at serious flaws in our model and in our inference. Our choice to adjust the initial estimates is based on: (a) the realization that the defaults are widely inconsistent with our expertise and (b) the understanding that the local modes do not describe a latent phenomenon of interest, as shown by our detailed analysis of how cyclical data interacts with a normal likelihood.
761765

762766
# Acknowledgement {-#Acknowledgement}
763767

764-
We thank Ben Bales, Matthew West, and Martin Modrák for helpful discussion.
768+
We thank Ben Bales, Matthew West, Martin Modrák, and Jonah Gabry for helpful discussion.
765769

766770
# References {-#my-section}

knitr/planetary_motion/tools.r

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,10 @@ ppc_plot <- function(fit, chains, pred = "qx_pred", pars = "k",
6666

6767
ppc_plot2D <- function(fit, pred = c("qx_pred", "qy_pred"), pars = "k", data_pred,
6868
plot_star = FALSE) {
69-
## TODO: add ribbons for ppcs
7069
qx_pred <- fit$draws(variables = pred[1])
7170
qy_pred <- fit$draws(variables = pred[2])
7271

7372
chains <- dim(qx_pred)[2]
74-
# TODO: Fix plot_star clause to not use rstan.
7573
if (plot_star) {
7674
# extract the median estimate for the star's position (for each chain)
7775
star <- array(NA, c(2, chains))

0 commit comments

Comments
 (0)