Skip to content

Commit

Permalink
Calculate MMD with kernlab #42
Browse files Browse the repository at this point in the history
  • Loading branch information
athowes committed May 9, 2023
1 parent be44f4e commit 6209e40
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 47 deletions.
49 changes: 6 additions & 43 deletions src/naomi-simple_mmd/mmd.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ output:
code_folding: show
theme: lumen
abstract: |
**Background** We have run the simplified Naomi model using a range of inference methods: `TMB`, `aghq`, `adam` and `tmbstan`.
**Background** We have run the simplified Naomi model using a range of inference methods: `TMB`, `aghq` and `tmbstan`.
**Task** In this report, we compare the accuracy of the posterior distributions obtained from these inference methods using maximum mean discrepancy.
---
Expand All @@ -24,9 +24,6 @@ Import these inference results as follows:
```{r}
tmb <- readRDS("depends/tmb.rds")
aghq <- readRDS("depends/aghq.rds")
adam <- readRDS("depends/adam.rds")
adam_time <- adam$time
adam <- adam$adam
tmbstan <- readRDS("depends/tmbstan.rds")
depends <- yaml::read_yaml("orderly.yml")$depends
Expand All @@ -36,49 +33,15 @@ Check that the parameters (latent field, hyperparameters, model outputs) sampled

```{r}
stopifnot(names(tmb$fit$sample) == names(aghq$quad$sample))
stopifnot(names(tmb$fit$sample) == names(adam$adam$sample))
stopifnot(names(tmb$fit$sample) == names(tmbstan$mcmc$sample))
```

## Run details {.tabset}

For more information about the conditions under which these results were generated, see:

### `TMB`

```{r}
dependency_details <- function(i) {
report_name <- names(depends[[i]])
print(paste0("Inference results obtained from ", report_name, " with the query ", depends[[i]][[report_name]]$id))
report_id <- orderly::orderly_search(query = depends[[i]][[report_name]]$id, report_name)
print(paste0("Obtained report had ID ", report_id, " and was run with the following parameters:"))
print(orderly::orderly_info(report_id, report_name)$parameters)
}
dependency_details(1)
```

### `aghq`

```{r}
dependency_details(2)
```

### `adam`

```{r}
dependency_details(3)
```

### `tmbstan`

```{r}
tmbstan_details <- dependency_details(4)
tmbstan_details
```

# Maximum mean discrepancy

```{r}
#' To write!
mmd1 <- kernlab::kmmd(matrix(tmb$fit$sample$beta_rho), matrix(tmbstan$mcmc$sample$beta_rho))
mmd2 <- kernlab::kmmd(matrix(aghq$quad$sample$beta_rho), matrix(tmbstan$mcmc$sample$beta_rho))
mmd1
mmd2
```
6 changes: 2 additions & 4 deletions src/naomi-simple_mmd/orderly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ packages:
- bayesplot
- patchwork
- gt
- kernlab

depends:
- naomi-simple_fit:
Expand All @@ -35,11 +36,8 @@ depends:
id: latest(parameter:aghq == TRUE && parameter:k == 3 && parameter:s == 8)
use:
depends/aghq.rds: out.rds
- naomi-simple_fit:
id: latest(parameter:adam == TRUE)
use:
depends/adam.rds: out.rds
- naomi-simple_fit:
id: latest(parameter:tmbstan == TRUE && parameter:niter > 50000)
use:
depends/tmbstan.rds: out.rds

0 comments on commit 6209e40

Please sign in to comment.