Skip to content

Commit 6b4f91f

Browse files
seabbs-botseabbs
andauthored
Fix #579: re-enable approx inference vignette evaluation (#591)
Co-authored-by: Sam Abbott <contact@samabbott.co.uk>
1 parent b8a9c4b commit 6b4f91f

3 files changed

Lines changed: 27 additions & 15 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ Suggests:
7070
tidybayes,
7171
usethis
7272
Remotes:
73+
paul-buerkner/brms,
7374
stan-dev/cmdstanr
7475
Config/Needs/website:
7576
r-lib/pkgdown,

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
the marginal model. See #583.
88
- Added `primarycensored (>= 1.4.0)` version bound to DESCRIPTION.
99
- Updated test expectations for changed primarycensored error handling.
10+
- Re-enabled approximate inference vignette evaluation using dev brms with
11+
pathfinder path fix. See #579.
1012

1113
## Package
1214

vignettes/approx-inference.Rmd

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ knitr::opts_chunk$set(
2929
message = FALSE,
3030
warning = FALSE,
3131
error = FALSE,
32-
eval = FALSE
32+
eval = TRUE
3333
)
3434
```
3535

@@ -138,7 +138,8 @@ obs_cens_trunc_samp <- simulate_gillespie(seed = 101) |>
138138

139139
</details>
140140

141-
We now prepare the data for fitting with the marginal model, and perform inference with HMC:
141+
We now prepare the data for fitting with the marginal model.
142+
We first pre-compile the Stan model so that compilation time is excluded from the timing comparisons below.
142143

143144
```{r results='hide'}
144145
linelist_data <- as_epidist_linelist_data(
@@ -151,8 +152,19 @@ linelist_data <- as_epidist_linelist_data(
151152
152153
data <- as_epidist_marginal_model(linelist_data)
153154
155+
# Pre-compile the model so compilation time is not included
156+
# in the timing comparisons
157+
fit_compile <- epidist(
158+
data = data, backend = "cmdstanr",
159+
chains = 1, iter = 5
160+
)
161+
```
162+
163+
We now perform inference with HMC:
164+
165+
```{r results='hide'}
154166
t <- proc.time()
155-
fit_hmc <- epidist(data = data, algorithm = "sampling", backend = "cmdstanr")
167+
fit_hmc <- update(fit_compile, chains = 4, iter = 2000)
156168
time_hmc <- proc.time() - t
157169
```
158170

@@ -163,27 +175,25 @@ To match the four Markov chains of length 1000 in HMC above, we then draw 4000 s
163175

164176
```{r results='hide'}
165177
t <- proc.time()
166-
fit_laplace <- epidist(
167-
data = data, algorithm = "laplace", draws = 4000, backend = "cmdstanr"
178+
fit_laplace <- update(
179+
fit_compile, algorithm = "laplace", draws = 4000
168180
)
169181
time_laplace <- proc.time() - t
170182
171183
t <- proc.time()
172-
fit_advi <- epidist(
173-
data = data, algorithm = "meanfield", draws = 4000, backend = "cmdstanr"
184+
fit_advi <- update(
185+
fit_compile, algorithm = "meanfield", draws = 4000
174186
)
175187
time_advi <- proc.time() - t
176188
```
177189

178190
For the Pathfinder algorithm we will set `num_paths = 1`.
179-
Although both the Laplace and ADVI methods ran without problems in all cases during testing, we found that Pathfinder often produced the error message "Error evaluating model log probability: Non-finite gradient."
180-
Although a `save_single_paths` option is available, which may have allowed recovery of individual Pathfinder paths (and therefore removing faulty paths), it does not appear to be working currently^[See https://github.com/stan-dev/cmdstanr/issues/878].
181191

182-
```{r}
192+
```{r results='hide'}
183193
t <- proc.time()
184-
fit_pathfinder <- epidist(
185-
data = data, algorithm = "pathfinder", draws = 4000, chains = 1,
186-
backend = "cmdstanr"
194+
fit_pathfinder <- update(
195+
fit_compile, algorithm = "pathfinder", draws = 4000,
196+
chains = 1
187197
)
188198
time_pathfinder <- proc.time() - t
189199
```
@@ -307,7 +317,6 @@ times
307317

308318
The range of alternative approximation algorithms available, and their ease of use, is an attractive feature of `brms`.
309319
We found that these algorithms do produce reasonable approximations in far less time than HMC.
310-
Of course, this vignette only includes one example, and a more thorough investigation would be required to make specific recommendations.
311-
That said, currently we do not recommend use of the Pathfinder algorithm due to its unstable performance in our testing and early stage software implementation.
320+
Of course, this vignette only includes one example, so results may differ for other models and datasets.
312321

313322
## References {-}

0 commit comments

Comments
 (0)