#!/usr/local/bin/perl -w # bernplot3.p modified from # bernplot2.p modified from # bernplot.p modified from bern.p # # called by berntable.p # # bernplot3.p r1=3 n1=10 r2=3 n2=100 # which should be equivalent to # bernplot2.p r1=4 n1=12 r2=4 n2=102 # # reads r1,n1,r2 and n2 from command line and finds # confidence intervals for bernoulli "p" # also plots graphs with cumulative probability, and computes P(p1>p2). # # The program writes to files called r.n, so run this somewhere # appropriate. # # if want to include prior that p lies in 1e-10 , 1-same, then recommend # adding 0.05 to r1 and 0.1 to n1 # # If additionally you want to know the probability # that p1 is more than 5 logit units greater than p2, then # you type # # bernplot3.p r1=3 n1=10 r2=3 n2=100 offset=5 # # and the output is this: # # prob ( p2 < p1 ) = 0.99622 # prob ( p2 > p1 ) = 0.0037776 # sum = 1 # =========== # [now finding probs for offset of 148 bins] # prob ( 4.9806 p2 < p1 ) = 0.82692 # prob ( 4.9806 p2 > p1 ) = 0.17308 # sum = 1 # =========== # # Things to note: # # (1) the normal answer is computed first [prob ( p2 < p1 ), etc.] # # (2) The desired question ("5 times greater") is not answered exactly; # instead, the program comes up with the nearest convenient # factor that it can do accurately, in this case, "4.9806 p2 < p1", # instead of "5 p2 < p1". # # Thus the output says that the probability that p1 is # more than 4.98 times greater than p2 is 83%. # # (3) Just in case you accidentally got p1 and p2 the wrong way round, # the program also computes the answer to the converse question # (the probability that p2 is more than 5 times greater than p1) # and spits it out too {not included above}. # # GRAPHICAL VERSION # The recommended usage is # # bernplot2.p r1=30 n1=1000000 r2=3 n2=10000 graphratio=1 samples=75 # bernplot3.p r1=30 n1=1000000 r2=3 n2=10000 graphratio=1 samples=75 # # This now gives two gnuplot commands at the end, with the # first one being the familiar graph of the two posterior probabilities # for p1 and p2, and the second one giving a graph of # the posterior probability of the ratio of (p1/p2). # # The horizontal axis is in powers of 10. # $email = 0 ; # If email then we don't do any of the file writing $rescalelike = 1 ; $verbose=0; $r1=-1; $n1=-1; $r2=-1; $n2=-1; $zrangemin = 4; # how many rough standard deviations we use to define range of integral $zrangemax = 4; $level = 0.135335 ; # level at which to find the highest posterior # density interv exp(-2.0) is 0.135335 $samples = 150; # number of samples in plotted function and cumulative integral # and number of samples when doing integral P(p1>p2). $psgnu=0; # whether to write to ps file when doing gnuplot stuff. $offset = 1.0 ; # factor by which p1 should be greater than p2 $graphratio = 0 ; # whether to make a graph of the posterior prob of the ratio # of p1 to p2 # new feature not included in bernplot2.p: $alpha = 1.0 ; # the default of 1 is the laplace-bayes prior. set this to zero # to get the improper prior. set to 0.05 to get a broadminded prior. eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift; if ( $r1 < 0 || $n1 < 0 || $r2 < 0 || $n2 < 0 ) { print STDERR "You must supply r and n\n" ; &print_usage (); exit(0); } if ( $alpha > 0.0 ) { $r1 += $alpha ; $n1 += 2.0 * $alpha ; $r2 += $alpha ; $n2 += 2.0 * $alpha ; } sub print_usage { print STDERR " bernplot2.p r1=30 n1=1000000 r2=3 n2=10000 [offset=2] [email=1]\n" ; print STDERR " bernplot2.p r1=30 n1=1000000 r2=3 n2=10000 graphratio=1 samples=75\n" ; print STDERR " this program writes confidence interval information to\n"; print STDERR " stderr and a set of commands to use within gnuplot to stdout.\n"; print STDERR " \n"; print STDERR " graphratio=1 produces a graph of the posterior prob of (p1/p2)\n" ; print STDERR " in which case samples=75 is recommended\n" ; } # $errors = $r1 ; # $trials = $n1 ; # # print STDERR "rough confidence interval 1:\n" ; &rangefinder($r1,$n1); # find_ebs_and_print ( 0 ) ; $lgmin1 = $lgmin ; $lgmax1 = $lgmax ; # $errors = $r2 ; # $trials = $n2 ; # #print STDERR "rough confidence interval 2:\n" ; &rangefinder($r2,$n2); # &find_ebs_and_print ( 0 ) ; if ( $lgmin > $lgmin1 ) { $lgmin = $lgmin1 ; } if ( $lgmax < $lgmax1 ) { $lgmax = $lgmax1 ; } # # &write_rN_file($r1,$n1); # writing conditional on email variable $file1=$file ; @c1 = @c ; @p1 = @mass ; &write_rN_file($r2,$n2); # these routines do the main work. $file2 = $file ; @c2 = @c ; @p2 = @mass ; # do integral of p1 c2 to find Prob( p2 p1 ) = %8.5g , %8.5g\n", $p1lt2l , $p1lt2 ; printf STDERR "sum = %8.5g , %8.5g\n", $p2lt1l+$p1lt2l , $p2lt1+$p1lt2 ; } $p2lt1 = ( $p2lt1 + $p2lt1l ) * 0.5 ; $p1lt2 = ( $p1lt2 + $p1lt2l ) * 0.5 ; printf STDERR "prob ( p2 < p1 ) = %8.5g\n", $p2lt1 ; printf STDERR "prob ( p2 > p1 ) = %8.5g\n", $p1lt2 ; printf STDERR "sum = %8.5g\n===========\n", $p2lt1+$p1lt2 ; if ( $offset != 1.0 ) { # also find Prob( p2<2 p1 ), Prob( p1 < 2 p2 ) (but using logit basis now ) print STDERR "this routine needs to be updated from bernplot2 to bernplot3\n" ; $ioffset = int ( ($offset)/$dlg ) ; $eoffset = ( $dlg * $ioffset ) ; if ( 1 || $ioffset >= 1 ) { &dooffset(); } $ioffset = - int ( ($offset)/$dlg ) ; $eoffset = ( $dlg * $ioffset ) ; if ( 1 || $ioffset >= 1 ) { &dooffset(); } } if (!$email) { # # talk to gnuplot # print STDOUT "#\n# gnuplot\n set size 0.7,0.7;set logs x; set nologs y; set nologs x; set nokey\n" ; # printf STDOUT "set xrange [%7.3g:%7.3g]\n", $pmin, $pmax ; printf STDOUT "set xrange [%7.3g:%7.3g]\n", $lgmin, $lgmax ; if ( $psgnu ) { print STDOUT "set output '$file.ps'\n" ; print STDOUT "set term post\n" ; } print STDOUT "plot '$file1' u 2:3 w lines 1 1, " ; print STDOUT " '$file2' u 2:3 w lines 2 2 " ; print STDOUT "\n" ; if ( !$psgnu ) { # print STDOUT "pause -1 'press return'\n" ; } ############### # # or plot ordinary prob versus p # # ################# print STDOUT "#\n# gnuplot\n set size 0.7,0.7;set logs x; set nologs y; set nologs x; set nokey\n" ; printf STDOUT "set xrange [0.0:1.0]\n" ; if ( $psgnu ) { print STDOUT "set output '$file.b.ps'\n" ; print STDOUT "set term post\n" ; } print STDOUT "plot '$file1' u 1:5 w lines 1 1" ; print STDOUT ", '$file2' u 1:5 w lines 2 2\n" ; print STDOUT "\n" ; if ( !$psgnu ) { # print STDOUT "pause -1 'press return'\n" ; } if ( $graphratio ) { $file = "$file1.$file2.r" ; open ( F , ">$file" ) ; &writegratio(); } } sub writegratio { # this now does a graph of logit, rather than ratio. print STDERR "this routine needs to be updated from bernplot2 to bernplot3\n" ; $logten = log(10.0) ; for ( $ds = -$samples + 1 ; $ds <= $samples-1 ; $ds ++ ) { $pratio[$ds+$samples] = 0.0 ; } for ( $s1 = 1 ; $s1 <= $samples ; $s1 ++ ) { $thisp1 = $p1[$s1] ; for ( $s2 = 1 ; $s2 <= $samples ; $s2 ++ ) { $ds = $s1 - $s2 + $samples ; $pratio[$ds] += $thisp1 * $p2[$s2] ; } } for ( $ds = 1 ; $ds <= 2*$samples-1 ; $ds ++ ) { $logratio[$ds] = ( $ds - $samples ) * $dlg ; $ratio[$ds] = exp( $logratio[$ds] ) ; $logratio[$ds] = $logratio[$ds] / $logten ; printf F "%3d %8.5g %8.5g %8.5g\n" , $ds , $logratio[$ds] , $ratio[$ds] , $pratio[$ds] ; } close (F); print STDOUT "# gnuplot\n" ; print STDOUT "set autos x ; set nologs x\n" ; print STDOUT "set xlabel 'p1/p2 in powers of ten'\n" ; print STDOUT "plot '$file' u 2:4 w lines 1 1" ; print STDOUT "\n" ; if ( !$psgnu ) { # print STDOUT "pause -1 'press return'\n" ; } } sub like { local($p,$r,$N)=@_; return $p**$r * (1-$p)**($N-$r) } sub likenorm { local($p,$r,$N)=@_; return &like($p,$r,$N) / &like( 1.0*$r/$N , $r , $N ); } sub logit { local($p)=@_ ; return -log((1-$p)/$p) ; } sub sigmoid { # inverse logit local($l)=@_ ; return 1.0/(1.0+exp(-$l)) ; } sub rangefinder { # this rangefinder returns global variables lgmax and lgmin local ( $r, $N ) = @_ ; # find reasonable range of p # $pml = $r / $N ; $ropp = $N-$r ; if ( ($r == 0 ) || ($ropp == 0) ) { printf STDERR "Sorry, you should use a non-zero regularizer . r=0 and r=N gives improper distributions\n" ; exit(0); } $lgml = log( $r * 1.0 / $ropp ) ; # old method on log scale for small p if ( $pml > 0.5 ) { $rs = $ropp ; } else { $rs = $r ; } $sdl = 1.0 / sqrt( $rs ) ; # print "old : $sdl ---" ; # new method $sdl = 2.0 * &cosh( $lgml / 2.0 ) / sqrt( $N ) ; # print "new : $sdl \n" ; $lgmin = $lgml - $zrangemin * $sdl ; $pmin=&sigmoid($lgmin); $lgmax = $lgml + $zrangemax * $sdl ; $pmax=&sigmoid($lgmax); if ( $lgmax > 40.0 ) { $lgmax = 40 ; } if ( $lgmin < -40 ) { $lgmin = -40 ; } # print STDERR "lpmax = $lpmax , $lpmin\n" ; } sub write_rN_file { local ( $r, $N ) = @_ ; $file = "$r.$N" ; if (!$email) { open ( F, ">$file" ) ; # lpmin and lpmax must be set } $pml = $r / $N ; # $lpml = log( $pml ) ; $sdl = 1.0 / sqrt( $r ) ; $pmin=&sigmoid($lgmin); $pmax=&sigmoid($lgmax); $dlg = ( $lgmax - $lgmin) / ( $samples - 1 ) ; $lg = $lgmin ; $cum = 0.0 ; $c[0] = 0.0 ; for ( $s = 1 ; $s <= $samples ; $s ++ , $lg += $dlg ) { $p= &sigmoid($lg) ; $ps[$s] = $p ; $lgs[$s] =$lg ; $l[$s] = &likenorm($p,$r,$N) ; # if the prior is included, this is actually the posterior over the logit. To get the posterior over p, need to rescale. Or use bernplot2.p! $cum += ( $l[$s] * $dlg ) ; $mass[$s] = $l[$s] * $dlg ; $c[$s] = $cum ; } $waiting05 = 1; $waiting10 = 1 ; $waiting90 = 1 ; $waiting95 = 1 ; $waiting1left = 1 ;$waiting1right = 1 ; for ( $s = 1 ; $s <= $samples ; $s ++ ) { $c[$s] /= $cum ; $newcum = $c[$s] ; $l[$s] /= $cum if ( $rescalelike ) ; $mass[$s] /= $cum ; # unused? if ( $waiting05 ) { if ( $newcum > 0.05 ) { $perc05 = $ps[$s] ; $waiting05 = 0 ; } } elsif ( $waiting10 ) { if ( $newcum > 0.10 ) { $perc10 = $ps[$s] ; $waiting10 = 0 ; } } elsif ( $waiting90 ) { if ( $newcum > 0.90 ) { $perc90 = $ps[$s] ; $waiting90 = 0 ; } } elsif ( $waiting95 ) { if ( $newcum > 0.95 ) { $perc95 = $ps[$s] ; $waiting95 = 0 ; } } if ( $waiting1left ) { if ( $l[$s] > $level ) { $waiting1left = 0 ; $leftpoint = $ps[$s] ; $leftc = $newcum; } } elsif ( $waiting1right ) { if ( $l[$s] < $level ) { $waiting1right = 0 ; $rightpoint = $ps[$s] ; $rightc = $newcum; } } if (!$email) { # %%%%%%%%%%%%%%%%%%%% main output %%%%%%%%%%%%%%%%%%%%%%%% $p = $ps[$s] ; $postp = $l[$s]/($p*(1.0-$p)) ; printf F "%9.5g %9.5g %9.5g %9.5g %9.5g\n" , $ps[$s] , $lgs[$s] , $l[$s] , $c[$s] , $postp ; } } # talk to the user. # $percent = "%" ; # printf STDERR "#\n# Accurate results:\n#\n" ; printf STDERR "# 5th percentile %7.3g\n" , $perc05 ; printf STDERR "# 10th percentile %7.3g\n" , $perc10 ; printf STDERR "# 90th percentile %7.3g\n" , $perc90 ; printf STDERR "# 95th percentile %7.3g\n" , $perc95 ; printf STDERR "# interval %7.3g - %7.3g contains %4.1f%s of the posterior\n" , $leftpoint , $rightpoint , ($rightc - $leftc)*100.0 , $percent ; # printf STDERR "# More refined confidence interval, %6.3g%s\n", ($rightc - $leftc)*100.0 , $percent ; # print STDERR # "# errors trials p_ML p_plaus point upper lower\n" ; print STDERR "# p_plaus upper lower\n" ; # printf STDERR # "%6s %6s " , # $r, $N ; printf STDERR "%8.4g %8.4g %8.4g ", $pml , $rightpoint, $leftpoint ; print STDERR "\n" ; } sub dooffset { print STDERR "[now finding probs for offset of $ioffset bins]\n" ; for ( $s = 1, $p2lt1=0.0 , $p1lt2=0.0, $p2lt1l=0.0 , $p1lt2l=0.0 ; $s <= $samples ; $s ++ ) { if (( $s+$ioffset <= $samples ) && ( $s+$ioffset >= 1 ) ) { $p2lt1 += $c2[$s] * $p1[$s+$ioffset] ; $p1lt2 += $c1[$s+$ioffset] * $p2[$s] ; $p2lt1l += $c2[$s-1] * $p1[$s+$ioffset] ; } elsif ( $s+$ioffset > $samples ) { $p1lt2 += $p2[$s] ; } if (( $s-1+$ioffset <= $samples ) && ( $s-1+$ioffset >= 1 ) ) { $p1lt2l += $c1[$s-1+$ioffset] * $p2[$s] ; } elsif ( $s-1+$ioffset > $samples ) { $p1lt2l += $p2[$s] ; } } if ( $ioffset < 0 ) { if($verbose){ printf STDERR "%3d %8.6g %8.6g\n" ,$samples + $ioffset , $c1[ $samples + $ioffset ] , (1.0 - $c1[ $samples + $ioffset ] ) ; } $p2lt1 += (1.0 - $c1[ $samples + $ioffset ] ) * 1.0 ; $p2lt1l += (1.0 - $c1[ $samples + $ioffset ] ) * 1.0 ; } if($verbose){ printf STDERR "prob ( p2 < p1 ) = %8.5g , %8.5g\n", $p2lt1l, $p2lt1 ; printf STDERR "prob ( p2 > p1 ) = %8.5g , %8.5g\n", $p1lt2l , $p1lt2 ; printf STDERR "sum = %8.5g , %8.5g\n", $p2lt1l+$p1lt2l , $p2lt1+$p1lt2 ; } $p2lt1 = ( $p2lt1 + $p2lt1l ) * 0.5 ; $p1lt2 = ( $p1lt2 + $p1lt2l ) * 0.5 ; printf STDERR "prob ( %8.5g p2 < p1 ) = %8.5g\n", $eoffset, $p2lt1 ; printf STDERR "prob ( %8.5g p2 > p1 ) = %8.5g\n", $eoffset, $p1lt2 ; printf STDERR "sum = %8.5g\n===========\n", $p2lt1+$p1lt2 ; } sub cosh { local($x)=@_; return ( 0.5 * ( exp(-$x) + exp($x) ) ) ; } # print "cosh(0.1) = ".&cosh(0.1)."\n" ;