aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
chi2.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief The class that represents the chi2 distribution
25  *
26  * The Chi2 class allows to easily compute critical values for the Chi2
27  * distribution.
28  *
29  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
30  */
31 
32 #ifndef GUM_LEARNING_CHI2_H
33 #define GUM_LEARNING_CHI2_H
34 
35 #include <agrum/agrum.h>
36 #include <agrum/tools/core/math/math_utils.h>
37 #include <agrum/tools/core/hashTable.h>
38 
39 #define GUM_LEARNING_CONFIDENCE_PROBA 0.05
40 
41 namespace gum {
42 
43  // =========================================================================
44  // === CHI2 CLASS ===
45  // =========================================================================
46 
47  /**
48  * @class Chi2
49  * @headerfile chi2.h <agrum/tools/core/math/chi2.h>
50  * @brief Represent the chi2 distribution.
51  * @ingroup math_group
52  *
53  * The class constructor receives a std::vector of unsigned integers. This
54  * std::vector represents the variables modalities. The variables indexes in
55  * the std::vector will be used as the variables identifiers.
56  */
57  class Chi2 {
58  public:
59  // ==========================================================================
60  /// @name Constructors / Destructors
61  // ==========================================================================
62  /// @{
63 
64  /**
65  * @brief Default constructor.
66  * @param var_modalities The variables modalities.
67  * @param confidence_proba The confidence probability.
68  */
69  Chi2(const std::vector< std::size_t >& var_modalities,
70  double confidence_proba = GUM_LEARNING_CONFIDENCE_PROBA);
71 
72  /**
73  * @brief Class destructor.
74  */
75  ~Chi2();
76 
77  /// @}
78  // ==========================================================================
79  /// @name Accessors / Modifiers
80  // ==========================================================================
81  /// @{
82 
83  /**
84  * @brief Sets the conditioning nodes (useful for computing degrees of
85  * freedom).
86  *
87  * @param db_conditioning_ids The conditioning nodes id.
88  */
89  void setConditioningNodes(const std::vector< Idx >& db_conditioning_ids);
90 
91  /**
92  * @brief Computes the critical value according to the number of degrees of
93  * freedom.
94  * @param pair A pair of variables ids.
95  * @return Returns the critical values.
96  */
97  double criticalValue(const std::pair< Idx, Idx >& pair);
98 
99  /**
100  * @brief Computes the critical value according to the number of degrees of
101  * freedom.
102  * @param var1 The first variable id.
103  * @param var2 The second variable id.
104  * @return Returns the critical value.
105  */
106  double criticalValue(Idx var1, Idx var2);
107 
108  /**
109  * @brief Returns the number of degrees of freedom.
110  * @param pair A pair of variables ids.
111  * @return Returns the number of degrees of freedom.
112  */
113  Size degreesOfFreedom(const std::pair< Idx, Idx >& pair);
114 
115  /**
116  * @brief Returns the number of degrees of freedom.
117  * @param var1 The first variable id.
118  * @param var2 The second variable id.
119  * @return Returns the number of degrees of freedom.
120  */
122 
123  /**
124  * @brief Modifies the confidence probability.
125  * @param new_proba The new confidence probability
126  */
127  void setConfidenceProba(double new_proba);
128 
129  /**
130  * @brief Computes the probability of chi2 value.
131  *
132  * This code has been written by Gary Perlman.
133  *
134  * ALGORITHM Compute probability of chi square value.
135  * Adapted from:
136  * Hill, I. D. and Pike, M. C. Algorithm 299
137  * Collected Algorithms for the CACM 1967 p. 243
138  * Updated for rounding errors based on remark in
139  * ACM TOMS June 1985, page 185
140  *
141  * @param x The chi2 value.
142  * @param df The number of degrees of freedom.
143  * @return The probability of x given df degrees of freedom.
144  */
145 
146  static double probaChi2(double x, Size df);
147  /// @}
148 
149  private:
150  /// The modalities of the random variables.
152 
153  /// The confidence probability used for critical values.
155 
156  /// The domain size of the conditioning nodes.
158 
159  /// A set of already computed critical values.
161 
162  /**
163  * @brief Computes the critical value of a given chi2 test (used by the
164  * cache).
165  *
166  * This code has been written by Gary Perlman.
167  *
168  * @param proba The probability value.
169  * @param df The number of degrees of freedom.
170  * @return Returns the critical value of a given chi2 test.
171  */
172  static double _criticalValue_(double proba, Size df);
173 
174 
175  /**
176  * @brief Computes the probability of normal z value.
177  *
178  * This code has been written by Gary Perlman.
179  *
180  * ALGORITHM Adapted from a polynomial approximation in:
181  * Ibbetson D, Algorithm 209
182  * Collected Algorithms of the CACM 1963 p. 616
183  *
184  * This routine has six digit accuracy, so it is only useful for absolute z
185  * values < 6. For z values >= to 6.0, _probaZValue_() returns 0.0.
186  *
187  * @param z A value.
188  * @return The probability of z.
189  */
190  static double _probaZValue_(double z);
191 
192  /// Forbid use of the copy constructor.
193  Chi2(const Chi2&) = delete;
194 
195  /// Forbid used of the copy operator.
196  Chi2& operator=(const Chi2&) = delete;
197  };
198 
199 } /* namespace gum */
200 
201 /// include the inlined functions if necessary
202 #ifndef GUM_NO_INLINE
203 # include <agrum/tools/core/math/chi2_inl.h>
204 #endif /* GUM_NO_INLINE */
205 
206 #endif /* GUM_LEARNING_CHI2_H */
static double _probaZValue_(double z)
Computes the probability of normal z value.
Definition: chi2.cpp:69
void setConditioningNodes(const std::vector< Idx > &db_conditioning_ids)
Sets the conditioning nodes (useful for computing degrees of freedom).
Definition: chi2_inl.h:36
HashTable< Idx, double > _critical_values_
A set of already computed critical values.
Definition: chi2.h:160
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
#define GUM_LEARNING_CONFIDENCE_PROBA
Definition: chi2.h:39
~Chi2()
Class destructor.
Definition: chi2.cpp:63
Size degreesOfFreedom(Idx var1, Idx var2)
Returns the number of degrees of freedom.
Definition: chi2_inl.h:49
Represent the chi2 distribution.
Definition: chi2.h:57
Size degreesOfFreedom(const std::pair< Idx, Idx > &pair)
Returns the number of degrees of freedom.
Definition: chi2_inl.h:44
double criticalValue(Idx var1, Idx var2)
Computes the critical value according to the number of degrees of freedom.
Definition: chi2_inl.h:59
static double probaChi2(double x, Size df)
Computes the probability of chi2 value.
Definition: chi2.cpp:126
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
Chi2(const Chi2 &)=delete
Forbid use of the copy constructor.
Chi2(const std::vector< std::size_t > &var_modalities, double confidence_proba=GUM_LEARNING_CONFIDENCE_PROBA)
Default constructor.
Definition: chi2.cpp:56
Size _conditioning_size_
The domain size of the conditioning nodes.
Definition: chi2.h:157
void setConfidenceProba(double new_proba)
Modifies the confidence probability.
Definition: chi2_inl.h:75
const std::vector< std::size_t > & _modalities_
The modalities of the random variables.
Definition: chi2.h:151
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
Chi2 & operator=(const Chi2 &)=delete
Forbid used of the copy operator.