SDDSlib
Loading...
Searching...
No Matches
sigLevel.c
Go to the documentation of this file.
1/**
2 * @file sigLevel.c
3 * @brief Routines for Student's t distribution, the F distribution, the linear-correlation coefficient distribution, and Poisson distribution.
4 *
5 * @copyright
6 * - (c) 2002 The University of Chicago, as Operator of Argonne National Laboratory.
7 * - (c) 2002 The Regents of the University of California, as Operator of Los Alamos National Laboratory.
8 *
9 * @license
10 * This file is distributed under the terms of the Software License Agreement
11 * found in the file LICENSE included with this distribution.
12 *
13 * @author M. Borland, C. Saunders, R. Soliday
14 */
15
16#include "mdb.h"
17
18#if defined(_WIN32) && !defined(__MINGW32__)
19# if _MSC_VER <= 1600
20# include "fdlibm.h"
21# define isnan(x) _isnan(x)
22# endif
23#endif
24
25#if defined(vxWorks) /* This may not be very compatible */
26# include <private/mathP.h>
27# define isnan(x) isNan(x)
28#endif
29
30/**
31 * @brief Computes the probability that a standard normal variable exceeds a given value.
32 *
33 * This function calculates the probability that a standard normal random variable Z satisfies Z > z0.
34 *
35 * @param z0 The value for which the probability is computed.
36 * @param tails Specifies whether to compute a one-tailed (1) or two-tailed (2) probability.
37 *
38 * @return The probability that Z exceeds z0. Returns -1 if the number of tails is invalid.
39 */
40double normSigLevel(double z0, long tails)
41{
42 if (z0 < 0)
43 z0 = -z0;
44 if (tails != 1 && tails != 2)
45 return -1;
46#if defined(vxWorks)
47 fprintf(stderr, "erfc function is not implemented on vxWorks\n");
48 exit(1);
49#else
50 return erfc(z0 / SQRT2) / (tails == 1 ? 2 : 1);
51#endif
52}
53
54/**
55 * @brief Computes the probability that a chi-squared variable exceeds a given value.
56 *
57 * This function calculates the probability that a chi-squared random variable with `nu` degrees of freedom exceeds `ChiSquared0`.
58 *
59 * @param ChiSquared0 The chi-squared value for which the probability is computed.
60 * @param nu The degrees of freedom of the chi-squared distribution.
61 *
62 * @return The probability that the chi-squared variable exceeds `ChiSquared0` for `nu` degrees of freedom. Returns -1 if `ChiSquared0` is negative.
63 */
64double ChiSqrSigLevel(double ChiSquared0, long nu)
65{
66 if (ChiSquared0 < 0)
67 return -1;
68 return gammaQ(nu / 2.0, ChiSquared0 / 2);
69}
70
71/**
72 * @brief Computes the probability that the absolute value of a t-distributed variable exceeds a given value.
73 *
74 * This function calculates the probability that |t| > t0 for a t-distribution with `nu` degrees of freedom.
75 *
76 * @param t0 The t-value for which the probability is computed.
77 * @param nu The degrees of freedom of the t-distribution.
78 * @param tails Specifies whether to compute a one-tailed (1) or two-tailed (2) probability.
79 *
80 * @return The probability that |t| exceeds `t0` for `nu` degrees of freedom. Returns -1 if the number of tails is invalid.
81 */
82double tTailSigLevel(double t0, long nu, long tails)
83{
84 if (tails != 1 && tails != 2)
85 return -1;
86 return betaInc(nu / 2.0, 0.5, nu / (nu + t0 * t0)) / (tails == 1 ? 2 : 1);
87}
88
89/**
90 * @brief Computes the probability that an F-distributed variable exceeds a given value.
91 *
92 * This function calculates the probability that an F-distributed random variable with `nu1` and `nu2` degrees of freedom exceeds F0, where F0 is defined as Max(var1, var2) / Min(var1, var2).
93 *
94 * @param var1 The first variance.
95 * @param var2 The second variance.
96 * @param nu1 The degrees of freedom associated with `var1`.
97 * @param nu2 The degrees of freedom associated with `var2`.
98 *
99 * @return The probability that F exceeds F0 for `nu1` and `nu2` degrees of freedom.
100 */
101double FSigLevel(double var1, double var2, long nu1, long nu2)
102{
103 if (var1 < var2) {
104 SWAP_DOUBLE(var1, var2);
105 SWAP_LONG(nu1, nu2);
106 }
107 return betaInc(nu2 / 2.0, nu1 / 2.0, nu2 / (nu2 + nu1 * var1 / var2));
108}
109
110/**
111 * @brief Computes the probability that the linear correlation coefficient exceeds a given value.
112 *
113 * This function calculates the probability that the linear correlation coefficient `r` exceeds |r0| for `nu` degrees of freedom.
114 *
115 * @param r0 The correlation coefficient value.
116 * @param nu The degrees of freedom.
117 *
118 * @return The probability that the linear correlation coefficient exceeds |r0| for `nu` degrees of freedom. Returns -1 if `nu` is less than 2 or if `r0` is not in the valid range.
119 */
120double rSigLevel(double r0, long nu)
121{
122 if (r0 < 0)
123 r0 = -r0;
124 if (nu < 2)
125 return -1;
126 if (r0 >= 1)
127 return 0;
128 return tTailSigLevel(r0 * sqrt(nu / (1 - r0 * r0)), nu, 2);
129}
130
131/**
132 * @brief Computes the probability that a Poisson-distributed random variable exceeds or equals a given value.
133 *
134 * This function calculates the probability that the number of events `n` is greater than or equal to a specified value `n0`, given a Poisson distribution with an expected number of events `n0`.
135 *
136 * @param n The number of events observed.
137 * @param n0 The expected number of events.
138 *
139 * @return The probability that `n` or more events are observed given a Poisson distribution with `n0` expected events.
140 */
141double poissonSigLevel(long n, double n0)
142{
143 double sum, term, result = 0;
144 long i;
145 if (n == 0)
146 return 1;
147 if (n < 0 || n0 <= 0) {
148 return n0 < n ? 0 : 1;
149 }
150 if (n0 > 200) {
151 result = normSigLevel((n - n0) / sqrt(n0), 1);
152 if (n < n0)
153 return 1 - result;
154 return result;
155 }
156 if (!exp(-n0))
157 return n0 < n ? 0 : 1;
158 sum = 1;
159 term = 1;
160 i = 0;
161 while (++i < n) {
162 term *= n0 / i;
163 sum += term;
164 }
165 result = 1 - sum * exp(-n0);
166 if (isnan(result)) {
167 return n0 < n ? 0 : 1;
168 }
169 if (result < 0)
170 return 0;
171 return result;
172}
double betaInc(double a, double b, double x)
Compute the incomplete beta function.
Definition betai.c:67
double gammaQ(double a, double x)
Compute the regularized upper incomplete gamma function Q(a,x).
Definition gammai.c:64
double ChiSqrSigLevel(double ChiSquared0, long nu)
Computes the probability that a chi-squared variable exceeds a given value.
Definition sigLevel.c:64
double tTailSigLevel(double t0, long nu, long tails)
Computes the probability that the absolute value of a t-distributed variable exceeds a given value.
Definition sigLevel.c:82
double normSigLevel(double z0, long tails)
Computes the probability that a standard normal variable exceeds a given value.
Definition sigLevel.c:40
double rSigLevel(double r0, long nu)
Computes the probability that the linear correlation coefficient exceeds a given value.
Definition sigLevel.c:120
double poissonSigLevel(long n, double n0)
Computes the probability that a Poisson-distributed random variable exceeds or equals a given value.
Definition sigLevel.c:141
double FSigLevel(double var1, double var2, long nu1, long nu2)
Computes the probability that an F-distributed variable exceeds a given value.
Definition sigLevel.c:101