-
Notifications
You must be signed in to change notification settings - Fork 1
/
moments.cpp
60 lines (56 loc) · 2.28 KB
/
moments.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <cmath>
#include <iostream>
#include "moments.h"
#include "rk4.h"
#include "util.h"
#include "mid.h"
namespace TrajectoryLikelihood {
Moments::Moments (const IndelParams& params, double tMax, double dt, int verbose) :
params (params),
mu (params.totalRightwardDeletionRatePerSite()),
lambda (params.totalInsertionRatePerSite()),
x (params.rIns),
y (params.rDel),
T (tMax / dt + 1, vector<double> (4, 0.)),
dt (dt),
tMax (tMax),
verbose (verbose)
{
auto eval_dmc_dt = [&] (double t, const vector<double>& T, vector<double>& dT_dt) {
const double a = this->a(T,t), b = this->b(T,t), c = this->c(T,t);
const double p = this->p(T,t), q = this->q(T,t), r = this->r(T,t);
const double f = this->f(T,t), g = this->g(T,t), h = this->h(T,t);
dT_dt[0] = (-1 + b + c) * (lambda + mu) - (b * f * mu * (-1 + y))/(1 + (-1 + f + h) * y);
dT_dt[1] = lambda - b * lambda - (b * (f + h) * mu)/(1 + (-1 + f + h) * y);
dT_dt[2] = -(-1 + b + c) * lambda - (f * (f + h) * mu * (c * q + b * (p + q)))/((h * p + f * (p + q)) * (1 + (-1 + f + h) * y));
dT_dt[3] = ((f + h) * mu * (-c * (-1 + f + h) * q + b * (p + q - h * q)))/((h * p + f * (p + q)) * (1 + (-1 + f + h) * y));
if (verbose > 3)
cerr << "t=" << t << " T=" << vec_to_string(T)
<< " SI=" << SI(t) << " SD=" << SD(t)
<< " a=" << a << " b=" << b << " c=" << c
<< " f=" << f << " g=" << g << " h=" << h
<< " p=" << p << " q=" << q << " r=" << r
<< " dT/dt=" << vec_to_string(dT_dt)
<< endl;
};
T[0][0] = 1.;
RungeKutta4 T_solver (4);
for (int i = 1; i < T.size(); ++i) {
const double t = dt * (double) (i - 1);
T_solver.step (eval_dmc_dt, t, dt, T[i-1], T[i]);
if (verbose > 2)
cerr << "t=" << t
<< " T=" << vec_to_string(T[i])
<< endl;
}
}
vector<vector<double> > Moments::chopZoneLikelihoods (int maxLen) const {
const auto& T = this->T.back();
const double t = tMax;
const double a = this->a(T,t), b = this->b(T,t), c = this->c(T,t);
const double f = this->f(T,t), g = this->g(T,t), h = this->h(T,t);
const double p = this->p(T,t), q = this->q(T,t), r = this->r(T,t);
const MID_HMM hmm (a, b, c, f, g, h, p, q, r, verbose);
return hmm.chopZoneLikelihoods (maxLen);
}
}