#!/usr/bin/perl use strict; use warnings; use Statistics::Descriptive; # 1. Simulate 10,000 draws of 116 obs from a random distribution between 0 and 9. # 2. Calculate: # - the odds one digit occurs 5 or less times (4% of 116) # - the odds one digit occurs 20 or more times (17% of 116) # - the mean and sd -> test 5 and 20 are outside the 95% CI # - the odds both occur my $RUNS = 10_000; my ($FIVES,$TWENTIES,$BOTH) = (0,0,0); my @SAMPLE; my $stat = Statistics::Descriptive::Full->new(); # Collect for my $i ( 1..$RUNS ) { my %h; $h{int(rand(10))}++ for (1..116); my ($old5,$old20) = ($FIVES,$TWENTIES); for ( values %h ) { # $stat->add_data($_); push @SAMPLE, $_; $FIVES++ if $_ <= 5; $TWENTIES++ if $_ >= 20; } $BOTH++ if $old5!=$FIVES and $old20!=$TWENTIES; } $stat->add_data(@SAMPLE); # Analyze printf "Mean:\t\t\t%.2f\nSD:\t\t\t%.3f\n",$stat->mean,$stat->standard_deviation; printf "Odds of 5 or less:\t%.3f\n",$FIVES/$RUNS; printf "Odds of 20 or higher:\t%.3f\n",$TWENTIES/$RUNS; printf "95 percent CI:\t\t%.3f --- %.3f\n", $stat->mean - 2.96 * $stat->standard_deviation, $stat->mean + 2.96 * $stat->standard_deviation; printf "Odds of both:\t\t%.3f\n",$BOTH/$RUNS;