Debian Codegraph @

include/plink2_stats.h

raw
1 #ifndef __PLINK2_STATS_H__
2 #define __PLINK2_STATS_H__
3
4 // This library is part of PLINK 2.0, copyright (C) 2005-2025 Shaun Purcell,
5 // Christopher Chang.
6 //
7 // This library is free software: you can redistribute it and/or modify it
8 // under the terms of the GNU Lesser General Public License as published by the
9 // Free Software Foundation, either version 3 of the License, or (at your
10 // option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but WITHOUT
13 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15 // for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 #include "plink2_string.h"
21
22 #ifdef __cplusplus
23 namespace plink2 {
24 #endif
25
26 // result of current PToChisq(5e-324, 1)
27 // static const double kMaxChisq1df = 1480.8852530551483;
28
29 // now thread-safe!
30 double ChisqToP(double chisq, uint32_t df);
31
32 double ChisqToLnP(double chisq, uint32_t df);
33
34 // only handles df=1 and 2 for now, plan to support 4 later
35 double PToChisq(double pval, uint32_t df);
36
37 // only handles df=1 for now
38 double LnPToChisq(double ln_pval);
39
40 // Better than the original TstatToP() when the same df comes up many times.
41 // (May want a higher-level interface that allocates and incrementally fills a
42 // table.)
43 double TstatToP2(double tt, double df, double cached_gamma_mult);
44
45 // No -9 error return since that's a legitimate p-value logarithm. Caller is
46 // responsible for validating input.
47 double TstatToLnP(double tt, uint32_t df);
48
49 double FstatToLnP(double ff, uint32_t df1, uint32_t df2);
50
51 double QuantileToZscore(double pval);
52
53 HEADER_INLINE double ZscoreToP(double zz) {
54 return ChisqToP(zz * zz, 1);
55 }
56
57 HEADER_INLINE double ZscoreToLnP(double zz) {
58 return ChisqToLnP(zz * zz, 1);
59 }
60
61 // Assumes xx is a nonnegative integer.
62 double Lfact(double xx);
63
64 // HweP() has been replaced by HweLnP(). HweThresh() and HweThreshMidp() have
65 // been replaced by HweThreshLn().
66
67 double HweLnP(int32_t obs_hets, int32_t obs_hom1, int32_t obs_hom2, uint32_t midp);
68
69 // these return 0 if close enough to Hardy-Weinberg equilibrium
70 uint32_t HweThresh(int32_t obs_hets, int32_t obs_hom1, int32_t obs_hom2, double thresh);
71
72 uint32_t HweThreshMidp(int32_t obs_hets, int32_t obs_hom1, int32_t obs_hom2, double thresh);
73
74 uint32_t HweThreshLnMain(int32_t obs_hets, int32_t obs_hom1, int32_t obs_hom2, uint32_t midp, double ln_thresh);
75
76 HEADER_INLINE uint32_t HweThreshLn(int32_t obs_hets, int32_t obs_hom1, int32_t obs_hom2, uint32_t midp, double thresh, double ln_thresh) {
77 // kLnNormalMin = -708.3964185...
78 if (ln_thresh > -708.396) {
79 if (!midp) {
80 return HweThresh(obs_hets, obs_hom1, obs_hom2, thresh);
81 } else {
82 return HweThreshMidp(obs_hets, obs_hom1, obs_hom2, thresh);
83 }
84 }
85 return HweThreshLnMain(obs_hets, obs_hom1, obs_hom2, midp, ln_thresh);
86 }
87
88 double FisherExact2x2P(uint32_t m11, uint32_t m12, uint32_t m21, uint32_t m22, uint32_t midp);
89
90 double HweXchrLnP(int32_t female_hets, int32_t female_hom1, int32_t female_hom2, int32_t male1, int32_t male2, uint32_t midp);
91
92 #ifdef __cplusplus
93 }
94 #endif
95
96 #endif // __PLINK2_STATS_H__