-
Notifications
You must be signed in to change notification settings - Fork 20
/
maximization_step.d
354 lines (322 loc) · 13.3 KB
/
maximization_step.d
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/* Copyright (c) 2012,2013 Genome Research Ltd.
*
* Author: Stephan Schiffels <stephan.schiffels@sanger.ac.uk>
*
* This file is part of msmc.
* msmc is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
import std.math;
import std.stdio;
import std.random;
import std.exception;
import std.algorithm;
import std.string;
import model.msmc_model;
import model.triple_index;
import model.triple_index_marginal;
import powell;
import logger;
MSMCmodel getMaximization(double[] eVec, double[][] eMat, MSMCmodel params,
in size_t[] timeSegmentPattern, bool fixedPopSize, bool fixedRecombination, bool boundCrossCoal,
double loBoundLambda, double hiBoundLambda) {
auto minFunc = new MinFunc(eVec, eMat, params, timeSegmentPattern, fixedPopSize,
fixedRecombination, boundCrossCoal, loBoundLambda, hiBoundLambda);
auto powell = new Powell!MinFunc(minFunc);
auto x = minFunc.initialValues();
auto startVal = minFunc(x);
auto xNew = powell.minimize(x);
auto endVal = minFunc(xNew);
logInfo(format(", Q-function before: %s, after:%s\n", startVal, endVal));
return minFunc.makeParamsFromVec(xNew);
}
class MinFunc {
MSMCmodel initialParams;
const size_t[] timeSegmentPattern;
size_t nrSubpopPairs, nrParams;
const double[] expectationResultVec;
const double[][] expectationResultMat;
bool fixedPopSize, fixedRecombination, boundCrossCoal;
double loBoundLambda, hiBoundLambda;
this(in double[] expectationResultVec, in double[][] expectationResultMat, MSMCmodel
initialParams, in size_t[] timeSegmentPattern, bool fixedPopSize, bool fixedRecombination,
bool boundCrossCoal, double loBoundLambda, double hiBoundLambda) {
this.initialParams = initialParams;
this.timeSegmentPattern = timeSegmentPattern;
this.expectationResultVec = expectationResultVec;
this.expectationResultMat = expectationResultMat;
this.fixedPopSize = fixedPopSize;
this.fixedRecombination = fixedRecombination;
this.boundCrossCoal = boundCrossCoal;
this.loBoundLambda = loBoundLambda;
this.hiBoundLambda = hiBoundLambda;
nrSubpopPairs = initialParams.nrSubpopulations * (initialParams.nrSubpopulations + 1) / 2;
nrParams = nrSubpopPairs * cast(size_t)timeSegmentPattern.length;
if(!fixedRecombination)
nrParams += 1;
if(fixedPopSize)
nrParams -= initialParams.nrSubpopulations * cast(size_t)timeSegmentPattern.length;
}
double opCall(in double[] x) {
MSMCmodel newParams = makeParamsFromVec(x);
return -logLikelihood(newParams);
};
double[] initialValues()
out(x) {
assert(x.length == nrParams);
}
do {
auto x = getXfromLambdaVec(initialParams.lambdaVec);
if(!fixedRecombination)
x ~= toScaledRecombination(initialParams.recombinationRate);
return x;
}
double toScaledRecombination(double rec) {
return log(rec);
}
double fromScaledRecombination(double scaledRec) {
return exp(scaledRec);
}
double[] getXfromLambdaVec(double[] lambdaVec)
out(x) {
if(fixedPopSize)
assert(x.length == timeSegmentPattern.length *
(nrSubpopPairs - initialParams.nrSubpopulations));
else
assert(x.length == timeSegmentPattern.length * nrSubpopPairs);
}
do {
double[] ret;
size_t count = 0;
foreach(nrIntervalsInSegment; timeSegmentPattern) {
foreach(subpopPairIndex; 0 .. nrSubpopPairs) {
auto lIndex = count * nrSubpopPairs + subpopPairIndex;
auto tripleIndex = initialParams.marginalIndex.getIndexFromMarginalIndex(lIndex);
auto triple = initialParams.marginalIndex.getTripleFromIndex(tripleIndex);
auto p1 = initialParams.subpopLabels[triple.ind1];
auto p2 = initialParams.subpopLabels[triple.ind2];
if(p1 == p2) {
if(!fixedPopSize) {
auto l = lambdaVec[lIndex];
if(l < loBoundLambda)
l = loBoundLambda + 0.000000001;
if(l > hiBoundLambda)
l = hiBoundLambda - 0.000000001;
ret ~= toScaledLambda(l);
}
}
else {
auto marginalIndex1 =
initialParams.marginalIndex.subpopulationTripleToMarginalIndexMap[triple.time][p1][p1];
auto marginalIndex2 =
initialParams.marginalIndex.subpopulationTripleToMarginalIndexMap[triple.time][p2][p2];
auto lambda1 = lambdaVec[marginalIndex1];
auto lambda2 = lambdaVec[marginalIndex2];
auto lambda12 = lambdaVec[lIndex];
if(boundCrossCoal) {
if(lambda12 >= 0.5 * (lambda1 + lambda2))
lambda12 = 0.4999999999 * (lambda1 + lambda2);
}
else {
if(lambda12 < loBoundLambda)
lambda12 = loBoundLambda + 0.000000001;
if(lambda12 > hiBoundLambda)
lambda12 = hiBoundLambda - 0.000000001;
}
ret ~= toScaledCrossLambda(lambda12, lambda1, lambda2);
}
}
count += nrIntervalsInSegment;
}
return ret;
}
double toScaledLambda(double lambda) {
if(hiBoundLambda < double.infinity) {
auto frac = (lambda - loBoundLambda) / (hiBoundLambda - loBoundLambda);
return tan(frac * PI - PI_2);
}
else
return log(lambda - loBoundLambda);
}
double fromScaledLambda(double scaledLambda) {
if(hiBoundLambda < double.infinity) {
auto scaledFrac = (atan(scaledLambda) + PI_2) / PI;
return scaledFrac * (hiBoundLambda - loBoundLambda) + loBoundLambda;
}
else {
return exp(scaledLambda) + loBoundLambda;
}
}
double toScaledCrossLambda(double crossLambda, double lambda1, double lambda2) {
if(boundCrossCoal) {
auto ratio = 2.0 * crossLambda / (lambda1 + lambda2);
return tan(ratio * PI - PI_2);
}
else
return toScaledLambda(crossLambda);
}
double fromScaledCrossLambda(double scaledCrossLambda, double lambda1, double lambda2) {
if(boundCrossCoal) {
auto ratio = (atan(scaledCrossLambda) + PI_2) / PI;
return ratio * 0.5 * (lambda1 + lambda2);
}
else
return fromScaledLambda(scaledCrossLambda);
}
MSMCmodel makeParamsFromVec(in double[] x) {
auto lambdaVec = fixedPopSize ? getLambdaVecFromXfixedPop(x) : getLambdaVecFromX(x);
auto recombinationRate =
fixedRecombination ? initialParams.recombinationRate : getRecombinationRateFromX(x);
return new MSMCmodel(initialParams.mutationRate, recombinationRate, initialParams.subpopLabels,
lambdaVec, initialParams.nrTimeIntervals, initialParams.nrTtotIntervals,
initialParams.emissionRate.directedEmissions);
}
double[] getLambdaVecFromXfixedPop(in double[] x)
in {
assert(x.length == nrParams);
}
do {
auto lambdaVec = initialParams.lambdaVec.dup;
auto timeIndex = 0U;
auto valuesPerTime = nrSubpopPairs - initialParams.nrSubpopulations;
foreach(segmentIndex, nrIntervalsInSegment; timeSegmentPattern) {
foreach(intervalIndex; 0 .. nrIntervalsInSegment) {
auto xIndex = 0;
foreach(subpopPairIndex; 0 .. nrSubpopPairs) {
auto lIndex = timeIndex * nrSubpopPairs + subpopPairIndex;
auto tripleIndex = initialParams.marginalIndex.getIndexFromMarginalIndex(lIndex);
auto triple = initialParams.marginalIndex.getTripleFromIndex(tripleIndex);
auto p1 = initialParams.subpopLabels[triple.ind1];
auto p2 = initialParams.subpopLabels[triple.ind2];
if(p1 != p2) {
auto marginalIndex1 =
initialParams.marginalIndex.subpopulationTripleToMarginalIndexMap[triple.time][p1][p1];
auto marginalIndex2 =
initialParams.marginalIndex.subpopulationTripleToMarginalIndexMap[triple.time][p2][p2];
auto lambda1 = lambdaVec[marginalIndex1];
auto lambda2 = lambdaVec[marginalIndex2];
auto scaledCrossLambda = x[segmentIndex * valuesPerTime + xIndex];
lambdaVec[lIndex] = fromScaledCrossLambda(scaledCrossLambda, lambda1, lambda2);
xIndex += 1;
}
}
timeIndex += 1;
}
}
return lambdaVec;
}
double[] getLambdaVecFromX(in double[] x)
in {
assert(x.length == nrParams);
}
do {
auto lambdaVec = initialParams.lambdaVec.dup;
auto timeIndex = 0U;
foreach(segmentIndex, nrIntervalsInSegment; timeSegmentPattern) {
foreach(intervalIndex; 0 .. nrIntervalsInSegment) {
foreach(subpopPairIndex; 0 .. nrSubpopPairs) {
auto lIndex = timeIndex * nrSubpopPairs + subpopPairIndex;
auto xIndex = segmentIndex * nrSubpopPairs + subpopPairIndex;
lambdaVec[lIndex] = fromScaledLambda(x[xIndex]);
}
foreach(subpopPairIndex; 0 .. nrSubpopPairs) {
auto lIndex = timeIndex * nrSubpopPairs + subpopPairIndex;
auto tripleIndex = initialParams.marginalIndex.getIndexFromMarginalIndex(lIndex);
auto triple = initialParams.marginalIndex.getTripleFromIndex(tripleIndex);
auto p1 = initialParams.subpopLabels[triple.ind1];
auto p2 = initialParams.subpopLabels[triple.ind2];
if(p1 != p2) {
auto xIndex = segmentIndex * nrSubpopPairs + subpopPairIndex;
auto marginalIndex1 =
initialParams.marginalIndex.subpopulationTripleToMarginalIndexMap[triple.time][p1][p1];
auto marginalIndex2 =
initialParams.marginalIndex.subpopulationTripleToMarginalIndexMap[triple.time][p2][p2];
auto lambda1 = lambdaVec[marginalIndex1];
auto lambda2 = lambdaVec[marginalIndex2];
lambdaVec[lIndex] = fromScaledCrossLambda(x[xIndex], lambda1, lambda2);
}
}
timeIndex += 1;
}
}
return lambdaVec;
}
double getRecombinationRateFromX(in double[] x)
in {
assert(!fixedRecombination);
}
do {
return fromScaledRecombination(x[$ - 1]);
}
double logLikelihood(MSMCmodel params) {
double ret = 0.0;
foreach(au; 0 .. initialParams.nrMarginals) {
foreach(bv; 0 .. initialParams.nrMarginals) {
ret +=
expectationResultMat[au][bv] * log(params.transitionRate.transitionProbabilityQ2(au, bv));
}
ret += expectationResultVec[au] * log(
params.transitionRate.transitionProbabilityQ1(au) +
params.transitionRate.transitionProbabilityQ2(au, au)
);
}
return ret;
}
}
unittest {
writeln("test minfunc.getLambdaFromX");
import std.conv;
auto lambdaVec = [1, 1.5, 3, 1, 1.5, 3, 4, 4.5, 6, 4, 4.5, 6];
auto params = new MSMCmodel(0.01, 0.001, [0U, 0, 1, 1], lambdaVec, 4, 4, false);
auto expectationResultVec = new double[params.nrMarginals];
auto expectationResultMat = new double[][](params.nrMarginals, params.nrMarginals);
auto timeSegmentPattern = [2UL, 2];
auto minFunc = new MinFunc(expectationResultVec, expectationResultMat, params, timeSegmentPattern, false, false, false, 0, double.infinity);
auto rho = 0.001;
auto x = minFunc.getXfromLambdaVec(lambdaVec);
x ~= minFunc.toScaledRecombination(rho);
auto lambdaFromX = minFunc.getLambdaVecFromX(x);
auto rhoFromX = minFunc.getRecombinationRateFromX(x);
foreach(i; 0 .. lambdaVec.length)
assert(approxEqual(lambdaFromX[i], lambdaVec[i], 1.0e-8, 0.0), text(lambdaFromX[i], " ", lambdaVec[i]));
assert(approxEqual(rhoFromX, rho, 1.0e-8, 0.0));
minFunc = new MinFunc(expectationResultVec, expectationResultMat, params, timeSegmentPattern, true, false, false, 0, double.infinity);
x = minFunc.getXfromLambdaVec(lambdaVec);
x ~= minFunc.toScaledRecombination(rho);
lambdaFromX = minFunc.getLambdaVecFromXfixedPop(x);
rhoFromX = minFunc.getRecombinationRateFromX(x);
foreach(i; 0 .. lambdaVec.length)
assert(approxEqual(lambdaFromX[i], lambdaVec[i], 1.0e-8, 0.0), text(lambdaFromX[i], " ", lambdaVec[i]));
assert(approxEqual(rhoFromX, rho, 1.0e-8, 0.0));
minFunc = new MinFunc(expectationResultVec, expectationResultMat, params, timeSegmentPattern, false, true, false, 0, double.infinity);
x = minFunc.getXfromLambdaVec(lambdaVec);
lambdaFromX = minFunc.getLambdaVecFromX(x);
foreach(i; 0 .. lambdaVec.length)
assert(approxEqual(lambdaFromX[i], lambdaVec[i], 1.0e-8, 0.0), text(lambdaFromX[i], " ", lambdaVec[i]));
}
// unittest {
// import std.random;
// writeln("test maximization step");
// auto lambdaVec = new double[12];
// lambdaVec[] = 1.0;
// auto params = new MSMCmodel(0.01, 0.001, [0UL, 0, 1, 1], lambdaVec, 4, 4);
//
// auto expectationMatrix = new double[][](12, 12);
// foreach(i; 0 .. 12) foreach(j; 0 .. 12)
// expectationMatrix[i][j] = params.transitionRate.transitionProbabilityMarginal(i, j) * uniform(700, 1300);
// auto timeSegmentPattern = [2UL, 2];
// auto updatedParams = getMaximization(expectationMatrix, params, timeSegmentPattern, false, true);
//
// writeln("Maximization test: actual params: ", params);
// writeln("Maximization test: inferred params: ", updatedParams);
// }