academic_home‎ > ‎

Perl

Subroutine--Simple Statistics

Mean, Standard Deviation, and Correlation

sub sum {
  my $sum;
  foreach (@_) {
      $sum += $_;
  }
  return $sum;
}

sub mean {
  return sum(@_) / @_;
}

sub std_dev {
  my @x2;
  foreach (@_) {
    push (@x2, ($_**2));
  }
  return sqrt( mean(@x2) - (mean(@_)**2));
}

sub z {
  my ($raw, $mean, $std_dev) = @_;
  return ( $raw - $mean ) / $std_dev;
}

sub center {
  my $avg = mean(@_);
  my @centered;
  foreach (@_) {
    push (@centered, ($_ - $avg));
  }
  return @centered;
}

sub covar {
  my @cx = center( @{$_[0]} );
  my @cy = center( @{$_[1]} );
  my @cxy, $n = @cx;
  for($i=0; $i<$n; ++$i) {
      push (@cxy, ($cx[$i] * $cy[$i]));
  }
  return sum(@cxy) / $n;
}

sub cor {
  return covar(@_) / (std_dev( @{$_[0]} ) * std_dev( @{$_[1]} ));
}

Application

my @x = (1,2,3,4,5);
my @y = (3,1,2,5,6);

my $mean_x = mean( @x );
my $std_x  = std_dev( @x );
my $cor_xy = cor( \@x, \@y );

print "Mean of x is "    . $mean_x . "<BR>";
print "Std_dev of x is " . $std_x  . "<BR>";
print "Cor(x, y) is "    . $cor_xy . "<BR>";
Comments