[R] Newton method iteration problem
Charles C. Berry
cberry at tajo.ucsd.edu
Sat Oct 27 18:17:46 CEST 2007
On Sat, 27 Oct 2007, kevinchang wrote:
>
> Thanks Chales for pointing out the errors.
> I fixed an errorr and R accepted my "rootFinding" code.
> But the problem right now is that my code will work only if the intialX
> value is close enough to the solution.
> Otherwise, R says there is missing true/false value in the codition test of
> while loop. I can't figure out what happens. Some advice , please??
I think I warned you that this is a tricky business.
Finding good starting values is part of the art as is dealing with the
consequences of bad starting values.
If you are simply trying to implement root finding, my suggestion to
use uniroot() and the like still holds.
If you are trying to learn about numerical optimization, then (in
addition to reading a book or two on the topic), I suggest you do this:
?debug ## find out about this helpful function first
debug( rootFinding )
rootFinding( 5, 0.05, 10 )
< now inspect each object just after it is created >
< also check out the values of target() and firstDerivative() >
When you see where this algorithm 'hit the wall', you may wish to
ponder how Newton and his algorithm could have got it so wrong. ;-)
I also suggest you look at the source used by qnorm() (and the other
q<distn> functions). If you hunt for this you will eventually find it
in <path to R sources>/src/nmath/qnorm.c
If you cannot grok what is going on from reading that source, then
referring to the algorithms referenced there (AS 111 and AS 241) will
likely be instructive. In the code, you will see is a comment that
suggests that finding
qnorm( value.near.zero.or.one )
is a bit delicate.
Chuck
>
> #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
> }
>
>
>
>
>
>
>
>
>
>
>
> Charles C. Berry wrote:
>>
>> On Fri, 26 Oct 2007, kevinchang wrote:
>>
>>>
>>> Hi all,
>>>
>>> I am coding for finding the root of f(x)= phi(x) -alpha where phi(x) is
>>> the
>>> cumulative density function and alpha is constant . The problem right now
>>> is
>>> I can't get the "initialX" representing the root out of the while loop
>>> when
>>> ending , it seems to me it disappear when the loop ends accroding to the
>>> error message. I need help . Please suggest the cause or solution to
>>> this
>>> problem. Thanks.
>>
>> Learn to type without making errors? Learn to format (space and indent)
>> your code so errors will be more obvious to you??
>>
>> Your code worked for me once I corrected the typos and syntax errors.
>>
>> It even agrees with qnorm( setAlpha ).
>>
>> If all you want is root finding capability, I suggest you see
>>
>> ?uniroot
>>
>> and friends.
>>
>> HTH,
>>
>> Chuck
>>
>>>
>>> # 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){
>>> while(target(initialX,setAlpha)!=0){
>>> initialX<-initialX-(target(initialX,setAlpha)/firstfirstDerivative(initialX)
>>> }
>>> initialX
>>> }
>>>
>>>
>>> --
>>> View this message in context:
>>> http://www.nabble.com/Newton-method-iteration-problem-tf4701085.html#a13439031
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>> Charles C. Berry (858) 534-2098
>> Dept of Family/Preventive
>> Medicine
>> E mailto:cberry at tajo.ucsd.edu UC San Diego
>> http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>
> --
> View this message in context: http://www.nabble.com/Newton-method-iteration-problem-tf4701085.html#a13442728
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
Charles C. Berry (858) 534-2098
Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
More information about the R-help
mailing list