Skip to content

Instantly share code, notes, and snippets.

@Util
Created March 9, 2012 19:06
Show Gist options
  • Save Util/2008126 to your computer and use it in GitHub Desktop.
Save Util/2008126 to your computer and use it in GitHub Desktop.
Create Z table for statistics
#!perl
use strict;
use warnings;
# This code duplicates "Appendix B The Standard Normal Table"
# from a book used by Alexis in 2012 statistics class.
# 2012-03-09 <bruce.gray@acm.org>
# Wrote program in Perl 6, then converted to Perl 5.
use constant SQRT_2PI => sqrt( 8 * atan2(1,1) );
# Probability density function
# Z(x) from http://people.math.sfu.ca/~cbm/aands/page_931.htm
# Also from http://en.wikipedia.org/wiki/Normal_distribution#Definition
sub pdf {
my ($x) = @_;
return exp($x * $x / -2) / SQRT_2PI;
}
# Cumulative distribution function
# P(x) from http://people.math.sfu.ca/~cbm/aands/page_932.htm
sub abramowitz_stegun_26_2_17 {
my ($x) = @_;
my $t = 1 / ( 1 + ($x * 0.2316419) );
my $sum = (($t ** 1) * 0.319381530)
+ (($t ** 2) * -0.356563782)
+ (($t ** 3) * 1.781477937)
+ (($t ** 4) * -1.821255978)
+ (($t ** 5) * 1.330274429);
return 1 - (pdf($x) * $sum);
}
my @ns = map { $_ / 100 } 0..350;
push @ns, map { $_ / 10 } 36..40;
# A is Z
# B is "Area between Mean and Z"
# C is "Area Beyond Z"
print " A\t B\t C\n";
for my $n (@ns) {
my $cdf = abramowitz_stegun_26_2_17($n) - 0.5;
printf "%.2f\t%.4f\t%.4f\n", $n, $cdf, 0.5 - $cdf;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment