In: Math
(R programming)
Generate 50 samples from a Poisson distribution with lambda to be 2 and define the log likelihood function
Use optimization to find the maximum likelihood estimator of lambda. Repeat for 100 times using forloop. You will need to save the results of the estimated values of lambda.
R-Code for log likelihood function:
set.seed(123)
lambda = 2
i = 1
lambda_est = rep(0,100)
log_likely = rep(0,100)
for (i in 1:100)
{
sample = rpois(50, lambda = 2)
negloglike = function(lam)
{
length(sample) * lam - sum(sample) * log(lam) +
sum(log(factorial(sample)))
}
log_likely[i] = negloglike(lambda)
lambda_est[i] = nlm(negloglike,p=c(0.5), hessian =
F)$estimate
}
log_likely
87.80200 82.04497 86.52106 76.66969 82.42672 87.34329 85.94124
89.22911 86.45207 86.59802 85.68173 87.05560 87.40895 81.35182
90.69237 76.73423 86.36246 85.15848 85.78709 82.37347 76.26423
82.79136 80.59414 84.75302 90.50559 81.69275 78.68459 82.43801
85.09395 99.14966 83.18440 83.24894 89.55293 84.21402 94.78671
86.92653 85.56395 85.78709 81.05172 78.97228 90.81835 88.78283
80.99960 90.49430 86.36246 93.50132 80.31775 86.48024 79.62460
81.98043 83.41997 79.15460 85.88800 90.21791 88.16664 82.73812
82.80378 86.25710 81.18192 86.95024 84.40080 86.88571 84.68848
81.28728 87.44532 86.30921 78.76155 82.26811 85.99781 80.76404
85.49941 86.28217 78.56681 86.25710 90.06376 96.06765 84.68848
86.30589 92.36634 89.12375 87.73746 83.30219 83.12783 82.55579
74.94247 78.52599 87.83836 82.09821 89.42386 77.54516 78.93145
83.87755 86.89813 79.25996 89.57801 85.27627 79.89986 84.31938
81.98043 85.85163
lambda_est
2.239999 1.799999 2.139999 1.979999 2.059999 1.799999 1.959999
2.099999 2.039999 1.959999 1.920000 2.159999 2.059999 1.920000
2.139999 1.739999 1.839999 1.999999 2.099999 1.920000 1.819999
1.900000 1.979999 1.859999 1.999999 1.999999 1.839999 1.659999
2.139999 2.459999 2.039999 1.719999 2.119998 1.920000 2.179999
2.219999 2.159999 2.119998 1.799999 1.839999 2.139999 1.939999
1.999999 2.159999 1.880000 2.179999 1.880000 2.099999 1.659999
1.900000 2.259999 1.799999 1.900000 1.999999 2.119998 1.920000
2.139999 1.880000 2.059999 1.900000 2.039999 2.139999 1.979999
1.799999 2.179999 1.739999 1.920000 2.039999 1.979999 1.859999
2.059999 2.339999 1.939999 1.999999 2.059999 2.139999 2.059999
1.920000 2.039999 2.119998 2.279999 1.999999 2.079999 1.699999
1.699999 1.759999 1.939999 2.079999 2.119998 1.819999 1.719999
1.959999 2.159999 1.799999 2.259999 2.159999 1.479999 1.799999
2.019999 1.939999