1 /*
2  *  shannon.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 1/7/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9 
10 #include "shannon.h"
11 
12 /***********************************************************************/
13 
getValues(SAbundVector * rank)14 EstOutput Shannon::getValues(SAbundVector* rank){
15 	try {
16 		//vector<double> shannonData(3,0);
17 		data.resize(3,0);
18 
19 		double shannon = 0.0000;  //hprime
20 		double hvara=0.0000;
21 
22 		double maxRank = rank->getMaxRank();
23 		int sampled = rank->getNumSeqs();
24 		int sobs = rank->getNumBins();
25 
26 		for(int i=1;i<=maxRank;i++){
27 			double p = ((double) i)/((double)sampled);
28 			shannon += (double)rank->get(i)*p*log(p); //hprime
29 			hvara  += (double)rank->get(i)*p*pow(log(p),2);
30 		}
31 		shannon = -shannon;
32 
33 		double hvar = (hvara-pow(shannon,2))/(double)sampled+(double)(sobs-1)/(double)(2*sampled*sampled);
34 
35 		double ci = 0;
36 
37 		if(hvar>0){
38 			ci = 1.96*pow(hvar,0.5);
39 		}
40 
41 		double shannonhci = shannon + ci;
42 		double shannonlci = shannon - ci;
43 
44 		data[0] = shannon;
45 		data[1] = shannonlci;
46 		data[2] = shannonhci;
47 
48 		if (isnan(data[0]) || isinf(data[0])) { data[0] = 0; }
49 		if (isnan(data[1]) || isinf(data[1])) { data[1] = 0; }
50 		if (isnan(data[2]) || isinf(data[2])) { data[2] = 0; }
51 
52 		return data;
53 	}
54 	catch(exception& e) {
55 		m->errorOut(e, "Shannon", "getValues");
56 		exit(1);
57 	}
58 }
59 
60 /***********************************************************************/
61