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

Represent the chi2 distribution. More...

#include <agrum/tools/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 57 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 56 of file chi2.cpp.

References gum::Set< Key, Alloc >::emplace().

56  :
57  _modalities_(var_modalities),
58  _confidence_proba_(confidence_proba) { // for debugging purposes
59  GUM_CONSTRUCTOR(Chi2);
60  }
double _confidence_proba_
The confidence probability used for critical values.
Definition: chi2.h:154
Chi2(const std::vector< std::size_t > &var_modalities, double confidence_proba=GUM_LEARNING_CONFIDENCE_PROBA)
Default constructor.
Definition: chi2.cpp:56
const std::vector< std::size_t > & _modalities_
The modalities of the random variables.
Definition: chi2.h:151
+ Here is the call graph for this function:

◆ ~Chi2()

gum::Chi2::~Chi2 ( )

Class destructor.

Definition at line 63 of file chi2.cpp.

References gum::Set< Key, Alloc >::emplace().

63  {
64  // for debugging purposes
65  GUM_DESTRUCTOR(Chi2);
66  }
Chi2(const std::vector< std::size_t > &var_modalities, double confidence_proba=GUM_LEARNING_CONFIDENCE_PROBA)
Default constructor.
Definition: chi2.cpp:56
+ Here is the call graph for this function:

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

References gum::Set< Key, Alloc >::emplace().

173  {
174  double minchisq = 0.0;
175  double maxchisq = GUM_CHI_MAX;
176  double chisqval;
177 
178  if (proba <= 0.0)
179  return (maxchisq);
180  else if (proba >= 1.0)
181  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)
187  maxchisq = chisqval;
188  else
189  minchisq = chisqval;
190 
191  chisqval = (maxchisq + minchisq) * 0.5;
192  }
193 
194  return (chisqval);
195  }
static double probaChi2(double x, Size df)
Computes the probability of chi2 value.
Definition: chi2.cpp:126
+ Here is the call 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 69 of file chi2.cpp.

References gum::Set< Key, Alloc >::emplace().

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

References gum::Set< Key, Alloc >::emplace().

54  {
55  return criticalValue(pair.first, pair.second);
56  }
double criticalValue(const std::pair< Idx, Idx > &pair)
Computes the critical value according to the number of degrees of freedom.
Definition: chi2_inl.h:54
+ Here is the call 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 59 of file chi2_inl.h.

References gum::Set< Key, Alloc >::emplace().

59  {
60  Size DF = degreesOfFreedom(var1, var2);
61 
62  // try to see if the threshold is not already in cache
63  try {
64  return _critical_values_[DF];
65  } catch (const Exception&) {
66  // here we have to compute the threshold of the chi2
67  // we use Gary Perlman's algorithm
68  double value = _criticalValue_(_confidence_proba_, DF);
69  _critical_values_.insert(DF, value);
70  return value;
71  }
72  }
HashTable< Idx, double > _critical_values_
A set of already computed critical values.
Definition: chi2.h:160
Size degreesOfFreedom(const std::pair< Idx, Idx > &pair)
Returns the number of degrees of freedom.
Definition: chi2_inl.h:44
static double _criticalValue_(double proba, Size df)
Computes the critical value of a given chi2 test (used by the cache).
Definition: chi2.cpp:173
double _confidence_proba_
The confidence probability used for critical values.
Definition: chi2.h:154
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
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 44 of file chi2_inl.h.

References gum::Set< Key, Alloc >::emplace().

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

References gum::Set< Key, Alloc >::emplace().

49  {
50  return (_conditioning_size_ * (_modalities_[var1] - 1) * (_modalities_[var2] - 1));
51  }
Size _conditioning_size_
The domain size of the conditioning nodes.
Definition: chi2.h:157
const std::vector< std::size_t > & _modalities_
The modalities of the random variables.
Definition: chi2.h:151
+ Here is the call graph for this function:

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

References gum::Set< Key, Alloc >::emplace().

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

References gum::Set< Key, Alloc >::emplace().

36  {
38  for (Idx i = 0; i < db_conditioning_ids.size(); ++i) {
39  _conditioning_size_ *= _modalities_[db_conditioning_ids[i]];
40  }
41  }
Size _conditioning_size_
The domain size of the conditioning nodes.
Definition: chi2.h:157
const std::vector< std::size_t > & _modalities_
The modalities of the random variables.
Definition: chi2.h:151
+ Here is the call graph for this function:

◆ setConfidenceProba()

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

Modifies the confidence probability.

Parameters
new_probaThe new confidence probability

Definition at line 75 of file chi2_inl.h.

References gum::Set< Key, Alloc >::emplace().

75  {
76  // if we did not change the confidence proba, do nothing
77  if (_confidence_proba_ == new_proba) return;
78 
79  _confidence_proba_ = new_proba;
80 
81  // remove the currently stored critical values
83  }
HashTable< Idx, double > _critical_values_
A set of already computed critical values.
Definition: chi2.h:160
double _confidence_proba_
The confidence probability used for critical values.
Definition: chi2.h:154
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 157 of file chi2.h.

◆ _confidence_proba_

double gum::Chi2::_confidence_proba_
private

The confidence probability used for critical values.

Definition at line 154 of file chi2.h.

◆ _critical_values_

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

A set of already computed critical values.

Definition at line 160 of file chi2.h.

◆ _modalities_

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

The modalities of the random variables.

Definition at line 151 of file chi2.h.


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