Debian Codegraph @

src/Ewens.cpp

raw
1 #include "Ewens.h"
2
3
4 long double alleleFrequencyProbability(const map<int, int>& alleleFrequencyCounts, long double theta) {
5
6 int M = 0;
7 long double p = 1;
8
9 for (map<int, int>::const_iterator f = alleleFrequencyCounts.begin(); f != alleleFrequencyCounts.end(); ++f) {
10 int frequency = f->first;
11 int count = f->second;
12 M += frequency * count;
13 p *= (double) pow((double) theta, (double) count) / ((double) pow((double) frequency, (double) count) * factorial(count));
14 }
15
16 long double thetaH = 1;
17 for (int h = 1; h < M; ++h)
18 thetaH *= theta + h;
19
20 return factorial(M) / (theta * thetaH) * p;
21
22 }
23
24 AlleleFrequencyProbabilityCache alleleFrequencyProbabilityCache;
25
26 long double alleleFrequencyProbabilityln(const map<int, int>& alleleFrequencyCounts, long double theta) {
27 return alleleFrequencyProbabilityCache.alleleFrequencyProbabilityln(alleleFrequencyCounts, theta);
28 }
29
30 // Implements Ewens' Sampling Formula, which provides probability of a given
31 // partition of alleles in a sample from a population
32 long double impl_alleleFrequencyProbabilityln(const map<int, int>& alleleFrequencyCounts, long double theta) {
33
34 int M = 0; // multiplicity of site
35 long double p = 0;
36 long double thetaln = log(theta);
37
38 for (map<int, int>::const_iterator f = alleleFrequencyCounts.begin(); f != alleleFrequencyCounts.end(); ++f) {
39 int frequency = f->first;
40 int count = f->second;
41 M += frequency * count;
42 p += powln(thetaln, count) - (powln(log(frequency), count) + factorialln(count));
43 }
44
45 long double thetaH = 0;
46 for (int h = 1; h < M; ++h)
47 thetaH += log(theta + h);
48
49 return factorialln(M) - (thetaln + thetaH) + p;
50
51 }