| 1 |
// This library is part of PLINK 2.0, copyright (C) 2005-2025 Shaun Purcell, |
| 2 |
// Christopher Chang. |
| 3 |
// |
| 4 |
// This library is free software: you can redistribute it and/or modify it |
| 5 |
// under the terms of the GNU Lesser General Public License as published by the |
| 6 |
// Free Software Foundation; either version 3 of the License, or (at your |
| 7 |
// option) any later version. |
| 8 |
// |
| 9 |
// This library is distributed in the hope that it will be useful, but WITHOUT |
| 10 |
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 12 |
// for more details. |
| 13 |
// |
| 14 |
// You should have received a copy of the GNU Lesser General Public License |
| 15 |
// along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
#include "plink2_stats.h" |
| 18 |
|
| 19 |
#ifdef __cplusplus |
| 20 |
namespace plink2 { |
| 21 |
#endif |
| 22 |
|
| 23 |
// Thread-unsafe portions of plink_stats.c have been replaced, mostly by code |
| 24 |
// derived from boost/math/special_functions/gamma.hpp and |
| 25 |
// boost/math/special_functions/detail/igamma_inverse.hpp in Boost 1.60 and |
| 26 |
// 1.84 (Maddock et al.). The derived portions are subject to the following |
| 27 |
// license: |
| 28 |
// |
| 29 |
// ***** |
| 30 |
// Boost Software License - Version 1.0 - August 17th, 2003 |
| 31 |
// |
| 32 |
// Permission is hereby granted, free of charge, to any person or organization |
| 33 |
// obtaining a copy of the software and accompanying documentation covered by |
| 34 |
// this license (the "Software") to use, reproduce, display, distribute, |
| 35 |
// execute, and transmit the Software, and to prepare derivative works of the |
| 36 |
// Software, and to permit third-parties to whom the Software is furnished to |
| 37 |
// do so, all subject to the following: |
| 38 |
// |
| 39 |
// The copyright notices in the Software and this entire statement, including |
| 40 |
// the above license grant, this restriction and the following disclaimer, |
| 41 |
// must be included in all copies of the Software, in whole or in part, and |
| 42 |
// all derivative works of the Software, unless such copies or derivative |
| 43 |
// works are solely in the form of machine-executable object code generated by |
| 44 |
// a source language processor. |
| 45 |
// |
| 46 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 47 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 48 |
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
| 49 |
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
| 50 |
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
| 51 |
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 52 |
// DEALINGS IN THE SOFTWARE. |
| 53 |
// ***** |
| 54 |
|
| 55 |
// ***** thread-safe ChisqToP ***** |
| 56 |
// port of Boost 1.60 implementation, float precision |
| 57 |
|
| 58 |
static const double kLogMinValue = -708.0; |
| 59 |
static const double kLogMaxValue = 709.0; |
| 60 |
|
| 61 |
static const double kLentzFpmin = 1.0e-30; |
| 62 |
|
| 63 |
static const double kFactorialRecips[34] = { |
| 64 |
1.0, |
| 65 |
1.0, |
| 66 |
0.5, |
| 67 |
0.16666666666666666, |
| 68 |
0.041666666666666664, |
| 69 |
0.008333333333333333, |
| 70 |
0.001388888888888889, |
| 71 |
0.0001984126984126984, |
| 72 |
2.48015873015873e-05, |
| 73 |
2.7557319223985893e-06, |
| 74 |
2.755731922398589e-07, |
| 75 |
2.505210838544172e-08, |
| 76 |
2.08767569878681e-09, |
| 77 |
1.6059043836821613e-10, |
| 78 |
1.1470745597729725e-11, |
| 79 |
7.647163731819816e-13, |
| 80 |
4.779477332387385e-14, |
| 81 |
2.8114572543455206e-15, |
| 82 |
1.5619206968586225e-16, |
| 83 |
8.22063524662433e-18, |
| 84 |
4.110317623312165e-19, |
| 85 |
1.9572941063391263e-20, |
| 86 |
8.896791392450574e-22, |
| 87 |
3.8681701706306835e-23, |
| 88 |
1.6117375710961184e-24, |
| 89 |
6.446950284384474e-26, |
| 90 |
2.4795962632247972e-27, |
| 91 |
9.183689863795546e-29, |
| 92 |
3.279889237069838e-30, |
| 93 |
1.1309962886447718e-31, |
| 94 |
3.7699876288159054e-33, |
| 95 |
1.2161250415535181e-34, |
| 96 |
3.800390754854744e-36, |
| 97 |
1.151633562077195e-37 |
| 98 |
}; |
| 99 |
|
| 100 |
// this may move to a more central location |
| 101 |
static const double kSmallRecips[30] = { |
| 102 |
0.0, // could make this nan, though that's annoying for C++03 |
| 103 |
1.0 / 1, |
| 104 |
1.0 / 2, |
| 105 |
1.0 / 3, |
| 106 |
1.0 / 4, |
| 107 |
1.0 / 5, |
| 108 |
1.0 / 6, |
| 109 |
1.0 / 7, |
| 110 |
1.0 / 8, |
| 111 |
1.0 / 9, |
| 112 |
1.0 / 10, |
| 113 |
1.0 / 11, |
| 114 |
1.0 / 12, |
| 115 |
1.0 / 13, |
| 116 |
1.0 / 14, |
| 117 |
1.0 / 15, |
| 118 |
1.0 / 16, |
| 119 |
1.0 / 17, |
| 120 |
1.0 / 18, |
| 121 |
1.0 / 19, |
| 122 |
1.0 / 20, |
| 123 |
1.0 / 21, |
| 124 |
1.0 / 22, |
| 125 |
1.0 / 23, |
| 126 |
1.0 / 24, |
| 127 |
1.0 / 25, |
| 128 |
1.0 / 26, |
| 129 |
1.0 / 27, |
| 130 |
1.0 / 28, |
| 131 |
1.0 / 29 |
| 132 |
}; |
| 133 |
|
| 134 |
double finite_gamma_q(uint32_t aa, double xx, double* p_derivative) { |
| 135 |
// a is a positive integer < 30; max(0.6, a-1) < x < kLogMaxValue |
| 136 |
// (e^{-x})(1 + x + x^2/2 + x^3/3! + x^4/4! + ... + x^{a-1}/(a-1)!) |
| 137 |
const double ee = exp(-xx); |
| 138 |
if (ee == 0.0) { |
| 139 |
return 0; |
| 140 |
} |
| 141 |
double sum = ee; |
| 142 |
double term = sum; |
| 143 |
for (uint32_t nn = 1; nn != aa; ++nn) { |
| 144 |
term *= xx * kSmallRecips[nn]; |
| 145 |
sum += term; |
| 146 |
} |
| 147 |
if (p_derivative) { |
| 148 |
*p_derivative = ee * pow(xx, u31tod(aa)) * kFactorialRecips[aa - 1]; |
| 149 |
} |
| 150 |
return sum; |
| 151 |
} |
| 152 |
|
| 153 |
static const double kSqrtPi = 1.7724538509055159; |
| 154 |
|
| 155 |
double lower_gamma_series(double aa, double zz, double init_value) { |
| 156 |
// evaluate init_value + 1 + (z/(a+1)) + (z^2 / ((a+1)(a+2))) + |
| 157 |
// (z^3 / ((a+1)(a+2)(a+3))) + ... |
| 158 |
// z shouldn't be larger than (a+170), otherwise overflow is possible |
| 159 |
double result = 1; |
| 160 |
double total = init_value + 1; |
| 161 |
do { |
| 162 |
aa += 1.0; |
| 163 |
result *= zz / aa; |
| 164 |
total += result; |
| 165 |
} while (fabs(result) > (kBigEpsilon * kBigEpsilon)); |
| 166 |
return total; |
| 167 |
} |
| 168 |
|
| 169 |
// ~6 digits of precision is appropriate for p-value computations |
| 170 |
static const double kContinuedFractionEpsilon = 3.0e-7; |
| 171 |
|
| 172 |
double upper_gamma_fraction(double a1, double z1) { |
| 173 |
// No overflow issues, this tends to be close to 1. |
| 174 |
|
| 175 |
// evaluate a_1 / (b_1 + (a_2 / (b_2 + (a_3 / (b_3 + ...))))) |
| 176 |
// see Boost continued_fraction_a(), upper_incomplete_gamma_fract |
| 177 |
double cur_b = z1 - a1 + 3; |
| 178 |
|
| 179 |
double hh = cur_b; |
| 180 |
const double a0 = a1 - 1.0; |
| 181 |
if (fabs(hh) < kLentzFpmin) { |
| 182 |
hh = kLentzFpmin; |
| 183 |
} |
| 184 |
double cc = hh; |
| 185 |
double dd = 0.0; |
| 186 |
for (double kk = 2.0; kk <= 100.0; kk += 1.0) { |
| 187 |
const double cur_a = kk * (a1 - kk); |
| 188 |
cur_b += 2.0; |
| 189 |
dd = cur_b + cur_a * dd; |
| 190 |
if (fabs(dd) < kLentzFpmin) { |
| 191 |
dd = kLentzFpmin; |
| 192 |
} |
| 193 |
cc = cur_b + cur_a / cc; |
| 194 |
if (fabs(cc) < kLentzFpmin) { |
| 195 |
cc = kLentzFpmin; |
| 196 |
} |
| 197 |
dd = 1.0 / dd; |
| 198 |
const double delta = cc * dd; |
| 199 |
hh *= delta; |
| 200 |
if (fabs(delta - 1.0) < kContinuedFractionEpsilon) { |
| 201 |
break; |
| 202 |
} |
| 203 |
} |
| 204 |
// const double cont_frac = a0 / hh; |
| 205 |
// return 1 / (z1 - a1 + 1 + cont_frac); |
| 206 |
return hh / (hh * (z1 - a1 + 1) + a0); |
| 207 |
} |
| 208 |
|
| 209 |
// from Numerical Recipes in Fortran 77: The Art of Scientific Computing, via |
| 210 |
// Wikipedia |
| 211 |
// maximal error of 1.2e-7 |
| 212 |
double erfc_fast(double zz) { |
| 213 |
const double tt = 1.0 / (1.0 + 0.5 * zz); |
| 214 |
const double tau = tt * exp(((((((((0.17087277 * tt - 0.82215223) * tt + 1.48851587) * tt - 1.13520398) * tt + 0.27886807) * tt - 0.18628806) * tt + 0.09678418) * tt + 0.37409196) * tt + 1.00002368) * tt - 1.26551223 - zz * zz); |
| 215 |
return tau; |
| 216 |
} |
| 217 |
|
| 218 |
static const double kSmallHalfRecips[30] = { |
| 219 |
1.0 / 0.5, |
| 220 |
1.0 / 1.5, |
| 221 |
1.0 / 2.5, |
| 222 |
1.0 / 3.5, |
| 223 |
1.0 / 4.5, |
| 224 |
1.0 / 5.5, |
| 225 |
1.0 / 6.5, |
| 226 |
1.0 / 7.5, |
| 227 |
1.0 / 8.5, |
| 228 |
1.0 / 9.5, |
| 229 |
1.0 / 10.5, |
| 230 |
1.0 / 11.5, |
| 231 |
1.0 / 12.5, |
| 232 |
1.0 / 13.5, |
| 233 |
1.0 / 14.5, |
| 234 |
1.0 / 15.5, |
| 235 |
1.0 / 16.5, |
| 236 |
1.0 / 17.5, |
| 237 |
1.0 / 18.5, |
| 238 |
1.0 / 19.5, |
| 239 |
1.0 / 20.5, |
| 240 |
1.0 / 21.5, |
| 241 |
1.0 / 22.5, |
| 242 |
1.0 / 23.5, |
| 243 |
1.0 / 24.5, |
| 244 |
1.0 / 25.5, |
| 245 |
1.0 / 26.5, |
| 246 |
1.0 / 27.5, |
| 247 |
1.0 / 28.5, |
| 248 |
1.0 / 29.5 |
| 249 |
}; |
| 250 |
|
| 251 |
// This can underflow. Use finite_half_gamma_q2_ln when that's a problem. |
| 252 |
double finite_half_gamma_q2(uint32_t a_minus_half, double xx, double* p_derivative) { |
| 253 |
// a is in {0.5, 1.5, ..., 29.5}; max(0.2, a-1) < x < kLogMaxValue |
| 254 |
const double sqrt_x = sqrt(xx); |
| 255 |
double ee = erfc_fast(sqrt_x); |
| 256 |
if ((ee != 0) && a_minus_half) { |
| 257 |
double term = sqrt_x * exp(-xx) * (2.0 / kSqrtPi); |
| 258 |
double sum = term; |
| 259 |
for (uint32_t n_minus_half = 1; n_minus_half != a_minus_half; ++n_minus_half) { |
| 260 |
term *= xx * kSmallHalfRecips[n_minus_half]; |
| 261 |
sum += term; |
| 262 |
} |
| 263 |
ee += sum; |
| 264 |
if (p_derivative) { |
| 265 |
*p_derivative = 0; |
| 266 |
} |
| 267 |
} else if (p_derivative) { |
| 268 |
*p_derivative = sqrt_x * exp(-xx) * (1.0 / kSqrtPi); |
| 269 |
} |
| 270 |
return ee; |
| 271 |
} |
| 272 |
|
| 273 |
static const double kLanczosFloatSumNumer[6] = {8706.3495925490091, 8523.650341121874, 3338.0292194764235, 653.64249942940087, 63.999518449381870, 2.5066282746310063}; |
| 274 |
static const double kLanczosFloatSumDenom[6] = {0, 24, 50, 35, 10, 1}; |
| 275 |
static const double kLanczosFloatSumExpgNumer[6] = {32.812445410297834, 32.123889414443320, 12.580347294552161, 2.4634444783532414, 0.2412010548258800, 0.0094469677045392}; |
| 276 |
|
| 277 |
// this depends on the polynomial coefficients above |
| 278 |
static const double kLanczosFloatG = 5.581; |
| 279 |
|
| 280 |
double lanczos_sum_f(double zz) { |
| 281 |
double s1; |
| 282 |
double s2; |
| 283 |
if (zz <= 1) { |
| 284 |
s1 = kLanczosFloatSumNumer[5]; |
| 285 |
s2 = kLanczosFloatSumDenom[5]; |
| 286 |
for (int32_t ii = 4; ii >= 0; --ii) { |
| 287 |
s1 *= zz; |
| 288 |
s2 *= zz; |
| 289 |
s1 += kLanczosFloatSumNumer[S_CAST(uint32_t, ii)]; |
| 290 |
s2 += kLanczosFloatSumDenom[S_CAST(uint32_t, ii)]; |
| 291 |
} |
| 292 |
} else { |
| 293 |
zz = 1 / zz; |
| 294 |
s1 = kLanczosFloatSumNumer[0]; |
| 295 |
s2 = kLanczosFloatSumDenom[0]; |
| 296 |
for (uint32_t uii = 1; uii != 6; ++uii) { |
| 297 |
s1 *= zz; |
| 298 |
s2 *= zz; |
| 299 |
s1 += kLanczosFloatSumNumer[uii]; |
| 300 |
s2 += kLanczosFloatSumDenom[uii]; |
| 301 |
} |
| 302 |
} |
| 303 |
return s1 / s2; |
| 304 |
} |
| 305 |
|
| 306 |
double lanczos_sum_f_expg_scaled_imp(double zz, double* s2_ptr) { |
| 307 |
double s1; |
| 308 |
double s2; |
| 309 |
if (zz <= 1) { |
| 310 |
s1 = kLanczosFloatSumExpgNumer[5]; |
| 311 |
s2 = kLanczosFloatSumDenom[5]; |
| 312 |
for (int32_t ii = 4; ii >= 0; --ii) { |
| 313 |
s1 *= zz; |
| 314 |
s2 *= zz; |
| 315 |
s1 += kLanczosFloatSumExpgNumer[S_CAST(uint32_t, ii)]; |
| 316 |
s2 += kLanczosFloatSumDenom[S_CAST(uint32_t, ii)]; |
| 317 |
} |
| 318 |
} else { |
| 319 |
zz = 1 / zz; |
| 320 |
s1 = kLanczosFloatSumExpgNumer[0]; |
| 321 |
s2 = kLanczosFloatSumDenom[0]; |
| 322 |
for (uint32_t uii = 1; uii != 6; ++uii) { |
| 323 |
s1 *= zz; |
| 324 |
s2 *= zz; |
| 325 |
s1 += kLanczosFloatSumExpgNumer[uii]; |
| 326 |
s2 += kLanczosFloatSumDenom[uii]; |
| 327 |
} |
| 328 |
} |
| 329 |
*s2_ptr = s2; |
| 330 |
return s1; |
| 331 |
} |
| 332 |
|
| 333 |
double lanczos_sum_f_expg_scaled(double zz) { |
| 334 |
double s2; |
| 335 |
double s1 = lanczos_sum_f_expg_scaled_imp(zz, &s2); |
| 336 |
return s1 / s2; |
| 337 |
} |
| 338 |
|
| 339 |
double lanczos_sum_f_expg_scaled_recip(double zz) { |
| 340 |
double s2; |
| 341 |
double s1 = lanczos_sum_f_expg_scaled_imp(zz, &s2); |
| 342 |
return s2 / s1; |
| 343 |
} |
| 344 |
|
| 345 |
// We want better-than-float precision for our log-factorial function. |
| 346 |
static const double kLanczosDoubleSumDenom[13] = {0, 39916800, 120543840, 150917976, 105258076, 45995730, 13339535, 2637558, 357423, 32670, 1925, 66, 1}; |
| 347 |
static const double kLanczosDoubleSumExpgNumer[13] = {56906521.913471564, 103794043.11634455, 86363131.288138591, 43338889.324676138, 14605578.087685068, 3481712.1549806459, 601859.61716810988, 75999.293040145426, 6955.9996025153761, 449.94455690631681, 19.519927882476175, 0.50984166556566762, 0.0060618423462489065}; |
| 348 |
|
| 349 |
// this depends on the polynomial coefficients above |
| 350 |
static const double kLanczosDoubleG = 6.0246800407767296; |
| 351 |
|
| 352 |
double lanczos_sum_d_expg_scaled_imp(double zz, double* s2_ptr) { |
| 353 |
double s1; |
| 354 |
double s2; |
| 355 |
// Only called by Lfact() for now, where zz is guaranteed to be > 1. |
| 356 |
/* |
| 357 |
if (zz <= 1) { |
| 358 |
s1 = kLanczosDoubleSumExpgNumer[12]; |
| 359 |
s2 = kLanczosDoubleSumDenom[12]; |
| 360 |
for (int32_t ii = 11; ii >= 0; --ii) { |
| 361 |
s1 *= zz; |
| 362 |
s2 *= zz; |
| 363 |
s1 += kLanczosDoubleSumExpgNumer[S_CAST(uint32_t, ii)]; |
| 364 |
s2 += kLanczosDoubleSumDenom[S_CAST(uint32_t, ii)]; |
| 365 |
} |
| 366 |
} else { |
| 367 |
*/ |
| 368 |
zz = 1 / zz; |
| 369 |
s1 = kLanczosDoubleSumExpgNumer[0]; |
| 370 |
s2 = kLanczosDoubleSumDenom[0]; |
| 371 |
for (uint32_t uii = 1; uii != 13; ++uii) { |
| 372 |
s1 *= zz; |
| 373 |
s2 *= zz; |
| 374 |
s1 += kLanczosDoubleSumExpgNumer[uii]; |
| 375 |
s2 += kLanczosDoubleSumDenom[uii]; |
| 376 |
} |
| 377 |
// } |
| 378 |
*s2_ptr = s2; |
| 379 |
return s1; |
| 380 |
} |
| 381 |
|
| 382 |
double lanczos_sum_d_expg_scaled(double zz) { |
| 383 |
double s2; |
| 384 |
double s1 = lanczos_sum_d_expg_scaled_imp(zz, &s2); |
| 385 |
return s1 / s2; |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
double log1pmx(double xx) { |
| 390 |
// log(1+x) - x |
| 391 |
// assumes abs(xx) < 0.95 |
| 392 |
const double aa = fabs(xx); |
| 393 |
if (aa < (kBigEpsilon / kSqrt2)) { // 2^{-21.5} |
| 394 |
return -xx * xx * 0.5; |
| 395 |
} |
| 396 |
double kk = 1.0; // skip first term of usual log(1+x) series |
| 397 |
const double m_mult = -xx; |
| 398 |
double m_prod = xx; |
| 399 |
double total = 0.0; |
| 400 |
double rr; |
| 401 |
do { |
| 402 |
m_prod *= m_mult; |
| 403 |
kk += 1.0; |
| 404 |
rr = m_prod / kk; |
| 405 |
total += rr; |
| 406 |
// todo: tune these epsilons, but let's wait until we know all of the |
| 407 |
// callers of these functions |
| 408 |
} while (fabs(rr) > (kBigEpsilon * kBigEpsilon)); |
| 409 |
return total; |
| 410 |
} |
| 411 |
|
| 412 |
// compute (z^a)(e^{-z})/tgamma(a) |
| 413 |
double regularized_gamma_prefix(double aa, double zz) { |
| 414 |
// assumes a == 0.5 if a < 1. assumes z > 0. |
| 415 |
// we are fine with float-level precision, so lanczos_n=6, |
| 416 |
// kLanczosFloatG=5.581 |
| 417 |
if (aa < 1) { |
| 418 |
return sqrt(zz) * exp(-zz) * (1.0 / kSqrtPi); |
| 419 |
} |
| 420 |
const double agh = aa + kLanczosFloatG - 0.5; |
| 421 |
const double agh_recip = 1.0 / agh; |
| 422 |
const double dd = ((zz - aa) - (kLanczosFloatG - 0.5)) * agh_recip; |
| 423 |
double prefix; |
| 424 |
if ((fabs(dd * dd * aa) <= 100) && (aa > 150)) { |
| 425 |
// abs(dd) < sqrt(2/3) < 0.95 |
| 426 |
prefix = aa * log1pmx(dd) + zz * (0.5 - kLanczosFloatG) * agh_recip; |
| 427 |
prefix = exp(prefix); |
| 428 |
} else { |
| 429 |
const double alz = aa * log(zz * agh_recip); |
| 430 |
const double amz = aa - zz; |
| 431 |
const double cur_minv = MINV(alz, amz); |
| 432 |
if ((cur_minv <= kLogMinValue) || (MAXV(alz, amz) >= kLogMaxValue)) { |
| 433 |
const double amza = amz / aa; |
| 434 |
double sq; |
| 435 |
if ((cur_minv > 2 * kLogMinValue) && (MAXV(alz, amz) < 2 * kLogMaxValue)) { |
| 436 |
// need to structure this to avoid overflow |
| 437 |
sq = pow(zz * agh_recip, aa * 0.5) * exp(amz * 0.5); |
| 438 |
prefix = sq * sq; |
| 439 |
} else if ((cur_minv > 4 * kLogMinValue) && (MAXV(alz, amz) < 4 * kLogMaxValue) && (zz > aa)) { |
| 440 |
sq = pow(zz * agh_recip, aa * 0.25) * exp(amz * 0.25); |
| 441 |
prefix = sq * sq; |
| 442 |
prefix *= prefix; |
| 443 |
} else if ((amza > kLogMinValue) && (amza < kLogMaxValue)) { |
| 444 |
prefix = pow((zz * exp(amza)) * agh_recip, aa); |
| 445 |
} else { |
| 446 |
prefix = exp(alz + amz); |
| 447 |
} |
| 448 |
} else { |
| 449 |
prefix = pow(zz * agh_recip, aa) * exp(amz); |
| 450 |
} |
| 451 |
} |
| 452 |
prefix *= sqrt(agh * kRecipE) * lanczos_sum_f_expg_scaled_recip(aa); |
| 453 |
return prefix; |
| 454 |
} |
| 455 |
|
| 456 |
static const double kTemmeC0[7] = {-0.333333333, 0.0833333333, -0.0148148148, 0.00115740741, 0.000352733686, -0.000178755144, 0.391926318e-4}; |
| 457 |
static const double kTemmeC1[5] = {-0.00185185185, -0.00347222222, 0.00264550265, -0.000990226337, 0.000205761317}; |
| 458 |
static const double kTemmeC2[3] = {0.00413359788, -0.00268132716, 0.000771604938}; |
| 459 |
|
| 460 |
double igamma_temme_large(double aa, double xx) { |
| 461 |
// 24-bit precision is fine |
| 462 |
const double sigma = (xx - aa) / aa; |
| 463 |
// abs(sigma) < 0.4 |
| 464 |
const double phi = -log1pmx(sigma); |
| 465 |
const double sqrt_a = sqrt(aa); |
| 466 |
const double sqrt_phi = sqrt(phi); |
| 467 |
const double yy = aa * phi; |
| 468 |
double zz = kSqrt2 * sqrt_phi; |
| 469 |
if (xx < aa) { |
| 470 |
zz = -zz; |
| 471 |
} |
| 472 |
double workspace[3]; |
| 473 |
workspace[0] = (((((kTemmeC0[6] * zz + kTemmeC0[5]) * zz + kTemmeC0[4]) * zz + kTemmeC0[3]) * zz + kTemmeC0[2]) * zz + kTemmeC0[1]) * zz + kTemmeC0[0]; |
| 474 |
workspace[1] = (((kTemmeC1[4] * zz + kTemmeC1[3]) * zz + kTemmeC1[2]) * zz + kTemmeC1[1]) * zz + kTemmeC1[0]; |
| 475 |
workspace[2] = (kTemmeC2[2] * zz + kTemmeC2[1]) * zz + kTemmeC2[0]; |
| 476 |
const double a_recip = 1 / aa; |
| 477 |
double result = (workspace[2] * a_recip + workspace[1]) * a_recip + workspace[0]; |
| 478 |
result *= exp(-yy) / ((kSqrt2 * kSqrtPi) * sqrt_a); |
| 479 |
if (xx < aa) { |
| 480 |
result = -result; |
| 481 |
} |
| 482 |
result += erfc_fast(sqrt_a * sqrt_phi) * 0.5; |
| 483 |
return result; |
| 484 |
} |
| 485 |
|
| 486 |
double gamma_incomplete_imp2(uint32_t df, double xx, uint32_t invert, double* p_derivative) { |
| 487 |
// normalized = true |
| 488 |
assert(df); |
| 489 |
assert(xx >= 0.0); |
| 490 |
const double aa = u31tod(df) * 0.5; |
| 491 |
const uint32_t is_small_a = (df < 60) && (aa <= xx + 1) && (xx < kLogMaxValue); |
| 492 |
uint32_t is_int = 0; |
| 493 |
uint32_t is_half_int = 0; |
| 494 |
if (is_small_a) { |
| 495 |
is_half_int = df % 2; |
| 496 |
is_int = !is_half_int; |
| 497 |
} |
| 498 |
uint32_t eval_method; |
| 499 |
if (is_int && (xx > 0.6)) { |
| 500 |
invert = !invert; |
| 501 |
eval_method = 0; |
| 502 |
} else if (is_half_int && (xx > 0.2)) { |
| 503 |
invert = !invert; |
| 504 |
eval_method = 1; |
| 505 |
} else if (xx < kSmallEpsilon) { |
| 506 |
// avoid computing log(0) |
| 507 |
// don't need more precision here, 6 digits is enough |
| 508 |
// invert always == 1 |
| 509 |
assert(!p_derivative); |
| 510 |
return 1.0; |
| 511 |
} else if (xx < 1.1) { |
| 512 |
// (simplified from original Boost code) |
| 513 |
eval_method = 2; |
| 514 |
} else { |
| 515 |
const double x_minus_a = xx - aa; |
| 516 |
uint32_t use_temme = 0; |
| 517 |
if (aa > 20) { |
| 518 |
// sigma = abs((x - a) / a); |
| 519 |
// igamma_temme_large() assumes abs(sigma) < 0.95 |
| 520 |
if (aa > 200) { |
| 521 |
// abs(sigma) < sqrt(20 / a) < 0.316... |
| 522 |
use_temme = (20 * aa > x_minus_a * x_minus_a); |
| 523 |
} else { |
| 524 |
// abs(sigma) < 0.4 |
| 525 |
const double sigma_times_a = fabs(x_minus_a); |
| 526 |
use_temme = (sigma_times_a < 0.4 * aa); |
| 527 |
} |
| 528 |
} |
| 529 |
if (use_temme) { |
| 530 |
eval_method = 5; |
| 531 |
} else { |
| 532 |
// x - (1 / (3 * x)) < a |
| 533 |
// x * x - (1/3) < a * x |
| 534 |
// x * x - a * x < 1/3 |
| 535 |
// x * (x - a) < 1/3 |
| 536 |
if (xx * x_minus_a < (1.0 / 3.0)) { |
| 537 |
eval_method = 2; |
| 538 |
} else { |
| 539 |
eval_method = 4; |
| 540 |
invert = !invert; |
| 541 |
} |
| 542 |
} |
| 543 |
} |
| 544 |
double result; |
| 545 |
switch(eval_method) { |
| 546 |
case 0: |
| 547 |
result = finite_gamma_q(df / 2, xx, p_derivative); |
| 548 |
break; |
| 549 |
case 1: |
| 550 |
// previously used erfc, but that was up to ~3x as slow as dcdflib (e.g. |
| 551 |
// ChisqToP(2.706, 1) case). |
| 552 |
result = finite_half_gamma_q2(df / 2, xx, p_derivative); |
| 553 |
if (p_derivative && (*p_derivative == 0)) { |
| 554 |
*p_derivative = regularized_gamma_prefix(aa, xx); |
| 555 |
} |
| 556 |
break; |
| 557 |
case 2: |
| 558 |
result = regularized_gamma_prefix(aa, xx); |
| 559 |
if (p_derivative) { |
| 560 |
*p_derivative = result; |
| 561 |
} |
| 562 |
if (result != 0) { |
| 563 |
// uint32_t optimized_invert = 0; |
| 564 |
double init_value = 0; |
| 565 |
if (invert) { |
| 566 |
init_value = -aa / result; |
| 567 |
// optimized_invert = 1; |
| 568 |
} |
| 569 |
result *= lower_gamma_series(aa, xx, init_value) / aa; |
| 570 |
// if (optimized_invert) { |
| 571 |
if (invert) { |
| 572 |
invert = 0; |
| 573 |
result = -result; |
| 574 |
} |
| 575 |
} |
| 576 |
break; |
| 577 |
// case 3: |
| 578 |
// only when df=1 and 0.449 < x < 1.1, so no overflow/underflow issues. |
| 579 |
// ...wait a sec, we never reach here at all since eval_method is always |
| 580 |
// 1 there. |
| 581 |
case 4: |
| 582 |
result = regularized_gamma_prefix(aa, xx); |
| 583 |
if (p_derivative) { |
| 584 |
*p_derivative = result; |
| 585 |
} |
| 586 |
if (result != 0) { |
| 587 |
result *= upper_gamma_fraction(aa, xx); |
| 588 |
} |
| 589 |
break; |
| 590 |
case 5: |
| 591 |
result = igamma_temme_large(aa, xx); |
| 592 |
if (xx >= aa) { |
| 593 |
invert = !invert; |
| 594 |
} |
| 595 |
if (p_derivative) { |
| 596 |
*p_derivative = regularized_gamma_prefix(aa, xx); |
| 597 |
} |
| 598 |
} |
| 599 |
if (result > 1) { |
| 600 |
result = 1; |
| 601 |
} |
| 602 |
if (invert) { |
| 603 |
result = 1 - result; |
| 604 |
} |
| 605 |
if (p_derivative) { |
| 606 |
if ((xx < 1) && (DBL_MAX * xx < (*p_derivative))) { |
| 607 |
*p_derivative = DBL_MAX / 2; // overflow; do we really need this? |
| 608 |
} else { |
| 609 |
*p_derivative /= xx; |
| 610 |
} |
| 611 |
} |
| 612 |
return result; |
| 613 |
} |
| 614 |
|
| 615 |
double ChisqToP(double chisq, uint32_t df) { |
| 616 |
// todo: figure out when we were depending on this to return -9, and decide |
| 617 |
// how to handle those situations now |
| 618 |
return gamma_incomplete_imp2(df, chisq * 0.5, 1, nullptr); |
| 619 |
} |
| 620 |
|
| 621 |
// ***** end thread-safe ChisqToP ***** |
| 622 |
|
| 623 |
|
| 624 |
// ***** ChisqToLnP ***** |
| 625 |
|
| 626 |
double finite_gamma_q_ln(uint32_t aa, double xx) { |
| 627 |
// a is a positive integer < 30; max(0.6, a-1) < x < 1e10 |
| 628 |
// (e^{-x})(1 + x + x^2/2 + x^3/3! + x^4/4! + ... + x^{a-1}/(a-1)!) |
| 629 |
// |
| 630 |
// logarithm: |
| 631 |
// log(1 + x + ... + x^{a-1}/(a-1)!) - x |
| 632 |
// no overflow or underflow danger for main term thanks to bounds |
| 633 |
double sum = 1.0; |
| 634 |
double term = 1.0; |
| 635 |
for (uint32_t nn = 1; nn != aa; ++nn) { |
| 636 |
// division is slow enough that the lookup table speeds up this function by |
| 637 |
// >3x |
| 638 |
term *= xx * kSmallRecips[nn]; |
| 639 |
sum += term; |
| 640 |
} |
| 641 |
return log(sum) - xx; |
| 642 |
} |
| 643 |
|
| 644 |
double erfc_fast2(double zz, double* tau_ln_plus_z2_ptr) { |
| 645 |
const double tt = 1.0 / (1.0 + 0.5 * zz); |
| 646 |
*tau_ln_plus_z2_ptr = ((((((((0.17087277 * tt - 0.82215223) * tt + 1.48851587) * tt - 1.13520398) * tt + 0.27886807) * tt - 0.18628806) * tt + 0.09678418) * tt + 0.37409196) * tt + 1.00002368) * tt - 1.26551223; |
| 647 |
return tt; |
| 648 |
} |
| 649 |
|
| 650 |
double finite_half_gamma_q2_ln(uint32_t a_minus_half, double xx) { |
| 651 |
// a is in {0.5, 1.5, ..., 29.5}; max(0.2, a-1) < x < 1e10 |
| 652 |
const double sqrt_x = sqrt(xx); |
| 653 |
double tau_ln_plus_x; |
| 654 |
double tt = erfc_fast2(sqrt_x, &tau_ln_plus_x); |
| 655 |
if (!a_minus_half) { |
| 656 |
return log(tt) + tau_ln_plus_x - xx; |
| 657 |
} |
| 658 |
// pre-multiply by e^x to avoid underflow |
| 659 |
double term = sqrt_x * (2.0 / kSqrtPi); |
| 660 |
double sum = term; |
| 661 |
for (uint32_t n_minus_half = 1; n_minus_half != a_minus_half; ++n_minus_half) { |
| 662 |
term *= xx * kSmallHalfRecips[n_minus_half]; |
| 663 |
sum += term; |
| 664 |
} |
| 665 |
// tau_ln + xx guaranteed to be small |
| 666 |
double ee = tt * exp(tau_ln_plus_x) + sum; |
| 667 |
return log(ee) - xx; |
| 668 |
} |
| 669 |
|
| 670 |
static const double kLnSqrtPi = 0.5723649429247001; |
| 671 |
|
| 672 |
// compute -log((z^a)(e^{-z})/tgamma(a)) |
| 673 |
double regularized_gamma_prefix_ln(double aa, double zz) { |
| 674 |
// assumes a == 0.5 if a < 1. assumes z > 0. |
| 675 |
// we are fine with float-level precision, so lanczos_n=6, |
| 676 |
// kLanczosFloatG=5.581 |
| 677 |
if (aa < 1) { |
| 678 |
return -zz + 0.5 * log(zz) - kLnSqrtPi; |
| 679 |
} |
| 680 |
const double agh = aa + kLanczosFloatG - 0.5; |
| 681 |
const double agh_recip = 1.0 / agh; |
| 682 |
const double dd = ((zz - aa) - (kLanczosFloatG - 0.5)) * agh_recip; |
| 683 |
double prefix_ln; |
| 684 |
if ((fabs(dd * dd * aa) <= 100) && (aa > 150)) { |
| 685 |
// abs(dd) < sqrt(2/3) < 0.95 |
| 686 |
prefix_ln = aa * log1pmx(dd) + zz * (0.5 - kLanczosFloatG) * agh_recip; |
| 687 |
} else { |
| 688 |
prefix_ln = (aa - zz) + aa * log(zz * agh_recip); |
| 689 |
} |
| 690 |
const double scaled_recip = lanczos_sum_f_expg_scaled_recip(aa); |
| 691 |
return prefix_ln + 0.5 * log(agh * kRecipE * scaled_recip * scaled_recip); |
| 692 |
} |
| 693 |
|
| 694 |
// does not guarantee return value <= 0 for now; caller must do that. |
| 695 |
double gamma_incomplete_imp2_ln(uint32_t df, double xx) { |
| 696 |
// normalized = true, invert = false |
| 697 |
assert(df); |
| 698 |
assert(xx >= 0.0); |
| 699 |
const double aa = u31tod(df) * 0.5; |
| 700 |
const uint32_t is_small_a = (df < 60) && (aa <= xx + 1) && (xx < 1e10); |
| 701 |
uint32_t is_int = 0; |
| 702 |
uint32_t is_half_int = 0; |
| 703 |
if (is_small_a) { |
| 704 |
is_half_int = df % 2; |
| 705 |
is_int = !is_half_int; |
| 706 |
} |
| 707 |
uint32_t eval_method; |
| 708 |
if (is_int && (xx > 0.6)) { |
| 709 |
eval_method = 0; |
| 710 |
} else if (is_half_int && (xx > 0.2)) { |
| 711 |
eval_method = 1; |
| 712 |
} else if (xx < kSmallEpsilon) { |
| 713 |
// avoid computing log(0) |
| 714 |
// don't need more precision here, 6 digits is enough |
| 715 |
return 0.0; |
| 716 |
} else if (xx < 1.1) { |
| 717 |
eval_method = 2; |
| 718 |
} else { |
| 719 |
const double x_minus_a = xx - aa; |
| 720 |
uint32_t use_temme = 0; |
| 721 |
if (aa > 20) { |
| 722 |
// sigma = abs((x - a) / a); |
| 723 |
// igamma_temme_large() assumes abs(sigma) < 0.95 |
| 724 |
if (aa > 200) { |
| 725 |
// abs(sigma) < sqrt(20 / a) < 0.316... |
| 726 |
use_temme = (20 * aa > x_minus_a * x_minus_a); |
| 727 |
} else { |
| 728 |
// abs(sigma) < 0.4 |
| 729 |
const double sigma_times_a = fabs(x_minus_a); |
| 730 |
use_temme = (sigma_times_a < 0.4 * aa); |
| 731 |
} |
| 732 |
} |
| 733 |
if (use_temme) { |
| 734 |
eval_method = 5; |
| 735 |
} else { |
| 736 |
// x - (1 / (3 * x)) < a |
| 737 |
// x * x - (1/3) < a * x |
| 738 |
// x * x - a * x < 1/3 |
| 739 |
// x * (x - a) < 1/3 |
| 740 |
if (xx * x_minus_a < (1.0 / 3.0)) { |
| 741 |
eval_method = 2; |
| 742 |
} else { |
| 743 |
eval_method = 4; |
| 744 |
} |
| 745 |
} |
| 746 |
} |
| 747 |
switch(eval_method) { |
| 748 |
case 0: |
| 749 |
return finite_gamma_q_ln(df / 2, xx); |
| 750 |
case 1: |
| 751 |
return finite_half_gamma_q2_ln(df / 2, xx); |
| 752 |
case 2: |
| 753 |
{ |
| 754 |
const double result_ln = regularized_gamma_prefix_ln(aa, xx); |
| 755 |
if (result_ln < kLogMinValue + 22) { |
| 756 |
// init_value overflows. Not a big deal, this just ends up getting |
| 757 |
// inverted to pval=1. |
| 758 |
// (+22 since aa could theoretically be as large as 2^31. Todo: find |
| 759 |
// the smallest result_ln value that could result in a nonzero value |
| 760 |
// being returned.) |
| 761 |
return 0.0; |
| 762 |
} |
| 763 |
const double init_value = -aa * exp(-result_ln); |
| 764 |
const double multiplier = -lower_gamma_series(aa, xx, init_value) / aa; |
| 765 |
return result_ln + log(multiplier); |
| 766 |
} |
| 767 |
case 4: |
| 768 |
{ |
| 769 |
const double result1_ln = regularized_gamma_prefix_ln(aa, xx); |
| 770 |
const double result2_ln = log(upper_gamma_fraction(aa, xx)); |
| 771 |
return result1_ln + result2_ln; |
| 772 |
} |
| 773 |
case 5: |
| 774 |
default: // silence compiler warning |
| 775 |
{ |
| 776 |
// aa large, fabs(xx - aa) relatively small, so no overflow/underflow |
| 777 |
// issues. |
| 778 |
double result = igamma_temme_large(aa, xx); |
| 779 |
if (xx < aa) { |
| 780 |
result = 1.0 - result; |
| 781 |
} |
| 782 |
return log(result); |
| 783 |
} |
| 784 |
} |
| 785 |
} |
| 786 |
|
| 787 |
double ChisqToLnP(double chisq, uint32_t df) { |
| 788 |
return MINV(gamma_incomplete_imp2_ln(df, chisq * 0.5), 0.0); |
| 789 |
} |
| 790 |
// ***** end ChisqToLnP ***** |
| 791 |
|
| 792 |
|
| 793 |
// ***** thread-safe PToChisq ***** |
| 794 |
// port of Boost 1.60 implementation |
| 795 |
|
| 796 |
double find_inverse_gamma2(uint32_t df, double pp, double qq, uint32_t* has_10_digits_ptr) { |
| 797 |
// currently assumes *has_10_digits_ptr initialized to zero |
| 798 |
if (df == 2) { |
| 799 |
return -log(qq); |
| 800 |
} |
| 801 |
if (df == 1) { |
| 802 |
// g == tgamma(0.5) == sqrt(pi) |
| 803 |
const double bb = qq * kSqrtPi; |
| 804 |
if (bb >= 0.45) { |
| 805 |
// b * q > 1e-8, q > 1e-5 guaranteed |
| 806 |
// u = pow(p * g * a, 1/a) |
| 807 |
// = pow(p * g * 0.5, 2) |
| 808 |
// = p * p * g * g * 0.25 |
| 809 |
// = p * p * pi * 0.25 |
| 810 |
const double uu = pp * pp * (0.25 * kPi); |
| 811 |
return (uu / (1 - (uu * (1.0 / 1.5)))); |
| 812 |
} else { |
| 813 |
const double yy = -log(bb); |
| 814 |
if (bb > 0.1) { |
| 815 |
const double uu = yy - 0.5 * log(yy); |
| 816 |
if (bb > 0.15) { |
| 817 |
return (yy - 0.5 * log(uu) - log(1 + 0.5 / uu)); |
| 818 |
} |
| 819 |
return (yy - 0.5 * log(uu) - log(((uu + 5) * uu + 3.75) / ((uu + 4.5) * uu + 2))); |
| 820 |
} else { |
| 821 |
const double c1 = -0.5 * log(yy); |
| 822 |
const double c1_2 = c1 * c1; |
| 823 |
const double c1_3 = c1_2 * c1; |
| 824 |
const double c1_4 = c1_2 * c1_2; |
| 825 |
// a_2 = 0.25 |
| 826 |
// a_3 = 0.125 |
| 827 |
|
| 828 |
const double c2 = -0.5 * (1 + c1); |
| 829 |
const double c3 = 0.25 * c1_2 + 0.75 * c1 + 0.875; |
| 830 |
const double c4 = c1_3 * (-1.0 / 6.0) - 0.875 * c1_2 - 1.875 * c1 - (26.75 / 12.0); |
| 831 |
const double c5 = 0.125 * c1_4 + (5.75 / 6.0) * c1_3 + 3.625 * c1_2 + 7.75 * c1 + (83.0625 / 12.0); |
| 832 |
|
| 833 |
const double y_recip = 1.0 / yy; |
| 834 |
const double y_recip_2 = y_recip * y_recip; |
| 835 |
const double y_recip_3 = y_recip_2 * y_recip; |
| 836 |
const double y_recip_4 = y_recip_2 * y_recip_2; |
| 837 |
if (bb < 1e-28) { |
| 838 |
*has_10_digits_ptr = 1; |
| 839 |
} |
| 840 |
// er, I'd think this should just use Horner's instead? |
| 841 |
return (yy + c1 + c2 * y_recip + c3 * y_recip_2 + c4 * y_recip_3 + c5 * y_recip_4); |
| 842 |
} |
| 843 |
} |
| 844 |
} |
| 845 |
// not implemented yet |
| 846 |
assert(0); |
| 847 |
exit(1); |
| 848 |
return 0; |
| 849 |
} |
| 850 |
|
| 851 |
double gamma_p_inv_imp2(uint32_t df, double qq) { |
| 852 |
assert(df); |
| 853 |
assert(qq > 0.0); |
| 854 |
if (qq >= 1.0 - kSmallEpsilon) { |
| 855 |
return 0; |
| 856 |
} |
| 857 |
double pp = 1.0 - qq; |
| 858 |
uint32_t has_10_digits = 0; |
| 859 |
double guess = find_inverse_gamma2(df, pp, qq, &has_10_digits); |
| 860 |
if (has_10_digits) { |
| 861 |
return guess; |
| 862 |
} |
| 863 |
double min_guess = kSmallEpsilon; |
| 864 |
double max_guess = DBL_MAX; |
| 865 |
if (guess < kSmallEpsilon) { |
| 866 |
guess = kSmallEpsilon; |
| 867 |
} |
| 868 |
// halley iteration, digits == 24, lower == kSmallEpsilon |
| 869 |
// see second_order_root_finder in boost/math/tools/roots.hpp |
| 870 |
const uint32_t invert = (pp > 0.9); |
| 871 |
if (invert) { |
| 872 |
pp = qq; |
| 873 |
} |
| 874 |
const double a_minus_1 = 0.5 * S_CAST(double, S_CAST(int32_t, df) - 2); |
| 875 |
const double factor = 1.1920928955078125e-07; // 2^{-23} |
| 876 |
double result = guess; |
| 877 |
double delta = 10000000; |
| 878 |
double delta1 = delta; |
| 879 |
uint32_t out_of_bounds_sentry = 0; |
| 880 |
do { |
| 881 |
double delta2 = delta1; |
| 882 |
delta1 = delta; |
| 883 |
|
| 884 |
// see gamma_p_inverse_func in |
| 885 |
// boost/math/special_functions/detail/igamma_inverse.hpp |
| 886 |
double f1; |
| 887 |
const double ff = gamma_incomplete_imp2(df, result, invert, &f1); |
| 888 |
const double div = (a_minus_1 - result) / result; |
| 889 |
double f2 = f1; |
| 890 |
if ((fabs(div) > 1) && (DBL_MAX / fabs(div) < f2)) { |
| 891 |
// overflow |
| 892 |
f2 = -DBL_MAX / 2; |
| 893 |
} else { |
| 894 |
f2 *= div; |
| 895 |
} |
| 896 |
if (invert) { |
| 897 |
f1 = -f1; |
| 898 |
f2 = -f2; |
| 899 |
} |
| 900 |
const double f0 = ff - pp; |
| 901 |
if (f0 == 0) { |
| 902 |
break; |
| 903 |
} |
| 904 |
assert(f1 != 0); // shouldn't be possible, function is monotonic |
| 905 |
delta = f0 / f1; |
| 906 |
if (f2 != 0) { |
| 907 |
// delta = Stepper::step(result, f0, f1, f2); |
| 908 |
const double denom = 2 * f0; |
| 909 |
const double numer = 2 * f1 - f0 * (f2 / f1); |
| 910 |
if ((fabs(numer) >= 1) || (fabs(denom) < fabs(numer) * DBL_MAX)) { |
| 911 |
const double halley_step = denom / numer; |
| 912 |
if (halley_step / delta < 0) { |
| 913 |
if (fabs(delta) > 2 * fabs(guess)) { |
| 914 |
delta = ((delta < 0)? -1 : 1) * 2 * fabs(guess); |
| 915 |
} |
| 916 |
} else { |
| 917 |
delta = halley_step; |
| 918 |
} |
| 919 |
} |
| 920 |
} |
| 921 |
double convergence = fabs(delta / delta2); |
| 922 |
if ((convergence > 0.8) && (convergence < 2)) { |
| 923 |
delta = (delta > 0)? (0.5 * (result - min_guess)) : (0.5 * (result - max_guess)); |
| 924 |
if (fabs(delta) > result) { |
| 925 |
delta = ((delta > 0)? 1 : -1) * result; |
| 926 |
} |
| 927 |
// delta2 = delta * 3; |
| 928 |
} |
| 929 |
guess = result; |
| 930 |
result -= delta; |
| 931 |
// do we actually need this? |
| 932 |
if (result < min_guess) { |
| 933 |
double diff = ((fabs(min_guess) < 1) && (fabs(result) > 1) && ((DBL_MAX / fabs(result)) < fabs(min_guess)))? 1000 : (result / min_guess); |
| 934 |
if (fabs(diff) < 1) { |
| 935 |
diff = 1 / diff; |
| 936 |
} |
| 937 |
if ((!out_of_bounds_sentry) && (diff > 0) && (diff < 3)) { |
| 938 |
delta = 0.99 * (guess - min_guess); |
| 939 |
result = guess - delta; |
| 940 |
out_of_bounds_sentry = 1; |
| 941 |
} else { |
| 942 |
delta = (guess - min_guess) * 0.5; |
| 943 |
result = guess - delta; |
| 944 |
if ((result == min_guess) || (result == max_guess)) { |
| 945 |
break; |
| 946 |
} |
| 947 |
} |
| 948 |
} else if (result > max_guess) { |
| 949 |
double diff = ((fabs(max_guess) < 1) && (fabs(result) > 1) && ((DBL_MAX / fabs(result)) < fabs(max_guess)))? 1000 : (result / max_guess); |
| 950 |
if (fabs(diff) < 1) { |
| 951 |
diff = 1 / diff; |
| 952 |
} |
| 953 |
if ((!out_of_bounds_sentry) && (diff > 0) && (diff < 3)) { |
| 954 |
delta = 0.99 * (guess - max_guess); |
| 955 |
result = guess - delta; |
| 956 |
out_of_bounds_sentry = 1; |
| 957 |
} else { |
| 958 |
delta = (guess - max_guess) * 0.5; |
| 959 |
result = guess - delta; |
| 960 |
if ((result == min_guess) || (result == max_guess)) { |
| 961 |
break; |
| 962 |
} |
| 963 |
} |
| 964 |
} |
| 965 |
if (delta > 0) { |
| 966 |
max_guess = guess; |
| 967 |
} else { |
| 968 |
min_guess = guess; |
| 969 |
} |
| 970 |
} while (fabs(result * factor) < fabs(delta)); |
| 971 |
return result; |
| 972 |
} |
| 973 |
|
| 974 |
double PToChisq(double pval, uint32_t df) { |
| 975 |
// only need this to handle df=1, 2, 4 for now |
| 976 |
return gamma_p_inv_imp2(df, pval) * 2; |
| 977 |
} |
| 978 |
|
| 979 |
// ***** end thread-safe PToChisq ***** |
| 980 |
|
| 981 |
double LnPToChisq(double ln_pval) { |
| 982 |
if (ln_pval > -65.04474754675797) { |
| 983 |
return gamma_p_inv_imp2(1, exp(ln_pval)) * 2; |
| 984 |
} |
| 985 |
// (bb < 1e-28) case in find_inverse_gamma2() |
| 986 |
const double yy = kLnSqrtPi - ln_pval; |
| 987 |
const double c1 = -0.5 * log(yy); |
| 988 |
const double c1_2 = c1 * c1; |
| 989 |
const double c1_3 = c1_2 * c1; |
| 990 |
const double c1_4 = c1_2 * c1_2; |
| 991 |
// a_2 = 0.25 |
| 992 |
// a_3 = 0.125 |
| 993 |
|
| 994 |
const double c2 = -0.5 * (1 + c1); |
| 995 |
const double c3 = 0.25 * c1_2 + 0.75 * c1 + 0.875; |
| 996 |
const double c4 = c1_3 * (-1.0 / 6.0) - 0.875 * c1_2 - 1.875 * c1 - (26.75 / 12.0); |
| 997 |
const double c5 = 0.125 * c1_4 + (5.75 / 6.0) * c1_3 + 3.625 * c1_2 + 7.75 * c1 + (83.0625 / 12.0); |
| 998 |
|
| 999 |
const double y_recip = 1.0 / yy; |
| 1000 |
const double y_recip_2 = y_recip * y_recip; |
| 1001 |
const double y_recip_3 = y_recip_2 * y_recip; |
| 1002 |
const double y_recip_4 = y_recip_2 * y_recip_2; |
| 1003 |
// er, I'd think this should just use Horner's instead? |
| 1004 |
return 2 * (yy + c1 + c2 * y_recip + c3 * y_recip_2 + c4 * y_recip_3 + c5 * y_recip_4); |
| 1005 |
} |
| 1006 |
|
| 1007 |
|
| 1008 |
// ***** thread-safe TstatToP ***** |
| 1009 |
// ...time to get rid of this |
| 1010 |
|
| 1011 |
// see Numerical Recipes, section 6.4 |
| 1012 |
// no overflow or convergence issues |
| 1013 |
double betacf_slow(double aa, double bb, double xx) { |
| 1014 |
double qab = aa + bb; |
| 1015 |
double qap = aa + 1.0; |
| 1016 |
double qam = aa - 1.0; |
| 1017 |
double cc = 1.0; |
| 1018 |
double dd = 1.0 - qab * xx / qap; |
| 1019 |
if (fabs(dd) < kLentzFpmin) { |
| 1020 |
dd = kLentzFpmin; |
| 1021 |
} |
| 1022 |
dd = 1.0 / dd; |
| 1023 |
double hh = dd; |
| 1024 |
// evaluate 1 / (1 + d_1 / (1 + d_2 / (1 + d_3 / (...)))) |
| 1025 |
// might need more iterations for very large df? |
| 1026 |
for (double mm = 1.0; mm <= 100.0; mm += 1.0) { |
| 1027 |
double m2 = 2 * mm; |
| 1028 |
|
| 1029 |
// d_{2m} |
| 1030 |
double tmp_aa = mm * (bb - mm) * xx / ((qam + m2) * (aa + m2)); |
| 1031 |
|
| 1032 |
dd = 1.0 + tmp_aa * dd; |
| 1033 |
if (fabs(dd) < kLentzFpmin) { |
| 1034 |
dd = kLentzFpmin; |
| 1035 |
} |
| 1036 |
cc = 1.0 + tmp_aa / cc; |
| 1037 |
if (fabs(cc) < kLentzFpmin) { |
| 1038 |
cc = kLentzFpmin; |
| 1039 |
} |
| 1040 |
dd = 1.0 / dd; |
| 1041 |
hh *= dd * cc; |
| 1042 |
|
| 1043 |
// d_{2m+1} |
| 1044 |
tmp_aa = -(aa + mm) * (qab + mm) * xx / ((aa + m2) * (qap + m2)); |
| 1045 |
|
| 1046 |
dd = 1.0 + tmp_aa * dd; |
| 1047 |
if (fabs(dd) < kLentzFpmin) { |
| 1048 |
dd = kLentzFpmin; |
| 1049 |
} |
| 1050 |
cc = 1.0 + tmp_aa / cc; |
| 1051 |
if (fabs(cc) < kLentzFpmin) { |
| 1052 |
cc = kLentzFpmin; |
| 1053 |
} |
| 1054 |
dd = 1.0 / dd; |
| 1055 |
double del = dd * cc; |
| 1056 |
hh *= del; |
| 1057 |
if (fabs(del - 1.0) < kContinuedFractionEpsilon) { |
| 1058 |
return hh; |
| 1059 |
} |
| 1060 |
} |
| 1061 |
// don't detect failure for now |
| 1062 |
return hh; |
| 1063 |
} |
| 1064 |
|
| 1065 |
// Returns log(1 - x^y). |
| 1066 |
// Assumes x in (0, 1), y positive. |
| 1067 |
double neg_powm1_imp_ln(double xx, double yy) { |
| 1068 |
const double ll = yy * log(xx); |
| 1069 |
if (ll > -1.0) { |
| 1070 |
// For tiny |l|, we may lose all precision when exp(l) evaluates to 1. |
| 1071 |
return log(-expm1(ll)); |
| 1072 |
} |
| 1073 |
// For larger |l|, it's the log step that may throw away precision. |
| 1074 |
if (ll < kLogMinValue) { |
| 1075 |
return 0.0; |
| 1076 |
} |
| 1077 |
return log1p(-exp(ll)); |
| 1078 |
} |
| 1079 |
|
| 1080 |
double binomial_ccdf_ln(uint32_t nn, uint32_t kk, double xx, double yy, uint32_t inv) { |
| 1081 |
// x^n + (n choose 1)x^{n-1}y + ... + (n choose (n-k-1))x^{k+1}y^{n-k-1} |
| 1082 |
// This is currently just designed to work with ibeta_imp2_ln(). So we |
| 1083 |
// assume x and y positive, y = 1 - x, (n-k) < 40, (n-k) <= (k+1)(y/x). |
| 1084 |
|
| 1085 |
// Thanks to the (n-k) <= (k+1)(y/x) condition, it's always reasonable to |
| 1086 |
// start from the rightmost term normalized to 1, and sum leftward; then we |
| 1087 |
// multiply by the rightmost term at the end. |
| 1088 |
const double n_d = u31tod(nn); |
| 1089 |
const double k_plus1_d = u31tod(kk + 1); |
| 1090 |
const double x_div_y = xx / yy; |
| 1091 |
double multiplier = 1.0; |
| 1092 |
double cur_term = 1.0; |
| 1093 |
// need to shift to avoid overflow for n > 1 billion, (n-k) close to 40 |
| 1094 |
double shifted_inv_binom_coeff = 1.0 / kExactTestBias; |
| 1095 |
double i_d = n_d - k_plus1_d; |
| 1096 |
for (uint32_t uii = nn - kk - 1; uii; --uii) { |
| 1097 |
// (n choose (i - 1)) / (n choose i) |
| 1098 |
// i!(n-i)! / (i-1)!(n-i+1)! |
| 1099 |
// = i / (n-i+1) |
| 1100 |
if (cur_term < kRecip2m53) { |
| 1101 |
// Only need to finish computing shifted_inv_binom_coeff: multiply by |
| 1102 |
// i!, divide by n!/(n-i)!. |
| 1103 |
// Since we support n up to 2^31, n!/(n-i)! may overflow for i>33. |
| 1104 |
for (; uii > 33; --uii) { |
| 1105 |
shifted_inv_binom_coeff *= i_d; |
| 1106 |
i_d -= 1.0; |
| 1107 |
shifted_inv_binom_coeff /= n_d - i_d; |
| 1108 |
} |
| 1109 |
double denom = kFactorialRecips[uii]; |
| 1110 |
double n_minus_i = n_d - i_d; |
| 1111 |
for (; uii; --uii) { |
| 1112 |
n_minus_i += 1.0; |
| 1113 |
denom *= n_minus_i; |
| 1114 |
} |
| 1115 |
shifted_inv_binom_coeff /= denom; |
| 1116 |
break; |
| 1117 |
} |
| 1118 |
double cur_binom_ratio = i_d; |
| 1119 |
i_d -= 1.0; |
| 1120 |
cur_binom_ratio /= n_d - i_d; |
| 1121 |
shifted_inv_binom_coeff *= cur_binom_ratio; |
| 1122 |
cur_term *= cur_binom_ratio * x_div_y; |
| 1123 |
multiplier += cur_term; |
| 1124 |
} |
| 1125 |
// 1.0 / kExactTestBias = 2^83 |
| 1126 |
// tried taking log(multiplier / shifted_inv_binom_coeff), but that was |
| 1127 |
// slower on my Mac? |
| 1128 |
const double result_ln = log(multiplier) + k_plus1_d * log(xx) + (n_d - k_plus1_d) * log(yy) - log(shifted_inv_binom_coeff) + 83 * kLn2; |
| 1129 |
if (!inv) { |
| 1130 |
return result_ln; |
| 1131 |
} |
| 1132 |
// The nightmare scenario of tiny result_ln shouldn't happen thanks to the |
| 1133 |
// (n-k) <= (k+1)(y/x) condition. |
| 1134 |
return log1p(-exp(result_ln)); |
| 1135 |
} |
| 1136 |
|
| 1137 |
double ibeta_series_ln(double aa, double bb, double xx, uint32_t inv) { |
| 1138 |
// BPSER in DiDonato and Morris. |
| 1139 |
// ibeta(a, b, x) ~= gamma(a+b) / (gamma(a) * gamma(b)) |
| 1140 |
// * x^a / a |
| 1141 |
// * (1 + a\sum_j=1^N [(1-b)(2-b)...(j-b)x^j / (j!(a+j))]) |
| 1142 |
// Currently always called with x <= 0.7, and either b*x <= 0.7 or b<40, so |
| 1143 |
// guaranteed to converge at reasonable speed. |
| 1144 |
|
| 1145 |
// normalized always true |
| 1146 |
|
| 1147 |
const double cc = aa + bb; |
| 1148 |
|
| 1149 |
const double agh = aa + kLanczosFloatG - 0.5; |
| 1150 |
const double bgh = bb + kLanczosFloatG - 0.5; |
| 1151 |
const double cgh = cc + kLanczosFloatG - 0.5; |
| 1152 |
double numer_a; |
| 1153 |
double denom_a = lanczos_sum_f_expg_scaled_imp(aa, &numer_a); |
| 1154 |
double numer_b; |
| 1155 |
double denom_b = lanczos_sum_f_expg_scaled_imp(bb, &numer_b); |
| 1156 |
double denom_c; |
| 1157 |
double numer_c = lanczos_sum_f_expg_scaled_imp(cc, &denom_c); |
| 1158 |
double result = (numer_a * numer_b * numer_c) / (denom_a * denom_b * denom_c); |
| 1159 |
double l1 = log(cgh / bgh) * (bb - 0.5); |
| 1160 |
double l2 = log(xx * cgh / agh) * aa; |
| 1161 |
double result_ln = log(result * result * agh) * 0.5 + l1 + l2 - 0.5; |
| 1162 |
|
| 1163 |
double series_sum = 0.0; |
| 1164 |
double term = 1.0; |
| 1165 |
double apn = aa; |
| 1166 |
double poch = 1.0 - bb; |
| 1167 |
double nn = 1.0; |
| 1168 |
while (1) { |
| 1169 |
double rr = term / apn; |
| 1170 |
series_sum += rr; |
| 1171 |
if (fabs(rr) <= fabs(series_sum * kEpsilon)) { |
| 1172 |
result_ln += log(series_sum); |
| 1173 |
if (!inv) { |
| 1174 |
return result_ln; |
| 1175 |
} |
| 1176 |
// assume for now that we wouldn't be inverting into the tiny result_ln |
| 1177 |
// case. |
| 1178 |
return log1p(-exp(result_ln)); |
| 1179 |
} |
| 1180 |
apn += 1.0; |
| 1181 |
term *= poch * xx / nn; |
| 1182 |
nn += 1.0; |
| 1183 |
poch += 1.0; |
| 1184 |
} |
| 1185 |
} |
| 1186 |
|
| 1187 |
double ln_sum(double aa, double bb) { |
| 1188 |
if (aa > bb) { |
| 1189 |
const double tmp = aa; |
| 1190 |
aa = bb; |
| 1191 |
bb = tmp; |
| 1192 |
} |
| 1193 |
const double diff = bb - aa; |
| 1194 |
if ((aa == -DBL_MAX) || (diff >= kLn2 * 53)) { |
| 1195 |
return bb; |
| 1196 |
} |
| 1197 |
return bb + log(1 + exp(-diff)); |
| 1198 |
} |
| 1199 |
|
| 1200 |
double tgamma_delta_ratio(double zz, double delta) { |
| 1201 |
// gamma(z) / gamma(z + delta) |
| 1202 |
// We only call this with delta = 0.5 or 1, so no overflow issues. |
| 1203 |
// zz >= 15 for now. |
| 1204 |
if (delta == 1.0) { |
| 1205 |
// Trivial case. |
| 1206 |
return 1.0 / zz; |
| 1207 |
} |
| 1208 |
assert(delta == 0.5); |
| 1209 |
// Can skip z < epsilon and z + delta == z branches for now. |
| 1210 |
double zgh = zz + kLanczosFloatG - 0.5; |
| 1211 |
// Also skip fabs(delta) >= 10 branch for now. |
| 1212 |
double result = exp((0.5 - zz) * log1p(delta / zgh)); |
| 1213 |
result *= lanczos_sum_f(zz) / lanczos_sum_f(zz + delta); |
| 1214 |
// exploit forced delta == 0.5 |
| 1215 |
result *= sqrt(kE / (zgh + delta)); |
| 1216 |
return result; |
| 1217 |
} |
| 1218 |
|
| 1219 |
double beta_small_b_large_a_series_ln(double aa, double bb, double xx, double yy, double s0_ln, uint32_t inv) { |
| 1220 |
// BGRAT in DiDonato and Morris. |
| 1221 |
|
| 1222 |
// normalized always true, mult always 1 |
| 1223 |
// a >= 15, b == 0.5 or 1, and ibeta_imp was patched to ensure x > 0.7. |
| 1224 |
double bm1 = bb - 1.0; |
| 1225 |
double tt = aa + bm1 * 0.5; |
| 1226 |
double lx; |
| 1227 |
if (yy < 0.35) { |
| 1228 |
lx = log1p(-yy); |
| 1229 |
} else { |
| 1230 |
lx = log(xx); |
| 1231 |
} |
| 1232 |
double uu = -tt * lx; |
| 1233 |
double hh_ln = regularized_gamma_prefix_ln(bb, uu); |
| 1234 |
double prefix_ln = hh_ln - log(tgamma_delta_ratio(aa, bb)) - bb * log(tt); |
| 1235 |
// validated up to this point |
| 1236 |
|
| 1237 |
double pp[15]; // ~8-15 digit accuracy |
| 1238 |
pp[0] = 1.0; |
| 1239 |
|
| 1240 |
double jj; |
| 1241 |
{ |
| 1242 |
double dxx; |
| 1243 |
if (bb == 0.5) { |
| 1244 |
// bugfix (17 Jun 2019): original expression could underflow |
| 1245 |
// jj = finite_half_gamma_q2(0, uu, nullptr); |
| 1246 |
dxx = finite_half_gamma_q2_ln(0, uu); |
| 1247 |
} else { |
| 1248 |
assert(bb == 1.0); |
| 1249 |
dxx = -uu; |
| 1250 |
} |
| 1251 |
// Underflow is harmless here, jj becomes nonzero for the other terms in |
| 1252 |
// the series in that case. |
| 1253 |
jj = exp(dxx - hh_ln); |
| 1254 |
} |
| 1255 |
double sum = jj; // patch in s0 and prefix at the end |
| 1256 |
uint32_t tnp1 = 1; |
| 1257 |
double lx2 = lx * 0.5; |
| 1258 |
lx2 *= lx2; |
| 1259 |
double lxp = 1.0; |
| 1260 |
double t4_recip = 0.25 / (tt * tt); |
| 1261 |
double b2n = bb; |
| 1262 |
for (uint32_t nn = 1; nn < 15; ++nn) { |
| 1263 |
tnp1 += 2; |
| 1264 |
double new_pn = 0.0; |
| 1265 |
uint32_t tmp1 = 3; |
| 1266 |
const double nn_d = u31tod(nn); |
| 1267 |
for (uint32_t mm = 1; mm < nn; ++mm) { |
| 1268 |
double mbn = u31tod(mm) * bb - nn_d; |
| 1269 |
new_pn += mbn * pp[nn - mm] * kFactorialRecips[tmp1]; |
| 1270 |
tmp1 += 2; |
| 1271 |
} |
| 1272 |
new_pn /= nn_d; |
| 1273 |
new_pn += bm1 * kFactorialRecips[tnp1]; |
| 1274 |
pp[nn] = new_pn; |
| 1275 |
|
| 1276 |
jj = (b2n * (b2n + 1) * jj + (uu + b2n + 1) * lxp) * t4_recip; |
| 1277 |
lxp *= lx2; |
| 1278 |
b2n += 2; |
| 1279 |
|
| 1280 |
double rr = new_pn * jj; |
| 1281 |
sum += rr; |
| 1282 |
if (rr > 1) { |
| 1283 |
if (fabs(rr) < fabs(kEpsilon * sum)) { |
| 1284 |
break; |
| 1285 |
} |
| 1286 |
} else { |
| 1287 |
if (fabs(rr * (1.0 / kEpsilon)) < fabs(sum)) { |
| 1288 |
break; |
| 1289 |
} |
| 1290 |
} |
| 1291 |
} |
| 1292 |
double result_ln = ln_sum(s0_ln, prefix_ln + log(sum)); |
| 1293 |
if (!inv) { |
| 1294 |
return result_ln; |
| 1295 |
} |
| 1296 |
return log1p(-exp(result_ln)); |
| 1297 |
} |
| 1298 |
|
| 1299 |
double ibeta_power_terms_ln(double aa, double bb, double xx, double yy) { |
| 1300 |
// returns log((x^a)(y^b) / Beta(a,b)) |
| 1301 |
// |
| 1302 |
// normalized always true |
| 1303 |
// prefix always 1 |
| 1304 |
double cc = aa + bb; |
| 1305 |
|
| 1306 |
const double agh = aa + kLanczosFloatG - 0.5; |
| 1307 |
const double bgh = bb + kLanczosFloatG - 0.5; |
| 1308 |
const double cgh = cc + kLanczosFloatG - 0.5; |
| 1309 |
double numer_a; |
| 1310 |
double denom_a = lanczos_sum_f_expg_scaled_imp(aa, &numer_a); |
| 1311 |
double numer_b; |
| 1312 |
double denom_b = lanczos_sum_f_expg_scaled_imp(bb, &numer_b); |
| 1313 |
double denom_c; |
| 1314 |
double numer_c = lanczos_sum_f_expg_scaled_imp(cc, &denom_c); |
| 1315 |
double result = (numer_a * numer_b * numer_c) / (denom_a * denom_b * denom_c); |
| 1316 |
result *= sqrt(agh * bgh * kRecipE / cgh); |
| 1317 |
double result_ln = log(result); |
| 1318 |
double l1 = (xx * bb - yy * agh) / agh; |
| 1319 |
double l2 = (yy * aa - xx * bgh) / bgh; |
| 1320 |
// Since we're returning log(result) rather than the original result (thus, |
| 1321 |
// no intermediate overflow/underflow problems), and we only need 6 digits of |
| 1322 |
// precision, we shouldn't need any of the numerous cases in the Boost code. |
| 1323 |
return result_ln + aa * log1p(l1) + bb * log1p(l2); |
| 1324 |
} |
| 1325 |
|
| 1326 |
double ibeta_a_step_ln(double aa, double bb, double xx, double yy, uint32_t kk) { |
| 1327 |
double prefix_ln = ibeta_power_terms_ln(aa, bb, xx, yy) - log(aa); |
| 1328 |
double sum = 1.0; |
| 1329 |
double term = 1.0; |
| 1330 |
const double a_plus_b = aa + bb; |
| 1331 |
const double k_minus_1pt5 = u31tod(kk) - 1.5; |
| 1332 |
for (double i_d = 0.0; i_d < k_minus_1pt5; ) { |
| 1333 |
term *= (a_plus_b + i_d) * xx; |
| 1334 |
i_d += 1.0; |
| 1335 |
term /= aa + i_d; |
| 1336 |
sum += term; |
| 1337 |
} |
| 1338 |
return prefix_ln + log(sum); |
| 1339 |
} |
| 1340 |
|
| 1341 |
double ibeta_fraction2_ln(double aa, double bb, double xx, double yy, uint32_t inv) { |
| 1342 |
// normalized always true |
| 1343 |
|
| 1344 |
// todo: original DiDonato and Morris paper notes that "x must also be a |
| 1345 |
// sufficient distance from p when a > 100"; check if we have a problem there |
| 1346 |
|
| 1347 |
double result_ln = ibeta_power_terms_ln(aa, bb, xx, yy); |
| 1348 |
|
| 1349 |
// see Boost continued_fraction_b() |
| 1350 |
const double ay_minus_bx_plus1 = aa * yy - bb * xx + 1.0; |
| 1351 |
double cc = (aa * ay_minus_bx_plus1) / (aa + 1.0); |
| 1352 |
if (fabs(cc) < kLentzFpmin) { |
| 1353 |
cc = kLentzFpmin; |
| 1354 |
} |
| 1355 |
const double a_plus_b = aa + bb; |
| 1356 |
const double x2 = xx * xx; |
| 1357 |
result_ln -= log(cc); |
| 1358 |
double dd = 0.0; |
| 1359 |
double mm = 0.0; |
| 1360 |
while (1) { |
| 1361 |
double cur_a = (aa + mm) * (a_plus_b + mm); |
| 1362 |
mm += 1.0; |
| 1363 |
cur_a *= mm * (bb - mm) * x2; |
| 1364 |
double denom = aa + 2 * mm - 1.0; |
| 1365 |
cur_a /= denom * denom; |
| 1366 |
double cur_b = mm; |
| 1367 |
cur_b += (mm * (bb - mm) * xx) / (aa + 2 * mm - 1.0); |
| 1368 |
cur_b += ((aa + mm) * (ay_minus_bx_plus1 + mm * (2.0 - xx))) / (aa + 2 * mm + 1.0); |
| 1369 |
dd = cur_b + cur_a * dd; |
| 1370 |
if (fabs(dd) < kLentzFpmin) { |
| 1371 |
dd = kLentzFpmin; |
| 1372 |
} |
| 1373 |
cc = cur_b + cur_a / cc; |
| 1374 |
if (fabs(cc) < kLentzFpmin) { |
| 1375 |
cc = kLentzFpmin; |
| 1376 |
} |
| 1377 |
dd = 1.0 / dd; |
| 1378 |
const double delta = cc * dd; |
| 1379 |
result_ln -= log(delta); |
| 1380 |
if (fabs(delta - 1.0) <= kContinuedFractionEpsilon) { |
| 1381 |
return inv? log1p(-exp(result_ln)) : result_ln; |
| 1382 |
} |
| 1383 |
} |
| 1384 |
} |
| 1385 |
|
| 1386 |
// the '2' in imp2 refers to df1 and df2 arguments instead of a and b |
| 1387 |
double ibeta_imp2_ln(uint32_t df1, uint32_t df2, double xx, uint32_t inv) { |
| 1388 |
// In addition to Boost beta.hpp and its dependencies, see DiDonato and |
| 1389 |
// Morris's original paper at |
| 1390 |
// https://apps.dtic.mil/dtic/tr/fulltext/u2/a210118.pdf . |
| 1391 |
|
| 1392 |
// normalized always true |
| 1393 |
// |
| 1394 |
// assume a >= 0, b >= 0, a+b > 0, x in [0, 1], a and b are multiples of 0.5 |
| 1395 |
// for now |
| 1396 |
// in practice, x always <= 0.5 right now |
| 1397 |
if (df1 == 0) { |
| 1398 |
return inv? -DBL_MAX : 0.0; |
| 1399 |
} |
| 1400 |
if (df2 == 0) { |
| 1401 |
return inv? 0.0 : -DBL_MAX; |
| 1402 |
} |
| 1403 |
if (xx == 0.0) { |
| 1404 |
return inv? 0.0 : -DBL_MAX; |
| 1405 |
} |
| 1406 |
if (xx == 1.0) { |
| 1407 |
return inv? -DBL_MAX : 0.0; |
| 1408 |
} |
| 1409 |
double yy = 1.0 - xx; |
| 1410 |
if ((df1 == 1) && (df2 == 1)) { |
| 1411 |
return log(asin(sqrt(inv? yy : xx)) * (2.0 / kPi)); |
| 1412 |
} |
| 1413 |
if (df1 == 2) { |
| 1414 |
df1 = df2; |
| 1415 |
df2 = 2; |
| 1416 |
|
| 1417 |
const double tmp = xx; |
| 1418 |
xx = yy; |
| 1419 |
yy = tmp; |
| 1420 |
|
| 1421 |
inv = !inv; |
| 1422 |
} |
| 1423 |
double aa = u31tod(df1) * 0.5; |
| 1424 |
if (df2 == 2) { |
| 1425 |
if (df1 == 2) { |
| 1426 |
return log(inv? yy : xx); |
| 1427 |
} |
| 1428 |
double ln_pp; |
| 1429 |
if (yy < 0.5) { |
| 1430 |
// pp = inv? -expm1(aa * log1p(-yy)) : exp(aa * log1p(-yy)) |
| 1431 |
// -> ln_pp = inv? log(-exp(aa * log1p(-yy)) + 1.0) : (aa * log1p(-yy)) |
| 1432 |
// = inv? log1p(-exp(aa * log1p(-yy))) : (aa * log1p(-yy)) |
| 1433 |
const double inner_term = aa * log1p(-yy); |
| 1434 |
ln_pp = inv? log1p(-exp(inner_term)) : inner_term; |
| 1435 |
} else { |
| 1436 |
// pp = inv? -powm1(xx, aa) : pow(xx, aa); |
| 1437 |
// -> ln_pp = inv? log(-powm1(xx, aa)) : (aa * log(xx)) |
| 1438 |
ln_pp = inv? neg_powm1_imp_ln(xx, aa) : (aa * log(xx)); |
| 1439 |
} |
| 1440 |
return ln_pp; |
| 1441 |
} |
| 1442 |
|
| 1443 |
double bb = u31tod(df2) * 0.5; |
| 1444 |
if ((df1 == 1) || (df2 == 1)) { |
| 1445 |
if (xx > 0.5) { |
| 1446 |
const uint32_t tmp1 = df1; |
| 1447 |
df1 = df2; |
| 1448 |
df2 = tmp1; |
| 1449 |
|
| 1450 |
double tmp2 = aa; |
| 1451 |
aa = bb; |
| 1452 |
bb = tmp2; |
| 1453 |
|
| 1454 |
tmp2 = xx; |
| 1455 |
xx = yy; |
| 1456 |
yy = tmp2; |
| 1457 |
|
| 1458 |
inv = !inv; |
| 1459 |
} |
| 1460 |
// can ignore max(a, b) <= 1 branch |
| 1461 |
if ((df2 == 1) || ((xx < 0.1) && (bb * xx <= 0.49))) { |
| 1462 |
return ibeta_series_ln(aa, bb, xx, inv); |
| 1463 |
} |
| 1464 |
// a/b swapped, x/y swapped, inv swapped |
| 1465 |
if (xx >= 0.3) { |
| 1466 |
return ibeta_series_ln(bb, aa, yy, !inv); |
| 1467 |
} |
| 1468 |
if (bb >= 15) { |
| 1469 |
return beta_small_b_large_a_series_ln(bb, aa, yy, xx, -DBL_MAX, !inv); |
| 1470 |
} |
| 1471 |
double fract_ln = ibeta_a_step_ln(bb, aa, yy, xx, 20); |
| 1472 |
return beta_small_b_large_a_series_ln(bb + 20.0, aa, yy, xx, fract_ln, !inv); |
| 1473 |
} |
| 1474 |
double lambda; |
| 1475 |
if (aa < bb) { |
| 1476 |
lambda = aa - (aa + bb) * xx; |
| 1477 |
} else { |
| 1478 |
lambda = (aa + bb) * yy - bb; |
| 1479 |
} |
| 1480 |
if (lambda < 0.0) { |
| 1481 |
const uint32_t tmp1 = df1; |
| 1482 |
df1 = df2; |
| 1483 |
df2 = tmp1; |
| 1484 |
|
| 1485 |
double tmp2 = aa; |
| 1486 |
aa = bb; |
| 1487 |
bb = tmp2; |
| 1488 |
|
| 1489 |
tmp2 = xx; |
| 1490 |
xx = yy; |
| 1491 |
yy = tmp2; |
| 1492 |
|
| 1493 |
inv = !inv; |
| 1494 |
} |
| 1495 |
|
| 1496 |
// a > 1, b > 1 guaranteed if we get here. |
| 1497 |
if (df2 >= 80) { |
| 1498 |
return ibeta_fraction2_ln(aa, bb, xx, yy, inv); |
| 1499 |
} |
| 1500 |
if ((!(df1 % 2)) && (!(df2 % 2)) && (yy != 1.0)) { |
| 1501 |
const uint32_t a_int = df1 / 2; |
| 1502 |
const uint32_t b_int = df2 / 2; |
| 1503 |
const uint32_t kk = a_int - 1; |
| 1504 |
const uint32_t nn = b_int + kk; |
| 1505 |
return binomial_ccdf_ln(nn, kk, xx, yy, inv); |
| 1506 |
} |
| 1507 |
// Changed from b*x <= 0.7 since BGRAT has problems with small x, while BPSER |
| 1508 |
// can handle this larger b*x case since b<40 is guaranteed. |
| 1509 |
if (xx <= 0.7) { |
| 1510 |
return ibeta_series_ln(aa, bb, xx, inv); |
| 1511 |
} |
| 1512 |
const uint32_t nn = (df2 - 1) / 2; |
| 1513 |
const double bbar = bb - u31tod(nn); |
| 1514 |
double fract_ln = ibeta_a_step_ln(bbar, aa, yy, xx, nn); |
| 1515 |
if (df1 > 30) { |
| 1516 |
return beta_small_b_large_a_series_ln(aa, bbar, xx, yy, fract_ln, inv); |
| 1517 |
} |
| 1518 |
fract_ln = ln_sum(fract_ln, ibeta_a_step_ln(aa, bbar, xx, yy, 20)); |
| 1519 |
return beta_small_b_large_a_series_ln(aa + 20.0, bbar, xx, yy, fract_ln, inv); |
| 1520 |
} |
| 1521 |
|
| 1522 |
double TstatToP2(double tt, double df, double cached_gamma_mult) { |
| 1523 |
// assumes cached_mult == exp(lgamma(df * 0.5 + 0.5) - lgamma(df * 0.5) - |
| 1524 |
// lgamma(0.5)) |
| 1525 |
// invert_thresh = (df + 2) / (df + 5) |
| 1526 |
double tt_sq = tt * tt; |
| 1527 |
double denom_recip = 1.0 / (df + tt_sq); |
| 1528 |
double xx = df * denom_recip; |
| 1529 |
double yy = tt_sq * denom_recip; |
| 1530 |
if ((xx < 0.0) || (yy < 0.0)) { |
| 1531 |
return -9; |
| 1532 |
} |
| 1533 |
uint32_t do_invert = (xx * (df + 5.0)) >= (df + 2.0); |
| 1534 |
if ((xx == 0.0) || (yy == 0.0)) { |
| 1535 |
return u31tod(do_invert); |
| 1536 |
} |
| 1537 |
double aa = df * 0.5; |
| 1538 |
double bt = cached_gamma_mult * pow(xx, aa) * sqrt(yy); |
| 1539 |
if (!do_invert) { |
| 1540 |
return bt * betacf_slow(aa, 0.5, xx) / aa; |
| 1541 |
} |
| 1542 |
return 1.0 - bt * 2 * betacf_slow(0.5, aa, yy); |
| 1543 |
} |
| 1544 |
// ***** end thread-safe TstatToP calculation ***** |
| 1545 |
|
| 1546 |
// ***** begin TstatToLnP ***** |
| 1547 |
|
| 1548 |
double TstatToLnP(double tt, uint32_t df) { |
| 1549 |
const double df_d = u31tod(df); |
| 1550 |
const double t2 = tt * tt; |
| 1551 |
if (df_d > 2 * t2) { |
| 1552 |
const double zz = t2 / (df_d + t2); |
| 1553 |
return ibeta_imp2_ln(1, df, zz, 1); |
| 1554 |
} |
| 1555 |
const double zz = df_d / (df_d + t2); |
| 1556 |
return ibeta_imp2_ln(df, 1, zz, 0); |
| 1557 |
} |
| 1558 |
// ***** end TstatToLnP ***** |
| 1559 |
|
| 1560 |
// ***** FstatToLnP ***** |
| 1561 |
|
| 1562 |
double FstatToLnP(double ff, uint32_t df1, uint32_t df2) { |
| 1563 |
// See cdf() for complemented2_type in boost/math/distributions/fisher_f.hpp. |
| 1564 |
const double df1_d = u31tod(df1); |
| 1565 |
const double df2_d = u31tod(df2); |
| 1566 |
const double v1x = df1_d * ff; |
| 1567 |
if (v1x > df2_d) { |
| 1568 |
return ibeta_imp2_ln(df2, df1, df2_d / (df2_d + v1x), 0); |
| 1569 |
} |
| 1570 |
return ibeta_imp2_ln(df1, df2, v1x / (df2_d + v1x), 1); |
| 1571 |
} |
| 1572 |
|
| 1573 |
// ***** end FstatToLnP ***** |
| 1574 |
|
| 1575 |
|
| 1576 |
// Inverse normal distribution |
| 1577 |
// (todo: check if boost implementation is better) |
| 1578 |
|
| 1579 |
// Lower tail quantile for standard normal distribution function. |
| 1580 |
// |
| 1581 |
// This function returns an approximation of the inverse cumulative |
| 1582 |
// standard normal distribution function. I.e., given P, it returns |
| 1583 |
// an approximation to the X satisfying P = Pr{Z <= X} where Z is a |
| 1584 |
// random variable from the standard normal distribution. |
| 1585 |
// |
| 1586 |
// The algorithm uses a minimax approximation by rational functions |
| 1587 |
// and the result has a relative error whose absolute value is less |
| 1588 |
// than 1.15e-9. |
| 1589 |
// |
| 1590 |
// Author: Peter J. Acklam |
| 1591 |
// Time-stamp: 2002-06-09 18:45:44 +0200 |
| 1592 |
// E-mail: [email protected] |
| 1593 |
// WWW URL: http://www.math.uio.no/~jacklam |
| 1594 |
// |
| 1595 |
// C implementation adapted from Peter's Perl version |
| 1596 |
|
| 1597 |
// Coefficients in rational approximations. |
| 1598 |
|
| 1599 |
static const double kIvnA[] = |
| 1600 |
{ |
| 1601 |
-3.969683028665376e+01, |
| 1602 |
2.209460984245205e+02, |
| 1603 |
-2.759285104469687e+02, |
| 1604 |
1.383577518672690e+02, |
| 1605 |
-3.066479806614716e+01, |
| 1606 |
2.506628277459239e+00 |
| 1607 |
}; |
| 1608 |
|
| 1609 |
static const double kIvnB[] = |
| 1610 |
{ |
| 1611 |
-5.447609879822406e+01, |
| 1612 |
1.615858368580409e+02, |
| 1613 |
-1.556989798598866e+02, |
| 1614 |
6.680131188771972e+01, |
| 1615 |
-1.328068155288572e+01 |
| 1616 |
}; |
| 1617 |
|
| 1618 |
static const double kIvnC[] = |
| 1619 |
{ |
| 1620 |
-7.784894002430293e-03, |
| 1621 |
-3.223964580411365e-01, |
| 1622 |
-2.400758277161838e+00, |
| 1623 |
-2.549732539343734e+00, |
| 1624 |
4.374664141464968e+00, |
| 1625 |
2.938163982698783e+00 |
| 1626 |
}; |
| 1627 |
|
| 1628 |
static const double kIvnD[] = |
| 1629 |
{ |
| 1630 |
7.784695709041462e-03, |
| 1631 |
3.224671290700398e-01, |
| 1632 |
2.445134137142996e+00, |
| 1633 |
3.754408661907416e+00 |
| 1634 |
}; |
| 1635 |
|
| 1636 |
static const double kIvnLow = 0.02425; |
| 1637 |
static const double kIvnHigh = 0.97575; |
| 1638 |
|
| 1639 |
double QuantileToZscore(double pp) { |
| 1640 |
// assumes 0 < pp < 1 |
| 1641 |
double q, r; |
| 1642 |
|
| 1643 |
if (pp < kIvnLow) { |
| 1644 |
// Rational approximation for lower region |
| 1645 |
q = sqrt(-2*log(pp)); |
| 1646 |
return (((((kIvnC[0]*q+kIvnC[1])*q+kIvnC[2])*q+kIvnC[3])*q+kIvnC[4])*q+kIvnC[5]) / |
| 1647 |
((((kIvnD[0]*q+kIvnD[1])*q+kIvnD[2])*q+kIvnD[3])*q+1); |
| 1648 |
} |
| 1649 |
if (pp > kIvnHigh) { |
| 1650 |
// Rational approximation for upper region |
| 1651 |
q = sqrt(-2*log(1-pp)); |
| 1652 |
return -(((((kIvnC[0]*q+kIvnC[1])*q+kIvnC[2])*q+kIvnC[3])*q+kIvnC[4])*q+kIvnC[5]) / |
| 1653 |
((((kIvnD[0]*q+kIvnD[1])*q+kIvnD[2])*q+kIvnD[3])*q+1); |
| 1654 |
} |
| 1655 |
// Rational approximation for central region |
| 1656 |
q = pp - 0.5; |
| 1657 |
r = q*q; |
| 1658 |
return (((((kIvnA[0]*r+kIvnA[1])*r+kIvnA[2])*r+kIvnA[3])*r+kIvnA[4])*r+kIvnA[5])*q / |
| 1659 |
(((((kIvnB[0]*r+kIvnB[1])*r+kIvnB[2])*r+kIvnB[3])*r+kIvnB[4])*r+1); |
| 1660 |
} |
| 1661 |
|
| 1662 |
static const double kLogFactorials[99] = { |
| 1663 |
0, |
| 1664 |
0, |
| 1665 |
0.69314718055994529, |
| 1666 |
1.791759469228055, |
| 1667 |
3.1780538303479458, |
| 1668 |
4.7874917427820458, |
| 1669 |
6.5792512120101012, |
| 1670 |
8.5251613610654129, |
| 1671 |
10.604602902745251, |
| 1672 |
12.801827480081471, |
| 1673 |
15.104412573075518, |
| 1674 |
17.502307845873887, |
| 1675 |
19.987214495661888, |
| 1676 |
22.552163853123428, |
| 1677 |
25.19122118273868, |
| 1678 |
27.89927138384089, |
| 1679 |
30.671860106080675, |
| 1680 |
33.505073450136891, |
| 1681 |
36.395445208033053, |
| 1682 |
39.339884187199495, |
| 1683 |
42.335616460753485, |
| 1684 |
45.380138898476915, |
| 1685 |
48.471181351835227, |
| 1686 |
51.606675567764377, |
| 1687 |
54.784729398112312, |
| 1688 |
58.003605222980525, |
| 1689 |
61.261701761002001, |
| 1690 |
64.557538627006323, |
| 1691 |
67.88974313718154, |
| 1692 |
71.257038967168015, |
| 1693 |
74.658236348830172, |
| 1694 |
78.092223553315307, |
| 1695 |
81.557959456115029, |
| 1696 |
85.05446701758153, |
| 1697 |
88.580827542197667, |
| 1698 |
92.136175603687093, |
| 1699 |
95.719694542143202, |
| 1700 |
99.330612454787428, |
| 1701 |
102.9681986145138, |
| 1702 |
106.63176026064346, |
| 1703 |
110.32063971475741, |
| 1704 |
114.03421178146171, |
| 1705 |
117.77188139974507, |
| 1706 |
121.53308151543862, |
| 1707 |
125.31727114935688, |
| 1708 |
129.12393363912722, |
| 1709 |
132.95257503561629, |
| 1710 |
136.80272263732635, |
| 1711 |
140.67392364823425, |
| 1712 |
144.56574394634487, |
| 1713 |
148.47776695177302, |
| 1714 |
152.40959258449737, |
| 1715 |
156.3608363030788, |
| 1716 |
160.3311282166309, |
| 1717 |
164.3201122631952, |
| 1718 |
168.32744544842768, |
| 1719 |
172.35279713916282, |
| 1720 |
176.39584840699732, |
| 1721 |
180.45629141754378, |
| 1722 |
184.53382886144945, |
| 1723 |
188.6281734236716, |
| 1724 |
192.73904728784493, |
| 1725 |
196.86618167289001, |
| 1726 |
201.00931639928149, |
| 1727 |
205.16819948264117, |
| 1728 |
209.34258675253682, |
| 1729 |
213.53224149456327, |
| 1730 |
217.73693411395425, |
| 1731 |
221.95644181913033, |
| 1732 |
226.19054832372763, |
| 1733 |
230.43904356577696, |
| 1734 |
234.70172344281826, |
| 1735 |
238.97838956183432, |
| 1736 |
243.26884900298268, |
| 1737 |
247.57291409618685, |
| 1738 |
251.89040220972319, |
| 1739 |
256.22113555000954, |
| 1740 |
260.56494097186322, |
| 1741 |
264.92164979855278, |
| 1742 |
269.29109765101975, |
| 1743 |
273.67312428569375, |
| 1744 |
278.06757344036617, |
| 1745 |
282.47429268763045, |
| 1746 |
286.89313329542699, |
| 1747 |
291.32395009427029, |
| 1748 |
295.7666013507606, |
| 1749 |
300.22094864701415, |
| 1750 |
304.68685676566872, |
| 1751 |
309.1641935801469, |
| 1752 |
313.65282994987905, |
| 1753 |
318.1526396202093, |
| 1754 |
322.66349912672621, |
| 1755 |
327.18528770377526, |
| 1756 |
331.71788719692847, |
| 1757 |
336.26118197919845, |
| 1758 |
340.81505887079902, |
| 1759 |
345.37940706226686, |
| 1760 |
349.95411804077025, |
| 1761 |
354.53908551944079 |
| 1762 |
}; |
| 1763 |
|
| 1764 |
// port of Boost 1.84 implementation, double-precision |
| 1765 |
double Lfact(double xx) { |
| 1766 |
if (xx < 99.0) { |
| 1767 |
const uint32_t uii = S_CAST(int32_t, xx); |
| 1768 |
return kLogFactorials[uii]; |
| 1769 |
} |
| 1770 |
const double zz = xx + 1; |
| 1771 |
double result = (log(zz + kLanczosDoubleG - 0.5) - 1) * (zz - 0.5); |
| 1772 |
result += log(lanczos_sum_d_expg_scaled(zz)); |
| 1773 |
return result; |
| 1774 |
} |
| 1775 |
|
| 1776 |
// obs_hets + obs_hom1 + obs_hom2 assumed to be <2^31. |
| 1777 |
double HweLnP(int32_t obs_hets, int32_t obs_hom1, int32_t obs_hom2, uint32_t midp) { |
| 1778 |
// This function implements an exact SNP test of Hardy-Weinberg |
| 1779 |
// Equilibrium as described in Wigginton, JE, Cutler, DJ, and |
| 1780 |
// Abecasis, GR (2005) A Note on Exact Tests of Hardy-Weinberg |
| 1781 |
// Equilibrium. American Journal of Human Genetics. 76: 887 - 893. |
| 1782 |
// |
| 1783 |
// The original version was written by Jan Wigginton. |
| 1784 |
// |
| 1785 |
// This version was written by Christopher Chang. It contains the following |
| 1786 |
// improvements over the original SNPHWE(): |
| 1787 |
// - Proper handling of >64k genotypes. Previously, there was a potential |
| 1788 |
// integer overflow. |
| 1789 |
// - Detection and efficient handling of floating point overflow and |
| 1790 |
// underflow. E.g. instead of summing a tail all the way down, the loop |
| 1791 |
// stops once the latest increment underflows the partial sum's 53-bit |
| 1792 |
// precision; this results in a large speedup when max heterozygote count |
| 1793 |
// >1k. |
| 1794 |
// - No malloc() call. It's only necessary to keep track of a few partial |
| 1795 |
// sums. |
| 1796 |
// - Support for the mid-p variant of this test. See Graffelman J, Moreno V |
| 1797 |
// (2013) The mid p-value in exact tests for Hardy-Weinberg equilibrium. |
| 1798 |
// - Log-p-value return (added Jan 2024). A p-value of 1e-400 may be worth |
| 1799 |
// distinguishing from 1e-40000 in a biobank-scale dataset. |
| 1800 |
// |
| 1801 |
// Note that the HweThreshLn() function is a lot more efficient for testing |
| 1802 |
// against a p-value inclusion threshold. HweLnP() should only be used if |
| 1803 |
// you need the actual p-value. |
| 1804 |
intptr_t obs_homc; |
| 1805 |
intptr_t obs_homr; |
| 1806 |
if (obs_hom1 < obs_hom2) { |
| 1807 |
obs_homc = obs_hom2; |
| 1808 |
obs_homr = obs_hom1; |
| 1809 |
} else { |
| 1810 |
obs_homc = obs_hom1; |
| 1811 |
obs_homr = obs_hom2; |
| 1812 |
} |
| 1813 |
const int64_t rare_ct = 2LL * obs_homr + obs_hets; |
| 1814 |
if (rare_ct < 2) { |
| 1815 |
if (midp) { |
| 1816 |
return -kLn2; |
| 1817 |
} |
| 1818 |
return 0; |
| 1819 |
} |
| 1820 |
// MAF: rare_ct / allele_ct |
| 1821 |
// modal #hets: |
| 1822 |
// sample_ct * 2 * MAF * (1 - MAF) |
| 1823 |
// = rare_ct * (1 - MAF) |
| 1824 |
const int64_t sample_ct = obs_hom1 + obs_hom2 + obs_hets; |
| 1825 |
const double rare_ctd = rare_ct; |
| 1826 |
const double sample_ctd = sample_ct; |
| 1827 |
const double allele_ctd = sample_ctd * 2; |
| 1828 |
const double maf = rare_ctd / allele_ctd; |
| 1829 |
const double modal_nhet = rare_ctd * (1 - maf); |
| 1830 |
double curr_hets = obs_hets; |
| 1831 |
double curr_homr = obs_homr; |
| 1832 |
double curr_homc = obs_homc; |
| 1833 |
const double c_minus_r = curr_homc - curr_homr; |
| 1834 |
int32_t tie_ct = 1; |
| 1835 |
if (curr_hets > modal_nhet) { |
| 1836 |
const double het_delta = curr_hets - modal_nhet; |
| 1837 |
if ((!midp) && (het_delta < 2.0)) { |
| 1838 |
// Fast path for p=1. |
| 1839 |
if (curr_hets * (curr_hets - 1) <= (4 * (1 + kSmallEpsilon)) * (curr_homr + 1) * (curr_homc + 1)) { |
| 1840 |
return 0; |
| 1841 |
} |
| 1842 |
} |
| 1843 |
// Tried using old algorithm on rare_ct < 64, but this didn't make a |
| 1844 |
// noticeable difference to --hardy execution time on 1000 Genomes data, so |
| 1845 |
// I'd rather not bloat this function further. |
| 1846 |
// (might still be worth it due to midp use case? revisit after chrX is |
| 1847 |
// working.) |
| 1848 |
|
| 1849 |
// If we're close enough to the center, we may be best off computing 1 - |
| 1850 |
// <sum of center probabilities>. But this approach is vulnerable to |
| 1851 |
// catastrophic cancellation. Doubles have 53 bits of precision, while |
| 1852 |
// kSmallEpsilon (which governs equality checks) is configured to 2^{-44}, |
| 1853 |
// so it makes sense to insist that the final subtraction does not cost us |
| 1854 |
// more than ~9 bits. |
| 1855 |
// If the starting cell has probability > 2^{-10}, that is close enough to |
| 1856 |
// true; and I think this catches most of the cases where we actively want |
| 1857 |
// to use the subtract-center strategy? |
| 1858 |
// |
| 1859 |
// From e.g. the Wigginton paper, P(N_{AB}=n_{AB} | N, n_A) is |
| 1860 |
// |
| 1861 |
// 2^{n_{AB}} N! n_A! n_B! |
| 1862 |
// ----------------------------- |
| 1863 |
// n_{AA}! n_{AB}! n_{BB}! (2N)! |
| 1864 |
const double common_lnprob_component = Lfact(sample_ctd) + Lfact(rare_ctd) + Lfact(allele_ctd - rare_ctd) - Lfact(allele_ctd); |
| 1865 |
const double starting_lnprob_other_component = curr_hets * kLn2 - Lfact(curr_hets) - Lfact(curr_homr) - Lfact(curr_homc); |
| 1866 |
const double starting_lnprob = common_lnprob_component + starting_lnprob_other_component; |
| 1867 |
double lastp = 1; |
| 1868 |
// Either obs_hets >= 2, or obs_homr == obs_hets == obs_homc == 1. In the |
| 1869 |
// latter case, starting_lnprob will be well over -10 * kLn2, so the other |
| 1870 |
// branch can assume obs_hets >= 2. |
| 1871 |
if (starting_lnprob > -10 * kLn2) { |
| 1872 |
// This condition stops triggering once we have more than ~3 million |
| 1873 |
// samples, but I expect it to be a significant timesaver before that |
| 1874 |
// point. |
| 1875 |
double centerp = 0; |
| 1876 |
// rescale this at the end. |
| 1877 |
while (curr_hets > 1.5) { |
| 1878 |
curr_homr += 1; |
| 1879 |
curr_homc += 1; |
| 1880 |
lastp *= (curr_hets * (curr_hets - 1)) / (4 * curr_homr * curr_homc); |
| 1881 |
if (lastp < 1 + kSmallEpsilon) { |
| 1882 |
tie_ct += (lastp > 1 - kSmallEpsilon); |
| 1883 |
break; |
| 1884 |
} |
| 1885 |
curr_hets -= 2; |
| 1886 |
centerp += lastp; |
| 1887 |
} |
| 1888 |
if (midp) { |
| 1889 |
centerp += S_CAST(double, tie_ct) * 0.5; |
| 1890 |
} |
| 1891 |
const double starting_prob = exp(starting_lnprob); |
| 1892 |
centerp *= starting_prob; |
| 1893 |
return log(1 - centerp); |
| 1894 |
} |
| 1895 |
double tailp = 1; |
| 1896 |
while (curr_homr > 0.5) { |
| 1897 |
curr_hets += 2; |
| 1898 |
lastp *= (4 * curr_homr * curr_homc) / (curr_hets * (curr_hets - 1)); |
| 1899 |
curr_homr -= 1; |
| 1900 |
curr_homc -= 1; |
| 1901 |
const double preaddp = tailp; |
| 1902 |
tailp += lastp; |
| 1903 |
if (tailp <= preaddp) { |
| 1904 |
break; |
| 1905 |
} |
| 1906 |
} |
| 1907 |
// Now we want to jump near the other tail, without evaluating that many |
| 1908 |
// terms in between. |
| 1909 |
// |
| 1910 |
// Each full log-likelihood evaluation requires 3 Lfact() calls. Lfact() |
| 1911 |
// tends to be ~2-3 times as expensive as log in my testing, and I've seen |
| 1912 |
// log be anywhere from ~3 to ~12 times as expensive as division, so we |
| 1913 |
// want to limit ourselves to 1-2 full evaluations most of the time. |
| 1914 |
// |
| 1915 |
// The current heuristic starts by reflecting (obs_homr + curr_homr) * 0.5 |
| 1916 |
// across the mode, performing a full log-likelihood check at the nearest |
| 1917 |
// valid point. Hopefully we find that we're in (starting_lnprob - 62 * |
| 1918 |
// kLn2, starting_lnprob], so we're at or near a cell that actually |
| 1919 |
// contributes to the tail-sum. (This window is chosen to be wide enough |
| 1920 |
// to guarantee that at least one point falls inside when sample_ct < |
| 1921 |
// 2^31.) |
| 1922 |
// |
| 1923 |
// If not, we jump again, using Newton's method. |
| 1924 |
// If curr_homr is too low (i.e. current log-likelihood is too high), when |
| 1925 |
// we increase curr_homr by 1, the likelihood gets multiplied by |
| 1926 |
// curr_hets * (curr_hets-1) / (4 * (curr_homr+1) * (curr_homc+1)) |
| 1927 |
// i.e. we're adding the logarithm of this value to the log-likelihood. |
| 1928 |
// If curr_homr is too high, when we decrease curr_homr by 1, the |
| 1929 |
// likelihood gets multiplied by |
| 1930 |
// 4 * curr_homr * curr_homc / ((curr_hets+2) * (curr_hets+1)) |
| 1931 |
// We use the log of the first expression as the Newton's method f'(x) when |
| 1932 |
// we're jumping to higher curr_homr, and the negative-log of the second |
| 1933 |
// expression when we're jumping to lower curr_homr. |
| 1934 |
// f''(x) is always negative, so we can aim for starting_lnprob instead of |
| 1935 |
// the middle of the interval. |
| 1936 |
|
| 1937 |
// curr_hets moves twice as fast as curr_homr. So if we add |
| 1938 |
// 0.5 * (curr_hets + S_CAST(double, obs_hets)) - modal_nhet |
| 1939 |
// to 0.5 * (curr_homr + S_CAST(double, obs_homr)), that reflects curr_homr |
| 1940 |
// across the mode. |
| 1941 |
const double max_homr = S_CAST(double, rare_ct / 2); |
| 1942 |
{ |
| 1943 |
const double delta = 0.5 * (curr_hets + S_CAST(double, obs_hets)) - modal_nhet; |
| 1944 |
curr_homr = 0.5 * (curr_homr + S_CAST(double, obs_homr)) + delta; |
| 1945 |
// Round up (to guarantee we've actually moved to the other side of the |
| 1946 |
// mode) and clamp. |
| 1947 |
curr_homr = S_CAST(double, S_CAST(int32_t, curr_homr + 1)); |
| 1948 |
if (curr_homr > max_homr) { |
| 1949 |
curr_homr = max_homr; |
| 1950 |
} |
| 1951 |
} |
| 1952 |
// starting_lnprob_other_component is guaranteed to be positive here. |
| 1953 |
// Lfact() should be accurate enough for us to use a smaller-than-usual |
| 1954 |
// epsilon for identifying ties here; this isn't enough to cancel out the |
| 1955 |
// effective increase in epsilon from working in log-space, but it helps. |
| 1956 |
const double max_diff = starting_lnprob_other_component * (kSmallEpsilon / 8); |
| 1957 |
while (1) { |
| 1958 |
curr_hets = rare_ctd - curr_homr * 2; |
| 1959 |
curr_homc = curr_homr + c_minus_r; |
| 1960 |
const double lnprob_other_component = curr_hets * kLn2 - Lfact(curr_hets) - Lfact(curr_homr) - Lfact(curr_homc); |
| 1961 |
const double lnprob_diff = lnprob_other_component - starting_lnprob_other_component; |
| 1962 |
if (lnprob_diff > max_diff) { |
| 1963 |
if (curr_homr >= max_homr) { |
| 1964 |
// All terms on this tail are larger than the starting term. Exit. |
| 1965 |
// (This is possible when obs_hom1 == obs_hom2 == 0.) |
| 1966 |
if (midp) { |
| 1967 |
tailp -= 0.5; |
| 1968 |
} |
| 1969 |
return starting_lnprob + log(tailp); |
| 1970 |
} |
| 1971 |
const double ll_deriv = log(curr_hets * (curr_hets - 1) / (4 * (curr_homr + 1) * (curr_homc + 1))); |
| 1972 |
// Round absolute value up, to guarantee that we make progress. |
| 1973 |
// (lnprob_diff is positive and ll_deriv is negative.) |
| 1974 |
// This may overshoot. But the function is guaranteed to terminate |
| 1975 |
// because we never overshoot (and we do always make progress on each |
| 1976 |
// step) once we're on the other side. |
| 1977 |
curr_homr += 1 - S_CAST(int64_t, (1 - kSmallEpsilon) * lnprob_diff / ll_deriv); |
| 1978 |
if (curr_homr > max_homr) { |
| 1979 |
curr_homr = max_homr; |
| 1980 |
} |
| 1981 |
} else if (lnprob_diff > -62 * kLn2) { |
| 1982 |
lastp = exp(lnprob_diff); |
| 1983 |
break; |
| 1984 |
} else { |
| 1985 |
const double ll_deriv = log((curr_hets + 2) * (curr_hets + 1) / (4 * curr_homr * curr_homc)); |
| 1986 |
// Round down, to guarantee we don't overshoot. |
| 1987 |
// We're guaranteed to make progress, since lnprob_diff >= 62 * log(2) |
| 1988 |
// and sample_ct < 2^31. |
| 1989 |
curr_homr -= S_CAST(int64_t, lnprob_diff / ll_deriv); |
| 1990 |
if (curr_homr < 0) { |
| 1991 |
curr_homr = 0; |
| 1992 |
} |
| 1993 |
} |
| 1994 |
} |
| 1995 |
// Sum toward center, until lastp >= 1 or we're about to double-count the |
| 1996 |
// starting cell (possible when midp true). |
| 1997 |
double lastp_tail = lastp; |
| 1998 |
if (lastp < 1 - kSmallEpsilon) { |
| 1999 |
const double starting_homr_p1 = obs_homr + 1; |
| 2000 |
double curr_homr_center = curr_homr; |
| 2001 |
double curr_homc_center = curr_homc; |
| 2002 |
double curr_hets_center = curr_hets; |
| 2003 |
while (curr_homr_center > starting_homr_p1) { |
| 2004 |
tailp += lastp; |
| 2005 |
curr_hets_center += 2; |
| 2006 |
lastp *= (4 * curr_homr_center * curr_homc_center) / (curr_hets_center * (curr_hets_center - 1)); |
| 2007 |
if (lastp >= 1 - kSmallEpsilon) { |
| 2008 |
break; |
| 2009 |
} |
| 2010 |
curr_homr_center -= 1; |
| 2011 |
curr_homc_center -= 1; |
| 2012 |
} |
| 2013 |
} |
| 2014 |
if ((lastp >= 1 - kSmallEpsilon) && (lastp < 1 + kSmallEpsilon)) { |
| 2015 |
tailp += lastp; |
| 2016 |
++tie_ct; |
| 2017 |
} |
| 2018 |
// Sum away from center, until sums stop changing. |
| 2019 |
while (1) { |
| 2020 |
curr_homr += 1; |
| 2021 |
curr_homc += 1; |
| 2022 |
lastp_tail *= (curr_hets * (curr_hets - 1)) / (4 * curr_homr * curr_homc); |
| 2023 |
const double preaddp = tailp; |
| 2024 |
tailp += lastp_tail; |
| 2025 |
if (tailp <= preaddp) { |
| 2026 |
break; |
| 2027 |
} |
| 2028 |
curr_hets -= 2; |
| 2029 |
} |
| 2030 |
if (midp) { |
| 2031 |
tailp -= S_CAST(double, tie_ct) * 0.5; |
| 2032 |
} |
| 2033 |
return starting_lnprob + log(tailp); |
| 2034 |
} |
| 2035 |
// Same as above, just with directions flipped. |
| 2036 |
const double het_delta = modal_nhet - curr_hets; |
| 2037 |
if ((!midp) && (het_delta < 2.0)) { |
| 2038 |
// Fast path for p=1. |
| 2039 |
if ((4 * (1 - kSmallEpsilon)) * curr_homr * curr_homc <= (curr_hets + 2) * (curr_hets + 1)) { |
| 2040 |
return 0; |
| 2041 |
} |
| 2042 |
} |
| 2043 |
const double common_lnprob_component = Lfact(sample_ctd) + Lfact(rare_ctd) + Lfact(allele_ctd - rare_ctd) - Lfact(allele_ctd); |
| 2044 |
const double starting_lnprob_other_component = curr_hets * kLn2 - Lfact(curr_hets) - Lfact(curr_homr) - Lfact(curr_homc); |
| 2045 |
const double starting_lnprob = common_lnprob_component + starting_lnprob_other_component; |
| 2046 |
double lastp = 1; |
| 2047 |
if (starting_lnprob > -10 * kLn2) { |
| 2048 |
double centerp = 0; |
| 2049 |
// rescale this at the end. |
| 2050 |
while (curr_homr > 0.5) { |
| 2051 |
curr_hets += 2; |
| 2052 |
lastp *= 4 * curr_homr * curr_homc / (curr_hets * (curr_hets - 1)); |
| 2053 |
curr_homr -= 1; |
| 2054 |
curr_homc -= 1; |
| 2055 |
if (lastp < 1 + kSmallEpsilon) { |
| 2056 |
tie_ct += (lastp > 1 - kSmallEpsilon); |
| 2057 |
break; |
| 2058 |
} |
| 2059 |
centerp += lastp; |
| 2060 |
} |
| 2061 |
if (midp) { |
| 2062 |
centerp += S_CAST(double, tie_ct) * 0.5; |
| 2063 |
} |
| 2064 |
const double starting_prob = exp(starting_lnprob); |
| 2065 |
centerp *= starting_prob; |
| 2066 |
return log(1 - centerp); |
| 2067 |
} |
| 2068 |
double tailp = 1; |
| 2069 |
while (curr_hets > 1.5) { |
| 2070 |
curr_homr += 1; |
| 2071 |
curr_homc += 1; |
| 2072 |
lastp *= curr_hets * (curr_hets - 1) / (4 * curr_homr * curr_homc); |
| 2073 |
curr_hets -= 2; |
| 2074 |
const double preaddp = tailp; |
| 2075 |
tailp += lastp; |
| 2076 |
if (tailp <= preaddp) { |
| 2077 |
break; |
| 2078 |
} |
| 2079 |
} |
| 2080 |
// Jump to other tail. |
| 2081 |
{ |
| 2082 |
const double delta = modal_nhet - 0.5 * (curr_hets + S_CAST(double, obs_hets)); |
| 2083 |
curr_homr = 0.5 * (curr_homr + S_CAST(double, obs_homr)) - delta; |
| 2084 |
// Round down (to guarantee we've actually moved to the other side of the |
| 2085 |
// mode) and clamp. |
| 2086 |
curr_homr = S_CAST(double, S_CAST(int32_t, curr_homr)); |
| 2087 |
if (curr_homr < 0) { |
| 2088 |
curr_homr = 0; |
| 2089 |
} |
| 2090 |
} |
| 2091 |
const double max_diff = starting_lnprob_other_component * (kSmallEpsilon / 8); |
| 2092 |
const double max_homr = S_CAST(double, rare_ct / 2); |
| 2093 |
while (1) { |
| 2094 |
curr_hets = rare_ctd - curr_homr * 2; |
| 2095 |
curr_homc = curr_homr + c_minus_r; |
| 2096 |
const double lnprob_other_component = curr_hets * kLn2 - Lfact(curr_hets) - Lfact(curr_homr) - Lfact(curr_homc); |
| 2097 |
const double lnprob_diff = lnprob_other_component - starting_lnprob_other_component; |
| 2098 |
if (lnprob_diff > max_diff) { |
| 2099 |
if (curr_homr <= 0) { |
| 2100 |
// All terms on this tail are larger than the starting term. Exit. |
| 2101 |
if (midp) { |
| 2102 |
tailp -= 0.5; |
| 2103 |
} |
| 2104 |
return starting_lnprob + log(tailp); |
| 2105 |
} |
| 2106 |
const double ll_deriv = log(4 * curr_homr * curr_homc / ((curr_hets + 2) * (curr_hets + 1))); |
| 2107 |
curr_homr += S_CAST(int64_t, (1 - kSmallEpsilon) * lnprob_diff / ll_deriv) - 1; |
| 2108 |
if (curr_homr < 0) { |
| 2109 |
curr_homr = 0; |
| 2110 |
} |
| 2111 |
} else if (lnprob_diff > -62 * kLn2) { |
| 2112 |
lastp = exp(lnprob_diff); |
| 2113 |
break; |
| 2114 |
} else { |
| 2115 |
const double ll_deriv = log(4 * (curr_homr + 1) * (curr_homc + 1) / (curr_hets * (curr_hets - 1))); |
| 2116 |
curr_homr += S_CAST(int64_t, lnprob_diff / ll_deriv); |
| 2117 |
if (curr_homr > max_homr) { |
| 2118 |
curr_homr = max_homr; |
| 2119 |
} |
| 2120 |
} |
| 2121 |
} |
| 2122 |
// Sum toward center, until lastp >= 1 or we're about to double-count the |
| 2123 |
// starting cell. |
| 2124 |
double lastp_tail = lastp; |
| 2125 |
if (lastp < 1 - kSmallEpsilon) { |
| 2126 |
const double starting_homr_m1 = obs_homr - 1; |
| 2127 |
double curr_homr_center = curr_homr; |
| 2128 |
double curr_homc_center = curr_homc; |
| 2129 |
double curr_hets_center = curr_hets; |
| 2130 |
while (curr_homr_center < starting_homr_m1) { |
| 2131 |
tailp += lastp; |
| 2132 |
curr_homr_center += 1; |
| 2133 |
curr_homc_center += 1; |
| 2134 |
lastp *= curr_hets_center * (curr_hets_center - 1) / (4 * curr_homr_center * curr_homc_center); |
| 2135 |
if (lastp >= 1 - kSmallEpsilon) { |
| 2136 |
break; |
| 2137 |
} |
| 2138 |
curr_hets_center -= 2; |
| 2139 |
} |
| 2140 |
} |
| 2141 |
if ((lastp >= 1 - kSmallEpsilon) && (lastp < 1 + kSmallEpsilon)) { |
| 2142 |
tailp += lastp; |
| 2143 |
++tie_ct; |
| 2144 |
} |
| 2145 |
// Sum away from center, until sums stop changing. |
| 2146 |
while (1) { |
| 2147 |
curr_hets += 2; |
| 2148 |
lastp_tail *= 4 * curr_homr * curr_homc / (curr_hets * (curr_hets - 1)); |
| 2149 |
const double preaddp = tailp; |
| 2150 |
tailp += lastp_tail; |
| 2151 |
if (tailp <= preaddp) { |
| 2152 |
break; |
| 2153 |
} |
| 2154 |
curr_homr -= 1; |
| 2155 |
curr_homc -= 1; |
| 2156 |
} |
| 2157 |
if (midp) { |
| 2158 |
tailp -= S_CAST(double, tie_ct) * 0.5; |
| 2159 |
} |
| 2160 |
return starting_lnprob + log(tailp); |
| 2161 |
} |
| 2162 |
|
| 2163 |
uint32_t HweThresh(int32_t obs_hets, int32_t obs_hom1, int32_t obs_hom2, double thresh) { |
| 2164 |
// Threshold-test-only version of HweP() which is usually able to exit |
| 2165 |
// from the calculation earlier. Returns 0 if these counts are close enough |
| 2166 |
// to Hardy-Weinberg equilibrium, 1 otherwise. |
| 2167 |
// |
| 2168 |
// Suppose, for definiteness, that the number of observed hets is no less |
| 2169 |
// than expectation. (Same ideas apply for the other case.) We proceed as |
| 2170 |
// follows: |
| 2171 |
// - Sum the *relative* likelihoods of more likely smaller het counts. |
| 2172 |
// - Determine the minimum tail mass to pass the threshold. |
| 2173 |
// - The majority of the time, the tail boundary elements are enough to pass |
| 2174 |
// the threshold; we never need to sum the remainder of the tails. |
| 2175 |
// - And in the case of disequilibrium, we will often be able to immediately |
| 2176 |
// determine that the tail sum cannot possibly pass the threshold, just by |
| 2177 |
// looking at the tail boundary elements and using a geometric series to |
| 2178 |
// upper-bound the tail sums. |
| 2179 |
// - Only when neither of these conditions hold do we start traveling down |
| 2180 |
// the tails. |
| 2181 |
intptr_t obs_homc; |
| 2182 |
intptr_t obs_homr; |
| 2183 |
if (obs_hom1 < obs_hom2) { |
| 2184 |
obs_homc = obs_hom2; |
| 2185 |
obs_homr = obs_hom1; |
| 2186 |
} else { |
| 2187 |
obs_homc = obs_hom1; |
| 2188 |
obs_homr = obs_hom2; |
| 2189 |
} |
| 2190 |
int64_t rare_copies = 2LL * obs_homr + obs_hets; |
| 2191 |
int64_t genotypes2 = (obs_hets + obs_homc + obs_homr) * 2LL; |
| 2192 |
double curr_hets_t2 = obs_hets; // tail 2 |
| 2193 |
double curr_homr_t2 = obs_homr; |
| 2194 |
double curr_homc_t2 = obs_homc; |
| 2195 |
|
| 2196 |
// Subtract epsilon from initial probability mass, so that we can compare to |
| 2197 |
// 1 when determining tail vs. center membership without floating point error |
| 2198 |
// biting us in the ass |
| 2199 |
double tailp1 = (1 - kSmallEpsilon) * kExactTestBias; |
| 2200 |
double centerp = 0; |
| 2201 |
double lastp2 = tailp1; |
| 2202 |
double tailp2 = 0; |
| 2203 |
double tail1_ceil; |
| 2204 |
double tail2_ceil; |
| 2205 |
double lastp1; |
| 2206 |
double curr_hets_t1; |
| 2207 |
double curr_homr_t1; |
| 2208 |
double curr_homc_t1; |
| 2209 |
|
| 2210 |
// Initially, if center sum reaches this, the test can immediately fail. |
| 2211 |
// Once center is summed, this is recalculated, and when tail sum has reached |
| 2212 |
// this, we've passed. |
| 2213 |
double exit_thresh; |
| 2214 |
double exit_threshx; |
| 2215 |
double ratio; |
| 2216 |
double preaddp; |
| 2217 |
if (!genotypes2) { |
| 2218 |
return 0; |
| 2219 |
} |
| 2220 |
|
| 2221 |
// Convert thresh into reverse odds ratio. |
| 2222 |
thresh = (1 - thresh) / thresh; |
| 2223 |
|
| 2224 |
// Expected het count: |
| 2225 |
// 2 * rarefreq * (1 - rarefreq) * genotypes |
| 2226 |
// = 2 * (rare_copies / (2 * genotypes)) * (1 - rarefreq) * genotypes |
| 2227 |
// = rare_copies * (1 - (rare_copies / (2 * genotypes))) |
| 2228 |
// = (rare_copies * (2 * genotypes - rare_copies)) / (2 * genotypes) |
| 2229 |
// |
| 2230 |
// The computational identity is |
| 2231 |
// P(nhets == n) := P(nhets == n+2) * (n+2) * (n+1) / |
| 2232 |
// (4 * homr(n) * homc(n)) |
| 2233 |
// where homr() and homc() are the number of homozygous rares/commons needed |
| 2234 |
// to maintain the same allele frequencies. |
| 2235 |
// This probability is always decreasing when proceeding away from the |
| 2236 |
// expected het count. |
| 2237 |
|
| 2238 |
if (obs_hets * genotypes2 > rare_copies * (genotypes2 - rare_copies)) { |
| 2239 |
// tail 1 = upper |
| 2240 |
if (obs_hets < 2) { |
| 2241 |
return 0; |
| 2242 |
} |
| 2243 |
|
| 2244 |
// An initial upper bound on the tail sum is useful, since it lets us |
| 2245 |
// report test failure before summing the entire center. We use the |
| 2246 |
// trivial bound of 1 + floor(rare_copies / 2): that's the total number |
| 2247 |
// of possible het counts, and the relative probability for each count must |
| 2248 |
// be <= 1 if it's in the tail. |
| 2249 |
exit_thresh = (1 + (rare_copies / 2)) * (thresh * kExactTestBias); |
| 2250 |
|
| 2251 |
// het_probs[curr_hets] = 1 |
| 2252 |
// het_probs[curr_hets - 2] = het_probs[curr_hets] * curr_hets * (curr_hets - 1) / (4 * (curr_homr + 1) * (curr_homc + 1)) |
| 2253 |
do { |
| 2254 |
curr_homr_t2 += 1; |
| 2255 |
curr_homc_t2 += 1; |
| 2256 |
lastp2 *= (curr_hets_t2 * (curr_hets_t2 - 1)) / (4 * curr_homr_t2 * curr_homc_t2); |
| 2257 |
curr_hets_t2 -= 2; |
| 2258 |
if (lastp2 < kExactTestBias) { |
| 2259 |
tailp2 = lastp2; |
| 2260 |
break; |
| 2261 |
} |
| 2262 |
centerp += lastp2; |
| 2263 |
if (centerp > exit_thresh) { |
| 2264 |
return 1; |
| 2265 |
} |
| 2266 |
} while (curr_hets_t2 > 1.5); |
| 2267 |
exit_thresh = centerp / thresh; |
| 2268 |
if (tailp1 + tailp2 >= exit_thresh) { |
| 2269 |
return 0; |
| 2270 |
} |
| 2271 |
// c + cr + cr^2 + ... = c/(1-r), which is an upper bound for the tail sum |
| 2272 |
ratio = (curr_hets_t2 * (curr_hets_t2 - 1)) / (4 * (curr_homr_t2 + 1) * (curr_homc_t2 + 1)); |
| 2273 |
tail2_ceil = tailp2 / (1 - ratio); |
| 2274 |
curr_hets_t1 = obs_hets + 2; |
| 2275 |
curr_homr_t1 = obs_homr; |
| 2276 |
curr_homc_t1 = obs_homc; |
| 2277 |
// ratio for the other tail |
| 2278 |
lastp1 = (4 * curr_homr_t1 * curr_homc_t1) / (curr_hets_t1 * (curr_hets_t1 - 1)); |
| 2279 |
tail1_ceil = tailp1 / (1 - lastp1); |
| 2280 |
if (tail1_ceil + tail2_ceil < exit_thresh) { |
| 2281 |
return 1; |
| 2282 |
} |
| 2283 |
lastp1 *= tailp1; |
| 2284 |
tailp1 += lastp1; |
| 2285 |
|
| 2286 |
if (obs_homr > 1) { |
| 2287 |
// het_probs[curr_hets + 2] = het_probs[curr_hets] * 4 * curr_homr * curr_homc / ((curr_hets + 2) * (curr_hets + 1)) |
| 2288 |
exit_threshx = exit_thresh - tailp2; |
| 2289 |
do { |
| 2290 |
curr_hets_t1 += 2; |
| 2291 |
curr_homr_t1 -= 1; |
| 2292 |
curr_homc_t1 -= 1; |
| 2293 |
lastp1 *= (4 * curr_homr_t1 * curr_homc_t1) / (curr_hets_t1 * (curr_hets_t1 - 1)); |
| 2294 |
preaddp = tailp1; |
| 2295 |
tailp1 += lastp1; |
| 2296 |
if (tailp1 > exit_threshx) { |
| 2297 |
return 0; |
| 2298 |
} |
| 2299 |
if (tailp1 <= preaddp) { |
| 2300 |
break; |
| 2301 |
} |
| 2302 |
} while (curr_homr_t1 > 1.5); |
| 2303 |
} |
| 2304 |
if (tailp1 + tail2_ceil < exit_thresh) { |
| 2305 |
return 1; |
| 2306 |
} |
| 2307 |
exit_threshx = exit_thresh - tailp1; |
| 2308 |
while (curr_hets_t2 > 1) { |
| 2309 |
curr_homr_t2 += 1; |
| 2310 |
curr_homc_t2 += 1; |
| 2311 |
lastp2 *= (curr_hets_t2 * (curr_hets_t2 - 1)) / (4 * curr_homr_t2 * curr_homc_t2); |
| 2312 |
preaddp = tailp2; |
| 2313 |
tailp2 += lastp2; |
| 2314 |
if (tailp2 >= exit_threshx) { |
| 2315 |
return 0; |
| 2316 |
} |
| 2317 |
if (tailp2 <= preaddp) { |
| 2318 |
return 1; |
| 2319 |
} |
| 2320 |
curr_hets_t2 -= 2; |
| 2321 |
} |
| 2322 |
return 1; |
| 2323 |
} |
| 2324 |
// tail 1 = lower |
| 2325 |
if (!obs_homr) { |
| 2326 |
return 0; |
| 2327 |
} |
| 2328 |
exit_thresh = (1 + (rare_copies / 2)) * (thresh * kExactTestBias); |
| 2329 |
do { |
| 2330 |
curr_hets_t2 += 2; |
| 2331 |
lastp2 *= (4 * curr_homr_t2 * curr_homc_t2) / (curr_hets_t2 * (curr_hets_t2 - 1)); |
| 2332 |
curr_homr_t2 -= 1; |
| 2333 |
curr_homc_t2 -= 1; |
| 2334 |
if (lastp2 < kExactTestBias) { |
| 2335 |
tailp2 = lastp2; |
| 2336 |
break; |
| 2337 |
} |
| 2338 |
centerp += lastp2; |
| 2339 |
if (centerp > exit_thresh) { |
| 2340 |
return 1; |
| 2341 |
} |
| 2342 |
} while (curr_homr_t2 > 0.5); |
| 2343 |
exit_thresh = centerp / thresh; |
| 2344 |
if (tailp1 + tailp2 >= exit_thresh) { |
| 2345 |
return 0; |
| 2346 |
} |
| 2347 |
ratio = (4 * curr_homr_t2 * curr_homc_t2) / ((curr_hets_t2 + 2) * (curr_hets_t2 + 1)); |
| 2348 |
tail2_ceil = tailp2 / (1 - ratio); |
| 2349 |
curr_hets_t1 = obs_hets; |
| 2350 |
curr_homr_t1 = obs_homr + 1; |
| 2351 |
curr_homc_t1 = obs_homc + 1; |
| 2352 |
lastp1 = (curr_hets_t1 * (curr_hets_t1 - 1)) / (4 * curr_homr_t1 * curr_homc_t1); |
| 2353 |
tail1_ceil = tailp1 / (1 - lastp1); |
| 2354 |
lastp1 *= tailp1; |
| 2355 |
tailp1 += lastp1; |
| 2356 |
|
| 2357 |
if (tail1_ceil + tail2_ceil < exit_thresh) { |
| 2358 |
return 1; |
| 2359 |
} |
| 2360 |
if (obs_hets >= 4) { |
| 2361 |
exit_threshx = exit_thresh - tailp2; |
| 2362 |
do { |
| 2363 |
curr_hets_t1 -= 2; |
| 2364 |
curr_homr_t1 += 1; |
| 2365 |
curr_homc_t1 += 1; |
| 2366 |
lastp1 *= (curr_hets_t1 * (curr_hets_t1 - 1)) / (4 * curr_homr_t1 * curr_homc_t1); |
| 2367 |
preaddp = tailp1; |
| 2368 |
tailp1 += lastp1; |
| 2369 |
if (tailp1 > exit_threshx) { |
| 2370 |
return 0; |
| 2371 |
} |
| 2372 |
if (tailp1 <= preaddp) { |
| 2373 |
break; |
| 2374 |
} |
| 2375 |
} while (curr_hets_t1 > 3.5); |
| 2376 |
} |
| 2377 |
if (tailp1 + tail2_ceil < exit_thresh) { |
| 2378 |
return 1; |
| 2379 |
} |
| 2380 |
exit_threshx = exit_thresh - tailp1; |
| 2381 |
while (curr_homr_t2 > 0.5) { |
| 2382 |
curr_hets_t2 += 2; |
| 2383 |
lastp2 *= (4 * curr_homr_t2 * curr_homc_t2) / (curr_hets_t2 * (curr_hets_t2 - 1)); |
| 2384 |
curr_homr_t2 -= 1; |
| 2385 |
curr_homc_t2 -= 1; |
| 2386 |
preaddp = tailp2; |
| 2387 |
tailp2 += lastp2; |
| 2388 |
if (tailp2 >= exit_threshx) { |
| 2389 |
return 0; |
| 2390 |
} |
| 2391 |
if (tailp2 <= preaddp) { |
| 2392 |
return 1; |
| 2393 |
} |
| 2394 |
} |
| 2395 |
return 1; |
| 2396 |
} |
| 2397 |
|
| 2398 |
uint32_t HweThreshMidp(int32_t obs_hets, int32_t obs_hom1, int32_t obs_hom2, double thresh) { |
| 2399 |
// Mid-p version of HweThresh(). (There are enough fiddly differences that I |
| 2400 |
// think it's better for this to be a separate function.) Assumes threshold |
| 2401 |
// is smaller than 0.5. |
| 2402 |
intptr_t obs_homc; |
| 2403 |
intptr_t obs_homr; |
| 2404 |
if (obs_hom1 < obs_hom2) { |
| 2405 |
obs_homc = obs_hom2; |
| 2406 |
obs_homr = obs_hom1; |
| 2407 |
} else { |
| 2408 |
obs_homc = obs_hom1; |
| 2409 |
obs_homr = obs_hom2; |
| 2410 |
} |
| 2411 |
int64_t rare_copies = 2LL * obs_homr + obs_hets; |
| 2412 |
int64_t genotypes2 = (obs_hets + obs_homc + obs_homr) * 2LL; |
| 2413 |
double curr_hets_t2 = obs_hets; // tail 2 |
| 2414 |
double curr_homr_t2 = obs_homr; |
| 2415 |
double curr_homc_t2 = obs_homc; |
| 2416 |
double tailp1 = (1 - kSmallEpsilon) * kExactTestBias * 0.5; |
| 2417 |
double centerp = tailp1; |
| 2418 |
double lastp2 = (1 - kSmallEpsilon) * kExactTestBias; |
| 2419 |
double tailp2 = 0; |
| 2420 |
double tail1_ceil; |
| 2421 |
double tail2_ceil; |
| 2422 |
double lastp1; |
| 2423 |
double curr_hets_t1; |
| 2424 |
double curr_homr_t1; |
| 2425 |
double curr_homc_t1; |
| 2426 |
double exit_thresh; |
| 2427 |
double exit_threshx; |
| 2428 |
double ratio; |
| 2429 |
double preaddp; |
| 2430 |
if (!genotypes2) { |
| 2431 |
return 0; |
| 2432 |
} |
| 2433 |
thresh = (1 - thresh) / thresh; |
| 2434 |
if (obs_hets * genotypes2 > rare_copies * (genotypes2 - rare_copies)) { |
| 2435 |
if (obs_hets < 2) { |
| 2436 |
return 0; |
| 2437 |
} |
| 2438 |
exit_thresh = (1 + (rare_copies / 2)) * (thresh * kExactTestBias); |
| 2439 |
do { |
| 2440 |
curr_homr_t2 += 1; |
| 2441 |
curr_homc_t2 += 1; |
| 2442 |
lastp2 *= (curr_hets_t2 * (curr_hets_t2 - 1)) / (4 * curr_homr_t2 * curr_homc_t2); |
| 2443 |
curr_hets_t2 -= 2; |
| 2444 |
if (lastp2 < kExactTestBias) { |
| 2445 |
if (lastp2 > (1 - 2 * kSmallEpsilon) * kExactTestBias) { |
| 2446 |
// tie with original contingency table, apply mid-p correction here |
| 2447 |
// too |
| 2448 |
tailp2 = tailp1; |
| 2449 |
centerp += tailp1; |
| 2450 |
} else { |
| 2451 |
tailp2 = lastp2; |
| 2452 |
} |
| 2453 |
break; |
| 2454 |
} |
| 2455 |
centerp += lastp2; |
| 2456 |
if (centerp > exit_thresh) { |
| 2457 |
return 1; |
| 2458 |
} |
| 2459 |
} while (curr_hets_t2 > 1.5); |
| 2460 |
exit_thresh = centerp / thresh; |
| 2461 |
if (tailp1 + tailp2 >= exit_thresh) { |
| 2462 |
return 0; |
| 2463 |
} |
| 2464 |
ratio = (curr_hets_t2 * (curr_hets_t2 - 1)) / (4 * (curr_homr_t2 + 1) * (curr_homc_t2 + 1)); |
| 2465 |
// this needs to work in both the tie and no-tie cases |
| 2466 |
tail2_ceil = tailp2 + lastp2 * ratio / (1 - ratio); |
| 2467 |
curr_hets_t1 = obs_hets + 2; |
| 2468 |
curr_homr_t1 = obs_homr; |
| 2469 |
curr_homc_t1 = obs_homc; |
| 2470 |
lastp1 = (4 * curr_homr_t1 * curr_homc_t1) / (curr_hets_t1 * (curr_hets_t1 - 1)); |
| 2471 |
// always a tie here |
| 2472 |
tail1_ceil = tailp1 * 2 / (1 - lastp1) - tailp1; |
| 2473 |
if (tail1_ceil + tail2_ceil < exit_thresh) { |
| 2474 |
return 1; |
| 2475 |
} |
| 2476 |
lastp1 *= tailp1 * 2; |
| 2477 |
tailp1 += lastp1; |
| 2478 |
|
| 2479 |
if (obs_homr > 1) { |
| 2480 |
exit_threshx = exit_thresh - tailp2; |
| 2481 |
do { |
| 2482 |
curr_hets_t1 += 2; |
| 2483 |
curr_homr_t1 -= 1; |
| 2484 |
curr_homc_t1 -= 1; |
| 2485 |
lastp1 *= (4 * curr_homr_t1 * curr_homc_t1) / (curr_hets_t1 * (curr_hets_t1 - 1)); |
| 2486 |
preaddp = tailp1; |
| 2487 |
tailp1 += lastp1; |
| 2488 |
if (tailp1 > exit_threshx) { |
| 2489 |
return 0; |
| 2490 |
} |
| 2491 |
if (tailp1 <= preaddp) { |
| 2492 |
break; |
| 2493 |
} |
| 2494 |
} while (curr_homr_t1 > 1.5); |
| 2495 |
} |
| 2496 |
if (tailp1 + tail2_ceil < exit_thresh) { |
| 2497 |
return 1; |
| 2498 |
} |
| 2499 |
exit_threshx = exit_thresh - tailp1; |
| 2500 |
while (curr_hets_t2 > 1) { |
| 2501 |
curr_homr_t2 += 1; |
| 2502 |
curr_homc_t2 += 1; |
| 2503 |
lastp2 *= (curr_hets_t2 * (curr_hets_t2 - 1)) / (4 * curr_homr_t2 * curr_homc_t2); |
| 2504 |
preaddp = tailp2; |
| 2505 |
tailp2 += lastp2; |
| 2506 |
if (tailp2 >= exit_threshx) { |
| 2507 |
return 0; |
| 2508 |
} |
| 2509 |
if (tailp2 <= preaddp) { |
| 2510 |
return 1; |
| 2511 |
} |
| 2512 |
curr_hets_t2 -= 2; |
| 2513 |
} |
| 2514 |
return 1; |
| 2515 |
} |
| 2516 |
if (!obs_homr) { |
| 2517 |
return 0; |
| 2518 |
} |
| 2519 |
exit_thresh = (1 + (rare_copies / 2)) * (thresh * kExactTestBias); |
| 2520 |
do { |
| 2521 |
curr_hets_t2 += 2; |
| 2522 |
lastp2 *= (4 * curr_homr_t2 * curr_homc_t2) / (curr_hets_t2 * (curr_hets_t2 - 1)); |
| 2523 |
curr_homr_t2 -= 1; |
| 2524 |
curr_homc_t2 -= 1; |
| 2525 |
if (lastp2 < kExactTestBias) { |
| 2526 |
if (lastp2 > (1 - 2 * kSmallEpsilon) * kExactTestBias) { |
| 2527 |
tailp2 = tailp1; |
| 2528 |
centerp += tailp1; |
| 2529 |
} else { |
| 2530 |
tailp2 = lastp2; |
| 2531 |
} |
| 2532 |
break; |
| 2533 |
} |
| 2534 |
centerp += lastp2; |
| 2535 |
if (centerp > exit_thresh) { |
| 2536 |
return 1; |
| 2537 |
} |
| 2538 |
} while (curr_homr_t2 > 0.5); |
| 2539 |
exit_thresh = centerp / thresh; |
| 2540 |
if (tailp1 + tailp2 >= exit_thresh) { |
| 2541 |
return 0; |
| 2542 |
} |
| 2543 |
ratio = (4 * curr_homr_t2 * curr_homc_t2) / ((curr_hets_t2 + 2) * (curr_hets_t2 + 1)); |
| 2544 |
tail2_ceil = tailp2 + lastp2 * ratio / (1 - ratio); |
| 2545 |
curr_hets_t1 = obs_hets; |
| 2546 |
curr_homr_t1 = obs_homr + 1; |
| 2547 |
curr_homc_t1 = obs_homc + 1; |
| 2548 |
lastp1 = (curr_hets_t1 * (curr_hets_t1 - 1)) / (4 * curr_homr_t1 * curr_homc_t1); |
| 2549 |
tail1_ceil = 2 * tailp1 / (1 - lastp1) - tailp1; |
| 2550 |
lastp1 *= 2 * tailp1; |
| 2551 |
tailp1 += lastp1; |
| 2552 |
|
| 2553 |
if (tail1_ceil + tail2_ceil < exit_thresh) { |
| 2554 |
return 1; |
| 2555 |
} |
| 2556 |
if (obs_hets >= 4) { |
| 2557 |
exit_threshx = exit_thresh - tailp2; |
| 2558 |
do { |
| 2559 |
curr_hets_t1 -= 2; |
| 2560 |
curr_homr_t1 += 1; |
| 2561 |
curr_homc_t1 += 1; |
| 2562 |
lastp1 *= (curr_hets_t1 * (curr_hets_t1 - 1)) / (4 * curr_homr_t1 * curr_homc_t1); |
| 2563 |
preaddp = tailp1; |
| 2564 |
tailp1 += lastp1; |
| 2565 |
if (tailp1 > exit_threshx) { |
| 2566 |
return 0; |
| 2567 |
} |
| 2568 |
if (tailp1 <= preaddp) { |
| 2569 |
break; |
| 2570 |
} |
| 2571 |
} while (curr_hets_t1 > 3.5); |
| 2572 |
} |
| 2573 |
if (tailp1 + tail2_ceil < exit_thresh) { |
| 2574 |
return 1; |
| 2575 |
} |
| 2576 |
exit_threshx = exit_thresh - tailp1; |
| 2577 |
while (curr_homr_t2 > 0.5) { |
| 2578 |
curr_hets_t2 += 2; |
| 2579 |
lastp2 *= (4 * curr_homr_t2 * curr_homc_t2) / (curr_hets_t2 * (curr_hets_t2 - 1)); |
| 2580 |
curr_homr_t2 -= 1; |
| 2581 |
curr_homc_t2 -= 1; |
| 2582 |
preaddp = tailp2; |
| 2583 |
tailp2 += lastp2; |
| 2584 |
if (tailp2 >= exit_threshx) { |
| 2585 |
return 0; |
| 2586 |
} |
| 2587 |
if (tailp2 <= preaddp) { |
| 2588 |
return 1; |
| 2589 |
} |
| 2590 |
} |
| 2591 |
return 1; |
| 2592 |
} |
| 2593 |
|
| 2594 |
uint32_t HweThreshLnMain(int32_t obs_hets, int32_t obs_hom1, int32_t obs_hom2, uint32_t midp, double ln_thresh) { |
| 2595 |
assert(ln_thresh < -708); |
| 2596 |
// Threshold-test-only version of HweLnP() which is usually able to exit |
| 2597 |
// from the calculation earlier. Returns 0 if these counts are close enough |
| 2598 |
// to Hardy-Weinberg equilibrium, 1 otherwise. |
| 2599 |
// |
| 2600 |
// Assumes ln_thresh < -708. |
| 2601 |
// |
| 2602 |
// Caller is responsible for including a tolerance in ln_thresh when |
| 2603 |
// appropriate; the only tolerance applied by this function is in the context |
| 2604 |
// of other-tail tie detection. |
| 2605 |
intptr_t obs_homc; |
| 2606 |
intptr_t obs_homr; |
| 2607 |
if (obs_hom1 < obs_hom2) { |
| 2608 |
obs_homc = obs_hom2; |
| 2609 |
obs_homr = obs_hom1; |
| 2610 |
} else { |
| 2611 |
obs_homc = obs_hom1; |
| 2612 |
obs_homr = obs_hom2; |
| 2613 |
} |
| 2614 |
const int64_t rare_ct = 2LL * obs_homr + obs_hets; |
| 2615 |
// Change this to "rare_ct < 2" if ln_thresh restriction is being loosened |
| 2616 |
// (to e.g. compare results against HweThresh()). |
| 2617 |
// TODO: also look at |obs_hets - modal_nhet|, bsru32(sample_ct) |
| 2618 |
if (rare_ct < 64) { |
| 2619 |
return 0; |
| 2620 |
} |
| 2621 |
const int64_t sample_ct = obs_hom1 + obs_hom2 + obs_hets; |
| 2622 |
const double rare_ctd = rare_ct; |
| 2623 |
const double sample_ctd = sample_ct; |
| 2624 |
const double allele_ctd = sample_ctd * 2; |
| 2625 |
|
| 2626 |
// 1. Compute log-likelihood of test cell. This may be high enough on its |
| 2627 |
// own to immediately return 0. |
| 2628 |
// If likelihood is lower than threshold / <total # of cells>, we can |
| 2629 |
// immediately return 1. |
| 2630 |
// 2. Determine tailsum we must hit to return 0. |
| 2631 |
// 3. The rest follows HweLnP(), except with an extra geometric-series-based |
| 2632 |
// early-exit attempt near the end. |
| 2633 |
const double common_lnprob_component = Lfact(sample_ctd) + Lfact(rare_ctd) + Lfact(allele_ctd - rare_ctd) - Lfact(allele_ctd); |
| 2634 |
double curr_hets = obs_hets; |
| 2635 |
double curr_homr = obs_homr; |
| 2636 |
double curr_homc = obs_homc; |
| 2637 |
const double starting_lnprob_other_component = curr_hets * kLn2 - Lfact(curr_hets) - Lfact(curr_homr) - Lfact(curr_homc); |
| 2638 |
const double starting_lnprob = common_lnprob_component + starting_lnprob_other_component; |
| 2639 |
const double midp_d = u31tod(midp); |
| 2640 |
if (ln_thresh <= starting_lnprob - midp_d * kLn2) { |
| 2641 |
return 0; |
| 2642 |
} |
| 2643 |
const double max_homr = S_CAST(double, rare_ct / 2); |
| 2644 |
if (ln_thresh > starting_lnprob + log(max_homr + 1 - midp_d * 0.5)) { |
| 2645 |
return 1; |
| 2646 |
} |
| 2647 |
|
| 2648 |
const double maf = rare_ctd / allele_ctd; |
| 2649 |
const double modal_nhet = rare_ctd * (1 - maf); |
| 2650 |
const double c_minus_r = curr_homc - curr_homr; |
| 2651 |
// This should be in (0.5, 2^30]. |
| 2652 |
const double tail_thresh = exp(ln_thresh - starting_lnprob); |
| 2653 |
double tailp = 1 - midp_d * 0.5; |
| 2654 |
double lastp = 1; |
| 2655 |
if (curr_hets > modal_nhet) { |
| 2656 |
// No center-sum (or p=1) code path, since it doesn't make sense to choose |
| 2657 |
// a HWE threshold that makes these relevant; we should have already |
| 2658 |
// returned 0. |
| 2659 |
// (So we can't assume obs_hets >= 2.) |
| 2660 |
curr_hets += 2; |
| 2661 |
while (curr_homr > 0.5) { |
| 2662 |
lastp *= (4 * curr_homr * curr_homc) / (curr_hets * (curr_hets - 1)); |
| 2663 |
const double preaddp = tailp; |
| 2664 |
tailp += lastp; |
| 2665 |
if (tailp <= preaddp) { |
| 2666 |
break; |
| 2667 |
} |
| 2668 |
if (tailp >= tail_thresh) { |
| 2669 |
return 0; |
| 2670 |
} |
| 2671 |
curr_hets += 2; |
| 2672 |
curr_homr -= 1; |
| 2673 |
curr_homc -= 1; |
| 2674 |
} |
| 2675 |
{ |
| 2676 |
const double delta = 0.5 * (curr_hets + S_CAST(double, obs_hets)) - modal_nhet; |
| 2677 |
curr_homr = 0.5 * (curr_homr + S_CAST(double, obs_homr)) + delta; |
| 2678 |
curr_homr = S_CAST(double, S_CAST(int32_t, curr_homr + 0.5)); |
| 2679 |
if (curr_homr > max_homr) { |
| 2680 |
curr_homr = max_homr; |
| 2681 |
} |
| 2682 |
} |
| 2683 |
const double max_diff = starting_lnprob_other_component * (kSmallEpsilon / 8); |
| 2684 |
while (1) { |
| 2685 |
curr_hets = rare_ctd - curr_homr * 2; |
| 2686 |
curr_homc = curr_homr + c_minus_r; |
| 2687 |
const double lnprob_other_component = curr_hets * kLn2 - Lfact(curr_hets) - Lfact(curr_homr) - Lfact(curr_homc); |
| 2688 |
const double lnprob_diff = lnprob_other_component - starting_lnprob_other_component; |
| 2689 |
if (lnprob_diff > max_diff) { |
| 2690 |
if (curr_homr >= max_homr) { |
| 2691 |
return 1; |
| 2692 |
} |
| 2693 |
const double ll_deriv = log(curr_hets * (curr_hets - 1) / (4 * (curr_homr + 1) * (curr_homc + 1))); |
| 2694 |
curr_homr += 1 - S_CAST(int64_t, (1 - kSmallEpsilon) * lnprob_diff / ll_deriv); |
| 2695 |
if (curr_homr > max_homr) { |
| 2696 |
curr_homr = max_homr; |
| 2697 |
} |
| 2698 |
} else if (lnprob_diff > -62 * kLn2) { |
| 2699 |
lastp = exp(lnprob_diff); |
| 2700 |
break; |
| 2701 |
} else { |
| 2702 |
const double ll_deriv = log((curr_hets + 2) * (curr_hets + 1) / (4 * curr_homr * curr_homc)); |
| 2703 |
curr_homr -= S_CAST(int64_t, lnprob_diff / ll_deriv); |
| 2704 |
if (curr_homr < 0) { |
| 2705 |
curr_homr = 0; |
| 2706 |
} |
| 2707 |
} |
| 2708 |
} |
| 2709 |
double lastp_tail = lastp; |
| 2710 |
if (lastp < 1 - kSmallEpsilon) { |
| 2711 |
double curr_homr_center = curr_homr; |
| 2712 |
double curr_homc_center = curr_homc; |
| 2713 |
double curr_hets_center = curr_hets; |
| 2714 |
while (1) { |
| 2715 |
tailp += lastp; |
| 2716 |
if (tailp >= tail_thresh) { |
| 2717 |
return 0; |
| 2718 |
} |
| 2719 |
curr_hets_center += 2; |
| 2720 |
lastp *= (4 * curr_homr_center * curr_homc_center) / (curr_hets_center * (curr_hets_center - 1)); |
| 2721 |
if (lastp >= 1 - kSmallEpsilon) { |
| 2722 |
break; |
| 2723 |
} |
| 2724 |
curr_homr_center -= 1; |
| 2725 |
curr_homc_center -= 1; |
| 2726 |
} |
| 2727 |
} |
| 2728 |
if (lastp < 1 + kSmallEpsilon) { |
| 2729 |
tailp += lastp * (1 - midp_d * 0.5); |
| 2730 |
if (tailp >= tail_thresh) { |
| 2731 |
return 0; |
| 2732 |
} |
| 2733 |
} |
| 2734 |
// We're down to one tail that can be tightly bounded by a geometric |
| 2735 |
// series. (ratio is always decreasing) |
| 2736 |
// c + cr + cr^2 + ... = c/(1-r) |
| 2737 |
curr_homr += 1; |
| 2738 |
curr_homc += 1; |
| 2739 |
const double cur_ratio = (curr_hets * (curr_hets - 1)) / (4 * curr_homr * curr_homc); |
| 2740 |
lastp_tail *= cur_ratio; |
| 2741 |
const double remaining_ceil = lastp_tail / (1 - cur_ratio); |
| 2742 |
if (tailp + remaining_ceil < tail_thresh) { |
| 2743 |
return 1; |
| 2744 |
} |
| 2745 |
while (1) { |
| 2746 |
const double preaddp = tailp; |
| 2747 |
tailp += lastp_tail; |
| 2748 |
if (tailp <= preaddp) { |
| 2749 |
return 1; |
| 2750 |
} |
| 2751 |
if (tailp >= tail_thresh) { |
| 2752 |
return 0; |
| 2753 |
} |
| 2754 |
curr_hets -= 2; |
| 2755 |
curr_homr += 1; |
| 2756 |
curr_homc += 1; |
| 2757 |
lastp_tail *= (curr_hets * (curr_hets - 1)) / (4 * curr_homr * curr_homc); |
| 2758 |
} |
| 2759 |
// unreachable |
| 2760 |
} |
| 2761 |
while (curr_hets > 1.5) { |
| 2762 |
curr_homr += 1; |
| 2763 |
curr_homc += 1; |
| 2764 |
lastp *= curr_hets * (curr_hets - 1) / (4 * curr_homr * curr_homc); |
| 2765 |
curr_hets -= 2; |
| 2766 |
const double preaddp = tailp; |
| 2767 |
tailp += lastp; |
| 2768 |
if (tailp <= preaddp) { |
| 2769 |
break; |
| 2770 |
} |
| 2771 |
if (tailp >= tail_thresh) { |
| 2772 |
return 0; |
| 2773 |
} |
| 2774 |
} |
| 2775 |
{ |
| 2776 |
const double delta = modal_nhet - 0.5 * (curr_hets + S_CAST(double, obs_hets)); |
| 2777 |
curr_homr = 0.5 * (curr_homr + S_CAST(double, obs_homr)) - delta; |
| 2778 |
curr_homr = S_CAST(double, S_CAST(int32_t, curr_homr + 0.5)); |
| 2779 |
if (curr_homr < 0) { |
| 2780 |
curr_homr = 0; |
| 2781 |
} |
| 2782 |
} |
| 2783 |
const double max_diff = starting_lnprob_other_component * (kSmallEpsilon / 8); |
| 2784 |
while (1) { |
| 2785 |
curr_hets = rare_ctd - curr_homr * 2; |
| 2786 |
curr_homc = curr_homr + c_minus_r; |
| 2787 |
const double lnprob_other_component = curr_hets * kLn2 - Lfact(curr_hets) - Lfact(curr_homr) - Lfact(curr_homc); |
| 2788 |
const double lnprob_diff = lnprob_other_component - starting_lnprob_other_component; |
| 2789 |
if (lnprob_diff > max_diff) { |
| 2790 |
if (curr_homr <= 0) { |
| 2791 |
return 1; |
| 2792 |
} |
| 2793 |
const double ll_deriv = log(4 * curr_homr * curr_homc / ((curr_hets + 2) * (curr_hets + 1))); |
| 2794 |
curr_homr += S_CAST(int64_t, (1 - kSmallEpsilon) * lnprob_diff / ll_deriv) - 1; |
| 2795 |
if (curr_homr < 0) { |
| 2796 |
curr_homr = 0; |
| 2797 |
} |
| 2798 |
} else if (lnprob_diff > -62 * kLn2) { |
| 2799 |
lastp = exp(lnprob_diff); |
| 2800 |
break; |
| 2801 |
} else { |
| 2802 |
const double ll_deriv = log(4 * (curr_homr + 1) * (curr_homc + 1) / (curr_hets * (curr_hets - 1))); |
| 2803 |
curr_homr += S_CAST(int64_t, lnprob_diff / ll_deriv); |
| 2804 |
if (curr_homr > max_homr) { |
| 2805 |
curr_homr = max_homr; |
| 2806 |
} |
| 2807 |
} |
| 2808 |
} |
| 2809 |
double lastp_tail = lastp; |
| 2810 |
if (lastp < 1 - kSmallEpsilon) { |
| 2811 |
double curr_homr_center = curr_homr; |
| 2812 |
double curr_homc_center = curr_homc; |
| 2813 |
double curr_hets_center = curr_hets; |
| 2814 |
while (1) { |
| 2815 |
tailp += lastp; |
| 2816 |
if (tailp >= tail_thresh) { |
| 2817 |
return 0; |
| 2818 |
} |
| 2819 |
curr_homr_center += 1; |
| 2820 |
curr_homc_center += 1; |
| 2821 |
lastp *= curr_hets_center * (curr_hets_center - 1) / (4 * curr_homr_center * curr_homc_center); |
| 2822 |
if (lastp >= 1 - kSmallEpsilon) { |
| 2823 |
break; |
| 2824 |
} |
| 2825 |
curr_hets_center -= 2; |
| 2826 |
} |
| 2827 |
} |
| 2828 |
if (lastp < 1 + kSmallEpsilon) { |
| 2829 |
tailp += lastp * (1 - midp_d * 0.5); |
| 2830 |
if (tailp >= tail_thresh) { |
| 2831 |
return 0; |
| 2832 |
} |
| 2833 |
} |
| 2834 |
curr_hets += 2; |
| 2835 |
const double cur_ratio = 4 * curr_homr * curr_homc / (curr_hets * (curr_hets - 1)); |
| 2836 |
lastp_tail *= cur_ratio; |
| 2837 |
const double remaining_ceil = lastp_tail / (1 - cur_ratio); |
| 2838 |
if (tailp + remaining_ceil < tail_thresh) { |
| 2839 |
return 1; |
| 2840 |
} |
| 2841 |
while (1) { |
| 2842 |
const double preaddp = tailp; |
| 2843 |
tailp += lastp_tail; |
| 2844 |
if (tailp <= preaddp) { |
| 2845 |
return 1; |
| 2846 |
} |
| 2847 |
if (tailp >= tail_thresh) { |
| 2848 |
return 0; |
| 2849 |
} |
| 2850 |
curr_homr -= 1; |
| 2851 |
curr_homc -= 1; |
| 2852 |
curr_hets += 2; |
| 2853 |
lastp_tail *= 4 * curr_homr * curr_homc / (curr_hets * (curr_hets - 1)); |
| 2854 |
} |
| 2855 |
} |
| 2856 |
|
| 2857 |
// 2^{-40} for now, since 2^{-44} was too small on real data |
| 2858 |
static const double kExactTestEpsilon2 = 0.0000000000009094947017729282379150390625; |
| 2859 |
|
| 2860 |
double FisherExact2x2P(uint32_t m11, uint32_t m12, uint32_t m21, uint32_t m22, uint32_t midp) { |
| 2861 |
// Basic 2x2 Fisher exact test p-value calculation. |
| 2862 |
double tprob = (1 - kExactTestEpsilon2) * kExactTestBias; |
| 2863 |
double cur_prob = tprob; |
| 2864 |
double cprob = 0; |
| 2865 |
int32_t tie_ct = 1; |
| 2866 |
uint32_t uii; |
| 2867 |
double cur11; |
| 2868 |
double cur12; |
| 2869 |
double cur21; |
| 2870 |
double cur22; |
| 2871 |
double preaddp; |
| 2872 |
// Ensure we are left of the distribution center, m11 <= m22, and m12 <= m21. |
| 2873 |
if (m12 > m21) { |
| 2874 |
uii = m12; |
| 2875 |
m12 = m21; |
| 2876 |
m21 = uii; |
| 2877 |
} |
| 2878 |
if (m11 > m22) { |
| 2879 |
uii = m11; |
| 2880 |
m11 = m22; |
| 2881 |
m22 = uii; |
| 2882 |
} |
| 2883 |
if ((S_CAST(uint64_t, m11) * m22) > (S_CAST(uint64_t, m12) * m21)) { |
| 2884 |
uii = m11; |
| 2885 |
m11 = m12; |
| 2886 |
m12 = uii; |
| 2887 |
uii = m21; |
| 2888 |
m21 = m22; |
| 2889 |
m22 = uii; |
| 2890 |
} |
| 2891 |
cur11 = m11; |
| 2892 |
cur12 = m12; |
| 2893 |
cur21 = m21; |
| 2894 |
cur22 = m22; |
| 2895 |
while (cur12 > 0.5) { |
| 2896 |
cur11 += 1; |
| 2897 |
cur22 += 1; |
| 2898 |
cur_prob *= (cur12 * cur21) / (cur11 * cur22); |
| 2899 |
cur12 -= 1; |
| 2900 |
cur21 -= 1; |
| 2901 |
if (cur_prob > DBL_MAX) { |
| 2902 |
return 0; |
| 2903 |
} |
| 2904 |
if (cur_prob < kExactTestBias) { |
| 2905 |
if (cur_prob > (1 - 2 * kExactTestEpsilon2) * kExactTestBias) { |
| 2906 |
tie_ct++; |
| 2907 |
} |
| 2908 |
tprob += cur_prob; |
| 2909 |
break; |
| 2910 |
} |
| 2911 |
cprob += cur_prob; |
| 2912 |
} |
| 2913 |
if ((cprob == 0) && (!midp)) { |
| 2914 |
return 1; |
| 2915 |
} |
| 2916 |
while (cur12 > 0.5) { |
| 2917 |
cur11 += 1; |
| 2918 |
cur22 += 1; |
| 2919 |
cur_prob *= (cur12 * cur21) / (cur11 * cur22); |
| 2920 |
cur12 -= 1; |
| 2921 |
cur21 -= 1; |
| 2922 |
preaddp = tprob; |
| 2923 |
tprob += cur_prob; |
| 2924 |
if (tprob <= preaddp) { |
| 2925 |
break; |
| 2926 |
} |
| 2927 |
} |
| 2928 |
if (m11) { |
| 2929 |
cur11 = m11; |
| 2930 |
cur12 = m12; |
| 2931 |
cur21 = m21; |
| 2932 |
cur22 = m22; |
| 2933 |
cur_prob = (1 - kExactTestEpsilon2) * kExactTestBias; |
| 2934 |
do { |
| 2935 |
cur12 += 1; |
| 2936 |
cur21 += 1; |
| 2937 |
cur_prob *= (cur11 * cur22) / (cur12 * cur21); |
| 2938 |
cur11 -= 1; |
| 2939 |
cur22 -= 1; |
| 2940 |
preaddp = tprob; |
| 2941 |
tprob += cur_prob; |
| 2942 |
if (tprob <= preaddp) { |
| 2943 |
if (!midp) { |
| 2944 |
return preaddp / (cprob + preaddp); |
| 2945 |
} |
| 2946 |
return (preaddp - ((1 - kExactTestEpsilon2) * kExactTestBias * 0.5) * tie_ct) / (cprob + preaddp); |
| 2947 |
} |
| 2948 |
} while (cur11 > 0.5); |
| 2949 |
} |
| 2950 |
if (!midp) { |
| 2951 |
return tprob / (cprob + tprob); |
| 2952 |
} |
| 2953 |
return (tprob - ((1 - kExactTestEpsilon2) * kExactTestBias * 0.5) * tie_ct) / (cprob + tprob); |
| 2954 |
} |
| 2955 |
|
| 2956 |
void HweLnFirstRow(double hetab, double homa, double homb, double* tailp_ptr, double* starting_lnprob_ptr, uint32_t* tie_ct_ptr, double* orig_base_lnprobl_ptr, double* orig_base_lnprobr_ptr, double* orig_saved_lhets_ptr, double* orig_saved_lhoma_ptr, double* orig_saved_lhomb_ptr, double* orig_saved_rhets_ptr, double* orig_saved_rhoma_ptr, double* orig_saved_rhomb_ptr) { |
| 2957 |
const double sample_ctd = hetab + homa + homb; |
| 2958 |
const double a_ctd = hetab + 2 * homa; |
| 2959 |
const double b_ctd = hetab + 2 * homb; |
| 2960 |
const double allele_ctd = sample_ctd * 2; |
| 2961 |
const double common_lnprob_component = Lfact(sample_ctd) + Lfact(a_ctd) + Lfact(b_ctd) - Lfact(allele_ctd); |
| 2962 |
const double starting_lnprob_other_component = hetab * kLn2 - Lfact(hetab) - Lfact(homa) - Lfact(homb); |
| 2963 |
const double starting_lnprob = common_lnprob_component + starting_lnprob_other_component; |
| 2964 |
*starting_lnprob_ptr = starting_lnprob; |
| 2965 |
double lastp = 1; |
| 2966 |
uint32_t tie_ct = 1; |
| 2967 |
double tmp_hets = hetab; |
| 2968 |
if (hetab * hetab > 4 * homa * homb) { |
| 2969 |
// Incrementing hetab decreases likelihood from this point on. |
| 2970 |
*orig_base_lnprobr_ptr = 0; |
| 2971 |
*orig_saved_rhets_ptr = hetab; |
| 2972 |
*orig_saved_rhoma_ptr = homa; |
| 2973 |
*orig_saved_rhomb_ptr = homb; |
| 2974 |
if (starting_lnprob > -10 * kLn2) { |
| 2975 |
// Sum center instead of tail. |
| 2976 |
double centerp = 0; |
| 2977 |
double tmp_homa = homa; |
| 2978 |
double tmp_homb = homb; |
| 2979 |
while (tmp_hets > 1.5) { |
| 2980 |
tmp_homa += 1; |
| 2981 |
tmp_homb += 1; |
| 2982 |
lastp *= (tmp_hets * (tmp_hets - 1)) / (4 * tmp_homa * tmp_homb); |
| 2983 |
tmp_hets -= 2; |
| 2984 |
if (lastp < 1 + kSmallEpsilon) { |
| 2985 |
tie_ct += (lastp > 1 - kSmallEpsilon); |
| 2986 |
break; |
| 2987 |
} |
| 2988 |
centerp += lastp; |
| 2989 |
} |
| 2990 |
*tie_ct_ptr = tie_ct; |
| 2991 |
*orig_saved_lhets_ptr = tmp_hets; |
| 2992 |
*orig_saved_lhoma_ptr = tmp_homa; |
| 2993 |
*orig_saved_lhomb_ptr = tmp_homb; |
| 2994 |
*orig_base_lnprobl_ptr = log(lastp); |
| 2995 |
*tailp_ptr = exp(-starting_lnprob) - centerp; |
| 2996 |
return; |
| 2997 |
} |
| 2998 |
double* orig_saved_lhomr_ptr = orig_saved_lhoma_ptr; |
| 2999 |
double* orig_saved_lhomc_ptr = orig_saved_lhomb_ptr; |
| 3000 |
double rare_ctd = a_ctd; |
| 3001 |
double c_minus_r = (b_ctd - a_ctd) * 0.5; |
| 3002 |
double tmp_homr = homa; |
| 3003 |
double tmp_homc = homb; |
| 3004 |
if (c_minus_r < 0.0) { |
| 3005 |
orig_saved_lhomr_ptr = orig_saved_lhomb_ptr; |
| 3006 |
orig_saved_lhomc_ptr = orig_saved_lhoma_ptr; |
| 3007 |
rare_ctd = b_ctd; |
| 3008 |
c_minus_r = -c_minus_r; |
| 3009 |
tmp_homr = homb; |
| 3010 |
tmp_homc = homa; |
| 3011 |
} |
| 3012 |
const double orig_homr = tmp_homr; |
| 3013 |
double tailp = 1; |
| 3014 |
while (tmp_homr > 0.5) { |
| 3015 |
tmp_hets += 2; |
| 3016 |
lastp *= (4 * tmp_homr * tmp_homc) / (tmp_hets * (tmp_hets - 1)); |
| 3017 |
tmp_homr -= 1; |
| 3018 |
tmp_homc -= 1; |
| 3019 |
const double preaddp = tailp; |
| 3020 |
tailp += lastp; |
| 3021 |
if (tailp <= preaddp) { |
| 3022 |
break; |
| 3023 |
} |
| 3024 |
} |
| 3025 |
const double maf = rare_ctd / allele_ctd; |
| 3026 |
const double modal_nhet = rare_ctd * (1 - maf); |
| 3027 |
const double max_homr = S_CAST(double, S_CAST(int32_t, rare_ctd * 0.5)); |
| 3028 |
{ |
| 3029 |
const double delta = 0.5 * (tmp_hets + hetab) - modal_nhet; |
| 3030 |
// Round and clamp. |
| 3031 |
tmp_homr = 0.5 * (tmp_homr + orig_homr) + delta; |
| 3032 |
tmp_homr = S_CAST(double, S_CAST(int32_t, tmp_homr + 0.5)); |
| 3033 |
if (tmp_homr > max_homr) { |
| 3034 |
tmp_homr = max_homr; |
| 3035 |
} |
| 3036 |
} |
| 3037 |
const double max_diff = starting_lnprob_other_component * (kSmallEpsilon / 8); |
| 3038 |
while (1) { |
| 3039 |
tmp_hets = rare_ctd - tmp_homr * 2; |
| 3040 |
tmp_homc = tmp_homr + c_minus_r; |
| 3041 |
const double lnprob_other_component = tmp_hets * kLn2 - Lfact(tmp_hets) - Lfact(tmp_homr) - Lfact(tmp_homc); |
| 3042 |
const double lnprob_diff = lnprob_other_component - starting_lnprob_other_component; |
| 3043 |
if (lnprob_diff > max_diff) { |
| 3044 |
if (tmp_homr >= max_homr) { |
| 3045 |
// All terms on this tail are larger than the starting term. Exit. |
| 3046 |
assert(tmp_homr == max_homr); |
| 3047 |
*tie_ct_ptr = 1; |
| 3048 |
*orig_saved_lhets_ptr = tmp_hets; |
| 3049 |
*orig_saved_lhomr_ptr = tmp_homr; |
| 3050 |
*orig_saved_lhomc_ptr = tmp_homc; |
| 3051 |
// exp(lnprob_diff) can be too large to fit in a double, so we make |
| 3052 |
// this return value a log-probability. |
| 3053 |
*orig_base_lnprobl_ptr = lnprob_diff; |
| 3054 |
*tailp_ptr = tailp; |
| 3055 |
return; |
| 3056 |
} |
| 3057 |
const double ll_deriv = log(tmp_hets * (tmp_hets - 1) / (4 * (tmp_homr + 1) * (tmp_homc + 1))); |
| 3058 |
// Round absolute value up, to guarantee that we make progress. |
| 3059 |
// (lnprob_diff is positive and ll_deriv is negative.) |
| 3060 |
// This may overshoot. But the function is guaranteed to terminate |
| 3061 |
// because we never overshoot (and we do always make progress on each |
| 3062 |
// step) once we're on the other side. |
| 3063 |
tmp_homr += 1 - S_CAST(int64_t, (1 - kSmallEpsilon) * lnprob_diff / ll_deriv); |
| 3064 |
if (tmp_homr > max_homr) { |
| 3065 |
tmp_homr = max_homr; |
| 3066 |
} |
| 3067 |
} else if (lnprob_diff > -62 * kLn2) { |
| 3068 |
lastp = exp(lnprob_diff); |
| 3069 |
break; |
| 3070 |
} else { |
| 3071 |
const double ll_deriv = log((tmp_hets + 2) * (tmp_hets + 1) / (4 * tmp_homr * tmp_homc)); |
| 3072 |
// Round down, to guarantee we don't overshoot. |
| 3073 |
// We're guaranteed to make progress, since lnprob_diff >= 62 * log(2) |
| 3074 |
// and sample_ct < 2^31. |
| 3075 |
tmp_homr -= S_CAST(int64_t, lnprob_diff / ll_deriv); |
| 3076 |
if (tmp_homr < 0) { |
| 3077 |
tmp_homr = 0; |
| 3078 |
} |
| 3079 |
} |
| 3080 |
} |
| 3081 |
// Sum toward center, until lastp >= 1... |
| 3082 |
// ...or we have returned to the starting cell; this might be possible due |
| 3083 |
// to floating-point error. Unlike HweLnP(), we didn't early-exit when the |
| 3084 |
// starting cell was a mode. |
| 3085 |
double lastp_tail = lastp; |
| 3086 |
{ |
| 3087 |
double tmp_homr_center = tmp_homr; |
| 3088 |
double tmp_homc_center = tmp_homc; |
| 3089 |
double tmp_hets_center = tmp_hets; |
| 3090 |
while (tmp_homr_center > orig_homr) { |
| 3091 |
if (lastp >= 1 - kSmallEpsilon) { |
| 3092 |
if (lastp < 1 + kSmallEpsilon) { |
| 3093 |
tailp += lastp; |
| 3094 |
++tie_ct; |
| 3095 |
} |
| 3096 |
break; |
| 3097 |
} |
| 3098 |
tailp += lastp; |
| 3099 |
tmp_hets_center += 2; |
| 3100 |
lastp *= (4 * tmp_homr_center * tmp_homc_center) / (tmp_hets_center * (tmp_hets_center - 1)); |
| 3101 |
tmp_homr_center -= 1; |
| 3102 |
tmp_homc_center -= 1; |
| 3103 |
} |
| 3104 |
*tie_ct_ptr = tie_ct; |
| 3105 |
*orig_saved_lhets_ptr = tmp_hets_center; |
| 3106 |
*orig_saved_lhomr_ptr = tmp_homr_center; |
| 3107 |
*orig_saved_lhomc_ptr = tmp_homc_center; |
| 3108 |
*orig_base_lnprobl_ptr = log(lastp); |
| 3109 |
} |
| 3110 |
// Sum away from center, until sums stop changing. |
| 3111 |
while (1) { |
| 3112 |
tmp_homr += 1; |
| 3113 |
tmp_homc += 1; |
| 3114 |
lastp_tail *= (tmp_hets * (tmp_hets - 1)) / (4 * tmp_homr * tmp_homc); |
| 3115 |
const double preaddp = tailp; |
| 3116 |
tailp += lastp_tail; |
| 3117 |
if (tailp <= preaddp) { |
| 3118 |
break; |
| 3119 |
} |
| 3120 |
tmp_hets -= 2; |
| 3121 |
} |
| 3122 |
*tailp_ptr = tailp; |
| 3123 |
return; |
| 3124 |
} |
| 3125 |
// Decrementing hetab decreases likelihood from this point on. |
| 3126 |
*orig_base_lnprobl_ptr = 0; |
| 3127 |
*orig_saved_lhets_ptr = hetab; |
| 3128 |
*orig_saved_lhoma_ptr = homa; |
| 3129 |
*orig_saved_lhomb_ptr = homb; |
| 3130 |
if (starting_lnprob > -10 * kLn2) { |
| 3131 |
const double tmp_hets_stop = MINV(a_ctd, b_ctd) - 1.5; |
| 3132 |
double centerp = 0; |
| 3133 |
double tmp_homa = homa; |
| 3134 |
double tmp_homb = homb; |
| 3135 |
while (tmp_hets < tmp_hets_stop) { |
| 3136 |
tmp_hets += 2; |
| 3137 |
lastp *= 4 * tmp_homa * tmp_homb / (tmp_hets * (tmp_hets - 1)); |
| 3138 |
tmp_homa -= 1; |
| 3139 |
tmp_homb -= 1; |
| 3140 |
if (lastp < 1 + kSmallEpsilon) { |
| 3141 |
// TODO: fix this in a5.11, wrong parentheses in a5.10 |
| 3142 |
tie_ct += (lastp > 1 - kSmallEpsilon); |
| 3143 |
break; |
| 3144 |
} |
| 3145 |
centerp += lastp; |
| 3146 |
} |
| 3147 |
*tie_ct_ptr = tie_ct; |
| 3148 |
*orig_saved_rhets_ptr = tmp_hets; |
| 3149 |
*orig_saved_rhoma_ptr = tmp_homa; |
| 3150 |
*orig_saved_rhomb_ptr = tmp_homb; |
| 3151 |
*orig_base_lnprobr_ptr = log(lastp); |
| 3152 |
*tailp_ptr = exp(-starting_lnprob) - centerp; |
| 3153 |
return; |
| 3154 |
} |
| 3155 |
double* orig_saved_rhomr_ptr = orig_saved_rhoma_ptr; |
| 3156 |
double* orig_saved_rhomc_ptr = orig_saved_rhomb_ptr; |
| 3157 |
double rare_ctd = a_ctd; |
| 3158 |
double c_minus_r = (b_ctd - a_ctd) * 0.5; |
| 3159 |
double tmp_homr = homa; |
| 3160 |
double tmp_homc = homb; |
| 3161 |
if (c_minus_r < 0.0) { |
| 3162 |
orig_saved_rhomr_ptr = orig_saved_rhomb_ptr; |
| 3163 |
orig_saved_rhomc_ptr = orig_saved_rhoma_ptr; |
| 3164 |
rare_ctd = b_ctd; |
| 3165 |
c_minus_r = -c_minus_r; |
| 3166 |
tmp_homr = homb; |
| 3167 |
tmp_homc = homa; |
| 3168 |
} |
| 3169 |
const double orig_homr = tmp_homr; |
| 3170 |
double tailp = 1; |
| 3171 |
while (tmp_hets > 1.5) { |
| 3172 |
tmp_homr += 1; |
| 3173 |
tmp_homc += 1; |
| 3174 |
lastp *= tmp_hets * (tmp_hets - 1) / (4 * tmp_homr * tmp_homc); |
| 3175 |
tmp_hets -= 2; |
| 3176 |
const double preaddp = tailp; |
| 3177 |
tailp += lastp; |
| 3178 |
if (tailp <= preaddp) { |
| 3179 |
break; |
| 3180 |
} |
| 3181 |
} |
| 3182 |
// Jump to other tail. |
| 3183 |
const double maf = rare_ctd / allele_ctd; |
| 3184 |
const double modal_nhet = rare_ctd * (1 - maf); |
| 3185 |
{ |
| 3186 |
const double delta = modal_nhet - 0.5 * (tmp_hets + hetab); |
| 3187 |
tmp_homr = 0.5 * (tmp_homr + orig_homr) - delta; |
| 3188 |
// Round and clamp. |
| 3189 |
tmp_homr = S_CAST(double, S_CAST(int32_t, tmp_homr + 0.5)); |
| 3190 |
if (tmp_homr < 0) { |
| 3191 |
tmp_homr = 0; |
| 3192 |
} |
| 3193 |
} |
| 3194 |
const double max_diff = starting_lnprob_other_component * (kSmallEpsilon / 8); |
| 3195 |
const double max_homr = S_CAST(double, S_CAST(int32_t, rare_ctd * 0.5)); |
| 3196 |
while (1) { |
| 3197 |
tmp_hets = rare_ctd - tmp_homr * 2; |
| 3198 |
tmp_homc = tmp_homr + c_minus_r; |
| 3199 |
const double lnprob_other_component = tmp_hets * kLn2 - Lfact(tmp_hets) - Lfact(tmp_homr) - Lfact(tmp_homc); |
| 3200 |
const double lnprob_diff = lnprob_other_component - starting_lnprob_other_component; |
| 3201 |
if (lnprob_diff > max_diff) { |
| 3202 |
if (tmp_homr <= 0) { |
| 3203 |
// All terms on this tail are larger than the starting term. Exit. |
| 3204 |
assert(tmp_homr == 0.0); |
| 3205 |
*tie_ct_ptr = 1; |
| 3206 |
*orig_saved_rhets_ptr = tmp_hets; |
| 3207 |
*orig_saved_rhomr_ptr = tmp_homr; |
| 3208 |
*orig_saved_rhomc_ptr = tmp_homc; |
| 3209 |
*orig_base_lnprobr_ptr = lnprob_diff; |
| 3210 |
*tailp_ptr = tailp; |
| 3211 |
return; |
| 3212 |
} |
| 3213 |
const double ll_deriv = log(4 * tmp_homr * tmp_homc / ((tmp_hets + 2) * (tmp_hets + 1))); |
| 3214 |
tmp_homr += S_CAST(int64_t, (1 - kSmallEpsilon) * lnprob_diff / ll_deriv) - 1; |
| 3215 |
if (tmp_homr < 0) { |
| 3216 |
tmp_homr = 0; |
| 3217 |
} |
| 3218 |
} else if (lnprob_diff > -62 * kLn2) { |
| 3219 |
lastp = exp(lnprob_diff); |
| 3220 |
break; |
| 3221 |
} else { |
| 3222 |
const double ll_deriv = log(4 * (tmp_homr + 1) * (tmp_homc + 1) / (tmp_hets * (tmp_hets - 1))); |
| 3223 |
tmp_homr += S_CAST(int64_t, lnprob_diff / ll_deriv); |
| 3224 |
if (tmp_homr > max_homr) { |
| 3225 |
tmp_homr = max_homr; |
| 3226 |
} |
| 3227 |
} |
| 3228 |
} |
| 3229 |
// Sum toward center, until lastp >= 1, or we have returned to the starting |
| 3230 |
// cell. |
| 3231 |
double lastp_tail = lastp; |
| 3232 |
{ |
| 3233 |
double tmp_homr_center = tmp_homr; |
| 3234 |
double tmp_homc_center = tmp_homc; |
| 3235 |
double tmp_hets_center = tmp_hets; |
| 3236 |
while (tmp_homr_center < orig_homr) { |
| 3237 |
if (lastp >= 1 - kSmallEpsilon) { |
| 3238 |
if (lastp < 1 + kSmallEpsilon) { |
| 3239 |
tailp += lastp; |
| 3240 |
++tie_ct; |
| 3241 |
} |
| 3242 |
break; |
| 3243 |
} |
| 3244 |
tailp += lastp; |
| 3245 |
tmp_homr_center += 1; |
| 3246 |
tmp_homc_center += 1; |
| 3247 |
lastp *= tmp_hets_center * (tmp_hets_center - 1) / (4 * tmp_homr_center * tmp_homc_center); |
| 3248 |
tmp_hets_center -= 2; |
| 3249 |
} |
| 3250 |
*tie_ct_ptr = tie_ct; |
| 3251 |
*orig_saved_rhets_ptr = tmp_hets_center; |
| 3252 |
*orig_saved_rhomr_ptr = tmp_homr_center; |
| 3253 |
*orig_saved_rhomc_ptr = tmp_homc_center; |
| 3254 |
*orig_base_lnprobr_ptr = log(lastp); |
| 3255 |
} |
| 3256 |
// Sum away from center, until sums stop changing. |
| 3257 |
while (1) { |
| 3258 |
tmp_hets += 2; |
| 3259 |
lastp_tail *= 4 * tmp_homr * tmp_homc / (tmp_hets * (tmp_hets - 1)); |
| 3260 |
const double preaddp = tailp; |
| 3261 |
tailp += lastp_tail; |
| 3262 |
if (tailp <= preaddp) { |
| 3263 |
break; |
| 3264 |
} |
| 3265 |
tmp_homr -= 1; |
| 3266 |
tmp_homc -= 1; |
| 3267 |
} |
| 3268 |
*tailp_ptr = tailp; |
| 3269 |
return; |
| 3270 |
} |
| 3271 |
|
| 3272 |
int32_t HweXchrLnPTailsum(uint32_t high_het_side, double* base_lnprobp, double* saved_hetsp, double* saved_hom1p, double* saved_hom2p, uint32_t* tie_ctp, double *totalp) { |
| 3273 |
// similar to fisher23_tailsum() |
| 3274 |
double total = 0; |
| 3275 |
double cur_lnprob = *base_lnprobp; |
| 3276 |
double tmp_hets = *saved_hetsp; |
| 3277 |
double tmp_hom1 = *saved_hom1p; |
| 3278 |
double tmp_hom2 = *saved_hom2p; |
| 3279 |
double tmps_hets; |
| 3280 |
double tmps_hom1; |
| 3281 |
double tmps_hom2; |
| 3282 |
// identify beginning of tail |
| 3283 |
if (high_het_side) { |
| 3284 |
if (cur_lnprob > kExactTestEpsilon2) { |
| 3285 |
double prev_numer = tmp_hom1 * tmp_hom2; |
| 3286 |
// Tried conditionally moving out of log-space, but wasn't able to make |
| 3287 |
// that faster. |
| 3288 |
while (prev_numer > 0.5) { |
| 3289 |
tmp_hets += 2; |
| 3290 |
cur_lnprob += log((4 * prev_numer) / (tmp_hets * (tmp_hets - 1))); |
| 3291 |
tmp_hom1 -= 1; |
| 3292 |
tmp_hom2 -= 1; |
| 3293 |
if (cur_lnprob <= kExactTestEpsilon2) { |
| 3294 |
break; |
| 3295 |
} |
| 3296 |
prev_numer = tmp_hom1 * tmp_hom2; |
| 3297 |
} |
| 3298 |
*base_lnprobp = cur_lnprob; |
| 3299 |
tmps_hets = tmp_hets; |
| 3300 |
tmps_hom1 = tmp_hom1; |
| 3301 |
tmps_hom2 = tmp_hom2; |
| 3302 |
} else { |
| 3303 |
tmps_hets = tmp_hets; |
| 3304 |
tmps_hom1 = tmp_hom1; |
| 3305 |
tmps_hom2 = tmp_hom2; |
| 3306 |
while (1) { |
| 3307 |
const double prev_lnprob = cur_lnprob; |
| 3308 |
tmp_hom1 += 1; |
| 3309 |
tmp_hom2 += 1; |
| 3310 |
cur_lnprob += log((tmp_hets * (tmp_hets - 1)) / (4 * tmp_hom1 * tmp_hom2)); |
| 3311 |
if (cur_lnprob < prev_lnprob) { |
| 3312 |
// this should never happen, but better to play it safe re: rounding |
| 3313 |
// error |
| 3314 |
return 1; |
| 3315 |
} |
| 3316 |
tmp_hets -= 2; |
| 3317 |
if (cur_lnprob > -kExactTestEpsilon2) { |
| 3318 |
if (cur_lnprob > kExactTestEpsilon2) { |
| 3319 |
break; |
| 3320 |
} |
| 3321 |
*tie_ctp += 1; |
| 3322 |
} |
| 3323 |
total += exp(cur_lnprob); |
| 3324 |
} |
| 3325 |
const double prev_lnprob = cur_lnprob; |
| 3326 |
cur_lnprob = *base_lnprobp; |
| 3327 |
*base_lnprobp = prev_lnprob; |
| 3328 |
} |
| 3329 |
} else { |
| 3330 |
if (cur_lnprob > kExactTestEpsilon2) { |
| 3331 |
while (tmp_hets > 1.5) { |
| 3332 |
tmp_hom1 += 1; |
| 3333 |
tmp_hom2 += 1; |
| 3334 |
cur_lnprob += log((tmp_hets * (tmp_hets - 1)) / (4 * tmp_hom1 * tmp_hom2)); |
| 3335 |
tmp_hets -= 2; |
| 3336 |
if (cur_lnprob <= kExactTestEpsilon2) { |
| 3337 |
break; |
| 3338 |
} |
| 3339 |
} |
| 3340 |
*base_lnprobp = cur_lnprob; |
| 3341 |
tmps_hets = tmp_hets; |
| 3342 |
tmps_hom1 = tmp_hom1; |
| 3343 |
tmps_hom2 = tmp_hom2; |
| 3344 |
} else { |
| 3345 |
tmps_hets = tmp_hets; |
| 3346 |
tmps_hom1 = tmp_hom1; |
| 3347 |
tmps_hom2 = tmp_hom2; |
| 3348 |
while (1) { |
| 3349 |
const double prev_lnprob = cur_lnprob; |
| 3350 |
tmp_hets += 2; |
| 3351 |
cur_lnprob += log((4 * tmp_hom1 * tmp_hom2) / (tmp_hets * (tmp_hets - 1))); |
| 3352 |
if (cur_lnprob < prev_lnprob) { |
| 3353 |
return 1; |
| 3354 |
} |
| 3355 |
tmp_hom1 -= 1; |
| 3356 |
tmp_hom2 -= 1; |
| 3357 |
if (cur_lnprob > -kExactTestEpsilon2) { |
| 3358 |
if (cur_lnprob > kExactTestEpsilon2) { |
| 3359 |
break; |
| 3360 |
} |
| 3361 |
*tie_ctp += 1; |
| 3362 |
} |
| 3363 |
total += exp(cur_lnprob); |
| 3364 |
} |
| 3365 |
const double prev_lnprob = cur_lnprob; |
| 3366 |
cur_lnprob = *base_lnprobp; |
| 3367 |
*base_lnprobp = prev_lnprob; |
| 3368 |
} |
| 3369 |
} |
| 3370 |
*saved_hetsp = tmp_hets; |
| 3371 |
*saved_hom1p = tmp_hom1; |
| 3372 |
*saved_hom2p = tmp_hom2; |
| 3373 |
if (cur_lnprob > -kExactTestEpsilon2) { |
| 3374 |
if (cur_lnprob > kExactTestEpsilon2) { |
| 3375 |
// even most extreme table on this side is too probable |
| 3376 |
*totalp = 0; |
| 3377 |
return 0; |
| 3378 |
} |
| 3379 |
*tie_ctp += 1; |
| 3380 |
} |
| 3381 |
double cur_prob = exp(cur_lnprob); |
| 3382 |
// sum tail to floating point precision limit |
| 3383 |
if (high_het_side) { |
| 3384 |
while (1) { |
| 3385 |
const double prev_tot = total; |
| 3386 |
total += cur_prob; |
| 3387 |
if (total <= prev_tot) { |
| 3388 |
break; |
| 3389 |
} |
| 3390 |
tmps_hets += 2; |
| 3391 |
cur_prob *= (4 * tmps_hom1 * tmps_hom2) / (tmps_hets * (tmps_hets - 1)); |
| 3392 |
tmps_hom1 -= 1; |
| 3393 |
tmps_hom2 -= 1; |
| 3394 |
} |
| 3395 |
} else { |
| 3396 |
while (1) { |
| 3397 |
const double prev_tot = total; |
| 3398 |
total += cur_prob; |
| 3399 |
if (total <= prev_tot) { |
| 3400 |
break; |
| 3401 |
} |
| 3402 |
tmps_hom1 += 1; |
| 3403 |
tmps_hom2 += 1; |
| 3404 |
cur_prob *= (tmps_hets * (tmps_hets - 1)) / (4 * tmps_hom1 * tmps_hom2); |
| 3405 |
tmps_hets -= 2; |
| 3406 |
} |
| 3407 |
} |
| 3408 |
*totalp = total; |
| 3409 |
return 0; |
| 3410 |
} |
| 3411 |
|
| 3412 |
double HweXchrLnP(int32_t female_hets, int32_t female_hom1, int32_t female_hom2, int32_t male1, int32_t male2, uint32_t midp) { |
| 3413 |
// See Graffelman J, Weir BS (2016) Testing for Hardy-Weinberg equilibrium at |
| 3414 |
// biallelic genetic markers on the X chromosome. |
| 3415 |
// Evaluation strategy is similar to fisher23(). |
| 3416 |
if ((!male1) && (!male2)) { |
| 3417 |
return HweLnP(female_hets, female_hom1, female_hom2, midp); |
| 3418 |
} |
| 3419 |
// 1. Determine relative tail vs. center masses for the male1/male2-unchanged |
| 3420 |
// slice. |
| 3421 |
double cur_female_hetd = S_CAST(double, female_hets); |
| 3422 |
double cur_female_hom1d = S_CAST(double, female_hom1); |
| 3423 |
double cur_female_hom2d = S_CAST(double, female_hom2); |
| 3424 |
const double male1d = u31tod(male1); |
| 3425 |
const double male2d = u31tod(male2); |
| 3426 |
const double n1 = cur_female_hetd + 2 * cur_female_hom1d + male1d; |
| 3427 |
const double n2 = cur_female_hetd + 2 * cur_female_hom2d + male2d; |
| 3428 |
double tailp; |
| 3429 |
double starting_lnprob; |
| 3430 |
uint32_t tie_ct; |
| 3431 |
// "left" = low hets side, "right" = high hets side |
| 3432 |
double orig_base_lnprobl; |
| 3433 |
double orig_base_lnprobr; |
| 3434 |
double orig_saved_lhets; |
| 3435 |
double orig_saved_lhom1; |
| 3436 |
double orig_saved_lhom2; |
| 3437 |
double orig_saved_rhets; |
| 3438 |
double orig_saved_rhom1; |
| 3439 |
double orig_saved_rhom2; |
| 3440 |
HweLnFirstRow(cur_female_hetd, cur_female_hom1d, cur_female_hom2d, &tailp, &starting_lnprob, &tie_ct, &orig_base_lnprobl, &orig_base_lnprobr, &orig_saved_lhets, &orig_saved_lhom1, &orig_saved_lhom2, &orig_saved_rhets, &orig_saved_rhom1, &orig_saved_rhom2); |
| 3441 |
|
| 3442 |
// A "row" holds male1/male2 constant. |
| 3443 |
// tailp, exp(orig_base_lnprobl), and exp(orig_base_lnprobr) are in units of |
| 3444 |
// exp(starting_lnprob) within the starting row. |
| 3445 |
for (uint32_t male1_decreasing = 0; male1_decreasing != 2; ++male1_decreasing) { |
| 3446 |
double cur_male1 = male1d; |
| 3447 |
double cur_male2 = male2d; |
| 3448 |
double ln_row_prob = -starting_lnprob; |
| 3449 |
double cur_lhets = orig_saved_lhets; |
| 3450 |
double cur_lhom1 = orig_saved_lhom1; |
| 3451 |
double cur_lhom2 = orig_saved_lhom2; |
| 3452 |
double cur_rhets = orig_saved_rhets; |
| 3453 |
double cur_rhom1 = orig_saved_rhom1; |
| 3454 |
double cur_rhom2 = orig_saved_rhom2; |
| 3455 |
double base_lnprobl = orig_base_lnprobl; |
| 3456 |
double base_lnprobr = orig_base_lnprobr; |
| 3457 |
uint32_t iter_ct; |
| 3458 |
if (male1_decreasing) { |
| 3459 |
iter_ct = 2 * female_hom2 + female_hets; |
| 3460 |
if (iter_ct > S_CAST(uint32_t, male1)) { |
| 3461 |
iter_ct = male1; |
| 3462 |
} |
| 3463 |
} else { |
| 3464 |
iter_ct = 2 * female_hom1 + female_hets; |
| 3465 |
if (iter_ct > S_CAST(uint32_t, male2)) { |
| 3466 |
iter_ct = male2; |
| 3467 |
} |
| 3468 |
} |
| 3469 |
for (uint32_t iter_idx = 0; iter_idx != iter_ct; ++iter_idx) { |
| 3470 |
if (male1_decreasing) { |
| 3471 |
const double old_male1 = cur_male1; |
| 3472 |
const double old_female2 = n2 - cur_male2; |
| 3473 |
cur_male2 += 1; |
| 3474 |
cur_male1 -= 1; |
| 3475 |
// row likelihood is ((n1 choose male1) * (n2 choose male2)) / |
| 3476 |
// ((n1 + n2) choose (male1 + male2)) |
| 3477 |
ln_row_prob += log((old_male1 * old_female2) / (cur_male2 * (n1 - cur_male1))); |
| 3478 |
// bugfix (19 Apr 2017): We cannot move to the right of the mode here. |
| 3479 |
// Otherwise, if the mode itself is more probable than our initial |
| 3480 |
// table, but the table to the immediate right of the mode is not, |
| 3481 |
// we'll fail to count the mode. |
| 3482 |
// ("right" = high het count, "left" = low het count.) |
| 3483 |
if (cur_lhets != 0.0) { |
| 3484 |
cur_lhom1 += 1; |
| 3485 |
base_lnprobl += log((old_male1 * cur_lhets) / (2 * cur_male2 * cur_lhom1)); |
| 3486 |
cur_lhets -= 1; |
| 3487 |
} else { |
| 3488 |
cur_lhets += 1; |
| 3489 |
base_lnprobl += log((2 * old_male1 * cur_lhom2) / (cur_male2 * cur_lhets)); |
| 3490 |
cur_lhom2 -= 1; |
| 3491 |
} |
| 3492 |
} else { |
| 3493 |
const double old_male2 = cur_male2; |
| 3494 |
const double old_female1 = n1 - cur_male1; |
| 3495 |
cur_male1 += 1; |
| 3496 |
cur_male2 -= 1; |
| 3497 |
ln_row_prob += log((old_male2 * old_female1) / (cur_male1 * (n2 - cur_male2))); |
| 3498 |
if (cur_lhets != 0.0) { |
| 3499 |
cur_lhom2 += 1; |
| 3500 |
base_lnprobl += log((old_male2 * cur_lhets) / (2 * cur_male1 * cur_lhom2)); |
| 3501 |
cur_lhets -= 1; |
| 3502 |
} else { |
| 3503 |
cur_lhets += 1; |
| 3504 |
base_lnprobl += log((2 * old_male2 * cur_lhom1) / (cur_male1 * cur_lhets)); |
| 3505 |
cur_lhom1 -= 1; |
| 3506 |
} |
| 3507 |
} |
| 3508 |
double tail_incr1; |
| 3509 |
if (HweXchrLnPTailsum(0, &base_lnprobl, &cur_lhets, &cur_lhom1, &cur_lhom2, &tie_ct, &tail_incr1)) { |
| 3510 |
// all tables in this row, and all subsequent rows, are less probable |
| 3511 |
// than the initial table. |
| 3512 |
double cur_female1 = n1 - cur_male1; |
| 3513 |
double cur_female2 = n2 - cur_male2; |
| 3514 |
double row_prob = exp(ln_row_prob); |
| 3515 |
if (male1_decreasing) { |
| 3516 |
while (1) { |
| 3517 |
const double preaddp = tailp; |
| 3518 |
tailp += row_prob; |
| 3519 |
if (tailp == preaddp) { |
| 3520 |
break; |
| 3521 |
} |
| 3522 |
cur_male2 += 1; |
| 3523 |
cur_female1 += 1; |
| 3524 |
row_prob *= (cur_male1 * cur_female2) / (cur_male2 * cur_female1); |
| 3525 |
cur_male1 -= 1; |
| 3526 |
cur_female2 -= 1; |
| 3527 |
} |
| 3528 |
} else { |
| 3529 |
while (1) { |
| 3530 |
const double preaddp = tailp; |
| 3531 |
tailp += row_prob; |
| 3532 |
if (tailp == preaddp) { |
| 3533 |
break; |
| 3534 |
} |
| 3535 |
cur_male1 += 1; |
| 3536 |
cur_female2 += 1; |
| 3537 |
row_prob *= (cur_male2 * cur_female1) / (cur_male1 * cur_female2); |
| 3538 |
cur_male2 -= 1; |
| 3539 |
cur_female1 -= 1; |
| 3540 |
} |
| 3541 |
} |
| 3542 |
break; |
| 3543 |
} |
| 3544 |
tailp += tail_incr1; |
| 3545 |
if (male1_decreasing) { |
| 3546 |
const double old_male1 = cur_male1 + 1; |
| 3547 |
if (cur_rhom2 != 0.0) { |
| 3548 |
cur_rhets += 1; |
| 3549 |
base_lnprobr += log((2 * old_male1 * cur_rhom2) / (cur_male2 * cur_rhets)); |
| 3550 |
cur_rhom2 -= 1; |
| 3551 |
} else { |
| 3552 |
cur_rhom1 += 1; |
| 3553 |
base_lnprobr += log((old_male1 * cur_rhets) / (2 * cur_male2 * cur_rhom1)); |
| 3554 |
cur_rhets -= 1; |
| 3555 |
} |
| 3556 |
} else { |
| 3557 |
const double old_male2 = cur_male2 + 1; |
| 3558 |
if (cur_rhom1 != 0.0) { |
| 3559 |
cur_rhets += 1; |
| 3560 |
base_lnprobr += log((2 * old_male2 * cur_rhom1) / (cur_male1 * cur_rhets)); |
| 3561 |
cur_rhom1 -= 1; |
| 3562 |
} else { |
| 3563 |
cur_rhom2 += 1; |
| 3564 |
base_lnprobr += log((old_male2 * cur_rhets) / (2 * cur_male1 * cur_rhom2)); |
| 3565 |
cur_rhets -= 1; |
| 3566 |
} |
| 3567 |
} |
| 3568 |
double tail_incr2 = 0.0; // maybe-uninitialized warning |
| 3569 |
HweXchrLnPTailsum(1, &base_lnprobr, &cur_rhets, &cur_rhom1, &cur_rhom2, &tie_ct, &tail_incr2); |
| 3570 |
tailp += tail_incr2; |
| 3571 |
} |
| 3572 |
} |
| 3573 |
// row likelihood is ((n1 choose male1) * (n2 choose male2)) / |
| 3574 |
// ((n1 + n2) choose (male1 + male2)) |
| 3575 |
// try to start with smaller terms to reduce cancellation hit |
| 3576 |
const double absolute_row_lnprob = -Lfact(male1d) - Lfact(n1 - male1d) + Lfact(n1) - Lfact(male2d) - Lfact(n2 - male2d) + Lfact(n2) + Lfact(male1d + male2d) + Lfact(n1 + n2 - male1d - male2d) - Lfact(n1 + n2); |
| 3577 |
if (midp) { |
| 3578 |
tailp -= S_CAST(double, tie_ct) * 0.5; |
| 3579 |
} |
| 3580 |
double result = log(tailp) + absolute_row_lnprob + starting_lnprob; |
| 3581 |
if (result > -kBigEpsilon) { |
| 3582 |
// true p-value should always be 1 here |
| 3583 |
return 0.0; |
| 3584 |
} |
| 3585 |
return result; |
| 3586 |
} |
| 3587 |
|
| 3588 |
#ifdef __cplusplus |
| 3589 |
} |
| 3590 |
#endif |