libsequence  1.9.5
Garud.cc
1 #include <algorithm>
2 #include <set>
3 #include <string>
4 #include <cmath>
5 #include <numeric>
6 #include <Sequence/SummStats/Garud.hpp>
7 
8 using namespace std;
9 
10 namespace Sequence
11 {
13  H1H12(const SimData &d)
22  {
23  if (d.empty())
24  return GarudStats();
25  set<string> uhaps(d.begin(), d.end());
26  vector<string> vuhaps(uhaps.size());
27  std::move(uhaps.begin(), uhaps.end(), vuhaps.begin());
28  vector<double> hapcounts;
29  hapcounts.reserve(uhaps.size());
30  for(auto & uh : uhaps)
31  {
32  hapcounts.push_back(static_cast<double>(std::count(d.begin(),d.end(),uh)));
33  }
34  const double denom = static_cast<double>(d.size() * (d.size() - 1));
35  double H1 = 0.0;
36  for(auto c : hapcounts)
37  {
38  H1 += c*(c-1.0);
39  }
40  H1 /= denom;
41 
42  sort(hapcounts.begin(), hapcounts.end(),
43  std::bind(greater<double>(), std::placeholders::_1,
44  std::placeholders::_2));
45  double H12 = H1
46  + 2. * hapcounts[0] * hapcounts[1]
47  / std::pow(double(d.size()), 2.);
48  double H2H1 = (H1
49  - double(hapcounts[0] * (hapcounts[0] - 1))
50  / double(d.size() * (d.size() - 1)))
51  / H1;
52  return GarudStats(H1, H12, H2H1);
53  }
54 }
GarudStats
Definition: Garud.cc:22
STL namespace.
The namespace in which this library resides.