Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Kullback–Leibler divergence Module?

by ZWcarp (Beadle)
on May 16, 2012 at 15:17 UTC ( #970857=perlquestion: print w/ replies, xml ) Need Help??
ZWcarp has asked for the wisdom of the Perl Monks concerning the following question:

Is anyone aware of a Kullback–Leibler divergence (symmetrized) perl implementation or module ? I basically coded one myself that takes in a file and does all the variables but its really not very efficient. Thanks for your time.

Comment on Kullback–Leibler divergence Module?
Re: Kullback–Leibler divergence Module?
by kennethk (Monsignor) on May 16, 2012 at 15:43 UTC

    A quick search on CPAN yielded no useful results, though Kullback Leibler Perl yielded a few results.

    If you've already gone through the trouble of coding, it might be worth while to post the code here for some aid in optimization. Have you run it through Devel::NYTProf to see where your bottle necks are?

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: Kullback–Leibler divergence Module?
by snape (Pilgrim) on May 16, 2012 at 19:19 UTC

    You may use a shell script, that calls perl (do the computations in perl) and then call R. R has a FNN package/module which has Kl.dist function which you may use for K-L distance.

    Update: I have written the KL Divergence for discrete probability distribution samples. It is very naive but might be useful.

    #!/usr/bin/perl -w ## for Discrete Probability Distribution ## considering that sum of probability values in both arrays equals to + 1 my $dist = 0; my @terms = ('a', 'b','c','d'); my @P = (0, 0.3, 0.4, 0.3); my @Q = (0.2, 0.2, 0.3, 0.3); if ( (scalar(@P) != scalar(@terms) ) && (scalar(@Q) != scalar(@terms)) + ){ print " The size should be same \n"; exit; } else{ for(my $i = 0; $i<= $#P; $i++){ my $temp = 0 if($P[$i] == 0 || $Q[$i] == 0); $temp = $P[$i]*log($P[$i]/$Q[$i]) if($P[$i] != 0 && $Q[$i] != +0); $dist = $dist + $temp; } } print "The Kullback Distance symmetric for discrete Distribution is :" +, $dist,"\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://970857]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2014-09-16 12:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (14 votes), past polls