aGrUM  0.20.2
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 57 of file chi2.cpp.

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

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

◆ ~Chi2()

gum::Chi2::~Chi2 ( )

Class destructor.

Definition at line 66 of file chi2.cpp.

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

66  {
67  // for debugging purposes
68  GUM_DESTRUCTOR(Chi2);
69  }
Chi2(const std::vector< std::size_t > &var_modalities, double confidence_proba=GUM_LEARNING_CONFIDENCE_PROBA)
Default constructor.
Definition: chi2.cpp:57
+ 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() [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 56 of file chi2_inl.h.

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

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

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

61  {
62  Size DF = degreesOfFreedom(var1, var2);
63 
64  // try to see if the threshold is not already in cache
65  try {
66  return critical_values__[DF];
67  } catch (const Exception&) {
68  // here we have to compute the threshold of the chi2
69  // we use Gary Perlman's algorithm
70  double value = criticalValue__(confidence_proba__, DF);
71  critical_values__.insert(DF, value);
72  return value;
73  }
74  }
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
static double criticalValue__(double proba, Size df)
Computes the critical value of a given chi2 test (used by the cache).
Definition: chi2.cpp:179
Size degreesOfFreedom(const std::pair< Idx, Idx > &pair)
Returns the number of degrees of freedom.
Definition: chi2_inl.h:45
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:

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

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

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

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

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

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

50  {
51  return (conditioning_size__ * (modalities__[var1] - 1)
52  * (modalities__[var2] - 1));
53  }
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 132 of file chi2.cpp.

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

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

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

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

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

37  {
39  for (Idx i = 0; i < db_conditioning_ids.size(); ++i) {
40  conditioning_size__ *= modalities__[db_conditioning_ids[i]];
41  }
42  }
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 77 of file chi2_inl.h.

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

77  {
78  // if we did not change the confidence proba, do nothing
79  if (confidence_proba__ == new_proba) return;
80 
81  confidence_proba__ = new_proba;
82 
83  // remove the currently stored critical values
85  }
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: