Skip to content

Commit

Permalink
Update FigA #55
Browse files Browse the repository at this point in the history
  • Loading branch information
athowes committed Jul 4, 2023
1 parent 7b70653 commit 24229b2
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 18 deletions.
Binary file modified docs/appendix.pdf
Binary file not shown.
Binary file modified docs/paper.pdf
Binary file not shown.
4 changes: 2 additions & 2 deletions src/docs_paper/2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ Type objective_function<Type>::operator()()
Type nll;
nll = Type(0.0);

Type phi1 = 0.3 * theta2;
Type phi2 = 0.5 * theta1 - 0.3 * theta2;
Type phi1 = 0.5 * theta1;
Type phi2 = 0.8 * theta1 - 0.5 * theta2;

nll -= dsn(phi1, Type(2.0), true);
nll -= dsn(phi2, Type(-2.0), true);
Expand Down
30 changes: 24 additions & 6 deletions src/docs_paper/figures.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
cols <- c("#56B4E9","#009E73", "#E69F00", "#F0E442", "#0072B2", "#D55E00", "#CC79A7", "#999999")
library(tidyverse)
library(patchwork)

#' Fig A
cols <- c("#56B4E9","#009E73", "#E69F00", "#F0E442", "#0072B2", "#D55E00", "#CC79A7", "#999999")

TMB::compile("2d.cpp")
dyn.load(TMB::dynlib("2d"))

obj <- TMB::MakeADFun(data = list(), parameters = list(theta1 = 0, theta2 = 0), DLL = "2d")

box_size <- 10

grid <- expand.grid(
theta1 = seq(-8, 8, length.out = 400),
theta2 = seq(-8, 8, length.out = 400)
theta1 = seq(-box_size, box_size, length.out = box_size * 50),
theta2 = seq(-box_size, box_size, length.out = box_size * 50)
)

ground_truth <- cbind(grid, pdf = apply(grid, 1, function(x) exp(-1 * obj$fn(x))))
Expand All @@ -32,7 +35,7 @@ cov <- sd_out$cov.fixed

figA0 <- ggplot(ground_truth, aes(x = theta1, y = theta2, z = pdf)) +
geom_contour(col = "lightgrey") +
coord_fixed(xlim = c(-8, 8), ylim = c(-8, 8), ratio = 1) +
coord_fixed(xlim = c(-box_size, box_size), ylim = c(-box_size, box_size), ratio = 1) +
labs(x = "", y = "") +
theme_minimal() +
guides(size = "none") +
Expand Down Expand Up @@ -93,7 +96,22 @@ mvQuad::rescale(gg5, m = mu, C = cov, dec.type = 1)
figA5 <- add_points(figA0, gg5) +
labs(size = "")

figA <- (figA1 + figA2) / (figA3 + figA4 + figA5) +
#' Scree
lambda <- eigen(cov)$values

figA6 <- data.frame(
n = 1:length(lambda),
tv = cumsum(lambda / sum(lambda))
) %>%
ggplot(aes(x = as.factor(n), y = tv)) +
geom_point() +
geom_hline(yintercept = 0.9, col = "grey", linetype = "dashed") +
annotate("text", x = 1, y = 0.85, label = "90% of total variation explained", col = "grey") +
scale_y_continuous(labels = scales::percent, limits = c(0, 1)) +
labs(x = "PCA dimensions included", y = "Total variation explained") +
theme_minimal()

figA <- (figA1 + figA2 + figA3) / (figA4 + figA5 + figA6) +
plot_annotation(tag_levels = "A") &
theme(plot.tag.position = c(0.15, 0.95))

Expand Down
2 changes: 1 addition & 1 deletion src/docs_paper/paper.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ Samples from this mixture of Gaussians may be obtained by drawing a node $\z$ wi

## Principal components analysis \label{sec:pca}

```{r aghq, fig.cap="The Gauss-Hermite quadrature nodes $\\z \\in \\mathcal{Q}(2, 3)$ for a two dimensional integral with three nodes per dimension (A). Adaption occurs based on the mode (B) and covariance matrix of the target via the Cholesky decomposition (C) or spectral decompostion (D) of the inverse curvature at the mode. In PCA-AGHQ (E) only nodes along the first $s$ principal components are kept. The integrand is $f(\\btheta) = \\text{sn}(0.3 \\theta_1, \\alpha = 2) \\cdot \\text{sn}(0.5 \\theta_1 - 0.3 \\theta_2, \\alpha = -2)$, where $\\text{sn}(\\cdot)$ is the standard skewnormal probability density function with shape parameter $\\alpha \\in \\mathbb{R}$."}
```{r aghq, fig.cap="The Gauss-Hermite quadrature nodes $\\z \\in \\mathcal{Q}(2, 3)$ for a two dimensional integral with three nodes per dimension (A). Adaption occurs based on the mode (B) and covariance matrix of the target via the Cholesky decomposition (C) or spectral decompostion (D) of the inverse curvature at the mode. In PCA-AGHQ (E) only nodes along the first $s = 1$ principal components are kept. The Scree plot (F) shows that over 90% of variation is explained by keeping just the first principal component. The integrand is $f(\\btheta) = \\text{sn}(0.5 \\theta_1, \\alpha = 2) \\cdot \\text{sn}(0.8 \\theta_1 - 0.5 \\theta_2, \\alpha = -2)$, where $\\text{sn}(\\cdot)$ is the standard skewnormal probability density function with shape parameter $\\alpha \\in \\mathbb{R}$."}
figA
```

Expand Down
10 changes: 1 addition & 9 deletions src/docs_paper/script.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
# orderly::orderly_develop_start("docs_paper")
# setwd("src/docs_paper")

#' Conversion of figures from .pdf to .jpeg at specified resolution
#' Conversion of figures from .pdf at specified resolution
Sys.setenv(PATH = paste(Sys.getenv("PATH"), "/opt/homebrew/bin", sep = ":"))

convert_pdf_jpeg <- function(name) {
command <- paste0(
"convert -density 300 depends/", name, ".pdf -scene 1 -background white",
" -alpha remove -alpha off -quality 80 depends/", name, ".jpeg"
)
system(command)
}

convert_pdf_png <- function(name) {
command <- paste0(
"convert -density 300 depends/", name, ".pdf -scene 1 -background white",
Expand Down

0 comments on commit 24229b2

Please sign in to comment.