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

Represent the chi2 distribution. More...

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

Collaboration diagram for gum::Chi2:

Public Member Functions

Constructors / Destructors
 Chi2 (const std::vector< std::size_t > &var_modalities, double confidence_proba=GUM_LEARNING_CONFIDENCE_PROBA)
 Default constructor.
 ~Chi2 ()
 Class destructor.

Private Member Functions

 Chi2 (const Chi2 &)=delete
 Forbid use of the copy constructor.
Chi2operator= (const Chi2 &)=delete
 Forbid used of the copy operator.

Static Private Member Functions

static double _criticalValue_ (double proba, Size df)
 Computes the critical value of a given chi2 test (used by the cache).
static double _probaZValue_ (double z)
 Computes the probability of normal z value.

Private Attributes

const std::vector< std::size_t > & _modalities_
 The modalities of the random variables.
double _confidence_proba_
 The confidence probability used for critical values.
Size _conditioning_size_
 The domain size of the conditioning nodes.
HashTable< Idx, double_critical_values_
 A set of already computed critical values.

Accessors / Modifiers

void setConditioningNodes (const std::vector< Idx > &db_conditioning_ids)
 Sets the conditioning nodes (useful for computing degrees of freedom).
double criticalValue (const std::pair< Idx, Idx > &pair)
 Computes the critical value according to the number of degrees of freedom.
double criticalValue (Idx var1, Idx var2)
 Computes the critical value according to the number of degrees of freedom.
Size degreesOfFreedom (const std::pair< Idx, Idx > &pair)
 Returns the number of degrees of freedom.
Size degreesOfFreedom (Idx var1, Idx var2)
 Returns the number of degrees of freedom.
void setConfidenceProba (double new_proba)
 Modifies the confidence probability.
static double probaChi2 (double x, Size df)
 Computes the probability of chi2 value.

Detailed Description

Represent the chi2 distribution.

The class constructor receives a std::vector of unsigned integers. This std::vector represents the variables modalities. The variables indexes in the std::vector will be used as the variables identifiers.

Definition at line 79 of file chi2.h.

Constructor & Destructor Documentation

◆ Chi2() [1/2]

gum::Chi2::Chi2 ( const std::vector< std::size_t > & var_modalities,
double confidence_proba = GUM_LEARNING_CONFIDENCE_PROBA )

Default constructor.

Parameters
var_modalitiesThe variables modalities.
confidence_probaThe confidence probability.

Definition at line 76 of file chi2.cpp.

76 :
77 _modalities_(var_modalities),
78 _confidence_proba_(confidence_proba) { // for debugging purposes
79 GUM_CONSTRUCTOR(Chi2);
80 }
double _confidence_proba_
The confidence probability used for critical values.
Definition chi2.h:176
const std::vector< std::size_t > & _modalities_
The modalities of the random variables.
Definition chi2.h:173
Chi2(const std::vector< std::size_t > &var_modalities, double confidence_proba=GUM_LEARNING_CONFIDENCE_PROBA)
Default constructor.
Definition chi2.cpp:76

References Chi2(), _confidence_proba_, and _modalities_.

Referenced by Chi2(), Chi2(), ~Chi2(), and operator=().

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

◆ ~Chi2()

gum::Chi2::~Chi2 ( )

Class destructor.

Definition at line 83 of file chi2.cpp.

83 {
84 // for debugging purposes
85 GUM_DESTRUCTOR(Chi2);
86 }

References Chi2().

Here is the call graph for this function:

◆ Chi2() [2/2]

gum::Chi2::Chi2 ( const Chi2 & )
privatedelete

Forbid use of the copy constructor.

References Chi2().

Here is the call graph for this function:

Member Function Documentation

◆ _criticalValue_()

double gum::Chi2::_criticalValue_ ( double proba,
Size df )
staticprivate

Computes the critical value of a given chi2 test (used by the cache).

This code has been written by Gary Perlman.

Parameters
probaThe probability value.
dfThe number of degrees of freedom.
Returns
Returns the critical value of a given chi2 test.

Definition at line 190 of file chi2.cpp.

