作者libertarian (我的暱稱)
看板Statistics
標題[問題] [R] 標準常態分佈cdf 求解
時間Sat Oct 27 21:01:52 2007
不好意思 ,實在沒什麼統計背景, 可能要讓版大們見笑了
小弟目前正用牛頓法求 f(x) = phi(x)-0.1 的解 phi(x) = 標準常態分佈的cdf
寫了一些R但只有在x的初始值與解夠近的時候 , 我的 while迴圈 才可以跑
要不然會得到錯誤訊息:錯誤在while ((target(initialX, setAlpha) != 0) && maxIter > 0) { :
需要 TRUE/FALSE 值的地方有缺值
拜托版大指點迷津 , 感恩 ~~
#code
#generate target function (phi(x)-alpha) (allow input x and alpha)
target<-function(x,alpha){
pnorm(x)-alpha
}
#generate the first derivative of the of the target function
firstDerivative<-function(x){
exp(-(x^2)/2)/sqrt(2*pi)
}
# Finding the root by Newton method
rootFinding<-function(initialX,setAlpha,maxIter){
while((target(initialX,setAlpha)!=0) && maxIter>0){
initialX<-initialX-(target(initialX,setAlpha)/firstDerivative(initialX))
maxIter<-maxIter-1
}
initialX
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 76.99.44.184