]> git.donarmstrong.com Git - mothur.git/blob - simpson.cpp
fixed rf memory issue in constructure
[mothur.git] / simpson.cpp
1 /*
2  *  simpson.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 "simpson.h"
11
12 /***********************************************************************/
13
14 EstOutput Simpson::getValues(SAbundVector* rank){
15         try {
16                 //vector<double> simpsonData(3,0);
17                 data.resize(3,0);
18                 double simpson = 0.0000;
19                 double ci = 0;
20         
21                 double maxRank = (double)rank->getMaxRank();
22                 double sampled = (double)rank->getNumSeqs();
23                 double sobs = (double)rank->getNumBins();
24         
25                 double firstTerm = 0;
26                 double secondTerm = 0;
27         
28                 if(sobs != 0){
29                         double simnum=0.0000;
30                 
31                         for(unsigned long long i=1;i<=maxRank;i++){
32                                 simnum += (double)(rank->get(i)*i*(i-1));
33                         }
34                         
35                         simpson = simnum / (sampled*(sampled-1));
36                 
37                         for(unsigned long long i=1;i<=maxRank;i++){
38                                 double piI = (double) i / (double)sampled;
39                                 firstTerm += rank->get(i) * pow(piI, 3);
40                                 secondTerm += rank->get(i) * pow(piI, 2);
41                         }
42                 
43                         double var = (4.0 / sampled) * (firstTerm - secondTerm*secondTerm);
44                         ci = 1.95 * pow(var, 0.5);
45                 }
46         
47                 double simpsonlci = simpson - ci;
48                 double simpsonhci = simpson + ci;
49                 
50                 data[0] = simpson;
51                 data[1] = simpsonlci;
52                 data[2] = simpsonhci;
53                 
54                 if (isnan(data[0]) || isinf(data[0])) { data[0] = 0; }
55                 if (isnan(data[1]) || isinf(data[1])) { data[1] = 0; }
56                 if (isnan(data[2]) || isinf(data[2])) { data[2] = 0; }
57         
58                 return data;
59         }
60         catch(exception& e) {
61                 m->errorOut(e, "Simpson", "getValues");
62                 exit(1);
63         }
64 }
65
66 /***********************************************************************/