26 #ifndef DOXYGEN_SHOULD_SKIP_THIS 33 template <
template <
typename >
class ALLOC >
35 const DBRowGeneratorParser< ALLOC >& parser,
36 const Apriori< ALLOC >& apriori,
37 const std::vector< std::pair< std::size_t, std::size_t >,
38 ALLOC< std::pair< std::size_t, std::size_t > > >& ranges,
39 const Bijection<
NodeId, std::size_t, ALLOC< std::size_t > >&
42 IndependenceTest< ALLOC >(parser, apriori, ranges, nodeId2columns, alloc),
43 __domain_sizes(parser.database().domainSizes()), __chi2(__domain_sizes) {
49 template <
template <
typename >
class ALLOC >
51 const DBRowGeneratorParser< ALLOC >& parser,
52 const Apriori< ALLOC >& apriori,
53 const Bijection<
NodeId, std::size_t, ALLOC< std::size_t > >&
57 __domain_sizes(parser.
database().domainSizes()), __chi2(__domain_sizes) {
63 template <
template <
typename >
class ALLOC >
65 const IndepTestG2< ALLOC >& from,
68 __chi2(__domain_sizes) {
74 template <
template <
typename >
class ALLOC >
80 template <
template <
typename >
class ALLOC >
82 IndepTestG2< ALLOC >&& from,
85 __domain_sizes(from.__domain_sizes), __chi2(__domain_sizes) {
91 template <
template <
typename >
class ALLOC >
97 template <
template <
typename >
class ALLOC >
100 ALLOC< IndepTestG2< ALLOC > > allocator(alloc);
101 IndepTestG2< ALLOC >* new_score = allocator.allocate(1);
103 allocator.construct(new_score, *
this, alloc);
105 allocator.deallocate(new_score, 1);
114 template <
template <
typename >
class ALLOC >
121 template <
template <
typename >
class ALLOC >
128 template <
template <
typename >
class ALLOC >
130 operator=(
const IndepTestG2< ALLOC >& from) {
140 template <
template <
typename >
class ALLOC >
152 template <
template <
typename >
class ALLOC >
155 std::vector< double, ALLOC< double > > N_xyz(
156 this->
_counter.counts(idset,
true));
157 const bool informative_external_apriori = this->
_apriori->isInformative();
158 if (informative_external_apriori)
159 this->
_apriori->addAllApriori(idset, N_xyz);
160 const std::size_t all_size = (N_xyz.size());
163 const auto& nodeId2cols = this->
_counter.nodeId2Columns();
166 if (nodeId2cols.empty()) {
170 var_x = nodeId2cols.second(idset[0]);
171 var_y = nodeId2cols.second(idset[1]);
174 const std::size_t X_size =
database.domainSize(var_x);
175 const std::size_t Y_size =
database.domainSize(var_y);
180 if (idset.hasConditioningSet()) {
181 const std::size_t Z_size = all_size / (X_size * Y_size);
184 std::vector< double, ALLOC< double > > N_xz =
185 this->
_marginalize(std::size_t(1), X_size, Y_size, Z_size, N_xyz);
186 std::vector< double, ALLOC< double > > N_yz =
187 this->
_marginalize(std::size_t(0), X_size, Y_size, Z_size, N_xyz);
188 std::vector< double, ALLOC< double > > N_z =
189 this->
_marginalize(std::size_t(2), X_size, Y_size, Z_size, N_xyz);
192 std::vector< Idx > cond_nodes;
193 cond_nodes.reserve(idset.nbRHSIds());
195 const auto cond_idset = idset.conditionalIdSet().ids();
196 if (nodeId2cols.empty()) {
197 for (
const auto node : cond_idset)
198 cond_nodes.push_back(node);
200 for (
const auto node : cond_idset)
201 cond_nodes.push_back(nodeId2cols.second(node));
204 __chi2.setConditioningNodes(cond_nodes);
211 for (std::size_t z = std::size_t(0),
212 beg_xz = std::size_t(0),
213 beg_yz = std::size_t(0),
214 xyz = std::size_t(0);
216 ++z, beg_xz += X_size, beg_yz += Y_size) {
218 for (std::size_t y = std::size_t(0), yz = beg_yz; y < Y_size;
220 for (std::size_t x = std::size_t(0), xz = beg_xz; x < X_size;
222 const double tmp1 = N_xyz[xyz] * N_z[z];
223 const double tmp2 = N_yz[yz] * N_xz[xz];
224 if ((tmp1 != 0.0) && (tmp2 != 0.0)) {
225 score += N_xyz[xyz] * std::log(tmp1 / tmp2);
237 const double alpha = __chi2.criticalValue(var_x, var_y);
238 score = (score - alpha) / alpha;
244 __chi2.setConditioningNodes(__empty_set);
249 std::vector< double, ALLOC< double > > N_x = this->
_marginalize(
250 std::size_t(1), X_size, Y_size, std::size_t(1), N_xyz);
251 std::vector< double, ALLOC< double > > N_y = this->
_marginalize(
252 std::size_t(0), X_size, Y_size, std::size_t(1), N_xyz);
262 for (std::size_t y = std::size_t(0), xy = 0; y < Y_size; ++y) {
263 const double tmp_Ny = N_y[y];
264 for (std::size_t x = 0; x < X_size; ++x, ++xy) {
265 const double tmp = (tmp_Ny * N_x[x]);
266 if (tmp) { score += N_xyz[xy] * std::log((N_xyz[xy] * N) / tmp); }
275 const double alpha = __chi2.criticalValue(var_x, var_y);
276 score = (score - alpha) / alpha;
ALLOC< NodeId > allocator_type
type for the allocators passed in arguments of methods
std::vector< double, ALLOC< double > > _marginalize(const std::size_t node_2_marginalize, const std::size_t X_size, const std::size_t Y_size, const std::size_t Z_size, const std::vector< double, ALLOC< double > > &N_xyz) const
returns a counting vector where variables are marginalized from N_xyz
virtual IndepTestG2< ALLOC > * clone() const
virtual copy constructor
RecordCounter< ALLOC > _counter
the record counter used for the countings over discrete variables
IndepTestG2(const DBRowGeneratorParser< ALLOC > &parser, const Apriori< ALLOC > &external_apriori, const std::vector< std::pair< std::size_t, std::size_t >, ALLOC< std::pair< std::size_t, std::size_t > > > &ranges, const Bijection< NodeId, std::size_t, ALLOC< std::size_t > > &nodeId2columns=Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(), const allocator_type &alloc=allocator_type())
default constructor
const DatabaseTable< ALLOC > & database() const
return the database used by the score
gum is the global namespace for all aGrUM entities
IndependenceTest(const DBRowGeneratorParser< ALLOC > &parser, const Apriori< ALLOC > &external_apriori, const std::vector< std::pair< std::size_t, std::size_t >, ALLOC< std::pair< std::size_t, std::size_t > > > &ranges, const Bijection< NodeId, std::size_t, ALLOC< std::size_t > > &nodeId2columns=Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(), const allocator_type &alloc=allocator_type())
default constructor
allocator_type getAllocator() const
returns the allocator used by the score
IndependenceTest< ALLOC > & operator=(const IndependenceTest< ALLOC > &from)
copy operator
double score(const NodeId var1, const NodeId var2)
returns the score of a pair of nodes
virtual ~IndepTestG2()
destructor
IndepTestG2< ALLOC > & operator=(const IndepTestG2< ALLOC > &from)
copy operator
virtual double _score(const IdSet< ALLOC > &idset) final
returns the score for a given IdSet
Size NodeId
Type for node ids.
Apriori< ALLOC > * _apriori
the expert knowledge a priori we add to the contongency tables