[system] / trunk / pg / macros / PGstatisticsmacros.pl Repository:
ViewVC logotype

Annotation of /trunk/pg/macros/PGstatisticsmacros.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1080 - (view) (download) (as text)

1 : sh002i 1050
2 : apizer 1080
3 :     sub _PGstatisticsmacros_init {
4 : sh002i 1050 foreach my $t (@Distributions::EXPORT_OK) {
5 :     *{$t} = *{"Distributions::$t"}
6 :     }
7 :     foreach my $t (@Regression::EXPORT_OK) {
8 :     *{$t} = *{"Regression::$t"}
9 :     }
10 :     }
11 :    
12 :     =head1 Statistics Macros
13 :    
14 :     =head3 Normal distribution
15 :    
16 : apizer 1080 =pod
17 : sh002i 1050
18 : apizer 1080 Usage: normal_prob(a, b, mean=>0, deviation=>1);
19 : sh002i 1050
20 :     Computes the probability of x being in the interval (a,b) for normal distribution.
21 : apizer 1080 The first two arguments are required. Use '-infty' for negative infinity, and 'infty' or '+infty' for positive infinity.
22 : sh002i 1050 The mean and deviation are optional, and are 0 and 1 respectively by default.
23 :     Load PGnumericalmacros.pl in your problem if you use this method.
24 :    
25 :     =cut
26 :    
27 : apizer 1080 sub normal_prob {
28 : sh002i 1050 warn 'You must also load PGnumericalmacros to use PGstatisticsmacros' unless defined(&_PGnumericalmacros_init);
29 :    
30 :     my $a = shift;
31 :     my $b = shift;
32 :     my %options=@_;
33 : apizer 1080
34 : sh002i 1050 my $mean = $options{'mean'} if defined ($options{'mean'});
35 :     $mean = 0 unless defined $mean;
36 : apizer 1080
37 : sh002i 1050 my $deviation = $options{'deviation'} if defined ($options{'deviation'});
38 :     $deviation = 1 unless defined $deviation;
39 : apizer 1080
40 : sh002i 1050 if ($deviation <= 0) {
41 :     warn 'Deviation must be a positive number.';
42 :     return;
43 :     }
44 :    
45 :     my $z_score_of_a;
46 :     my $z_score_of_b;
47 :    
48 : apizer 1080 if ( $a eq '-infty' ) {
49 : sh002i 1050 $z_score_of_a = -6;
50 : apizer 1080 } else {
51 : sh002i 1050 $z_score_of_a = ($a - $mean)/$deviation;
52 :     }
53 :    
54 : apizer 1080 if (($b eq 'infty') or ($b eq '+infty')) {
55 :     $z_score_of_b = 6;
56 :     } else {
57 : sh002i 1050 $z_score_of_b = ($b - $mean)/$deviation;
58 :     }
59 :    
60 :     my $function = sub { my $x=shift;
61 :     $E**(-$x**2/2)/sqrt(2*$PI);
62 : apizer 1080 };
63 : sh002i 1050
64 :     my $prob = romberg($function, $z_score_of_a, $z_score_of_b, 8);
65 :     $prob;
66 :     }
67 :    
68 :     =head3 "Inverse" of normal distribution
69 :    
70 : apizer 1080 =pod
71 : sh002i 1050
72 :     Usage: normal_distr(prob, mean=>0, deviation=>1);
73 :    
74 :     Computes the positive number b such that the probability of x being in the interval (0,b)
75 :     is equal to the given probability (first argument). The mean and deviation are
76 : apizer 1080 optional, and are 0 and 1 respectively by default.
77 : sh002i 1050 Caution: since students may use tables, they may only be able to provide the answer correct to 2 or 3
78 :     decimal places. Use tolerance when evaluating answers.
79 :     Load PGnumericalmacros.pl if you use this method.
80 :    
81 :     =cut
82 :    
83 : apizer 1080 sub normal_distr {
84 : sh002i 1050 warn 'You must also load PGnumericalmacros to use PGstatisticsmacros' unless defined(&_PGnumericalmacros_init);
85 :    
86 :     my $prob = shift;
87 :     my %options=@_;
88 : apizer 1080
89 : sh002i 1050 my $mean = $options{'mean'} if defined ($options{'mean'});
90 :     $mean = 0 unless defined $mean;
91 : apizer 1080
92 : sh002i 1050 my $deviation = $options{'deviation'} if defined ($options{'deviation'});
93 :     $deviation = 1 unless defined $deviation;
94 :    
95 :     if ($deviation <= 0) {
96 :     warn 'Deviation must be a positive number.';
97 :     return;
98 :     }
99 :    
100 :     my $function = sub { my $x=shift;
101 :     $E**(-$x**2/2)/sqrt(2*$PI);
102 :     };
103 :    
104 :     my $z_score_of_b = inv_romberg($function, 0, $prob);
105 :    
106 :     my $b = $z_score_of_b * $deviation + $mean;
107 :     $b;
108 : apizer 1080 }
109 : sh002i 1050
110 :     ##########################################
111 :    
112 :     1;
113 :    

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9