aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
Chi2TestPolicy.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
41
50#ifndef GUM_MULTI_DIM_FUNCTION_GRAPH_CHI2_TEST_POLICY_H
51#define GUM_MULTI_DIM_FUNCTION_GRAPH_CHI2_TEST_POLICY_H
52
53// ============================================================================
57
58// ============================================================================
59
60namespace gum {
61
71 template < typename GUM_SCALAR >
72 class Chi2TestPolicy: public ITestPolicy< GUM_SCALAR > {
73 public:
74 Chi2TestPolicy() : ITestPolicy< GUM_SCALAR >(), _conTab_(), _chi2Score_(0) {
75 GUM_CONSTRUCTOR(Chi2TestPolicy);
76 }
77
78 virtual ~Chi2TestPolicy() {
79 GUM_DESTRUCTOR(Chi2TestPolicy);
80 ;
81 }
82
83 // ============================================================================
85 // ============================================================================
86 void* operator new(size_t s) { return SmallObjectAllocator::instance().allocate(s); }
87
88 void operator delete(void* p) {
90 }
91
92 // ############################################################################
94 // ############################################################################
96
97 // ============================================================================
99 // ============================================================================
100 void addObservation(Idx attr, GUM_SCALAR value);
101
103
104
105 // ############################################################################
107 // ############################################################################
109
110 // ============================================================================
113 // ============================================================================
114 bool isTestRelevant() const {
115 return (this->nbObservation() > 20 && this->nbObservation() > _conTab_.attrASize() * 5);
116 }
117
119
120
121 // ############################################################################
123 // ############################################################################
125
126 // ============================================================================
128 // ============================================================================
129 void computeScore() const;
130
131 // ============================================================================
133 // ============================================================================
134 double score() const;
135
136 // ============================================================================
138 // ============================================================================
139 virtual double secondaryscore() const;
140
142
144
145 void add(const Chi2TestPolicy< GUM_SCALAR >& src);
146
147 std::string toString() const {
148 std::stringstream ss;
149 ss << ITestPolicy< GUM_SCALAR >::toString() << "\t\t\tContingency Table : " << std::endl
150 << _conTab_.toString() << std::endl
151 << "\t\t\tGStat : " << this->score() << std::endl
152 << "\t\t\tGStat : " << this->secondaryscore() << std::endl;
153 return ss.str();
154 }
155
156 private:
159
160 mutable double _chi2Score_;
161 };
162
163} // End of namespace gum
164
166
167#endif /* GUM_MULTI_DIM_FUNCTION_GRAPH_CHI2_TEST_POLICY_H */
Template implementations for the Chi2TestPolicy class.
Headers of the ITestPolicy.
Headers of the ChiSquare class.
<agrum/base/multidim/core/testPolicy/Chi2TestPolicy.h>
ContingencyTable< Idx, GUM_SCALAR > _conTab_
The contingency table used to keeps records of all observation.
void addObservation(Idx attr, GUM_SCALAR value)
Comptabilizes the new observation.
std::string toString() const
void add(const Chi2TestPolicy< GUM_SCALAR > &src)
virtual double secondaryscore() const
Returns a second criterion to severe ties.
void computeScore() const
Recomputes the statistic from the beginning.
bool isTestRelevant() const
Returns true if enough observation were made so that the test can be relevant.
const ContingencyTable< Idx, GUM_SCALAR > & ct() const
double score() const
Returns the performance of current variable according to the test.
Idx nbObservation() const
Comptabilizes the new observation.
ITestPolicy()
Allocators and Deallocators redefinition.
Definition ITestPolicy.h:81
static SmallObjectAllocator & instance()
void * allocate(const size_t &objectSize)
Allocates a block.
void deallocate(void *pDeallocatedObject, const size_t &objectSize)
Deallocates an object.
Headers of the ContingencyTable class.
Size Idx
Type for indexes.
Definition types.h:79
gum is the global namespace for all aGrUM entities
Definition agrum.h:46