Quetzal-CoaTL
The Coalescence Template Library
Loading...
Searching...
No Matches
tajimasD.hpp
1// Copyright 2022 Arnaud Becheler <arnaud.becheler@gmail.com>
2
3/*********************************************************************** * This program is free software; you can
4 *redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free
5 *Software Foundation; either version 2 of the License, or * (at your option) any later version. *
6 * *
7 ***************************************************************************/
8
9#ifndef __TAJIMAS_D_H_INCLUDED__
10#define __TAJIMAS_D_H_INCLUDED__
11
12#include "../utils/null_output_iterator.hpp"
13
14#include <algorithm> // std::generate
15#include <cmath> // sqrt
16#include <iostream>
17
19{
26template <typename T = double> class tajimasD
27{
28 public:
29 using value_type = T;
30
31 private:
32 value_type _pi;
33 value_type _S;
34 value_type _n;
35 std::pair<value_type, value_type> _a;
36 value_type _b1;
37 value_type _b2;
38 value_type _c1;
39 value_type _c2;
40 value_type _e1;
41 value_type _e2;
42 value_type _D;
48 std::pair<value_type, value_type> compute_a(int n)
49 {
50 auto a = std::make_pair(0.0, 0.0);
53 std::generate(it1, it2, [i = 1, &a]() mutable {
54 a.first += 1. / i;
55 a.second += 1. / (i * i);
56 return i++;
57 });
58 return a;
59 }
65 constexpr auto compute_b1(double n)
66 {
67 return (n + 1.0) / (3.0 * (n - 1.0));
68 }
74 constexpr auto compute_b2(double n)
75 {
76 return (2.0 * (n * n + n + 3.0)) / (9.0 * n * (n - 1.0));
77 }
83 constexpr auto compute_c1(value_type a1, value_type b1)
84 {
85 return b1 - 1.0 / a1;
86 }
92 constexpr auto compute_c2(double n, value_type a1, value_type a2, value_type b2)
93 {
94 return b2 - (n + 2.0) / (a1 * n) + a2 / (a1 * a1);
95 }
99 constexpr auto compute_e1(value_type c1, value_type a1)
100 {
101 return c1 / a1;
102 }
106 constexpr auto compute_e2(value_type a1, value_type a2, value_type c2)
107 {
108 return c2 / (a1 * a1 + a2);
109 }
115 constexpr auto compute_D(value_type pi, value_type S, value_type a1, value_type e1, value_type e2)
116 {
117 return (pi - S / a1) / std::sqrt(e1 * S + e2 * S * (S - 1.0));
118 }
119
120 public:
128 tajimasD(double pi, double S, double n)
129 : _pi(pi), _S(S), _n(n), _a(compute_a(n)), _b1(compute_b1(n)), _b2(compute_b2(n)),
130 _c1(compute_c1(_a.first, _b1)), _c2(compute_c2(n, _a.first, _a.second, _b2)), _e1(compute_e1(_c1, _a.first)),
131 _e2(compute_e2(_a.first, _a.second, _c2)), _D(compute_D(pi, S, _a.first, _e1, _e2))
132 {
133 }
137 constexpr value_type get()
138 {
139 return _D;
140 }
141
142 value_type pi() const
143 {
144 return _pi;
145 };
146 value_type S() const
147 {
148 return _S;
149 };
150 value_type n() const
151 {
152 return _n;
153 };
154 value_type a1() const
155 {
156 return _a.first;
157 };
158 value_type a2() const
159 {
160 return _a.second;
161 };
162 value_type b1() const
163 {
164 return _b1;
165 };
166 value_type b2() const
167 {
168 return _b2;
169 };
170 value_type c1() const
171 {
172 return _c1;
173 };
174 value_type c2() const
175 {
176 return _c2;
177 };
178 value_type e1() const
179 {
180 return _e1;
181 };
182 value_type e2() const
183 {
184 return _e2;
185 };
186 value_type D() const
187 {
188 return _D;
189 };
190}; // class TajimasD
191
192} // namespace quetzal::polymorphism::statistics
193
194#endif
Class to compute Tajima's D.
Definition tajimasD.hpp:27
tajimasD(double pi, double S, double n)
Constructor.
Definition tajimasD.hpp:128
constexpr value_type get()
Get the computed value.
Definition tajimasD.hpp:137
Allows to discard the output of functions that requires an output iterator.
Definition null_output_iterator.hpp:23
Classical summary statistics computations.
Definition polymorphism.hpp:21