Recall "Maximum likelihood estimation". We're going to do this by hand. The Poisson Likelihood is given by [ L(\lambda ; y_1,\ldots,y_n) = \prod_{i=1}^n \frac{\lambda^{y_i}\exp(-\lambda)}{y_i!}. ]
y = rpois(100, 3)
I need the derivative of the likelihood.
log
, I don't change the minimizer. Use this trick!! Write a function which gives the key ingredient as a function of $\lambda$.deriv_loglike <- function(lambda, y){ sum(-1 + y/lambda) }
You should get $3+5-2=6$. My function gives r deriv_loglike(1,c(3,5))
.
gam=.01
and start at 23. Set tol=1e-5
. Is this the value you would expect? What should the answer be?I should see $\lambda = \overline{y} =$ r mean(y)
.
maxiter = 1000 conv = FALSE gam = 0.01 lam = 23 tol = 1e-5 for(iter in 1:maxiter){ lam.new = lam + gam * deriv_loglike(lam, y) conv = abs(lam - lam.new) < tol lam = lam.new if(conv) break } lam
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.