Last active
October 16, 2017 20:24
-
-
Save dirmeier/303d250e697ca411ed6ac9f72f2f6d54 to your computer and use it in GitHub Desktop.
Maximum likelihood estimation of the success probability on a Bernoulli experiment in R.
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
library(microbenchmark) | |
bernoulli.loglik.derivative <- function(p, dat) | |
{ | |
-(sum(dat) - length(dat) * p) | |
} | |
optim <- function(dat) | |
{ | |
p.hat <- 1 | |
for (i in seq(10000)) | |
{ | |
p.hat <- p.hat - 0.0001 * bernoulli.loglik.derivative(p.hat, dat) | |
} | |
p.hat | |
} | |
dat <- sample(0:1, 10000, prob=c(.25, .75), replace=TRUE) | |
print(mean(dat)) | |
print(optim(dat)) | |
res <- microbenchmark(f=optim(dat)) | |
print(res) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment