aGrUM  0.16.0
gum::Chi2 Class Reference

Represent the chi2 distribution. More...

#include <agrum/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. More...
 
 ~Chi2 ()
 Class destructor. More...
 

Accessors / Modifiers

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

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

59  :
60  __modalities(var_modalities),
61  __confidence_proba(confidence_proba) {
62  // for debugging purposes
63  GUM_CONSTRUCTOR(Chi2);
64  }
double __confidence_proba
The confidence probability used for critical values.
Definition: chi2.h:155
Chi2(const std::vector< std::size_t > &var_modalities, double confidence_proba=GUM_LEARNING_CONFIDENCE_PROBA)
Default constructor.
Definition: chi2.cpp:58
const std::vector< std::size_t > & __modalities
The modalities of the random variables.
Definition: chi2.h:152

◆ ~Chi2()

gum::Chi2::~Chi2 ( )

Class destructor.

Definition at line 67 of file chi2.cpp.

67  {
68  // for debugging purposes
69  GUM_DESTRUCTOR(Chi2);
70  }
Chi2(const std::vector< std::size_t > &var_modalities, double confidence_proba=GUM_LEARNING_CONFIDENCE_PROBA)
Default constructor.
Definition: chi2.cpp:58

◆ Chi2() [2/2]

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

Forbid use of the copy constructor.

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

References probaChi2().

Referenced by criticalValue().

180  {
181  double minchisq = 0.0;
182  double maxchisq = GUM_CHI_MAX;
183  double chisqval;
184 
185  if (proba <= 0.0)
186  return (maxchisq);
187  else if (proba >= 1.0)
188  return (0.0);
189 
190  chisqval = df / std::sqrt(proba); /* fair first value */
191 
192  while (maxchisq - minchisq > GUM_CHI_EPSILON) {
193  if (probaChi2(chisqval, df) < proba)
194  maxchisq = chisqval;
195  else
196  minchisq = chisqval;
197 
198  chisqval = (maxchisq + minchisq) * 0.5;
199  }
200 
201  return (chisqval);
202  }
static double probaChi2(double x, Size df)
Computes the probability of chi2 value.
Definition: chi2.cpp:133
+ 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 73 of file chi2.cpp.

Referenced by probaChi2().

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

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

◆ 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 62 of file chi2_inl.h.

References __confidence_proba, __critical_values, __criticalValue(), degreesOfFreedom(), and gum::HashTable< Key, Val, Alloc >::insert().

62  {
63  Size DF = degreesOfFreedom(var1, var2);
64 
65  // try to see if the threshold is not already in cache
66  try {
67  return __critical_values[DF];
68  } catch (const Exception&) {
69  // here we have to compute the threshold of the chi2
70  // we use Gary Perlman's algorithm
71  double value = __criticalValue(__confidence_proba, DF);
72  __critical_values.insert(DF, value);
73  return value;
74  }
75  }
static double __criticalValue(double proba, Size df)
Computes the critical value of a given chi2 test (used by the cache).
Definition: chi2.cpp:180
double __confidence_proba
The confidence probability used for critical values.
Definition: chi2.h:155
HashTable< Idx, double > __critical_values
A set of already computed critical values.
Definition: chi2.h:161
Size degreesOfFreedom(const std::pair< Idx, Idx > &pair)
Returns the number of degrees of freedom.
Definition: chi2_inl.h:46
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
+ 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 46 of file chi2_inl.h.

Referenced by criticalValue().

46  {
47  return degreesOfFreedom(pair.first, pair.second);
48  }
Size degreesOfFreedom(const std::pair< Idx, Idx > &pair)
Returns the number of degrees of freedom.
Definition: chi2_inl.h:46
+ 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 51 of file chi2_inl.h.

References __conditioning_size, and __modalities.

51  {
52  return (__conditioning_size * (__modalities[var1] - 1)
53  * (__modalities[var2] - 1));
54  }
Size __conditioning_size
The domain size of the conditioning nodes.
Definition: chi2.h:158
const std::vector< std::size_t > & __modalities
The modalities of the random variables.
Definition: chi2.h:152

◆ operator=()

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

Forbid used of the copy operator.

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

References __probaZValue().

Referenced by __criticalValue().

133  {
134  double a, y = 0, s;
135  double e, c, z;
136  int even; /* true if df is an even number */
137 
138  if ((x <= 0.0) || (df < 1)) return (1.0);
139 
140  a = 0.5 * x;
141 
142  even = (2 * (df / 2)) == df;
143 
144  if (df > 1) y = gum__ex(-a);
145 
146  s = (even ? y : (2.0 * __probaZValue(-std::sqrt(x))));
147 
148  if (df > 2) {
149  x = 0.5 * (df - 1.0);
150  z = (even ? 1.0 : 0.5);
151 
152  if (a > GUM_BIGX) {
153  e = (even ? 0.0 : GUM_LOG_SQRT_PI);
154  c = std::log(a);
155 
156  while (z <= x) {
157  e = std::log(z) + e;
158  s += gum__ex(c * z - a - e);
159  z += 1.0;
160  }
161 
162  return (s);
163  } else {
164  e = (even ? 1.0 : (GUM_I_SQRT_PI / std::sqrt(a)));
165  c = 0.0;
166 
167  while (z <= x) {
168  e = e * (a / z);
169  c = c + e;
170  z += 1.0;
171  }
172 
173  return (c * y + s);
174  }
175  } else
176  return (s);
177  }
static double __probaZValue(double z)
Computes the probability of normal z value.
Definition: chi2.cpp:73
+ 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 38 of file chi2_inl.h.

References __conditioning_size, and __modalities.

38  {
40  for (Idx i = 0; i < db_conditioning_ids.size(); ++i) {
41  __conditioning_size *= __modalities[db_conditioning_ids[i]];
42  }
43  }
Size __conditioning_size
The domain size of the conditioning nodes.
Definition: chi2.h:158
const std::vector< std::size_t > & __modalities
The modalities of the random variables.
Definition: chi2.h:152

◆ setConfidenceProba()

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

Modifies the confidence probability.

Parameters
new_probaThe new confidence probability

Definition at line 78 of file chi2_inl.h.

References __confidence_proba, __critical_values, and gum::HashTable< Key, Val, Alloc >::clear().

78  {
79  // if we did not change the confidence proba, do nothing
80  if (__confidence_proba == new_proba) return;
81 
82  __confidence_proba = new_proba;
83 
84  // remove the currently stored critical values
86  }
double __confidence_proba
The confidence probability used for critical values.
Definition: chi2.h:155
HashTable< Idx, double > __critical_values
A set of already computed critical values.
Definition: chi2.h:161
void clear()
Removes all the elements in the hash table.
+ Here is the call graph for this function:

Member Data Documentation

◆ __conditioning_size

Size gum::Chi2::__conditioning_size
private

The domain size of the conditioning nodes.

Definition at line 158 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 155 of file chi2.h.

Referenced by criticalValue(), and setConfidenceProba().

◆ __critical_values

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

A set of already computed critical values.

Definition at line 161 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 152 of file chi2.h.

Referenced by degreesOfFreedom(), and setConditioningNodes().


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