Dear Monks,

Need to calculate the probability that the sum of random variables equals some value.

Training set: Number of objects (e.g. arrays of numbers), calculate the frequency that the sum of the object equals 0, 1, 2..max.

Probability: Find the probability that the sum of a number of random variables equals some number (between 0 and max), given those frequencies from the training set.

Example: After sampling (X) objects, the following frequencies have been found from the training set:
Freq(sum=0) = 0.1
Freq(sum=1) = 0.2
Freq(sum=2) = 0.7

What is the probability that the sum of 2 elements = 4? This code does the job:

#!/usr/bin/perl -w use strict; use Algorithm::Loops qw(NestedLoops); use List::Util qw(sum); #Set no. of elements, frequencies and the sum value my $nElements = 2; my @freq = qw (0.1 0.2 0.7); my $targetSum = 4; #Ways of generating sum given no. of elements my @pos = (0..$#freq); my (@combo, @matrix); for my $c (0..$nElements-1) { push @combo, [@pos]; } my @nest = NestedLoops(\@combo, sub { [ @_ ] } ); for my $n (0..$#nest) { push @{$matrix[sum @{$nest[$n]}]}, join ('', @{$nest[$n]}); } my @ways = @{$matrix[$targetSum]}; #Now calculate P-value my @pVal; foreach my $w (0..$#ways) { my @f = split ('', $ways[$w]); #print "@f\n"; my $p = $freq[$f[0]]; #print $p,"\n"; for my $g (1..$#f) { $p *= $freq[$f[$g]]; } push @pVal, $p; } my $pVal = sum @pVal; print "P-value(sum of $nElements = $targetSum) = $pVal\n"; #Prints: P-value(sum of 2 = 4) = 0.49
I have much LARGER SUMS to evaluate that derive from larger number of elements (thousands), with MORE FREQUENCIES (up to 10) and doubt that this code will ever scale up well.

Is there a module that can be used for this ? Or is there some way to do this that would scale up nicely. Convolutions ? FFT ? I am no mathematician, if you may please provide details.

Thank you very much, best.

FFRANK


In reply to Probability sum of random variables by FFRANK

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.