5 // Created by Sarah Westcott on 1/21/14.
6 // Copyright (c) 2014 Schloss Lab. All rights reserved.
9 #include "sharedrjsd.h"
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 RJSD::getValues(vector<SharedRAbundVector*> shared) {
22 vector<int> countA = shared[0]->getAbundances();
23 vector<int> countB = shared[1]->getAbundances();
27 for (int i = 0; i < shared[0]->getNumBins(); i++) {
32 for (int i = 0; i < shared[0]->getNumBins(); i++) {
33 double tempA = countA[i] / totalA;
34 double tempB = countB[i] / totalB;
36 tempA = countA[i] / totalA;
37 tempB = countB[i] / totalB;
39 if (tempA == 0) { tempA = 0.000001; }
40 if (tempB == 0) { tempB = 0.000001; }
42 double denom = (tempA+tempB)/(double)2.0;
44 if (tempA != 0) { KLD1 += tempA * log(tempA/denom); } //KLD(x,m)
45 if (tempB != 0) { KLD2 += tempB * log(tempB/denom); } //KLD(y,m)
49 data[0] = sqrt((0.5*KLD1) + (0.5*KLD2));
51 if (isnan(data[0]) || isinf(data[0])) { data[0] = 0; }
56 m->errorOut(e, "RJSD", "getValues");