#!/usr/local/bin/perl -w $N=10; # number of data points $U=10; # number of urns $r=3; # observed value of sum $o="urn" ; $co="urn.c" ; $fo="urn.f" ; # forward probability (for chapter 0) $uexample = 3 ; # which value of u to use for an example distbn over r eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift; $max = 0 ; $cmax = 0 ; $ctot = 0 ; $pu = 1.0/($U+1.0) ; $out = "" ; $cond = "" ; $sum = 0.0 ; foreach $u (0..$U ) { $choose = 1 ; $fu = $u * 1.0 / $U ; $ofu = 1.0 - $fu ; foreach $n (0..$N ) { if ( $n > 0 ) { $choose = $choose * ( $N - $n + 1 ) / $n ; } $prob = $choose * ( $fu ** $n) * ( $ofu ** ($N-$n) ) ; $sum += $prob * $pu ; $this = sprintf "%8.2g " , $prob * $pu ; $out .= $this ; if ( $n == $r ) { $cond .= $this ; $cond2[$u] = $prob * $pu ; if ( $cmax < $prob * $pu ) { $cmax = $prob * $pu ; } $ctot += $prob * $pu ; } if ( $u == $uexample ) { $forward[$n] = sprintf "%8.2g\n", $prob ; if ( $fmax < $prob ) { $fmax = $prob ; } } if ( $max < $prob * $pu ) { $max = $prob * $pu ; } } $out .= "\n" ; } $cond .= "\n" ; open ( O , ">$o" ) ; print O $cond , $out ; close(O) ; ############################ $cond = "\# conditional prob of u given r=$r , N=$N\n\#$cond# u\tP(u|r,N)\n" ; $pred = 0.0 ; foreach $u ( 0..$U ) { $cond2[$u] = $cond2[$u] / $ctot ; $cond .= sprintf ( "%d\t%10.2g\n" , $u, $cond2[$u] ); $pred += $cond2[$u] * $u / $U ; } print "predictive probability = $pred\n" ; open ( O , ">$co" ) ; print O $cond ; close(O) ; ################################ $cond = "\# forward prob of r given u=$u , N=$N\n" ; open ( O , ">$fo" ) ; print O $cond ; foreach $n ( 0..$N ) { print O "$n\t $forward[$n]" ; } close(O) ; ######################################### $NN = $N + 1 ; $UU = $U + 1 ; $this = sprintf "%.2f " , $cmax ; print "sum = $sum\n" ; print "head -1 $o | hintond.p o=$o.cond.ps zero=0.0 range=$this rows=1 background=0.0 $UU\n" ; $this = sprintf "%.2f " , $max ; print "tail -$UU $o | hintond.p rotate=1 o=$o.joint.ps zero=0.0 range=$this rows=$UU bellh=$NN background=0.0 $UU\n" ; print "gnuplot\n set nokey\n set term post\n set size 0.4,0.5\n" ; print "set tics out; set yrange [-0.04:0.34] ; set xrange [-0.50:10.5]\n" ; print "set xlabel 'u'\n set xtics border nomirror 0,1,10\n set ytics nomirror\n" ; print "set ylabel 'P(u|n=$r)'\n" ; print "set output '$o.c.g.ps'\n" ; print "plot '$co' u 1:2 w impulses \n" ; print "set term X\n" ; print "replot\n pause -1 '?'\n" ; print "set term post ; set output '$o.c.g.l.ps'\n" ; print "set logs y ; set autos y\n" ; print "plot '$co' u 1:2 w impulses \n" ; print "set term X\n" ; print "replot\n" ; print "gnuplot\n set nokey\n set term post\n set size 0.4,0.5\n" ; print "set tics out; set yrange [-0.04:0.34] ; set xrange [-0.50:10.5]\n" ; print "set xlabel 'r'\n set xtics border nomirror 0,1,10\n set ytics nomirror\n" ; print "set ylabel 'P(r|f,N)'\n" ; print "set output '$o.f.g.ps'\n" ; print "plot '$fo' u 1:2 w impulses \n" ; print "set term X\n" ; print "replot\n pause -1 '?'\n" ; print "set term post ; set output '$o.f.l.ps'\n" ; print "set logs y ; set autos y\n" ; print "plot '$fo' u 1:2 w impulses \n" ; print "set term X\n" ; print "replot\n" ;