aGrUM  0.14.1
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 55 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 55 of file chi2.cpp.

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

◆ ~Chi2()

gum::Chi2::~Chi2 ( )

Class destructor.

Definition at line 64 of file chi2.cpp.

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

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

References probaChi2().

Referenced by criticalValue().

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

Referenced by probaChi2().

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

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

◆ 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 __confidence_proba, __critical_values, __criticalValue(), degreesOfFreedom(), and gum::HashTable< Key, Val, Alloc >::insert().

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  }
static double __criticalValue(double proba, Size df)
Computes the critical value of a given chi2 test (used by the cache).
Definition: chi2.cpp:177
double __confidence_proba
The confidence probability used for critical values.
Definition: chi2.h:152
HashTable< Idx, double > __critical_values
A set of already computed critical values.
Definition: chi2.h:158
Size degreesOfFreedom(const std::pair< Idx, Idx > &pair)
Returns the number of degrees of freedom.
Definition: chi2_inl.h:43
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
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 43 of file chi2_inl.h.

Referenced by criticalValue().

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

References __conditioning_size, and __modalities.

48  {
49  return (__conditioning_size * (__modalities[var1] - 1)
50  * (__modalities[var2] - 1));
51  }
Size __conditioning_size
The domain size of the conditioning nodes.
Definition: chi2.h:155
const std::vector< std::size_t > & __modalities
The modalities of the random variables.
Definition: chi2.h:149

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

References __probaZValue().

Referenced by __criticalValue().

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

◆ 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 __confidence_proba, __critical_values, and gum::HashTable< Key, Val, Alloc >::clear().

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  }
double __confidence_proba
The confidence probability used for critical values.
Definition: chi2.h:152
HashTable< Idx, double > __critical_values
A set of already computed critical values.
Definition: chi2.h:158
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 155 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 152 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 158 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 149 of file chi2.h.

Referenced by degreesOfFreedom(), and setConditioningNodes().


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