aGrUM 3.0.0
a C++ library for (probabilistic) graphical models
gum::Chi2 Class Reference

Static math utilities for the chi2 distribution. More...

#include <agrum/base/core/math/chi2.h>

Static Public Member Functions

Static math utilities
static double probaChi2 (double x, Size df)
 Computes the probability of chi2 value.
static double criticalValue (double proba, Size df)
 Computes the critical chi2 value for a given confidence probability and number of degrees of freedom.

Private Member Functions

 Chi2 ()=delete

Static Private Member Functions

static double _probaZValue_ (double z)
 Computes the probability of normal z value.

Detailed Description

Static math utilities for the chi2 distribution.

Definition at line 73 of file chi2.h.

Constructor & Destructor Documentation

◆ Chi2()

gum::Chi2::Chi2 ( )
privatedelete

Member Function Documentation

◆ _probaZValue_()

double gum::Chi2::_probaZValue_ ( double z)
staticprivate

Computes the probability of normal z value.

This code has been written by Gary Perlman.

ALGORITHM Adapted from a polynomial approximation in: Ibbetson D, Algorithm 209 Collected Algorithms of the CACM 1963 p. 616

This routine has six digit accuracy, so it is only useful for absolute z values < 6. For z values >= to 6.0, probaZValue() returns 0.0.

Parameters
zA value.
Returns
The probability of z.

Definition at line 72 of file chi2.cpp.

72 {
73 double y, x, w;
74
75 if (z == 0.0) x = 0.0;
76 else {
77 y = 0.5 * std::fabs(z);
78
79 if (y >= (GUM_Z_MAX * 0.5)) x = 1.0;
80 else if (y < 1.0) {
81 w = y * y;
82 x = ((((((((0.000124818987 * w - 0.001075204047) * w + 0.005198775019) * w - 0.019198292004)
83 * w
84 + 0.059054035642)
85 * w
86 - 0.151968751364)
87 * w
88 + 0.319152932694)
89 * w
90 - 0.531923007300)
91 * w
92 + 0.797884560593)
93 * y * 2.0;
94 } else {
95 y -= 2.0;
96 x = (((((((((((((-0.000045255659 * y + 0.000152529290) * y - 0.000019538132) * y
97 - 0.000676904986)
98 * y
99 + 0.001390604284)
100 * y
101 - 0.000794620820)
102 * y
103 - 0.002034254874)
104 * y
105 + 0.006549791214)
106 * y
107 - 0.010557625006)
108 * y
109 + 0.011630447319)
110 * y
111 - 0.009279453341)
112 * y
113 + 0.005353579108)
114 * y
115 - 0.002141268741)
116 * y
117 + 0.000535310849)
118 * y
119 + 0.999936657524;
120 }
121 }
122
123 return (z > 0.0 ? ((x + 1.0) * 0.5) : ((1.0 - x) * 0.5));
124 }

Referenced by probaChi2().

Here is the caller graph for this function:

◆ criticalValue()

double gum::Chi2::criticalValue ( double proba,
Size df )
static

Computes the critical chi2 value for a given confidence probability and number of degrees of freedom.

This code has been written by Gary Perlman.

Parameters
probaThe confidence probability.
dfThe number of degrees of freedom.
Returns
The critical chi2 value.

Definition at line 173 of file chi2.cpp.

173 {
174 double minchisq = 0.0;
175 double maxchisq = GUM_CHI_MAX;
176 double chisqval;
177
178 if (df == 0) return (0.0);
179
180 if (proba <= 0.0) return (maxchisq);
181 else if (proba >= 1.0) return (0.0);
182
183 chisqval = df / std::sqrt(proba); /* fair first value */
184
185 while (maxchisq - minchisq > GUM_CHI_EPSILON) {
186 if (probaChi2(chisqval, df) < proba) maxchisq = chisqval;
187 else minchisq = chisqval;
188
189 chisqval = (maxchisq + minchisq) * 0.5;
190 }
191
192 return (chisqval);
193 }
static double probaChi2(double x, Size df)
Computes the probability of chi2 value.
Definition chi2.cpp:127

References probaChi2().

Here is the call graph for this function:

◆ probaChi2()

double gum::Chi2::probaChi2 ( double x,
Size df )
static

Computes the probability of chi2 value.

This code has been written by Gary Perlman.

ALGORITHM Compute probability of chi square value. Adapted from: Hill, I. D. and Pike, M. C. Algorithm 299 Collected Algorithms for the CACM 1967 p. 243 Updated for rounding errors based on remark in ACM TOMS June 1985, page 185

Parameters
xThe chi2 value.
dfThe number of degrees of freedom.
Returns
The probability of x given df degrees of freedom.

Definition at line 127 of file chi2.cpp.

127 {
128 double a, y = 0, s;
129 double e, c, z;
130 int even; /* true if df is an even number */
131
132 if ((x <= 0.0) || (df < 1)) return (1.0);
133
134 a = 0.5 * x;
135
136 even = (2 * (df / 2)) == df;
137
138 if (df > 1) y = _gum_ex(-a);
139
140 s = (even ? y : (2.0 * _probaZValue_(-std::sqrt(x))));
141
142 if (df > 2) {
143 x = 0.5 * (df - 1.0);
144 z = (even ? 1.0 : 0.5);
145
146 if (a > GUM_BIGX) {
147 e = (even ? 0.0 : GUM_LOG_SQRT_PI);
148 c = std::log(a);
149
150 while (z <= x) {
151 e = std::log(z) + e;
152 s += _gum_ex(c * z - a - e);
153 z += 1.0;
154 }
155
156 return (s);
157 } else {
158 e = (even ? 1.0 : (GUM_I_SQRT_PI / std::sqrt(a)));
159 c = 0.0;
160
161 while (z <= x) {
162 e = e * (a / z);
163 c = c + e;
164 z += 1.0;
165 }
166
167 return (c * y + s);
168 }
169 } else return (s);
170 }
static double _probaZValue_(double z)
Computes the probability of normal z value.
Definition chi2.cpp:72

References _probaZValue_().

Referenced by criticalValue().

Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following files: