This (untested) approach uses a cache to get the Sum of Squared Deviations to the mean one time. Your approach does this calculation for each call to the correlation sub. So this saves about 11 billion or more (more I think :-) )calculations.

#!/tools/bin/perl use strict; use warnings; use List::Util 'sum'; my @keys; my $probes; open my $FILE, "data.txt" or die "ERROR: cannot read file $!\n"; while (<$FILE>){ chomp; my ($key, @line) = split /\t/; $probes->{$key}{vals} = \@line; $probes->{$key}{mean} = sum(@line) / @line; push @keys, $key; } close($FILE) or die $!; my $cache; ## Sum of Squared Deviations to the mean for my $key (@keys) { my $mean = $probes->{$key}{mean}; my $ss; for my $dat (@{ $probes->{$key}{vals} }) { $ss += ($dat - $mean) ** 2; } $cache->{$key} = $ss; } ## Correlation Calculation my $count = 1; for my $i (0 .. $#keys){ for my $j ($i+1 .. $#keys){ my $cor = correlation($probes, $cache, @keys[$i, $j]); # $calProbes{$probesArray[$i]."-".$probesArray[$j]} = $cor; print $count++,"\t", "$keys[$i]-$keys[$j]\t$cor\n"; } } ## Sum of Squared Deviations to the mean sub correlation { my ($probes, $cache, $key1, $key2) = @_; my $arr1 = $probes->{$key1}{vals}; my $arr2 = $probes->{$key2}{vals}; my $mean_x = $probes->{$key1}{mean}; my $mean_y = $probes->{$key2}{mean}; my ($ssxx, $ssxy, $ssyy) = ($cache->{$key1}, 0, $cache->{$key2}); for(my $i = 0; $i < @$arr1; $i++){ $ssxy += ($arr1->[$i] - $mean_x)*($arr2->[$i] - $mean_y) ; } return $ssxy/sqrt( $ssxx * $ssyy ); }
Update: as moritz suggested, PDL::Stats::Basic might provide the optimal solution. My response is using plain perl.

In reply to Re: Improving the Nested For Loop by Cristoforo
in thread Improving the Nested For Loop by Anonymous Monk

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.