aGrUM  0.13.2
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< Idx > &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...
 

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 56 of file chi2.h.

Constructor & Destructor Documentation

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

Default constructor.

Parameters
var_modalitiesThe variables modalities.
confidence_probaThe confidence probability.

Definition at line 55 of file chi2.cpp.

55  :
56  __modalities(var_modalities), __confidence_proba(confidence_proba) {
57  // for debugging purposes
58  GUM_CONSTRUCTOR(Chi2);
59  }
double __confidence_proba
The confidence probability used for critical values.
Definition: chi2.h:135
Chi2(const std::vector< Idx > &var_modalities, double confidence_proba=GUM_LEARNING_CONFIDENCE_PROBA)
Default constructor.
Definition: chi2.cpp:55
const std::vector< Idx > & __modalities
The modalities of the random variables.
Definition: chi2.h:132
gum::Chi2::~Chi2 ( )

Class destructor.

Definition at line 62 of file chi2.cpp.

62  {
63  // for debugging purposes
64  GUM_DESTRUCTOR(Chi2);
65  }
Chi2(const std::vector< Idx > &var_modalities, double confidence_proba=GUM_LEARNING_CONFIDENCE_PROBA)
Default constructor.
Definition: chi2.cpp:55
gum::Chi2::Chi2 ( const Chi2 )
privatedelete

Forbid use of the copy constructor.

Member Function Documentation

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

References __probaChi2().

Referenced by criticalValue().

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

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

double gum::Chi2::__probaChi2 ( double  x,
Size  df 
)
staticprivate

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

References __probaZValue().

Referenced by __criticalValue().

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

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Referenced by __probaChi2().

68  {
69  double y, x, w;
70 
71  if (z == 0.0)
72  x = 0.0;
73  else {
74  y = 0.5 * std::fabs(z);
75 
76  if (y >= (GUM_Z_MAX * 0.5))
77  x = 1.0;
78  else if (y < 1.0) {
79  w = y * y;
80  x = ((((((((0.000124818987 * w - 0.001075204047) * w + 0.005198775019) * w
81  - 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 =
96  (((((((((((((-0.000045255659 * y + 0.000152529290) * y - 0.000019538132)
97  * y
98  - 0.000676904986)
99  * y
100  + 0.001390604284)
101  * y
102  - 0.000794620820)
103  * y
104  - 0.002034254874)
105  * y
106  + 0.006549791214)
107  * y
108  - 0.010557625006)
109  * y
110  + 0.011630447319)
111  * y
112  - 0.009279453341)
113  * y
114  + 0.005353579108)
115  * y
116  - 0.002141268741)
117  * y
118  + 0.000535310849)
119  * y
120  + 0.999936657524;
121  }
122  }
123 
124  return (z > 0.0 ? ((x + 1.0) * 0.5) : ((1.0 - x) * 0.5));
125  }

+ Here is the caller graph for this function:

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

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

55  {
56  Idx DF = degreesOfFreedom(pair);
57 
58  // try to see if the threshold is not already in cache
59  try {
60  return __critical_values[DF];
61  } catch (const Exception&) {
62  // here we have to compute the threshold of the chi2
63  // we use Gary Perlman's algorithm
64  double value = __criticalValue(__confidence_proba, DF);
65  __critical_values.insert(DF, value);
66  return value;
67  }
68  }
static double __criticalValue(double proba, Size df)
Computes the critical value of a given chi2 test (used by the cache).
Definition: chi2.cpp:175
double __confidence_proba
The confidence probability used for critical values.
Definition: chi2.h:135
HashTable< Idx, double > __critical_values
A set of already computed critical values.
Definition: chi2.h:141
Size degreesOfFreedom(const std::pair< Idx, Idx > &pair)
Returns the number of degrees of freedom.
Definition: chi2_inl.h:43
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
unsigned long Idx
Type for indexes.
Definition: types.h:43

+ Here is the call graph for this function:

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

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

71  {
72  Size DF = degreesOfFreedom(var1, var2);
73 
74  // try to see if the threshold is not already in cache
75  try {
76  return __critical_values[DF];
77  } catch (const Exception&) {
78  // here we have to compute the threshold of the chi2
79  // we use Gary Perlman's algorithm
80  double value = __criticalValue(__confidence_proba, DF);
81  __critical_values.insert(DF, value);
82  return value;
83  }
84  }
unsigned long Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:50
static double __criticalValue(double proba, Size df)
Computes the critical value of a given chi2 test (used by the cache).
Definition: chi2.cpp:175
double __confidence_proba
The confidence probability used for critical values.
Definition: chi2.h:135
HashTable< Idx, double > __critical_values
A set of already computed critical values.
Definition: chi2.h:141
Size degreesOfFreedom(const std::pair< Idx, Idx > &pair)
Returns the number of degrees of freedom.
Definition: chi2_inl.h:43
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:

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

References __conditioning_size, and __modalities.

Referenced by criticalValue().

43  {
44  return (__conditioning_size * (__modalities[pair.first] - 1)
45  * (__modalities[pair.second] - 1));
46  }
Size __conditioning_size
The domain size of the conditioning nodes.
Definition: chi2.h:138
const std::vector< Idx > & __modalities
The modalities of the random variables.
Definition: chi2.h:132

+ Here is the caller graph for this function:

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 __conditioning_size, and __modalities.

49  {
50  return (__conditioning_size * (__modalities[var1] - 1)
51  * (__modalities[var2] - 1));
52  }
Size __conditioning_size
The domain size of the conditioning nodes.
Definition: chi2.h:138
const std::vector< Idx > & __modalities
The modalities of the random variables.
Definition: chi2.h:132
Chi2& gum::Chi2::operator= ( const Chi2 )
privatedelete

Forbid used of the copy operator.

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

References __conditioning_size, and __modalities.

35  {
37  for (Idx i = 0; i < db_conditioning_ids.size(); ++i) {
38  __conditioning_size *= __modalities[db_conditioning_ids[i]];
39  }
40  }
Size __conditioning_size
The domain size of the conditioning nodes.
Definition: chi2.h:138
unsigned long Idx
Type for indexes.
Definition: types.h:43
const std::vector< Idx > & __modalities
The modalities of the random variables.
Definition: chi2.h:132
INLINE void gum::Chi2::setConfidenceProba ( double  new_proba)

Modifies the confidence probability.

Parameters
new_probaThe new confidence probability

Definition at line 87 of file chi2_inl.h.

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

87  {
88  // if we did not change the confidence proba, do nothing
89  if (__confidence_proba == new_proba) return;
90 
91  __confidence_proba = new_proba;
92 
93  // remove the currently stored critical values
95  }
double __confidence_proba
The confidence probability used for critical values.
Definition: chi2.h:135
HashTable< Idx, double > __critical_values
A set of already computed critical values.
Definition: chi2.h:141
void clear()
Removes all the elements in the hash table.

+ Here is the call graph for this function:

Member Data Documentation

Size gum::Chi2::__conditioning_size
private

The domain size of the conditioning nodes.

Definition at line 138 of file chi2.h.

Referenced by degreesOfFreedom(), and setConditioningNodes().

double gum::Chi2::__confidence_proba
private

The confidence probability used for critical values.

Definition at line 135 of file chi2.h.

Referenced by criticalValue(), and setConfidenceProba().

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

A set of already computed critical values.

Definition at line 141 of file chi2.h.

Referenced by criticalValue(), and setConfidenceProba().

const std::vector< Idx >& gum::Chi2::__modalities
private

The modalities of the random variables.

Definition at line 132 of file chi2.h.

Referenced by degreesOfFreedom(), and setConditioningNodes().


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