http://blog.csdn.net/flyingworm_eley/article/details/6517853 Newton-Raphson算法在统计中广泛应用于求解MLE的参数估计。
对应的单变量如下图:
多元函数算法:
Example:(implemented in R)
#定义函数f(x)
f=function(x){
1/x+1/(1-x)
}
#定义f_d1为一阶导函数
f_d1=function(x){
-1/x^2+1/(x-1)^2
}
#定义f_d2为二阶导函数
f_d2=function(x){
2/x^3-2/(x-1)^3
}
#NR算法
NR=function(time,init){
X=NULL
D1=NULL #储存Xi一阶导函数值
D2=NULL #储存Xi二阶导函数值
count=0
X[1]=init
l=seq(0.02,0.98,0.0002)
plot(l,f(l),pch='.')
points(X[1],f(X[1]),pch=2,col=1)
for (i in 2:time){
D1[i-1]=f_d1(X[i-1])
D2[i-1]=f_d2(X[i-1])
X[i]=X[i-1]-1/(D2[i-1])*(D1[i-1]) #NR算法迭代式
if (abs(D1[i-1])<0.05)break
points(X[i],f(X[i]),pch=2,col=i)
count=count+1
}
return(list(x=X,Deriviative_1=D,deriviative2=D2,count))
}
o=NR(30,0.9)
结果如下图:图中不同颜色的三角形表示i次迭代产生的估计值Xi
o=NR(30,0.9)
#另取函数f(x)
f=function(x){
return(exp(3.5*cos(x))+4*sin(x))
}
f_d1=function(x){
return(-3.5*exp(3.5*cos(x))*sin(x)+4*cos(x))
}
f_d2=function(x){
return(-4*sin(x)+3.5^2*exp(3.5*cos(x))*(sin(x))^2-3.5*exp(3.5*cos(x))*cos(x))
}
得到结果如下:
Reference from:
Kevin Quinn
Assistant Professor
Univ Washington