190 {
191 double minchisq = 0.0;
192 double maxchisq = GUM_CHI_MAX;
193 double chisqval;
194
195 if (proba <= 0.0) return (maxchisq);
196 else if (proba >= 1.0) return (0.0);
197
198 chisqval = df / std::sqrt(proba); /* fair first value */
199
200 while (maxchisq - minchisq > GUM_CHI_EPSILON) {
201 if (probaChi2(chisqval, df) < proba) maxchisq = chisqval;
202 else minchisq = chisqval;
203
204 chisqval = (maxchisq + minchisq) * 0.5;
205 }
206
207 return (chisqval);
208 }
static double probaChi2(double x, Size df)
Computes the probability of chi2 value.
Definition chi2.cpp:144

References probaChi2().

Referenced by criticalValue().

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

◆ _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 89 of file chi2.cpp.

89 {
90 double y, x, w;
91
92 if (z == 0.0) x = 0.0;
93 else {
94 y = 0.5 * std::fabs(z);
95
96 if (y >= (GUM_Z_MAX * 0.5)) x = 1.0;
97 else if (y < 1.0) {
98 w = y * y;
99 x = ((((((((0.000124818987 * w - 0.001075204047) * w + 0.005198775019) * w - 0.019198292004)
100 * w
101 + 0.059054035642)
102 * w
103 - 0.151968751364)
104 * w
105 + 0.319152932694)
106 * w
107 - 0.531923007300)
108 * w
109 + 0.797884560593)
110 * y * 2.0;
111 } else {
112 y -= 2.0;
113 x = (((((((((((((-0.000045255659 * y + 0.000152529290) * y - 0.000019538132) * y
114 - 0.000676904986)
115 * y
116 + 0.001390604284)
117 * y
118 - 0.000794620820)
119 * y
120 - 0.002034254874)
121 * y
122 + 0.006549791214)
123 * y
124 - 0.010557625006)
125 * y
126 + 0.011630447319)
127 * y
128 - 0.009279453341)
129 * y
130 + 0.005353579108)
131 * y
132 - 0.002141268741)
133 * y
134 + 0.000535310849)
135 * y
136 + 0.999936657524;
137 }
138 }
139
140 return (z > 0.0 ? ((x + 1.0) * 0.5) : ((1.0 - x) * 0.5));
141 }

Referenced by probaChi2().

Here is the caller graph for this function:

◆ criticalValue() [1/2]

ALWAYS_INLINE double gum::Chi2::criticalValue ( const std::pair< Idx, Idx > & pair)

Computes the critical value according to the number of degrees of freedom.

Parameters
pairA pair of variables ids.
Returns
Returns the critical values.

Definition at line 74 of file chi2_inl.h.

74 {
75 return criticalValue(pair.first, pair.second);
76 }
double criticalValue(const std::pair< Idx, Idx > &pair)
Computes the critical value according to the number of degrees of freedom.
Definition chi2_inl.h:74

References criticalValue().

Referenced by criticalValue().

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

◆ criticalValue() [2/2]

ALWAYS_INLINE double gum::Chi2::criticalValue ( Idx var1,
Idx var2 )

Computes the critical value according to the number of degrees of freedom.

Parameters
var1The first variable id.
var2The second variable id.
Returns
Returns the critical value.

Definition at line 79 of file chi2_inl.h.

79 {
80 Size DF = degreesOfFreedom(var1, var2);
81
82 // try to see if the threshold is not already in cache
83 try {
84 return _critical_values_[DF];
85 } catch (const Exception&) {
86 // here we have to compute the threshold of the chi2
87 // we use Gary Perlman's algorithm
88 double value = _criticalValue_(_confidence_proba_, DF);
89 _critical_values_.insert(DF, value);
90 return value;
91 }
92 }
HashTable< Idx, double > _critical_values_
A set of already computed critical values.
Definition chi2.h:182
Size degreesOfFreedom(const std::pair< Idx, Idx > &pair)
Returns the number of degrees of freedom.
Definition chi2_inl.h:64
static double _criticalValue_(double proba, Size df)
Computes the critical value of a given chi2 test (used by the cache).
Definition chi2.cpp:190
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74

References _confidence_proba_, _critical_values_, _criticalValue_(), and degreesOfFreedom().

Here is the call graph for this function:

◆ degreesOfFreedom() [1/2]

INLINE Size gum::Chi2::degreesOfFreedom ( const std::pair< Idx, Idx > & pair)

Returns the number of degrees of freedom.

Parameters
pairA pair of variables ids.
Returns
Returns the number of degrees of freedom.

Definition at line 64 of file chi2_inl.h.

64 {
65 return degreesOfFreedom(pair.first, pair.second);
66 }

