Skip to content

Instantly share code, notes, and snippets.

@statwonk
Created October 23, 2017 04:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save statwonk/6bbbe8ba313c2191ae9f785de86a525f to your computer and use it in GitHub Desktop.
Save statwonk/6bbbe8ba313c2191ae9f785de86a525f to your computer and use it in GitHub Desktop.
100 exponential random variables and their relative likelihoods
suppressPackageStartupMessages({
library(dplyr)
library(ggplot2)
library(purrr)
})
exp_likelihood <- function(x, lambda) {
(lambda^length(x))*exp(-lambda*length(x)*mean(x))
}
seq_len(1e2) %>%
lapply(function(i) {
x <- rexp(100, rate = 3)
lapply(seq(1, 5, 0.01),
function(lambda) { exp_likelihood(x, lambda) }) %>% unlist() %>%
{ data.frame(lambda = seq(1, 5, 0.01), likelihood = .) } %>%
mutate(rel_likelihood = likelihood / max(likelihood)) %>%
mutate(boot = i)
}) %>%
bind_rows() %>%
ggplot(aes(x = lambda, y = rel_likelihood)) +
geom_line(aes(group = factor(boot)),
alpha = 0.3, size = 0.5) +
theme_minimal() +
ggtitle("100 exponential(rate = 3) relative likelihoods") +
ylab("Relative likelihood estimates") +
xlab("rate")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment