[system] / trunk / webwork / system / courseScripts / PGstatisticsmacros.pl Repository: Repository Listing bbplugincoursesdistsnplrochestersystemwww

# Annotation of /trunk/webwork/system/courseScripts/PGstatisticsmacros.pl

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