#!/usr/local/bin/perl -w # reads r and n from command line and returns the Bayesian # confidence interval (1-sigma by default) for bernoulli "p" $latex_printlower = 0 ; $lowerbound = 1e-06; $z_score = 1.0 ; $z=1; $r=-1; $n=-1; eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift; # you can use either argument to change z_score. $z_score = $z_score * $z ; if ( $r < 0 || $n < 1 ) { print STDERR "You must supply r and n\n" ; &print_usage (); exit(0); } sub print_usage { print STDERR " bern.p r=3 n=10\n" ; } $errors = $r ; $trials = $n ; # &find_ebs_and_print ( 1 ) ; sub find_ebs_and_print { # this routine looks at the global variables $errors and $trials # and computes a confidence interval and maximum likelihood value. # these are called $p_lower and $p_upper, and $p_point (ML value) # the size of the interval is dictated by $zscore, e.g. 2.0 # # [a value called $p_plaus is also computed, which is the point to plot # if one wants a plot which just shows point estimates. # this is not currently used in the paper, Sep 30 95] # # a bit error probability is also computed, $bitep, by looking at the # global variables $bitswrong and $FEN # local ( $arg ) = @_ ; # typesetting # $symbol = $arg ? $finalsymbol : $indivsymbol ; # $Neff = ( $errors == 0 ) ? 1 : $errors ; # make the point $p_plaus = $Neff / $trials ; # estimate $p_plaus # # Confidence interval # if ( $errors == 0 ) { $p_ml = 0.0 ; $p_point = $lowerbound ; $p_lower = $lowerbound ; $p_upper = 1.0 - exp( - $z_score / $trials ) ; } else { $p_point = $errors / $trials ; $p_ml = $p_point ; # factor corresponding z sd's in log p $factor = exp ( $z_score * sqrt( ($trials - $errors ) / ( $errors * $trials ) ) ) ; # this is a lot like sqrt( $errors ) $p_upper = $p_point * $factor ; $p_lower = $p_point / $factor ; } # # end of confidence interval bit # # p_ML is the true ML estimate # p_plaus differs by replacing 0 errors by 1 error # p_point is p_ML, except "lowerbound" is used instead of zero. # similarly p_lower is the lower error bar, with lowerbound replacing zero # I think the most useful pair of numbers are p_ML and p_upper. print STDERR "# errors trials p_ML p_plaus point upper lower\n" ; printf STDERR "%6s %6s " , $errors, $trials ; printf STDERR "%8.4g %8.4g %8.4g %8.4g %8.4g ", $p_ml , $p_plaus, $p_point , $p_upper , $p_lower ; print STDERR "\n" ; if ( $arg == 1 ) { # print some latex stuff too print STDERR "% errors trials p_ML p^+ (p_-)\n" ; printf STDERR "%4s & %6s " , $errors, $trials ; printf STDERR "& %8.3g & %8.3g " , $p_ml , $p_upper ; if ( $latex_printlower ) { printf STDERR "& %8.3g ", $p_lower ; } print STDERR "\n" ; print STDERR "\\\\ \n" ; } } #