54#ifndef DOXYGEN_SHOULD_SKIP_THIS
70 template <
typename CellContribFn >
72 CellContribFn cellContrib) {
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();
79 const auto& nodeId2cols = this->
counter_.nodeId2Columns();
82 if (nodeId2cols.empty()) {
86 var_x = nodeId2cols.second(idset[0]);
87 var_y = nodeId2cols.second(idset[1]);
90 const std::size_t X_size =
database.domainSize(var_x);
91 const std::size_t Y_size =
database.domainSize(var_y);
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);
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);
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) {
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) {
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]);
122 n_skipped += X_size * Y_size;
123 xyz += X_size * Y_size;
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);
133 for (
const auto n_x: N_x)
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);
147 Size df = degreesOfFreedom_(X_size, Y_size, Z_size, n_skipped);
149 return {cumulStat, pValue};
The class that represents the chi2 distribution.
static double probaChi2(double x, Size df)
Computes the probability of chi2 value.
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.
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.
Size Idx
Type for indexes.
the base class for all the independence tests used for learning
include the inlined functions if necessary
gum is the global namespace for all aGrUM entities