Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.