help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Integrating a normal distribution many times


From: David Bateman
Subject: Re: Integrating a normal distribution many times
Date: Thu, 17 May 2007 15:10:04 +0200
User-agent: Thunderbird 1.5.0.7 (X11/20060921)

Søren Hauberg wrote:
> Hi,
>    This is not necessarily an Octave question. but here goes anyway.
>    I need to evaluate the integrate a normal distribution over a 
> specific interval with many mean values. My current Octave 
> implementation just calls 'quad' at mean value, i.e.
> 
> for t = linspace(0, 32, 750)
>    qq = @(x) normal_pdf(x, t, 2.5);
>    result = quad(qq, 19, 26, 0)
> endfor
> 
> This is, however, fairly slow. At some point my system needs to run in 
> real-time (not in an Octave implementation), so I doubt that the above 
> strategy will work. Does anybody have some hints on how to speed this 
> up? Should I precalculate a lookup table with some values and then 
> interpolate when I need to evaluate the function at a new mean value?
> 
> Søren

The pdf of a normal distribution is

1 / (sigma * sqrt(2 * %pi)) * exp (- ((x - mean) / sigma)^2 / 2)


running that through maxima gives

%i1 integrate(1 / (sigma * sqrt(2 * %pi)) * exp (- ((x - mean) /
sigma)^2 / 2), x);

                                 x         SQRT(2) mean
                       ERF(------------- - ------------)
                           SQRT(2) SIGMA     2 SIGMA
(%o8)                  ---------------------------------
                                       2

or the indefinite integral of the normal pdf. The definite integral
between 19 and 26 is then fairly easy to derive.

mean = linspace(0, 32, 750);
sigma = sqrt(2.5);
f = @(x) erf((x  - mean)/ sqrt(2) / sigma) / 2;
result = f(26) - f(19);

Closed forms, always a bit faster :-)

Cheers
David


reply via email to

[Prev in Thread] Current Thread [Next in Thread]