-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupdate_mc_params.cpp
More file actions
419 lines (387 loc) · 13.1 KB
/
Copy pathupdate_mc_params.cpp
File metadata and controls
419 lines (387 loc) · 13.1 KB
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*IMa3 2018 Jody Hey, Rasmus Nielsen, Sang Chul Choi, Vitor Sousa, Janeen Pisciotta, Yujin Chung and Arun Sethuraman */
#undef GLOBVARS
#include "ima.hpp"
/****** global - used mostly here but setup in initialize.c *********/
// should probably make these dynamic
int urri[2 * MAXLOCI][2 * MAXLOCI]; // double it - some loci might have multiple rates
double urrlow[2 * MAXLOCI][2 * MAXLOCI], urrhi[2 * MAXLOCI][2 * MAXLOCI];
/*********** LOCAL STUFF **********/
/* pick a d value that reduces the distance between uj and uk by half
tried using this on chain with beta = 0.0
but did not resolve problem associated with calculation marginal likelihood
double newd(double uj, double uk)
{
return (uj - uk + sqrt(uj*uj + 14.0 * uj *uk + uk*uk))/(4.0 * uj);
} */
/*** GLOBAL FUNCTIONS ***/
#define MAXURCHECK 100 /* not sure how difficult it will be to find random ratios that fit the priors */
/* changeu picks new values on a log scale over a very wide range
the recorded range is set in IMn1.c to be 1/1000 -1000 (i.e. over a
1,000,000 fold range. However the actual range is set to be much greater
This effectively sets the range to be infinite, and causes mutation rates to be picked without a prior.
By setting the actual range to be very wide, the prior probability within the range of 1/1000 -1000 becomes flat */
int
changeu (int ci, int j, int *k)
{
/* update the ratio between two mutation rate scalars . The upate is drawn from a uniform log scale between 1/maxratio and maxratio
maxratio is set to be 3 times the maximum value of the individual scalars */
double U, newr, metropolishastingsratio, olduj, olduk;
double likelihoodratio;
double newpdg[2], newkappa[2];
double d, r;
int i, li, lj, lk, ai, aj, ak;
static int start = 0;
static double windowsize, maxratio;
double tempr, temp;
int urcheck, counturcheck, uindex;
double newlikelihood = 0.0, oldlikelihood = 0.0;
#ifdef DEBUG
double prodcheck;
#endif /* DEBUG */
/* windowsize and maxratio are on log scales
all mutation rate scalars have the same priors and windowsize */
if (start == 0)
{
/* 5/27/2011 changed the range expansion factor from 3 to 2, based on modeling that showed 2 is sufficient */
/* 11/6/2011 changed back to 3. no point changing to 2, and makes new results inconsistent with old results */
maxratio = 3.0 * L[0].u_rec[0].pr.max;
if (calcoptions[MUTATIONPRIORRANGE])
windowsize = L[0].u_rec[0].pr.max;
else
windowsize = L[0].u_rec[0].win;
start = 1;
}
/* 5/19/2011 JH adding thermodynamic integration (TI)*/
/* during TI one chain has a beta of zero, and in the case of updating mutation rate scalars this causes
all mutation scalar updates to be accepted - even very extreme values
don't quite understand this, but have turned off this update when beta=0, and have fixed mutation scalars to 1 in these cases
if (calcoptions[CALCMARGINALLIKELIHOOD] && ci == numchainspp - 1)
{
C[ci]->G[lj].uvals[aj] = C[ci]->G[lk].uvals[ak] = 1.0;
return 0;
} */
#ifdef DEBUG
for (prodcheck = 1, ai = 0; ai < nurates; ai++)
{
prodcheck *= C[ci]->G[ul[ai].l].uvals[ul[ai].u];
}
assert (prodcheck < 1.00001 && prodcheck > 0.99999);
#endif /* DEBUG */
if (nurates > 2)
{
do
{
*k = (int) (uniform () * nurates);
}
while (*k == j || *k < 0 || *k >= nurates);
}
else
{
assert (j == 0);
*k = 1;
}
lj = ul[j].l;
aj = ul[j].u;
lk = ul[*k].l;
ak = ul[*k].u;
assert (*k != j);
olduj = C[ci]->G[lj].uvals[aj];
olduk = C[ci]->G[lk].uvals[ak];
r = log (olduj / olduk);
if (calcoptions[MUTATIONPRIORRANGE])
{
counturcheck = 0;
do
{
if (counturcheck >= MAXURCHECK)
{
return -1;
}
U = uniform ();
if (U > 0.5)
newr = r + (2.0 * U - 1.0) * windowsize;
else
newr = r - windowsize * U * 2.0;
if (newr > maxratio)
newr = 2.0 * maxratio - newr;
else if (newr < -maxratio)
newr = 2.0 * (-maxratio) - newr;
d = exp ((newr - r) / 2);
/* urri[i][j] has a 0 if neither i nor j has a prior. 1 if i has a prior and j does not, -1 if i does not have a pior and j does */
/* must check if the new rates will cause the ratios among scalars to fall outside of the allowed ranges of ratios */
/* if they do, then they are rejected */
urcheck = 0;
counturcheck++;
if (urri[j][*k] == 2) // both scalars have priors
{
urcheck = (newr <= urrlow[j][*k] || newr >= urrhi[j][*k]);
for (uindex = 0; uindex < nurates; uindex++)
{
if (urri[j][uindex] == 2 && uindex != *k)
{
tempr =
log (d * C[ci]->G[lj].uvals[aj] /
C[ci]->G[ul[uindex].l].uvals[ul[uindex].u]);
urcheck = urcheck || (tempr <= urrlow[j][uindex]
|| tempr >= urrhi[j][uindex]);
}
if (urcheck)
break;
}
for (uindex = 0; uindex < nurates; uindex++)
{
if (urri[uindex][*k] == 2 && uindex != j)
{
tempr =
log (C[ci]->G[ul[uindex].l].uvals[ul[uindex].u] /
(C[ci]->G[lk].uvals[ak] / d));
urcheck = urcheck || (tempr <= urrlow[uindex][*k]
|| tempr >= urrhi[uindex][*k]);
}
if (urcheck)
break;
}
}
else
{
if (urri[j][*k] == 1) // the uj scalar has a prior, but not the uk scalar
{
for (uindex = 0; uindex < nurates; uindex++)
{
if (urri[j][uindex] == 2)
{
tempr =
log (d * C[ci]->G[lj].uvals[aj] /
C[ci]->G[ul[uindex].l].uvals[ul[uindex].u]);
urcheck = urcheck || (tempr <= urrlow[j][uindex]
|| tempr >= urrhi[j][uindex]);
}
if (urcheck)
break;
}
}
if (urri[j][*k] == -1) // the uk scalar has a prior, but not the uj scalar
{
for (uindex = 0; uindex < nurates; uindex++)
{
if (urri[uindex][*k] == 2)
{
tempr =
log (C[ci]->G[ul[uindex].l].uvals[ul[uindex].u] /
(C[ci]->G[lk].uvals[ak] / d));
urcheck = urcheck || (tempr <= urrlow[uindex][*k]
|| tempr >= urrhi[uindex][*k]);
}
if (urcheck)
break;
}
}
}
}
while (urcheck);
}
else
{
U = uniform ();
if (U > 0.5)
newr = r + (2.0 * U - 1.0) * windowsize;
else
newr = r - windowsize * U * 2.0;
if (newr > maxratio)
newr = 2.0 * maxratio - newr;
else if (newr < -maxratio)
newr = 2.0 * (-maxratio) - newr;
d = exp ((newr - r) / 2);
}
C[ci]->G[lj].uvals[aj] *= d;
C[ci]->G[lk].uvals[ak] /= d;
/*JH 5/25/2011, 9/28/2011 */
/* this block of code causes rejection of cases when the scalar is outside the range of tempUMAX
played around with this 5/25/2011 and 9/28/2011 to see effect on marginal likelihood estimates
using thermodynamic integration. Did not see much effect.
This does cause a nonzero rejection rate when run without data and in the absence of data it causes
the curves for these scalaras to not be flat, but have a broad curving peak centered on 1
as of 10/3/2011 - not in use */
/* why is this here ?? 8/22/2016 turn it off
#define tempUMAX 100.0//UMAX //1000.0 //1000.0
if (C[ci]->G[lj].uvals[aj] > tempUMAX || C[ci]->G[lj].uvals[aj] < 1.0/tempUMAX || C[ci]->G[lk].uvals[ak] > tempUMAX || C[ci]->G[lk].uvals[ak] < 1.0/tempUMAX)
{
C[ci]->G[lj].uvals[aj] = olduj;
C[ci]->G[lk].uvals[ak] = olduk;
if (L[lj].umodel[aj] == HKY)
restorescalefactors (ci, lj);
if (L[lk].umodel[ak] == HKY)
restorescalefactors (ci, lk);
return 0;
} */
likelihoodratio = 0.0;
for (i = 0; i < 2; i++)
{
if (i == 0)
{
li = lj;
ai = aj;
}
else
{
li = lk;
ai = ak;
}
switch (L[li].umodel[ai])
{
case HKY:
U = uniform ();
if (U > 0.5)
{
newkappa[i] =
C[ci]->G[li].kappaval + (2.0 * U - 1.0) * L[li].kappa_rec->win;
if (newkappa[i] > L[li].kappa_rec->pr.max)
newkappa[i] = 2.0 * L[li].kappa_rec->pr.max - newkappa[i];
}
else
{
newkappa[i] = C[ci]->G[li].kappaval - L[li].kappa_rec->win * U * 2.0;
if (newkappa[i] < 0)
newkappa[i] = -newkappa[i];
}
if (ci == 0)
L[li].kappa_rec->upinf->tries++;
newpdg[i] =
likelihoodHKY (ci, li, C[ci]->G[li].uvals[ai], newkappa[i],
-1, -1, -1, -1);
break;
case INFINITESITES:
newpdg[i] = likelihoodIS (ci, li, C[ci]->G[li].uvals[ai]);
break;
case STEPWISE:
newpdg[i] = likelihoodSW (ci, li, ai, C[ci]->G[li].uvals[ai], 1.0);
break;
}
newlikelihood += newpdg[i];
oldlikelihood += C[ci]->G[li].pdg_a[ai];
likelihoodratio += newpdg[i] - C[ci]->G[li].pdg_a[ai];
/* 5/19/2011 JH adding thermodynamic integration - only the likelihood ratio gets raised to beta, not the prior ratio */
/* this use of beta is not affected by whether or not the probability of the genealogy is included for this update,
since it is not present in this MH term */
// metropolishastingsterm = exp (beta[ci] * likenewsum); // 8/26/2011 this should be outside the loop
}
#ifdef TURNONCHECKS
//propose_new_given_old = propose_old_given_new = 0.0;
checkdetailedbalance(newlikelihood,oldlikelihood,0.0,0.0,0.0,0.0,beta[ci]);
#endif //TURNONCHECKS
metropolishastingsratio = beta[ci] * likelihoodratio;
if (metropolishastingsdecide(metropolishastingsratio,1))
{
for (i = 0; i < 2; i++)
{
if (i == 0)
{
li = lj;
ai = aj;
}
else
{
li = lk;
ai = ak;
}
C[ci]->G[li].pdg += newpdg[i] - C[ci]->G[li].pdg_a[ai];
C[ci]->G[li].pdg_a[ai] = newpdg[i];
if (L[li].umodel[ai] == HKY)
{
assert (ai == 0);
C[ci]->G[li].kappaval = newkappa[i];
if (ci == 0)
L[li].kappa_rec->upinf->accp++;
copyfraclike (ci, li);
storescalefactors (ci, li);
}
}
C[ci]->allpcalc.pdg += likelihoodratio;
#ifdef DEBUG
for (prodcheck = 1, ai = 0; ai < nurates; ai++)
{
prodcheck *= C[ci]->G[ul[ai].l].uvals[ul[ai].u];
}
assert (prodcheck < 1.00001 && prodcheck > 0.99999);
#endif /* */
return 1;
}
else
{
C[ci]->G[lj].uvals[aj] = olduj;
C[ci]->G[lk].uvals[ak] = olduk;
if (L[lj].umodel[aj] == HKY)
restorescalefactors (ci, lj);
if (L[lk].umodel[ak] == HKY)
restorescalefactors (ci, lk);
if (L[lj].umodel[aj] == STEPWISE)
{
temp = likelihoodSW (ci, lj, aj, olduj, 1.0);
C[ci]->G[lj].pdg += temp - C[ci]->G[lj].pdg_a[aj];
C[ci]->G[lj].pdg_a[aj] = temp;
}
if (L[lk].umodel[ak] == STEPWISE)
{
temp = likelihoodSW (ci, lk, ak, olduk, 1.0);
C[ci]->G[lk].pdg += temp - C[ci]->G[lk].pdg_a[ak];
C[ci]->G[lk].pdg_a[ak] = temp;
}
#ifdef DEBUG
for (prodcheck = 1, ai = 0; ai < nurates; ai++)
{
prodcheck *= C[ci]->G[ul[ai].l].uvals[ul[ai].u];
}
assert (prodcheck < 1.00001 && prodcheck > 0.99999);
#endif /* */
return 0;
}
} /* changeu */
/* only use for HKY model with single locus data sets, otherwise kappa is updated in changeu */
int
changekappa (int ci)
{
int li;
double U, metropolishastingsratio, newpdg, newkappa;
double likelihoodratio;
li = 0;
U = uniform ();
L[li].kappa_rec->upinf->tries++;
if (U > 0.5)
{
newkappa = C[ci]->G[li].kappaval + (2.0 * U - 1.0) * L[li].kappa_rec->win;
if (newkappa > L[li].kappa_rec->pr.max)
newkappa = 2.0 * L[li].kappa_rec->pr.max - newkappa;
}
else
{
newkappa = C[ci]->G[li].kappaval - L[li].kappa_rec->win * U * 2.0;
if (newkappa < 0)
newkappa = -newkappa;
}
newpdg =
likelihoodHKY (ci, li, C[ci]->G[li].uvals[0], newkappa, -1, -1, -1, -1);
likelihoodratio = newpdg - C[ci]->G[li].pdg_a[0];
/* 5/19/2011 JH adding thermodynamic integration - only the likelihood ratio gets raised to beta, not the prior ratio */
/* this use of beta is not affected by whether or not the probability of the genealogy is included,
since prior ratios cancel out in this MH term */
/* metropolishastingsratio = exp (beta[ci] * likelihoodratio);
U = uniform ();
if (U < DMIN(1.0, metropolishastingsratio)) */
metropolishastingsratio = beta[ci] * likelihoodratio;
if (metropolishastingsdecide(metropolishastingsratio,1))
{
C[ci]->allpcalc.pdg += newpdg - C[ci]->G[li].pdg;
C[ci]->G[li].pdg = C[ci]->G[li].pdg_a[0] = newpdg;
C[ci]->G[li].kappaval = newkappa;
L[li].kappa_rec->upinf->accp++;
copyfraclike (ci, li);
storescalefactors (ci, li);
return (1);
}
else
{
restorescalefactors (ci, li);
return (0);
}
} /* changekappa */
#undef MAXPRIORSCALETRY