blib/lib/Math/example.pl untested

filter: Statementstmt 0.0% 0 / 318 Branchbran 0.0% 0 / 88 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 420 CC 21 SCAR 53.3
CC
21
Cov
0%
true_stats
306.0
sampstat
30.0
print_results
2.0
CRAP 206.2
1 #!/usr/bin/perl -w
2 use Math::Random qw(:all);
3 use Carp;
4 use strict;
5  
6 my $max_choice = 17;
7 my $ans = "";
8 my $input = "";
9 my @args = ();
10 my @result = ();
11  
12 TEST: while ( 1 ) {
13 print " Enter number corresponding to choice:\n",
14 " (0) Exit this program\n",
15 " (1) Generate Chi-Square deviates\n",
16 " (2) Generate noncentral Chi-Square deviates\n",
17 " (3) Generate F deviates\n",
18 " (4) Generate noncentral F deviates\n",
19 " (5) Generate random permutation\n",
20 " (6) Generate uniform integers\n",
21 " (7) Generate uniform reals\n",
22 " (8) Generate beta deviates\n",
23 " (9) Generate binomial outcomes\n",
24 " (10) Generate Poisson outcomes\n",
25 " (11) Generate exponential deviates\n",
26 " (12) Generate gamma deviates\n",
27 " (13) Generate multinomial outcomes\n",
28 " (14) Generate normal deviates\n",
29 " (15) Generate negative binomial outcomes\n",
30 " (16) Generate multivariate normal deviates\n",
31 " (17) Generate random permuted index\n";
32 $ans = <stdin>;
33 chomp $ans;
34 $ans = int( $ans );
35 last TEST if $ans == 0;
36 unless ( $ans > 0 and $ans <= $max_choice ) {
37 print "Try one of (1, ... $max_choice)\n";
38 next TEST;
39 }
40 print "Enter phrase to initialize seeds:\n";
41 my $phrase = <stdin>;
42 chomp $phrase;
43 random_set_seed_from_phrase( $phrase );
44  
45 if ( $ans == 1 ) {
46 print "Enter (space-separated) N, DF for chi-square deviates:\n";
47 $input = <stdin>;
48 chomp $input;
49 @args = split( /\s+/, $input );
50 @result = random_chi_square( @args );
51 shift( @args );
52 print_results( sampstat( @result ), scalar( @result ), true_stats( 'chis', @args ) );
53 next TEST;
54 }
55  
56 if ( $ans == 2 ) {
57 print "Enter (space-separated) N, DF, NONC for ", "noncentral chi-square deviates:\n";
58 $input = <stdin>;
59 chomp $input;
60 @args = split( /\s+/, $input );
61 @result = random_noncentral_chi_square( @args );
62 shift( @args );
63 print_results( sampstat( @result ), scalar( @result ), true_stats( 'ncch', @args ) );
64 next TEST;
65 }
66  
67 if ( $ans == 3 ) {
68 print "Enter (space-separated) N, DFN, DFD for F deviates:\n";
69 $input = <stdin>;
70 chomp $input;
71 @args = split( /\s+/, $input );
72 @result = random_f( @args );
73 shift( @args );
74 print_results( sampstat( @result ), scalar( @result ), true_stats( 'f', @args ) );
75 next TEST;
76 }
77  
78 if ( $ans == 4 ) {
79 print "Enter (space-separated) N, DFN, DFD, NONC for ", "non-central F deviates:\n";
80 $input = <stdin>;
81 chomp $input;
82 @args = split( /\s+/, $input );
83 @result = random_noncentral_f( @args );
84 shift( @args );
85 print_results( sampstat( @result ), scalar( @result ), true_stats( 'ncf', @args ) );
86 next TEST;
87 }
88  
89 if ( $ans == 5 ) {
90 print "Enter (space-separated) list for random permutation:\n";
91 $input = <stdin>;
92 chomp $input;
93 @args = split( /\s+/, $input );
94 print "Result (' : ' separated):\n", join( " : ", random_permutation( @args ) ), "\n";
95 next TEST;
96 }
97  
98 if ( $ans == 6 ) {
99 print "Enter (space-separated) Maximum integer, Replications ", "per integer:\n";
100 $input = <stdin>;
101 chomp $input;
102 @args = split( /\s+/, $input );
103 @result = random_uniform_integer( $args[0] * $args[1], 1, $args[0] );
104 my @totals = ( 0 ) x ( $args[0] + 1 );
105 my $val = 0;
106 foreach $val ( @result ) { $totals[$val]++; }
107 shift @totals;
108 print "Result (' : ' separated):\n", join( " : ", @totals ), "\n";
109 next TEST;
110 }
111  
112 if ( $ans == 7 ) {
113 print "Enter (space-separated) N, LOWER, UPPER for ", "uniform real deviates:\n";
114 $input = <stdin>;
115 chomp $input;
116 @args = split( /\s+/, $input );
117 @result = random_uniform( @args );
118 shift( @args );
119 $args[0] = 0 unless defined( $args[0] );
120 $args[1] = 1 unless defined( $args[1] );
121 print_results( sampstat( @result ), scalar( @result ), true_stats( 'unif', @args ) );
122 next TEST;
123 }
124  
125 if ( $ans == 8 ) {
126 print "Enter (space-separated) N, A, B for ", "beta deviates:\n";
127 $input = <stdin>;
128 chomp $input;
129 @args = split( /\s+/, $input );
130 @result = random_beta( @args );
131 shift( @args );
132 print_results( sampstat( @result ), scalar( @result ), true_stats( 'beta', @args ) );
133 next TEST;
134 }
135  
136 if ( $ans == 9 ) {
137 print "Enter (space-separated) N, NTrials, P for ", "binomial outcomes:\n";
138 $input = <stdin>;
139 chomp $input;
140 @args = split( /\s+/, $input );
141 @result = random_binomial( @args );
142 shift( @args );
143 print_results( sampstat( @result ), scalar( @result ), true_stats( 'bin', @args ) );
144 next TEST;
145 }
146  
147 if ( $ans == 10 ) {
148 print "Enter (space-separated) N, MU for ", "poisson outcomes:\n";
149 $input = <stdin>;
150 chomp $input;
151 @args = split( /\s+/, $input );
152 @result = random_poisson( @args );
153 shift( @args );
154 print_results( sampstat( @result ), scalar( @result ), true_stats( 'pois', @args ) );
155 next TEST;
156 }
157  
158 if ( $ans == 11 ) {
159 print "Enter (space-separated) N, AV for ", "exponential deviates:\n";
160 $input = <stdin>;
161 chomp $input;
162 @args = split( /\s+/, $input );
163 @result = random_exponential( @args );
164 shift( @args );
165 $args[0] = 1 unless defined( $args[0] );
166 print_results( sampstat( @result ), scalar( @result ), true_stats( 'expo', @args ) );
167 next TEST;
168 }
169  
170 if ( $ans == 12 ) {
171 print "Enter (space-separated) N, A, R for ", "gamma deviates:\n";
172 $input = <stdin>;
173 chomp $input;
174 @args = split( /\s+/, $input );
175 @result = random_gamma( @args );
176 shift( @args );
177 print_results( sampstat( @result ), scalar( @result ), true_stats( 'gamm', @args ) );
178 next TEST;
179 }
180  
181 if ( $ans == 13 ) {
182 print "Enter (space-separated) list of prob.s for categories ", "for multinomial outcomes:\n";
183 my $input = <stdin>;
184 chomp $input;
185 @args = split( /\s+/, $input );
186 print "Please enter number of events to be classified:\n";
187 my $n = <stdin>;
188 chomp $n;
189 @result = random_multinomial( $n, @args );
190 print "Result:\n", join( " ", @result ), "\n";
191 my $sum = 0;
192 my $val;
193 foreach $val ( @result ) { $sum += $val; }
194 foreach $val ( @result ) { $val /= $sum; }
195 print "Observed proportions:\n", join( " ", @result ), "\n";
196 pop @args;
197 $sum = 0;
198 foreach $val ( @args ) { $sum += $val; }
199 push @args, ( 1 - $sum );
200 print "Expected proportions:\n", join( " ", @args ), "\n";
201 next TEST;
202 }
203  
204 if ( $ans == 14 ) {
205 print "Enter (space-separated) N, AV, SD for ", "normal deviates:\n";
206 $input = <stdin>;
207 chomp $input;
208 @args = split( /\s+/, $input );
209 @result = random_normal( @args );
210 shift( @args );
211 $args[0] = 0 unless defined( $args[0] );
212 $args[1] = 1 unless defined( $args[1] );
213 print_results( sampstat( @result ), scalar( @result ), true_stats( 'norm', @args ) );
214 next TEST;
215 }
216  
217 if ( $ans == 15 ) {
218 print "Enter (space-separated) N, NEvents, P for ", "negative binomial outcomes:\n";
219 $input = <stdin>;
220 chomp $input;
221 @args = split( /\s+/, $input );
222 @result = random_negative_binomial( @args );
223 shift( @args );
224 print_results( sampstat( @result ), scalar( @result ), true_stats( 'nbin', @args ) );
225 next TEST;
226 }
227  
228 if ( $ans == 16 ) {
229 print "Enter dimension of multivariate deviate:\n";
230 my $p = <stdin>;
231 chomp $p;
232 print "Enter mean vector of length $p (space separated):\n";
233 my $temp = <stdin>;
234 chomp $temp;
235 $temp =~ s/^\s*//;
236 my @mean;
237 @mean = split( /\s+/, $temp );
238 print "Enter (symmetric, $p by $p) covariance matrix\n", "One space-separated row per line:\n";
239 my $val;
240 my @covariance = ( 0 ) x $p;
241 foreach $val ( @covariance ) {
242 $temp = <stdin>;
243 chomp $temp;
244 $temp =~ s/^\s*//;
245 $val = [ split( /\s+/, $temp ) ];
246 }
247 print "Enter number of observations:\n";
248 my $n = <stdin>;
249 chomp $n;
250 my @ans = random_multivariate_normal( $n, @mean, @covariance );
251 my $template = join( " ", ( '%15.7g' ) x $p );
252 print "\nResults:\n";
253 foreach $val ( @ans ) {
254 printf "$template\n", @{$val};
255 }
256 }
257  
258 if ( $ans == 17 ) {
259 print "Enter N (size of array) for random permuted index:\n";
260 $input = <stdin>;
261 chomp $input;
262 print "Result (' : ' separated):\n", join( " : ", random_permuted_index( $input ) ), "\n";
263 next TEST;
264 }
265 }
266 print "Normal termination of tester.\n";
267  
268 sub sampstat { # gets sample statistics for array - returns array of stats
269 my $n = scalar( @_ );
270 return () unless $n > 0;
271 return ( $_[0], 0, $_[0], $_[0] ) unless $n > 1;
272 my ( $min ) = my ( $max ) = $_[0];
273 my $val;
274 my $avg = 0;
275 foreach $val ( @_ ) {
276 $avg += $val;
277 $min = $val if $val < $min;
278 $max = $val if $val > $max;
279 }
280 $avg /= $n;
281 my $var = 0;
282 foreach $val ( @_ ) {
283 $var += ( $val - $avg )**2;
284 }
285 $var /= ( $n - 1 );
286 return ( $avg, $var, $min, $max );
287 }
288  
289 sub print_results { # Arguments: $avg, $var, $min, $max, $nobs, $travg, $trvar
290 my ( $avg, $var, $min, $max, $nobs, $travg, $trvar ) = @_;
291 print "Results:\n";
292 printf "Number of observations: %d\n", $nobs;
293 printf "Mean : %15.7g True : %15.7g\n", $avg, $travg;
294 printf "Variance: %15.7g True : %15.7g\n", $var, $trvar;
295 printf "Minimum : %15.7g Maximum : %15.7g\n", $min, $max;
296 }
297  
298 sub true_stats { # Arguments: $type, @parin; Returns: $av, $var
299  
300 ########################################################################
301 # Returns mean and variance for a number of statistical distribution
302 # as a function of their parameters.
303 #
304 #
305 # Arguments
306 #
307 #
308 # $type --> Character string indicating type of distribution
309 # 'chis' chisquare
310 # 'ncch' noncentral chisquare
311 # 'f' F (variance ratio)
312 # 'ncf' noncentral f
313 # 'unif' uniform
314 # 'beta' beta distribution
315 # 'bin' binomial
316 # 'pois' poisson
317 # 'expo' exponential
318 # 'gamm' gamma
319 # 'norm' normal
320 # 'nbin' negative binomial
321 #
322 # @parin --> Array containing parameters of distribution
323 # chisquare
324 # $parin[0] is df
325 # noncentral chisquare
326 # $parin[0] is df
327 # $parin[1] is noncentrality parameter
328 # F (variance ratio)
329 # $parin[0] is df numerator
330 # $parin[1] is df denominator
331 # noncentral F
332 # $parin[0] is df numerator
333 # $parin[1] is df denominator
334 # $parin[2] is noncentrality parameter
335 # uniform
336 # $parin[0] is LOW bound
337 # $parin[1] is HIGH bound
338 # beta
339 # $parin[0] is A
340 # $parin[1] is B
341 # binomial
342 # $parin[0] is Number of trials
343 # $parin[1] is Prob Event at Each Trial
344 # poisson
345 # $parin[0] is Mean
346 # exponential
347 # $parin[0] is Mean
348 # gamma
349 # $parin[0] is A
350 # $parin[1] is R
351 # normal
352 # $parin[0] is Mean
353 # $parin[1] is Standard Deviation
354 # negative binomial
355 # $parin[0] is required Number of events
356 # $parin[1] is Probability of event
357 #
358 # $av <-- Mean of specified distribution with specified parameters
359 #
360 # $var <-- Variance of specified distribution with specified paramete
361 #
362 #
363 # Note
364 #
365 #
366 # $av and $var will be returned -1 if mean or variance is infinite
367 #
368 #**********************************************************************
369 my $type = shift( @_ );
370 my @parin = @_;
371 my ( $av, $var, $a, $b, $range ) = ( -1, -1, 0, 0, 0 );
372  
373 TYPE: {
374 if ( ( 'chis' ) eq ( $type ) ) {
375 $av = $parin[0];
376 $var = 2.0 * $parin[0];
377 last TYPE;
378 }
379  
380 if ( ( 'ncch' ) eq ( $type ) ) {
381 $a = $parin[0] + $parin[1];
382 $b = $parin[1] / $a;
383 $av = $a;
384 $var = 2.0 * $a * ( 1.0 + $b );
385 last TYPE;
386 }
387  
388 if ( ( 'f' ) eq ( $type ) ) {
389 unless ( $parin[1] <= 2.0001 ) {
390 $av = $parin[1] / ( $parin[1] - 2.0 );
391 }
392 unless ( $parin[1] <= 4.0001 ) {
393 $var = ( 2.0 * $parin[1]**2 * ( $parin[0] + $parin[1] - 2.0 ) )
394 / ( $parin[0] * ( $parin[1] - 2.0 )**2 * ( $parin[1] - 4.0 ) );
395 }
396 last TYPE;
397 }
398  
399 if ( ( 'ncf' ) eq ( $type ) ) {
400 unless ( $parin[1] <= 2.0001 ) {
401 $av = ( $parin[1] * ( $parin[0] + $parin[2] ) ) / ( ( $parin[1] - 2.0 ) * $parin[0] );
402 }
403 unless ( $parin[1] <= 4.0001 ) {
404 $a = ( $parin[0] + $parin[2] )**2 + ( $parin[0] + 2.0 * $parin[2] ) * ( $parin[1] - 2.0 );
405 $b = ( $parin[1] - 2.0 )**2 * ( $parin[1] - 4.0 );
406 $var = 2.0 * ( $parin[1] / $parin[0] )**2 * ( $a / $b );
407 }
408 last TYPE;
409 }
410  
411 if ( ( 'unif' ) eq ( $type ) ) {
412 $range = $parin[1] - $parin[0];
413 $av = $parin[0] + $range / 2.0;
414 $var = $range**2 / 12.0;
415 last TYPE;
416 }
417  
418 if ( ( 'beta' ) eq ( $type ) ) {
419 $av = $parin[0] / ( $parin[0] + $parin[1] );
420 $var = ( $av * $parin[1] ) / ( ( $parin[0] + $parin[1] ) * ( $parin[0] + $parin[1] + 1.0 ) );
421 last TYPE;
422 }
423  
424 if ( ( 'bin' ) eq ( $type ) ) {
425 $av = $parin[0] * $parin[1];
426 $var = $av * ( 1.0 - $parin[1] );
427 last TYPE;
428 }
429  
430 if ( ( 'pois' ) eq ( $type ) ) {
431 $av = $parin[0];
432 $var = $parin[0];
433 last TYPE;
434 }
435  
436 if ( ( 'expo' ) eq ( $type ) ) {
437 $av = $parin[0];
438 $var = $parin[0]**2;
439 last TYPE;
440 }
441  
442 if ( ( 'gamm' ) eq ( $type ) ) {
443 $av = $parin[1] / $parin[0];
444 $var = $av / $parin[0];
445 last TYPE;
446 }
447  
448 if ( ( 'norm' ) eq ( $type ) ) {
449 $av = $parin[0];
450 $var = $parin[1]**2;
451 last TYPE;
452 }
453  
454 if ( ( 'nbin' ) eq ( $type ) ) {
455 $av = $parin[0] * ( 1.0 - $parin[1] ) / $parin[1];
456 $var = $av / $parin[1];
457 last TYPE;
458 }
459  
460 croak "Unimplemented \$type: $type in true_stats";
461 }
462 return ( $av, $var );
463 }