2.5 E,F,G Weiqing Gao
Dec 13, 2015
2.5 E,F,G
Weiqing Gao
Declare your functions#b1,b2,N forms a matrix, then each function will automatically return the sum
oil<-read.table("/Users/weiqg/Downloads/datasets 2/oilspills.dat", header=TRUE)
l<-function(k){ p<-sum(-(k[1]*oil$importexport+k[2]*oil$domestic)+oil$spills*log(k[1]*oil$importexport+k[2]*oil$domestic)-log(factorial(oil$spills))) return(p)}dlda1<-function(k){ p<-sum(-oil$importexport+oil$spills*oil$importexport/(k[1]*oil$importexport+k[2]*oil$domestic)) return(p)}dlda2<-function(k){ p<-sum(-oil$domestic+oil$spills*oil$domestic/(k[1]*oil$importexport+k[2]*oil$domestic)) return(p)}d2lda1da1<-function(k){ p<-sum(oil$spills*(-oil$importexport^2/(k[1]*oil$importexport+k[2]*oil$domestic)^2)) return(p)}d2lda2da2<-function(k){ p<-sum(oil$spills*(-oil$domestic^2/(k[1]*oil$importexport+k[2]*oil$domestic)^2)) return(p)}d2lda1da2<-function(k){ p<-sum(oil$spills*oil$domestic*oil$importexport*(-1/(k[1]*oil$importexport+k[2]*oil$domestic)^2)) return(p)}
Ascent Algorithm
We usually update our parameters using:
We are using M(t) to approximate the complex g’’(x(t))• M(t) is always a positive definite matrix• M(t)=-I (identity matrix) is usually a simple and popular choice
Ascent with No Back-Trackingm<-matrix(c(1,0,0,1), nrow=2) #identity matrix
diff<-4iter<-0t<-matrix(c(1,1))#initial
while((diff>0.001)&&(iter<30)) { oldt<-t t<-t+solve(m)%*%matrix(c(dlda1(t),dlda2(t)), nrow=2) diff<-sum((t-oldt)^2) iter<-iter+1 print(c(iter,diff)) }
l(t)t
[1] 1.000000 1.388281[1] 2.0000 193.8244[1] 3.000 1307.505[1] 4.000 1094.796[1] 5.000 1068.684[1] 6.000 1057.952[1] 7.000 1052.072[1] 8.000 1048.353[1] 9.000 1045.787[1] 10.000 1043.908[1] 11.000 1042.473[1] 12.000 1041.341[1] 13.000 1040.425[1] 14.000 1039.668[1] 15.000 1039.033[1] 16.000 1038.492[1] 17.000 1038.025[1] 18.000 1037.618[1] 19.000 1037.261[1] 20.000 1036.945[1] 21.000 1036.662[1] 22.000 1036.409[1] 23.00 1036.18[1] 24.000 1035.973[1] 25.000 1035.784[1] 26.000 1035.611[1] 27.000 1035.452[1] 28.000 1035.306[1] 29.000 1035.171[1] 30.000 1035.045> > l(t)[1] NaNWarning message:In log(k[1] * oil$importexport + k[2] * oil$domestic) : NaNs produced> t [,1][1,] -767.5518[2,] -506.3151
Ascent with No Back-tracking: Results
Why should we use back tracking
• When alpha is sufficiently small-ascent condition CAN BE ASSURED– Steps always go up-hill– As proved in class
• We will update using• We will make sure uphill condition is fulfilled
by:
Ascent Algorithm with Back Tracking
m<-diag(-1,2,2)
diff<-4iter<-0alpha<-1t<-matrix(c(1,1))
while((diff>0.001)&&(iter<30)) { newt<-t-alpha*solve(m)%*%matrix(c(dlda1(t),dlda2(t)), nrow=2) while(l(newt)<l(t)) { alpha<-alpha/2 newt<-t-alpha*solve(m)%*%matrix(c(dlda1(t),dlda2(t)), nrow=2) }
diff<-sum((t-newt)^2) t<-newt print(c(iter,diff)) alpha=1 iter=iter+1}
l(t)t
Ascent with Back Tracking: Results
[1] 0.000000000 0.005422972[1] 1.000000000 0.001161431[1] 2.000000000 0.001121136[1] 3.0000000000 0.0005545932> > l(t)[1] -48.02997> t [,1][1,] 1.0681510[2,] 0.9717389
Quasi Newton Algorithm
We will update our parameters use:
We will update our M matrix every time using:
Quasi Newtondiff<-4iter<-0t<-matrix(c(1,1))m<-matrix(c(d2lda1da1(t), d2lda1da2(t), d2lda1da2(t), d2lda2da2(t)), nrow=2)
while((diff>0.001)&&(iter<30)) { oldt<-t oldm<-m t<-t-solve(m)%*%matrix(c(dlda1(t),dlda2(t)), nrow=2) y<-matrix(c(dlda1(t),dlda2(t)))-matrix(c(dlda1(oldt),dlda2(oldt))) z<-t-oldt v<-y-oldm%*%z m<-oldm+as.numeric(1/(t(v)%*%z))*v%*%t(v)
diff<-sum((t-oldt)^2) iter<-iter+1 print(c(iter,diff))}
l(t)t
Quasi Newton Result
[1] 1.00000000 0.01153638[1] 2.000000e+00 5.792689e-05> > l(t)[1] -48.02716> t [,1][1,] 1.0967545[2,] 0.9378965
Quasi Newton with Back-trackingdiff<-4iter<-0alpha<-1t<-matrix(c(1,1))m<-matrix(c(d2lda1da1(t), d2lda1da2(t), d2lda1da2(t), d2lda2da2(t)), nrow=2)
while((diff>0.0001)&&(iter<30)) { oldm<-m newt<-t-alpha*solve(m)%*%matrix(c(dlda1(t),dlda2(t)), nrow=2)
while(l(newt)<l(t)) { alpha<-alpha/2 newt<-t-alpha*solve(m)%*%matrix(c(dlda1(t),dlda2(t)), nrow=2) } diff<-sum((t-newt)^2) t<-newt y<-matrix(c(dlda1(t),dlda2(t)))-matrix(c(dlda1(oldt),dlda2(oldt))) z<-t-oldt v<-y-oldm%*%z m<-oldm+as.numeric(1/(t(v)%*%z))*v%*%t(v) alpha<-1 iter<-iter+1 print(c(iter,diff))}
l(t)t
Quasi Newton with Back-tracking: Result
[1] 1.0000000000 0.0007210235[1] 2.00000000 0.00778564[1] 3.000000e+00 1.646971e-07> > l(t)[1] -48.02716> t [,1][1,] 1.0971512[2,] 0.9375561
Plottingx1_2.max = max(1.1,ceiling(max(t.values[1,])))x1_2.min = min(0.9,floor(min(t.values[1,])))x2_2.max = max(1.1,ceiling(max(t.values[2,])))x2_2.min = min(0.9,floor(min(t.values[2,])))x1_2 = seq(x1_2.min,x1_2.max,length=100)x2_2 = seq(x2_2.min,x2_2.max,length=100)z2 = matrix(0,100,100)
for(i in 1:100){ for(j in 1:100){ z2[i,j] = l(c(x1_2[i],x2_2[j])) }}
contour(x1_2,x2_2,z2,nlevels=15,drawlabels=FALSE)for(i in 1:iter){ segments(t.values[1,i],t.values[2,i],t.values[1,i+1], t.values[2,i+1],lty=2,col='pink')}
Plotting Result
Newton-Raphson Fisher Scoring Ascent Algorithm
Ascent Algorithm with Back-tracking
Quasi Newton Quasi Newton with Back-tracking