[R] function of probability for normal distribution
Greg.Snow at imail.org
Thu Aug 20 19:33:03 CEST 2009
It looks like you are reinventing wheels here (not necessarily a bad thing).
If you want the probabilities associated with the normal distribution, then look at the help for dnorm and pnorm functions.
If you are recreating this as a learning experience/homework, then you should be up front about that, what is the assignment? What level of help are you allowed? Etc. Then be more explicit in what parts you understand and what you need help with. This list is generally not for homework help (definitely not for doing it for you), but there are cases where the question and limitations are clear that we have given appropriate hints.
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
greg.snow at imail.org
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Rene
> Sent: Thursday, August 20, 2009 5:19 AM
> To: r-help at r-project.org
> Subject: Re: [R] function of probability for normal distribution
> Dear all,
> Because my last email was in html format, so it was a disaster to read.
> have second thought of my question asked in my last email, and came up
> solution to myself, but I found the result was a bit weird, can someone
> please help look at my coding and advise where I have done wrong?
> I need to write a function which compute the probability for standard
> distribution. The formula is
> P(z)= 1/2 + 1/(sqrt(2*pi)) * sum ((-1)^k*z^(2*k+1) /(2^k*k!*(2*k+1))
> >From series expansion for the exponential function, we know e^x= sum
> (x^n/n!), we can substitute x = -t^2/2, so e^(-t^2/2) = sum
> ((-1)^k*t^(2*k)/(2^k*k!)). Comparing with the formula above, the sum
> ((-1)^k*z^(2*k+1) /(2^k*k!*(2*k+1)) is very similar to e^(-t^2/2),
> there is z/(2*k+1) in the formula.
> So I create a function for the sum ((-1)^k*z^(2*k+1) /(2^k*k!*(2*k+1)),
> which is
> expf = function (t)
> t = ifelse(neg,-t,t)
> oldsum = sum
> k = k+1
> term = term*(((-1)^1*t^2) / (2*k))
> sum = sum + (t/(2*k+1))*term
> Now the sum part of the probability can be replaced by expf function,
> then I
> create the function for the probability p(z), which is
> prob = function(z)
> Am I doing the right thing here? However, when I test the probability
> function, e.g. prob(1:10), the result appear some negative value and
> very large value which do not seem right to me as probability values.
> someone please guide me where I have done wrong here?
> Thanks a lot.
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help