# No.25 Perl Sample (code key - 73) # Learning Perl: count log probability (対数確率を計算する) # Tomonori Nagano # Last Update: January 18, 2008 # # This file is encoded in Unicode (UTF-8). If you see gibberish characters, # please re-encode the file in utf-8. # $probA = 50/1000 ; # supporse word A appeard 50 times in a 1000-word corpus $probB = 10/1000 ; # supporse word B appeard 10 times in a 1000-word corpus print "Probability of A (what it usually looks): $probA\n" ; # prob of A is 0.05 print "Probability of B (what it usually looks): $probB\n" ; # prob of B is 0.01 printf "Probability of A (what precisely Perl gets): %20.20f\n",$probA ; # this should be 0.05 printf "Probability of B (what precisely Perl gets): %20.20f\n",$probB ; # this should be 0.01 print "Independent prob of A*B (the value on surface): ".$probA*$probB."\n" ; # the surface value printf "Independent prob of A*B (the precise value): %30.30f\n",$probA*$probB ; # the real value $logProbA = log(50/1000) ; # taking log probability $logProbB = log(10/1000) ; # taking log probability print "Independent log prob of A*B: ".(-1*($logProbA+$logProbB))."\n" ; # compute log probability