aGrUM 3.0.0
a C++ library for (probabilistic) graphical models
independenceTest_tpl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 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-2026 *
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
42#pragma once
43
50
52#include <agrum/base/stattests/independenceTest.h> // to ease IDE parser
53
54#ifndef DOXYGEN_SHOULD_SKIP_THIS
55
56namespace gum {
57
58 namespace learning {
59
61
70 template < typename CellContribFn >
71 std::pair< double, double > IndependenceTest::computeStatistics_(const IdCondSet& idset,
72 CellContribFn cellContrib) {
73 // get the counts
74 std::vector< double > N_xyz(this->counter_.counts(idset, true));
75 const bool informative_external_prior = this->prior_->isInformative();
76 if (informative_external_prior) { this->prior_->addJointPseudoCount(idset, N_xyz); }
77 const std::size_t all_size = N_xyz.size();
78
79 const auto& nodeId2cols = this->counter_.nodeId2Columns();
80 const auto& database = this->counter_.database();
81 Idx var_x, var_y;
82 if (nodeId2cols.empty()) {
83 var_x = idset[0];
84 var_y = idset[1];
85 } else {
86 var_x = nodeId2cols.second(idset[0]);
87 var_y = nodeId2cols.second(idset[1]);
88 }
89
90 const std::size_t X_size = database.domainSize(var_x);
91 const std::size_t Y_size = database.domainSize(var_y);
92
93 double cumulStat = 0.0;
94 std::size_t n_skipped = 0;
95 const std::size_t Z_size
96 = idset.hasConditioningSet() ? all_size / (X_size * Y_size) : std::size_t(1);
97
98 if (idset.hasConditioningSet()) {
99 std::vector< double > N_xz
100 = this->marginalize_(std::size_t(1), X_size, Y_size, Z_size, N_xyz);
101 std::vector< double > N_yz
102 = this->marginalize_(std::size_t(0), X_size, Y_size, Z_size, N_xyz);
103 std::vector< double > N_z
104 = this->marginalize_(std::size_t(2), X_size, Y_size, Z_size, N_xyz);
105
106 for (std::size_t z = 0, beg_xz = 0, beg_yz = 0, xyz = 0; z < Z_size;
107 ++z, beg_xz += X_size, beg_yz += Y_size) {
108 if (N_z[z] > 0) {
109 for (std::size_t y = std::size_t(0), yz = beg_yz; y < Y_size; ++yz, ++y) {
110 for (std::size_t x = std::size_t(0), xz = beg_xz; x < X_size; ++xz, ++x, ++xyz) {
111 // structural zero (a marginal is 0 → E undefined): skip, reduce df.
112 // Sampling zeros (O=0, E>0) are left to cellContrib (return 0).
113 // Agresti, "Categorical Data Analysis", 3rd ed. (Wiley, 2013).
114 if (N_yz[yz] * N_xz[xz] != 0.0) {
115 cumulStat += cellContrib(N_xyz[xyz], N_xz[xz], N_yz[yz], N_z[z]);
116 } else {
117 ++n_skipped;
118 }
119 }
120 }
121 } else {
122 n_skipped += X_size * Y_size;
123 xyz += X_size * Y_size;
124 }
125 }
126 } else {
127 std::vector< double > N_x
128 = this->marginalize_(std::size_t(1), X_size, Y_size, std::size_t(1), N_xyz);
129 std::vector< double > N_y
130 = this->marginalize_(std::size_t(0), X_size, Y_size, std::size_t(1), N_xyz);
131
132 double N = 0.0;
133 for (const auto n_x: N_x)
134 N += n_x;
135
136 for (std::size_t y = std::size_t(0), xy = 0; y < Y_size; ++y) {
137 for (std::size_t x = 0; x < X_size; ++x, ++xy) {
138 if (N_y[y] * N_x[x] != 0.0) {
139 cumulStat += cellContrib(N_xyz[xy], N_x[x], N_y[y], N);
140 } else {
141 ++n_skipped;
142 }
143 }
144 }
145 }
146
147 Size df = degreesOfFreedom_(X_size, Y_size, Z_size, n_skipped);
148 double pValue = Chi2::probaChi2(cumulStat, df);
149 return {cumulStat, pValue};
150 }
151
152 } /* namespace learning */
153
154} /* namespace gum */
155
156#endif /* DOXYGEN_SHOULD_SKIP_THIS */
The class that represents the chi2 distribution.
static double probaChi2(double x, Size df)
Computes the probability of chi2 value.
Definition chi2.cpp:127
RecordCounter counter_
the record counter used for the counts over discrete variables
Prior * prior_
the expert knowledge prior added to the contingency tables
const DatabaseTable & database() const
return the database used by the score
A class for storing a pair of sets of NodeIds, the second one corresponding to a conditional set.
Definition idCondSet.h:214
std::pair< double, double > computeStatistics_(const IdCondSet &idset, CellContribFn cellContrib)
shared loop for chi-squared-family statistics
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
the base class for all the independence tests used for learning
include the inlined functions if necessary
Definition CSVParser.h:55
gum is the global namespace for all aGrUM entities
Definition agrum.h:46