It looks as though what
oakbox is after is the integral of the function:
min(p1(x),p2(x)) dx
where p1 and p2 are the two probability distributions. That is, he's trying to find the area under both curves. Now, this isn't actually all _that_ hard, though the answer will include some calls to
erf.
Let's see.... (ten minutes of scribbling on paper later, accompanied by some looking up of things Mathworld)
Ok, well, it's ugly, but this _should_ get the same results as the given procedure:
use Math::Libm qw(erf erfc M_SQRT2);
sub compare_bell_curves {
my ($self,$m1,$sd1,$m2,$sd2) = @_;
if ($sd1 > $sd2) {
($m1,$sd1,$m2,$sd2)=($m2,$sd2,$m1,$sd1);
} elsif ($sd1 == $sd2) {
# stupid corner case
my $dist = abs($m1-$m2)/$sd1;
return erfc($dist/2/M_SQRT2);
}
$m2 -= $m1;
$m1 = 0;
# Some terms omitted since $m1 = 0
my $sd2s= $sd2*$sd2;
my $sd1s= $sd1*$sd1;
my $A = ($sd2s - $sd1s);
my $B = 2*($m2*$sd1s);
my $C = 2*(log($sd1)-log($sd2))*$sd1s*$sd2s - $m2*$m2*$sd1s;
my $disc = $B*$B - 4*$A*$C;
my $rdisc = sqrt($disc);
my $lower = (-$B - $rdisc)/(2*$A);
my $upper = (-$B + $rdisc)/(2*$A);
my $p1 = 0.5 + erf(($lower-$m2)/$sd2/M_SQRT2)/2;
my $p2 = (erf($upper/$sd1/M_SQRT2)-erf($lower/$sd1/M_SQRT2))/2;
my $p3 = erfc(($upper-$m2)/$sd2/M_SQRT2)/2;
$p1+$p2+$p3;
}
Note that it took much, much longer to write this note and get the code working than to do the math. (Mostly, that was tracing down transcription errors in going from paper to code) The math itself was a matter of finding the intersections (which boils down to just solving a quadratic equation in
x, albeit with messy coefficients), and then using the fact that the cumulative distribution function for a normal distribution is as given in equation 9 of
http://mathworld.wolfram.com/NormalDistribution.html.
True, there are many problems which cannot be solved or even vaguely approached analytically, but this isn't one of them.
--
@/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/;
map{y/X_/\n /;print}map{pop@$_}@/for@/
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.