5 // Created by SarahsWork on 12/9/13.
6 // Copyright (c) 2013 Schloss Lab. All rights reserved.
11 /***********************************************************************/
12 //KLD <- function(x,y) sum(x *log(x/y))
13 //JSD<- function(x,y) sqrt(0.5 * KLD(x, (x+y)/2) + 0.5 * KLD(y, (x+y)/2))
14 EstOutput JSD::getValues(vector<SharedRAbundVector*> shared) {
22 for (int i = 0; i < shared[0]->getNumBins(); i++) {
23 //store in temps to avoid multiple repetitive function calls
24 double tempA = shared[0]->getAbundance(i);
25 double tempB = shared[1]->getAbundance(i);
27 if (tempA == 0) { tempA = 0.000001; }
28 if (tempB == 0) { tempB = 0.000001; }
30 double denom = (tempA+tempB)/(double)2.0;
32 if (tempA != 0) { KLD1 += tempA * log(tempA/denom); } //KLD(x,m)
33 if (tempB != 0) { KLD2 += tempB * log(tempB/denom); } //KLD(y,m)
36 data[0] = sqrt((0.5*KLD1) + (0.5*KLD2));
38 if (isnan(data[0]) || isinf(data[0])) { data[0] = 0; }
43 m->errorOut(e, "JSD", "getValues");
48 /***********************************************************************/