|
| 1 | +# Toy example for N(0, sigma2) to show effect of rbm |
| 2 | +library(tidyverse) |
| 3 | + |
| 4 | +set.seed(123) |
| 5 | +n <- 50 |
| 6 | +sigma2 <- 1 # theta |
| 7 | +x <- rnorm(n, mean = 0, sd = sqrt(sigma2)) |
| 8 | +S <- sum(x^2) |
| 9 | + |
| 10 | +# Ingredients |
| 11 | +loglik <- function(theta, xx = x) sum(dnorm(xx, sd = sqrt(theta), log = TRUE)) |
| 12 | +score <- function(theta, xx = x) { |
| 13 | + sum(-1 / (2 * theta) + (xx - 0) ^ 2 / (2 * theta ^ 2)) |
| 14 | +} |
| 15 | + |
| 16 | + |
| 17 | +e <- function(theta) { |
| 18 | + tmp <- -1 / (2 * theta) + (x - 0) ^ 2 / (2 * theta ^ 2) |
| 19 | + sum(tmp ^ 1) |
| 20 | +} |
| 21 | + |
| 22 | +j <- function(theta) { |
| 23 | + tmp <- 1 / (2 * theta ^ 2) - (x - 0) ^ 2 / (theta ^ 3) |
| 24 | + -1 * sum(tmp) |
| 25 | +} |
| 26 | + |
| 27 | +# enum <- function(theta) { |
| 28 | +# res <- x |
| 29 | +# for (i in seq_along(res)) { |
| 30 | +# res[i] <- numDeriv::grad(function(theta) dnorm(x[i], sd = sqrt(theta), log = TRUE), theta) |
| 31 | +# } |
| 32 | +# sum(res ^ 2) |
| 33 | +# } |
| 34 | +# |
| 35 | +# jnum <- function(theta) { |
| 36 | +# as.numeric(-1 * numDeriv::hessian(loglik, theta)) |
| 37 | +# } |
| 38 | +# |
| 39 | +# |
| 40 | +penalty <- function(theta) -0.5 * e(theta) / j(theta) |
| 41 | +# |
| 42 | +# theta_hat <- nlminb(1, function(x) -1 * loglik(x), lower = 0.01, upper = 5)$par |
| 43 | +# theta_til <- nlminb(1, function(x) -(loglik(x) + penalty(x)), lower = 0.01, upper = 5)$par |
| 44 | +# c(theta_hat, theta_til, sigma2) |
| 45 | +# |
| 46 | +# tibble(theta = seq(0.3, 1.5, length = 100)) |> |
| 47 | +# mutate( |
| 48 | +# # loglik = map_dbl(theta, loglik), |
| 49 | +# pen = map_dbl(theta, penalty), |
| 50 | +# # pen = loglik + pen |
| 51 | +# ) |> |
| 52 | +# # pivot_longer(cols = c(loglik, pen), names_to = "which", values_to = "value") |> |
| 53 | +# ggplot(aes(theta, pen)) + |
| 54 | +# geom_line() + |
| 55 | +# theme_bw() |
| 56 | +# |
| 57 | +# tibble(theta = seq(0.5, 1.5, length = 100)) |> |
| 58 | +# rowwise() |> |
| 59 | +# mutate( |
| 60 | +# e = e(theta), |
| 61 | +# j = j(theta), |
| 62 | +# jinv = 1 / j, |
| 63 | +# pen = penalty(theta) |
| 64 | +# ) |> |
| 65 | +# pivot_longer(cols = c(e), names_to = "which", values_to = "value") |> |
| 66 | +# ggplot(aes(theta, value, col = which)) + |
| 67 | +# geom_line() + |
| 68 | +# theme_bw() |
| 69 | + |
| 70 | + |
| 71 | +## ----------------------------------------------------------------------------- |
| 72 | +set.seed(123) |
| 73 | +n <- 15 |
| 74 | +B <- 50 |
| 75 | +sigma2 <- 1 # theta |
| 76 | +X <- lapply(1:B, function(i) { |
| 77 | + rnorm(n, mean = 0, sd = sqrt(sigma2)) |
| 78 | +}) |
| 79 | + |
| 80 | +bump <- 3 / n # exagerate the bias, diminishes as n -> Inf |
| 81 | + |
| 82 | +plot_df <- |
| 83 | + tibble(b = 1:B) |> |
| 84 | + mutate( |
| 85 | + X = map(b, \(i) X[[i]]), |
| 86 | + theta = list(seq(0.32, 1.25, length = 1000)), |
| 87 | + score = map2(X, theta, \(xx, tt) 2 * map_dbl(tt + bump, score, xx)) |
| 88 | + ) |> |
| 89 | + select(b, theta, score) |> |
| 90 | + unnest(c(theta, score)) |
| 91 | + |
| 92 | +plot_df2 <- summarise(plot_df, score = mean(score), .by = theta) |
| 93 | +theta_hat <- |
| 94 | + plot_df2 |> |
| 95 | + mutate(absscore = abs(score)) |> |
| 96 | + filter(absscore == min(absscore)) |> |
| 97 | + slice(1) |> |
| 98 | + pull(theta) |
| 99 | +bias <- theta_hat - sigma2 |
| 100 | +plot_df3 <- |
| 101 | + plot_df2 |> |
| 102 | + mutate(theta = theta - bias) |> |
| 103 | + filter(theta <= 1.25) |
| 104 | + |
| 105 | +ggplot(plot_df, aes(theta, score, group = b)) + |
| 106 | + geom_line(linewidth = 0.1, col = "gray40") + |
| 107 | + geom_hline(yintercept = 0, linetype = "dashed") + |
| 108 | + geom_line( |
| 109 | + data = plot_df2, |
| 110 | + aes(theta, score), |
| 111 | + col = "blue", |
| 112 | + inherit.aes = FALSE, |
| 113 | + linewidth = 1 |
| 114 | + ) + |
| 115 | + geom_line( |
| 116 | + data = plot_df3, |
| 117 | + aes(theta, score), |
| 118 | + inherit.aes = FALSE, |
| 119 | + col = "red3", |
| 120 | + linewidth = 1 |
| 121 | + ) + |
| 122 | + scale_x_continuous( |
| 123 | + breaks = c(theta_hat, sigma2), |
| 124 | + labels = c(expression(hat(theta), theta[0])), |
| 125 | + name = NULL |
| 126 | + ) + |
| 127 | + theme_bw() + |
| 128 | + theme( |
| 129 | + panel.grid.major.y = element_blank(), |
| 130 | + panel.grid.minor.y = element_blank(), |
| 131 | + axis.text.x = element_text(size = 12), |
| 132 | + axis.text.y = element_blank(), |
| 133 | + axis.ticks.y = element_blank() |
| 134 | + ) + |
| 135 | + coord_cartesian(ylim = c(-8, 28)) + |
| 136 | + labs(y = expression(Score~U(theta))) |
| 137 | + |
| 138 | + |
0 commit comments