aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
chi2_inl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40#pragma once
41
51
52
53namespace gum {
54
55 // sets the conditioning nodes (useful for computing degrees of freedom)
56 INLINE void Chi2::setConditioningNodes(const std::vector< Idx >& db_conditioning_ids) {
58 for (Idx i = 0; i < db_conditioning_ids.size(); ++i) {
59 _conditioning_size_ *= _modalities_[db_conditioning_ids[i]];
60 }
61 }
62
63 // returns the number of degrees of freedom
64 INLINE Size Chi2::degreesOfFreedom(const std::pair< Idx, Idx >& pair) {
65 return degreesOfFreedom(pair.first, pair.second);
66 }
67
68 // returns the number of degrees of freedom
69 INLINE Size Chi2::degreesOfFreedom(Idx var1, Idx var2) {
70 return (_conditioning_size_ * (_modalities_[var1] - 1) * (_modalities_[var2] - 1));
71 }
72
73 // computes the critical value according to the number of degrees of freedom
74 ALWAYS_INLINE double Chi2::criticalValue(const std::pair< Idx, Idx >& pair) {
75 return criticalValue(pair.first, pair.second);
76 }
77
78 // computes the critical value according to the number of degrees of freedom
79 ALWAYS_INLINE double Chi2::criticalValue(Idx var1, Idx var2) {
80 Size DF = degreesOfFreedom(var1, var2);
81
82 // try to see if the threshold is not already in cache
83 try {
84 return _critical_values_[DF];
85 } catch (const Exception&) {
86 // here we have to compute the threshold of the chi2
87 // we use Gary Perlman's algorithm
88 double value = _criticalValue_(_confidence_proba_, DF);
89 _critical_values_.insert(DF, value);
90 return value;
91 }
92 }
93
94 // modifies the confidence proba
95 INLINE void Chi2::setConfidenceProba(double new_proba) {
96 // if we did not change the confidence proba, do nothing
97 if (_confidence_proba_ == new_proba) return;
98
99 _confidence_proba_ = new_proba;
100
101 // remove the currently stored critical values
102 _critical_values_.clear();
103 }
104
105} /* namespace gum */
double _confidence_proba_
The confidence probability used for critical values.
Definition chi2.h:176
Size _conditioning_size_
The domain size of the conditioning nodes.
Definition chi2.h:179
double criticalValue(const std::pair< Idx, Idx > &pair)
Computes the critical value according to the number of degrees of freedom.
Definition chi2_inl.h:74
HashTable< Idx, double > _critical_values_
A set of already computed critical values.
Definition chi2.h:182
void setConfidenceProba(double new_proba)
Modifies the confidence probability.
Definition chi2_inl.h:95
const std::vector< std::size_t > & _modalities_
The modalities of the random variables.
Definition chi2.h:173
void setConditioningNodes(const std::vector< Idx > &db_conditioning_ids)
Sets the conditioning nodes (useful for computing degrees of freedom).
Definition chi2_inl.h:56
Size degreesOfFreedom(const std::pair< Idx, Idx > &pair)
Returns the number of degrees of freedom.
Definition chi2_inl.h:64
static double _criticalValue_(double proba, Size df)
Computes the critical value of a given chi2 test (used by the cache).
Definition chi2.cpp:190
Base class for all aGrUM's exceptions.
Definition exceptions.h:118
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size Idx
Type for indexes.
Definition types.h:79
gum is the global namespace for all aGrUM entities
Definition agrum.h:46