STA 410/2102, Fall 2015, Script for Assignment #1.
> source("ass1-funs.r")
>
> options(digits=17)
Set the sample sizes and observed counts for the data in the assignment handout.
> n <- 130
> m1 <- 25
> m2 <- 25
>
> x <- 75
> x1 <- 20
> x2 <- 6
Create a contour plot of the log likelihood function, for reference when discussing the results.
> log_likelihood_contour <- function (n,m1,m2,x,x1,x2, ...)
+ {
+ grid <- seq(0.01, 0.99, by=0.01)
+ ll <- matrix(nrow=length(grid),ncol=length(grid))
+ for (i in 1:length(grid))
+ for (j in 1:length(grid))
+ ll[i,j] <- log_likelihood(c(grid[i],grid[j]),n,m1,m2,x,x1,x2)
+ contour (grid, grid, ll, ...)
+ }
>
> log_likelihood_contour(n,m1,m2,x,x1,x2,nlevels=200)
Try the methods with various initial values, to see how sensitive they are to initial values. Does only a few iterations for Newton and method of scoring, just to see if they are converging or diverging.
> init_list <- list (c(x1/m1,x2/m2),
+ c(0.5,0.5),
+ c(0.1,0.1),
+ c(0.05,0.95))
>
> for (init in init_list) {
+ cat ("\nTRYING INITIAL VALUE",init,"\n\n")
+ cat("\nAlternating maximization\n\n")
+ p_alt <- mle_alt (n,m1,m2,x,x1,x2,init)
+ cat("\nMultivariate Newton iteration\n")
+ p_mvn <- mle_mvn (n,m1,m2,x,x1,x2,init,7)
+ cat("\nMethod of scoring\n")
+ p_mos <- mle_mos (n,m1,m2,x,x1,x2,init,7)
+ cat("\nUsing nlm\n\n")
+ p_nlm <- mle_nlm (n,m1,m2,x,x1,x2,init)
+ print(p_nlm)
+ }
TRYING INITIAL VALUE 0.80000000000000004 0.23999999999999999
Alternating maximization
i = 1 p1 = 0.84615973276776923 p2 = 0.27469089013085279
i = 2 p1 = 0.83341349646038121 p2 = 0.28149745269102089
i = 3 p1 = 0.83077805762026391 p2 = 0.28291450844583144
i = 4 p1 = 0.83022377614011722 p2 = 0.28321295545116498
i = 5 p1 = 0.83010679171269075 p2 = 0.28327596277724615
i = 6 p1 = 0.83008208329244559 p2 = 0.28328927144453253
i = 7 p1 = 0.83007686378814238 p2 = 0.28329208285633756
i = 8 p1 = 0.83007576116331827 p2 = 0.28329267677114889
i = 9 p1 = 0.8300755282312533 p2 = 0.28329280223711006
i = 10 p1 = 0.83007547902373968 p2 = 0.28329282874212869
i = 11 p1 = 0.83007546862851922 p2 = 0.28329283434138597
i = 12 p1 = 0.83007546643250052 p2 = 0.28329283552424422
i = 13 p1 = 0.83007546596858561 p2 = 0.28329283577412634
i = 14 p1 = 0.83007546587058245 p2 = 0.28329283582691445
i = 15 p1 = 0.83007546584987901 p2 = 0.28329283583806608
i = 16 p1 = 0.8300754658455054 p2 = 0.28329283584042197
i = 17 p1 = 0.83007546584458147 p2 = 0.28329283584091958
i = 18 p1 = 0.8300754658443863 p2 = 0.28329283584102466
i = 19 p1 = 0.830075465844345 p2 = 0.28329283584104697
i = 20 p1 = 0.83007546584433611 p2 = 0.28329283584105169
i = 21 p1 = 0.83007546584433434 p2 = 0.28329283584105264
i = 22 p1 = 0.83007546584433411 p2 = 0.2832928358410528
i = 23 p1 = 0.83007546584433389 p2 = 0.28329283584105291
i = 24 p1 = 0.83007546584433389 p2 = 0.28329283584105291
Multivariate Newton iteration
At iteration 1 :
x = 0.80000000000000004 0.23999999999999999
f(x) = 14.823717948717935 14.823717948717942
At iteration 2 :
x = 0.83428607068977545 0.27908612058634408
f(x) = -0.86976160891783039 0.47342823773695031
At iteration 3 :
x = 0.83015086405183069 0.28323651182752291
f(x) = -0.017738990011661571 0.003806334172718806
At iteration 4 :
x = 0.83007548857122737 0.28329282085591129
f(x) = -5.6052169519205108e-06 6.6454701297402607e-07
At iteration 5 :
x = 0.83007546584433589 0.28329283584105158
f(x) = -4.9382720135326963e-13 6.0396132539608516e-14
At iteration 6 :
x = 0.83007546584433389 0.28329283584105291
f(x) = 3.5527136788005009e-15 3.5527136788005009e-15
At iteration 7 :
x = 0.83007546584433389 0.28329283584105291
f(x) = 3.5527136788005009e-15 3.5527136788005009e-15
Method of scoring
At iteration 1 :
x = 0.80000000000000004 0.23999999999999999
f(x) = 14.823717948717935 14.823717948717942
At iteration 2 :
x = 0.83408567480423768 0.27885766927683092
f(x) = -0.77161236262138999 0.55533650956522607
At iteration 3 :
x = 0.82976324803289614 0.28325032333335248
f(x) = 0.10930969254761891 0.051029116387525164
At iteration 4 :
x = 0.83010794430802637 0.28327245194968109
f(x) = -0.0081458386823385354 0.00070004080751928655
At iteration 5 :
x = 0.83007262893246225 0.28329344974897508
f(x) = 0.00086362013892227196 0.00022142662407276248
At iteration 6 :
x = 0.83007573922780453 0.2832927112886568
f(x) = -7.4693360510025286e-05 -5.4989056863519181e-06
At iteration 7 :
x = 0.83007544093492192 0.28329284386753745
f(x) = 7.2391330618870597e-06 1.3057199481636417e-06
Using nlm
Warning: NaNs produced```
Warning: NaNs produced“
Warning: NaNs produced```
Warning: NA/Inf replaced by maximum positive value”
Warning: NaNs produced```
Warning: NaNs produced“
Warning: NaNs produced```
Warning: NA/Inf replaced by maximum positive value”
[1] 0.83007505464909737 0.28329257092751337
TRYING INITIAL VALUE 0.5 0.5
Alternating maximization
i = 1 p1 = 0.72361886741069914 p2 = 0.34283701825511093
i = 2 p1 = 0.80500792288627254 p2 = 0.29693581283413562
i = 3 p1 = 0.8246347826591256 p2 = 0.28623022422249378
i = 4 p1 = 0.82891926397693649 p2 = 0.28391592022870082
i = 5 p1 = 0.8298309059909057 p2 = 0.28342457889857198
i = 6 p1 = 0.83002378808147925 p2 = 0.28332067205126765
i = 7 p1 = 0.83006454815877273 p2 = 0.28329871654530048
i = 8 p1 = 0.83007315942597226 p2 = 0.28329407816606073
i = 9 p1 = 0.8300749786059447 p2 = 0.28329309828604987
i = 10 p1 = 0.83007536291384254 p2 = 0.28329289128329349
i = 11 p1 = 0.8300754440999849 p2 = 0.28329284755337814
i = 12 p1 = 0.8300754612507808 p2 = 0.2832928383153136
i = 13 p1 = 0.83007546487393324 p2 = 0.2832928363637473
i = 14 p1 = 0.8300754656393341 p2 = 0.28329283595147359
i = 15 p1 = 0.8300754658010272 p2 = 0.28329283586437948
i = 16 p1 = 0.83007546583518521 p2 = 0.28329283584598081
i = 17 p1 = 0.83007546584240122 p2 = 0.28329283584209397
i = 18 p1 = 0.83007546584392555 p2 = 0.28329283584127285
i = 19 p1 = 0.83007546584424774 p2 = 0.28329283584109921
i = 20 p1 = 0.83007546584431569 p2 = 0.2832928358410628
i = 21 p1 = 0.83007546584433001 p2 = 0.28329283584105502
i = 22 p1 = 0.830075465844333 p2 = 0.28329283584105347
i = 23 p1 = 0.83007546584433367 p2 = 0.28329283584105303
i = 24 p1 = 0.83007546584433389 p2 = 0.28329283584105291
i = 25 p1 = 0.83007546584433389 p2 = 0.28329283584105291
Multivariate Newton iteration
At iteration 1 :
x = 0.5 0.5
f(x) = 50 -6
At iteration 2 :
x = 0.84111111111111114 0.28111111111111109
f(x) = -3.517004814101913 -0.91234658703651661
At iteration 3 :
x = 0.83056113245492258 0.28303627802419079
f(x) = -0.12832273494266744 -0.0012082060053444366
At iteration 4 :
x = 0.83007638334334566 0.28329228937802359
f(x) = -0.00023391889409296596 1.2659267238035454e-05
At iteration 5 :
x = 0.83007546584762726 0.2832928358390312
f(x) = -8.3179685361756128e-10 6.0019544889655663e-11
At iteration 6 :
x = 0.83007546584433389 0.28329283584105291
f(x) = 3.5527136788005009e-15 3.5527136788005009e-15
At iteration 7 :
x = 0.83007546584433389 0.28329283584105291
f(x) = 3.5527136788005009e-15 3.5527136788005009e-15
Method of scoring
At iteration 1 :
x = 0.5 0.5
f(x) = 50 -6
At iteration 2 :
x = 0.84111111111111114 0.28111111111111109
f(x) = -3.517004814101913 -0.91234658703651661
At iteration 3 :
x = 0.8289713554946514 0.28380883458143547
f(x) = 0.29877051723324044 0.019114575240173792
At iteration 4 :
x = 0.83017542691563551 0.28326133479363469
f(x) = -0.029152997816730419 -0.0054114966262375219
At iteration 5 :
x = 0.83006604664825401 0.28329662579869042
f(x) = 0.0026388212825452229 0.00031090853885373804
At iteration 6 :
x = 0.83007633505968859 0.28329252881786304
f(x) = -0.00024909611053303138 -3.9038135117408501e-05
At iteration 7 :
x = 0.83007538469141307 0.28329286677222643
f(x) = 2.296069085971908e-05 3.095761361038285e-06
Using nlm
Warning: NaNs produced```
Warning: NaNs produced“
Warning: NaNs produced```
Warning: NA/Inf replaced by maximum positive value”
Warning: NaNs produced```
Warning: NaNs produced“
Warning: NaNs produced```
Warning: NA/Inf replaced by maximum positive value”
[1] 0.83007508394180418 0.28329254230349477
TRYING INITIAL VALUE 0.10000000000000001 0.10000000000000001
Alternating maximization
i = 1 p1 = 0.88698866375147856 p2 = 0.25345364104794316
i = 2 p1 = 0.84135152056808926 p2 = 0.27724921503290234
i = 3 p1 = 0.83242816353323779 p2 = 0.28202687686109884
i = 4 p1 = 0.83057119984760441 p2 = 0.28302587207024621
i = 5 p1 = 0.83018013418394432 p2 = 0.28323646008211723
i = 6 p1 = 0.83009757478318114 p2 = 0.28328092724790055
i = 7 p1 = 0.83008013630485622 p2 = 0.28329032016024114
i = 8 p1 = 0.83007645248679363 p2 = 0.28329230439850195
i = 9 p1 = 0.83007567427501505 p2 = 0.28329272357245039
i = 10 p1 = 0.83007550987587275 p2 = 0.2832928121240087
i = 11 p1 = 0.8300754751461159 p2 = 0.28329283083076295
i = 12 p1 = 0.8300754678093607 p2 = 0.28329283478261547
i = 13 p1 = 0.83007546625945117 p2 = 0.28329283561745511
i = 14 p1 = 0.83007546593202863 p2 = 0.28329283579381725
i = 15 p1 = 0.83007546586285974 p2 = 0.28329283583107412
i = 16 p1 = 0.83007546584824765 p2 = 0.28329283583894477
i = 17 p1 = 0.83007546584516079 p2 = 0.2832928358406076
i = 18 p1 = 0.83007546584450853 p2 = 0.28329283584095888
i = 19 p1 = 0.83007546584437075 p2 = 0.28329283584103304
i = 20 p1 = 0.83007546584434166 p2 = 0.28329283584104881
i = 21 p1 = 0.83007546584433545 p2 = 0.28329283584105214
i = 22 p1 = 0.83007546584433411 p2 = 0.2832928358410528
i = 23 p1 = 0.83007546584433389 p2 = 0.28329283584105291
i = 24 p1 = 0.83007546584433389 p2 = 0.28329283584105291
Multivariate Newton iteration
At iteration 1 :
x = 0.10000000000000001 0.10000000000000001
f(x) = 538.88888888888891 383.33333333333337
At iteration 2 :
x = 0.20123443945310113 0.17624943388374117
f(x) = 257.9129237804741 175.7634442457915
At iteration 3 :
x = 0.40258807888954373 0.26159347960293122
f(x) = 113.05680635732676 68.952938877867297
At iteration 4 :
x = 0.73152747021928377 0.27052930871859071
f(x) = 28.44887614103823 15.865162259158641
At iteration 5 :
x = 0.85096424846355778 0.27193479839869461
f(x) = -5.9614253413840856 0.052370508864541421
At iteration 6 :
x = 0.8319220987559095 0.28220438995308733
f(x) = -0.47569953879673221 0.022953873836428329
At iteration 7 :
x = 0.83008889943228614 0.28328462469519311
f(x) = -0.0033976859600350906 0.00023625198214816123
Method of scoring
At iteration 1 :
x = 0.10000000000000001 0.10000000000000001
f(x) = 538.88888888888891 383.33333333333337
At iteration 2 :
x = 0.84111111111111103 0.2811111111111112
f(x) = -3.5170048141018881 -0.91234658703652727
At iteration 3 :
x = 0.8289713554946514 0.28380883458143552
f(x) = 0.29877051723321202 0.019114575240138265
At iteration 4 :
x = 0.83017542691563551 0.28326133479363463
f(x) = -0.029152997816730419 -0.0054114966262304165
At iteration 5 :
x = 0.83006604664825401 0.28329662579869036
f(x) = 0.0026388212825452229 0.00031090853885729075
At iteration 6 :
x = 0.83007633505968859 0.28329252881786299
f(x) = -0.00024909611053303138 -3.9038135110303074e-05
At iteration 7 :
x = 0.83007538469141295 0.28329286677222637
f(x) = 2.2960690913009785e-05 3.0957614001181355e-06
Using nlm
Warning: NaNs produced```
Warning: NaNs produced“
Warning: NaNs produced```
Warning: NA/Inf replaced by maximum positive value”
Warning: NaNs produced```
Warning: NaNs produced“
Warning: NaNs produced```
Warning: NA/Inf replaced by maximum positive value”
Warning: NaNs produced```
Warning: NaNs produced“
Warning: NaNs produced```
Warning: NA/Inf replaced by maximum positive value”
[1] 0.83007508012498665 0.28329254323677638
TRYING INITIAL VALUE 0.050000000000000003 0.94999999999999996
Alternating maximization
i = 1 p1 = 0.44104245811033771 p2 = 0.5075183971287609
i = 2 p1 = 0.71926431434545268 p2 = 0.34534691343672519
i = 3 p1 = 0.80387631705391649 p2 = 0.2975580468754252
i = 4 p1 = 0.82438234696517454 p2 = 0.28636684136015877
i = 5 p1 = 0.8288652866082189 p2 = 0.28394502411505818
i = 6 p1 = 0.82981947359459074 p2 = 0.28343073814853559
i = 7 p1 = 0.83002137163359424 p2 = 0.28332197370055023
i = 8 p1 = 0.83006403761848424 p2 = 0.28329899154417715
i = 9 p1 = 0.83007305157030853 p2 = 0.28329413626130051
i = 10 p1 = 0.83007495582102431 p2 = 0.28329311055887108
i = 11 p1 = 0.83007535810046085 p2 = 0.28329289387596235
i = 12 p1 = 0.83007544308314474 p2 = 0.28329284810108646
i = 13 p1 = 0.83007546103597063 p2 = 0.28329283843101838
i = 14 p1 = 0.83007546482855399 p2 = 0.28329283638819025
i = 15 p1 = 0.83007546562974754 p2 = 0.28329283595663723
i = 16 p1 = 0.83007546579900193 p2 = 0.28329283586547038
i = 17 p1 = 0.83007546583475733 p2 = 0.28329283584621118
i = 18 p1 = 0.83007546584231084 p2 = 0.2832928358421426
i = 19 p1 = 0.83007546584390646 p2 = 0.28329283584128306
i = 20 p1 = 0.83007546584424352 p2 = 0.28329283584110165
i = 21 p1 = 0.8300754658443148 p2 = 0.28329283584106324
i = 22 p1 = 0.8300754658443299 p2 = 0.28329283584105502
i = 23 p1 = 0.830075465844333 p2 = 0.28329283584105347
i = 24 p1 = 0.83007546584433367 p2 = 0.28329283584105303
i = 25 p1 = 0.83007546584433389 p2 = 0.28329283584105291
i = 26 p1 = 0.83007546584433389 p2 = 0.28329283584105291
Multivariate Newton iteration
At iteration 1 :
x = 0.050000000000000003 0.94999999999999996
f(x) = 414.73684210526318 -353.68421052631544
At iteration 2 :
x = 0.10172278955049116 0.90341546314672949
f(x) = 210.37910275163478 -170.74486002366203
At iteration 3 :
x = 0.20874160952736437 0.81848257195160867
f(x) = 105.96625150288438 -80.869440436666977
At iteration 4 :
x = 0.41924489962667999 0.66730112387863272
f(x) = 47.910376614455153 -39.302212598514281
At iteration 5 :
x = 0.73216430317339853 0.41359783600139088
f(x) = 9.7218468116646442 -16.820390978866168
At iteration 6 :
x = 0.85488475458952362 0.26513936306492381
f(x) = -6.5992765445826933 1.2354968347209336
At iteration 7 :
x = 0.83277308542574047 0.28156286637384897
f(x) = -0.67898586187908094 0.067695103486204999
Method of scoring
At iteration 1 :
x = 0.050000000000000003 0.94999999999999996
f(x) = 414.73684210526318 -353.68421052631544
At iteration 2 :
x = 0.81882195448460515 0.2588219544846051
f(x) = 6.7945465231664244 7.5134679678775278
At iteration 3 :
x = 0.8317867227386222 0.28110307976884369
f(x) = -0.28648914754666777 0.30815852163750534
At iteration 4 :
x = 0.82994983428043534 0.28325680060490549
f(x) = 0.046477731833590497 0.02511896757659926
At iteration 5 :
x = 0.83008895684027195 0.28328347849333491
f(x) = -0.0032672416353811684 0.00050639825345299982
At iteration 6 :
x = 0.83007430706165708 0.2832930366953782
f(x) = 0.00035927278398872886 0.00010253355080891424
At iteration 7 :
x = 0.83007557860864156 0.28329278193065172
f(x) = -3.0478457286875482e-05 -1.6540392699937456e-06
Using nlm
Warning: NaNs produced```
Warning: NaNs produced“
Warning: NaNs produced```
Warning: NA/Inf replaced by maximum positive value”
Warning: NaNs produced```
Warning: NaNs produced“
Warning: NaNs produced```
Warning: NA/Inf replaced by maximum positive value”
Warning: NaNs produced```
Warning: NaNs produced“
Warning: NA/Inf replaced by maximum positive value```
[1] 0.83007508052643253 0.28329254347670729
Use the run with the first initial value above to get precise estimates.
```r
> init <- init_list[[1]]
> p_alt <- mle_alt (n,m1,m2,x,x1,x2,init)
i = 1 p1 = 0.84615973276776923 p2 = 0.27469089013085279
i = 2 p1 = 0.83341349646038121 p2 = 0.28149745269102089
i = 3 p1 = 0.83077805762026391 p2 = 0.28291450844583144
i = 4 p1 = 0.83022377614011722 p2 = 0.28321295545116498
i = 5 p1 = 0.83010679171269075 p2 = 0.28327596277724615
i = 6 p1 = 0.83008208329244559 p2 = 0.28328927144453253
i = 7 p1 = 0.83007686378814238 p2 = 0.28329208285633756
i = 8 p1 = 0.83007576116331827 p2 = 0.28329267677114889
i = 9 p1 = 0.8300755282312533 p2 = 0.28329280223711006
i = 10 p1 = 0.83007547902373968 p2 = 0.28329282874212869
i = 11 p1 = 0.83007546862851922 p2 = 0.28329283434138597
i = 12 p1 = 0.83007546643250052 p2 = 0.28329283552424422
i = 13 p1 = 0.83007546596858561 p2 = 0.28329283577412634
i = 14 p1 = 0.83007546587058245 p2 = 0.28329283582691445
i = 15 p1 = 0.83007546584987901 p2 = 0.28329283583806608
i = 16 p1 = 0.8300754658455054 p2 = 0.28329283584042197
i = 17 p1 = 0.83007546584458147 p2 = 0.28329283584091958
i = 18 p1 = 0.8300754658443863 p2 = 0.28329283584102466
i = 19 p1 = 0.830075465844345 p2 = 0.28329283584104697
i = 20 p1 = 0.83007546584433611 p2 = 0.28329283584105169
i = 21 p1 = 0.83007546584433434 p2 = 0.28329283584105264
i = 22 p1 = 0.83007546584433411 p2 = 0.2832928358410528
i = 23 p1 = 0.83007546584433389 p2 = 0.28329283584105291
i = 24 p1 = 0.83007546584433389 p2 = 0.28329283584105291
> p_mvn <- mle_mvn (n,m1,m2,x,x1,x2,init,7)
At iteration 1 :
x = 0.80000000000000004 0.23999999999999999
f(x) = 14.823717948717935 14.823717948717942
At iteration 2 :
x = 0.83428607068977545 0.27908612058634408
f(x) = -0.86976160891783039 0.47342823773695031
At iteration 3 :
x = 0.83015086405183069 0.28323651182752291
f(x) = -0.017738990011661571 0.003806334172718806
At iteration 4 :
x = 0.83007548857122737 0.28329282085591129
f(x) = -5.6052169519205108e-06 6.6454701297402607e-07
At iteration 5 :
x = 0.83007546584433589 0.28329283584105158
f(x) = -4.9382720135326963e-13 6.0396132539608516e-14
At iteration 6 :
x = 0.83007546584433389 0.28329283584105291
f(x) = 3.5527136788005009e-15 3.5527136788005009e-15
At iteration 7 :
x = 0.83007546584433389 0.28329283584105291
f(x) = 3.5527136788005009e-15 3.5527136788005009e-15
> p_mos <- mle_mos (n,m1,m2,x,x1,x2,init,17)
At iteration 1 :
x = 0.80000000000000004 0.23999999999999999
f(x) = 14.823717948717935 14.823717948717942
At iteration 2 :
x = 0.83408567480423768 0.27885766927683092
f(x) = -0.77161236262138999 0.55533650956522607
At iteration 3 :
x = 0.82976324803289614 0.28325032333335248
f(x) = 0.10930969254761891 0.051029116387525164
At iteration 4 :
x = 0.83010794430802637 0.28327245194968109
f(x) = -0.0081458386823385354 0.00070004080751928655
At iteration 5 :
x = 0.83007262893246225 0.28329344974897508
f(x) = 0.00086362013892227196 0.00022142662407276248
At iteration 6 :
x = 0.83007573922780453 0.2832927112886568
f(x) = -7.4693360510025286e-05 -5.4989056863519181e-06
At iteration 7 :
x = 0.83007544093492192 0.28329284386753745
f(x) = 7.2391330618870597e-06 1.3057199481636417e-06
At iteration 8 :
x = 0.83007546818690647 0.28329283490770762
f(x) = -6.5750548827736566e-07 -7.9557398890983677e-08
At iteration 9 :
x = 0.83007546562794932 0.28329283591797366
f(x) = 6.1946515472754982e-08 9.5996455229396815e-09
At iteration 10 :
x = 0.83007546586452552 0.28329283583338261
f(x) = -5.716191964211248e-09 -7.7647754892495868e-10
At iteration 11 :
x = 0.83007546584246061 0.2832928358417387
f(x) = 5.3369930697044765e-10 7.829825676708424e-11
At iteration 12 :
x = 0.83007546584450831 0.28329283584098774
f(x) = -4.9514170541442581e-11 -6.9704242378065828e-12
At iteration 13 :
x = 0.83007546584431768 0.28329283584105891
f(x) = 4.6043169277254492e-12 6.5014660322049167e-13
At iteration 14 :
x = 0.83007546584433545 0.2832928358410523
f(x) = -4.2987835513486061e-13 -4.9737991503207013e-14
At iteration 15 :
x = 0.83007546584433378 0.28329283584105297
f(x) = 2.8421709430404007e-14 -3.5527136788005009e-15
At iteration 16 :
x = 0.83007546584433389 0.28329283584105291
f(x) = 3.5527136788005009e-15 3.5527136788005009e-15
At iteration 17 :
x = 0.83007546584433389 0.28329283584105291
f(x) = 3.5527136788005009e-15 3.5527136788005009e-15
> p_nlm <- mle_nlm (n,m1,m2,x,x1,x2,init)
Warning: NaNs produced```
Warning: NaNs produced”
Warning: NaNs produced```
Warning: NA/Inf replaced by maximum positive value“
Warning: NaNs produced```
Warning: NaNs produced”
Warning: NaNs produced```
Warning: NA/Inf replaced by maximum positive value“
>
> print (rbind (alt=p_alt,mvn=p_mvn,mos=p_mos,nlm=p_nlm))
[,1] [,2]
alt 0.83007546584433389 0.28329283584105291
mvn 0.83007546584433389 0.28329283584105291
mos 0.83007546584433389 0.28329283584105291
nlm 0.83007505464909737 0.28329257092751337
>
> print (rbind (alt_log_likilhood=log_likelihood(p_alt,n,m1,m2,x,x1,x2),
+ mvn_log_likilhood=log_likelihood(p_mvn,n,m1,m2,x,x1,x2),
+ mos_log_likilhood=log_likelihood(p_mos,n,m1,m2,x,x1,x2),
+ nlm_log_likilhood=log_likelihood(p_nlm,n,m1,m2,x,x1,x2)))
[,1]
alt_log_likilhood -6.2759460142732166
mvn_log_likilhood -6.2759460142732166
mos_log_likilhood -6.2759460142732166
nlm_log_likilhood -6.2759460143240515
Find standard errors using the observed and Fisher information.
> print (observed_inf <- -log_likelihood_hessian(p_alt,n,m1,m2,x,x1,x2))
[,1] [,2]
[1,] 332.65877470916689 130.46817610698540
[2,] 130.46817610698540 242.21881950015884
> print (fisher_inf <- fisher_information(p_alt,n,m1,m2,x,x1,x2))
[,1] [,2]
[1,] 308.93443970601913 131.6925617707314
[2,] 131.69256177073140 254.8222191948708
>
> print (sqrt (diag (solve (observed_inf))))
[1] 0.061735016955748602 0.072348098624353513
> print (sqrt (diag (solve (fisher_inf))))
[1] 0.064432307981021203 0.070944413745480592
Try another data set in which the difference in standard errors obtained in these two ways might be bigger.
> x <- 125
> x1 <- 5
> x2 <- 15
>
> print (p_alt <- mle_alt (n,m1,m2,x,x1,x2,c(0.5,0.5)))
i = 1 p1 = 0.79172293290784024 p2 = 0.87107812393584161
i = 2 p1 = 0.72530400681384943 p2 = 0.87861532261203212
i = 3 p1 = 0.7235953358617272 p2 = 0.87879152938305705
i = 4 p1 = 0.72355509282460395 p2 = 0.87879567050128737
i = 5 p1 = 0.72355414688849606 p2 = 0.87879576783576019
i = 6 p1 = 0.72355412465474989 p2 = 0.8787957701235547
i = 7 p1 = 0.72355412413215747 p2 = 0.87879577017732802
i = 8 p1 = 0.72355412411987441 p2 = 0.87879577017859201
i = 9 p1 = 0.72355412411958564 p2 = 0.87879577017862165
i = 10 p1 = 0.72355412411957887 p2 = 0.87879577017862243
i = 11 p1 = 0.72355412411957865 p2 = 0.87879577017862243
i = 12 p1 = 0.72355412411957865 p2 = 0.87879577017862243
[1] 0.72355412411957865 0.87879577017862243
>
> print (observed_inf <- -log_likelihood_hessian(p_alt,n,m1,m2,x,x1,x2))
[,1] [,2]
[1,] 351.559627946642252 80.305446252556735
[2,] 80.305446252556735 780.442034448176514
> print (fisher_inf <- fisher_information(p_alt,n,m1,m2,x,x1,x2))
[,1] [,2]
[1,] 329.01098894131746 204.0257082010655
[2,] 204.02570820106550 438.7371571727540
>
> print (sqrt (diag (solve (observed_inf))))
[1] 0.053971609661859876 0.036223844651239602
> print (sqrt (diag (solve (fisher_inf))))
[1] 0.065353471974237728 0.056594165209041640
Finally, try a data set in which the difference in standard errors obtained in these two ways should be zero.
> x <- 52
> x1 <- 5
> x2 <- 15
>
> print (p_alt <- mle_alt (n,m1,m2,x,x1,x2,c(0.5,0.5)))
i = 1 p1 = 0.25103538505571177 p2 = 0.5710059338952711
i = 2 p1 = 0.21386690280515047 p2 = 0.59214923971454381
i = 3 p1 = 0.20367501173683139 p2 = 0.59792187140207931
i = 4 p1 = 0.2009669536078523 p2 = 0.59945339465785386
i = 5 p1 = 0.20025392699270611 p2 = 0.59985647145931575
i = 6 p1 = 0.20006664831513804 p2 = 0.59996232889685852
i = 7 p1 = 0.20001749084834949 p2 = 0.599990113846534
i = 8 p1 = 0.20000459004757382 p2 = 0.59999740562378356
i = 9 p1 = 0.20000120453510323 p2 = 0.59999931917570781
i = 10 p1 = 0.2000003160972193 p2 = 0.59999982133634711
i = 11 p1 = 0.2000000829509973 p2 = 0.59999995311465315
i = 12 p1 = 0.20000002176819784 p2 = 0.59999998769623586
i = 13 p1 = 0.20000000571246204 p2 = 0.59999999677121707
i = 14 p1 = 0.20000000149907776 p2 = 0.5999999991526952
i = 15 p1 = 0.20000000039339147 p2 = 0.59999999977764817
i = 16 p1 = 0.20000000010323477 p2 = 0.59999999994164988
i = 17 p1 = 0.20000000002709112 p2 = 0.59999999998468767
i = 18 p1 = 0.20000000000710932 p2 = 0.59999999999598175
i = 19 p1 = 0.20000000000186563 p2 = 0.59999999999894538
i = 20 p1 = 0.20000000000048967 p2 = 0.5999999999997232
i = 21 p1 = 0.20000000000012852 p2 = 0.59999999999992726
i = 22 p1 = 0.20000000000003376 p2 = 0.59999999999998088
i = 23 p1 = 0.20000000000000889 p2 = 0.59999999999999498
i = 24 p1 = 0.20000000000000234 p2 = 0.59999999999999876
i = 25 p1 = 0.20000000000000057 p2 = 0.59999999999999964
i = 26 p1 = 0.20000000000000018 p2 = 0.59999999999999987
i = 27 p1 = 0.20000000000000009 p2 = 0.59999999999999998
i = 28 p1 = 0.20000000000000001 p2 = 0.59999999999999998
i = 29 p1 = 0.20000000000000001 p2 = 0.59999999999999998
[1] 0.20000000000000001 0.59999999999999998
>
> print (observed_inf <- -log_likelihood_hessian(p_alt,n,m1,m2,x,x1,x2))
[,1] [,2]
[1,] 291.66666666666663 135.41666666666666
[2,] 135.41666666666666 239.58333333333331
> print (fisher_inf <- fisher_information(p_alt,n,m1,m2,x,x1,x2))
[,1] [,2]
[1,] 291.66666666666669 135.41666666666669
[2,] 135.41666666666669 239.58333333333337
>
> print (sqrt (diag (solve (observed_inf))))
[1] 0.068179330098143240 0.075225975357060354
> print (sqrt (diag (solve (fisher_inf))))
[1] 0.068179330098143226 0.075225975357060354