Created
October 23, 2017 04:14
-
-
Save statwonk/6bbbe8ba313c2191ae9f785de86a525f to your computer and use it in GitHub Desktop.
100 exponential random variables and their relative likelihoods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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