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@/

In reply to Re^2: Empirically solving complex problems by fizbin
in thread Empirically solving complex problems by oakbox

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.