References degreesOfFreedom().

Referenced by criticalValue(), and degreesOfFreedom().

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

◆ degreesOfFreedom() [2/2]

INLINE Size gum::Chi2::degreesOfFreedom ( Idx var1,
Idx var2 )

Returns the number of degrees of freedom.

Parameters
var1The first variable id.
var2The second variable id.
Returns
Returns the number of degrees of freedom.

Definition at line 69 of file chi2_inl.h.

69 {
70 return (_conditioning_size_ * (_modalities_[var1] - 1) * (_modalities_[var2] - 1));
71 }
Size _conditioning_size_
The domain size of the conditioning nodes.
Definition chi2.h:179

References _conditioning_size_, and _modalities_.

◆ operator=()

Chi2 & gum::Chi2::operator= ( const Chi2 & )
privatedelete

Forbid used of the copy operator.

References Chi2().

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 144 of file chi2.cpp.

144 {
145 double a, y = 0, s;
146 double e, c, z;
147 int even; /* true if df is an even number */
148
149 if ((x <= 0.0) || (df < 1)) return (1.0);
150
151 a = 0.5 * x;
152
153 even = (2 * (df / 2)) == df;
154
155 if (df > 1) y = _gum_ex(-a);
156
157 s = (even ? y : (2.0 * _probaZValue_(-std::sqrt(x))));
158
159 if (df > 2) {
160 x = 0.5 * (df - 1.0);
161 z = (even ? 1.0 : 0.5);
162
163 if (a > GUM_BIGX) {
164 e = (even ? 0.0 : GUM_LOG_SQRT_PI);
165 c = std::log(a);
166
167 while (z <= x) {
168 e = std::log(z) + e;
169 s += _gum_ex(c * z - a - e);
170 z += 1.0;
171 }
172
173 return (s);
174 } else {
175 e = (even ? 1.0 : (GUM_I_SQRT_PI / std::sqrt(a)));
176 c = 0.0;
177
178 while (z <= x) {
179 e = e * (a / z);
180 c = c + e;
181 z += 1.0;
182 }
183
184 return (c * y + s);
185 }
186 } else return (s);
187 }
static double _probaZValue_(double z)
Computes the probability of normal z value.
Definition chi2.cpp:89

References _probaZValue_().

Referenced by _criticalValue_().

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

◆ setConditioningNodes()

INLINE void gum::Chi2::setConditioningNodes ( const std::vector< Idx > & db_conditioning_ids)

Sets the conditioning nodes (useful for computing degrees of freedom).

Parameters
db_conditioning_idsThe conditioning nodes id.

Definition at line 56 of file chi2_inl.h.

56 {
58 for (Idx i = 0; i < db_conditioning_ids.size(); ++i) {
59 _conditioning_size_ *= _modalities_[db_conditioning_ids[i]];
60 }
61 }
Size Idx
Type for indexes.
Definition types.h:79

References _conditioning_size_, and _modalities_.

◆ setConfidenceProba()

INLINE void gum::Chi2::setConfidenceProba ( double new_proba)

Modifies the confidence probability.

Parameters
new_probaThe new confidence probability

Definition at line 95 of file chi2_inl.h.

95 {
96 // if we did not change the confidence proba, do nothing
97 if (_confidence_proba_ == new_proba) return;
98
99 _confidence_proba_ = new_proba;
100
101 // remove the currently stored critical values
102 _critical_values_.clear();
103 }

References _confidence_proba_, and _critical_values_.

Member Data Documentation

◆ _conditioning_size_

Size gum::Chi2::_conditioning_size_
private

The domain size of the conditioning nodes.

Definition at line 179 of file chi2.h.

Referenced by degreesOfFreedom(), and setConditioningNodes().

◆ _confidence_proba_

double gum::Chi2::_confidence_proba_
private

The confidence probability used for critical values.

Definition at line 176 of file chi2.h.

Referenced by Chi2(), criticalValue(), and setConfidenceProba().

◆ _critical_values_

HashTable< Idx, double > gum::Chi2::_critical_values_
private

A set of already computed critical values.

Definition at line 182 of file chi2.h.

Referenced by criticalValue(), and setConfidenceProba().

◆ _modalities_

const std::vector< std::size_t >& gum::Chi2::_modalities_
private

The modalities of the random variables.

Definition at line 173 of file chi2.h.

Referenced by Chi2(), degreesOfFreedom(), and setConditioningNodes().


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