aGrUM  0.13.2
chi2_inl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
31 namespace gum {
32 
33  // sets the conditioning nodes (useful for computing degrees of freedom)
34  INLINE void
35  Chi2::setConditioningNodes(const std::vector< Idx >& db_conditioning_ids) {
37  for (Idx i = 0; i < db_conditioning_ids.size(); ++i) {
38  __conditioning_size *= __modalities[db_conditioning_ids[i]];
39  }
40  }
41 
42  // returns the number of degrees of freedom
43  INLINE Size Chi2::degreesOfFreedom(const std::pair< Idx, Idx >& pair) {
44  return (__conditioning_size * (__modalities[pair.first] - 1)
45  * (__modalities[pair.second] - 1));
46  }
47 
48  // returns the number of degrees of freedom
49  INLINE Size Chi2::degreesOfFreedom(Idx var1, Idx var2) {
50  return (__conditioning_size * (__modalities[var1] - 1)
51  * (__modalities[var2] - 1));
52  }
53 
54  // computes the critical value according to the number of degrees of freedom
55  ALWAYS_INLINE double Chi2::criticalValue(const std::pair< Idx, Idx >& pair) {
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  }
69 
70  // computes the critical value according to the number of degrees of freedom
71  ALWAYS_INLINE double Chi2::criticalValue(Idx var1, Idx var2) {
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  }
85 
86  // modifies the confidence proba
87  INLINE void Chi2::setConfidenceProba(double new_proba) {
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  }
96 
97 } /* namespace gum */
void setConditioningNodes(const std::vector< Idx > &db_conditioning_ids)
Sets the conditioning nodes (useful for computing degrees of freedom).
Definition: chi2_inl.h:35
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
Size __conditioning_size
The domain size of the conditioning nodes.
Definition: chi2.h:138
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
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
Base class for all aGrUM&#39;s exceptions.
Definition: exceptions.h:103
void clear()
Removes all the elements in the hash table.
void setConfidenceProba(double new_proba)
Modifies the confidence probability.
Definition: chi2_inl.h:87
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
double criticalValue(const std::pair< Idx, Idx > &pair)
Computes the critical value according to the number of degrees of freedom.
Definition: chi2_inl.h:55
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