27 #ifndef DOXYGEN_SHOULD_SKIP_THIS 34 template <
template <
typename >
class ALLOC >
36 const DBRowGeneratorParser< ALLOC >& parser,
37 const Apriori< ALLOC >& apriori,
38 const std::vector< std::pair< std::size_t, std::size_t >,
39 ALLOC< std::pair< std::size_t, std::size_t > > >& ranges,
40 const Bijection<
NodeId, std::size_t, ALLOC< std::size_t > >&
43 IndependenceTest< ALLOC >(parser, apriori, ranges, nodeId2columns, alloc),
44 __param_complexity(alloc) {
45 GUM_CONSTRUCTOR(
KNML);
50 template <
template <
typename >
class ALLOC >
52 const DBRowGeneratorParser< ALLOC >& parser,
53 const Apriori< ALLOC >& apriori,
54 const Bijection<
NodeId, std::size_t, ALLOC< std::size_t > >&
58 __param_complexity(alloc) {
59 GUM_CONSTRUCTOR(
KNML);
64 template <
template <
typename >
class ALLOC >
69 __param_complexity(from.__param_complexity, alloc) {
75 template <
template <
typename >
class ALLOC >
81 template <
template <
typename >
class ALLOC >
86 __param_complexity(
std::move(from.__param_complexity), alloc) {
92 template <
template <
typename >
class ALLOC >
98 template <
template <
typename >
class ALLOC >
101 ALLOC< KNML< ALLOC > > allocator(alloc);
102 KNML< ALLOC >* new_score = allocator.allocate(1);
104 allocator.construct(new_score, *
this, alloc);
106 allocator.deallocate(new_score, 1);
115 template <
template <
typename >
class ALLOC >
122 template <
template <
typename >
class ALLOC >
124 GUM_DESTRUCTOR(
KNML);
129 template <
template <
typename >
class ALLOC >
133 __param_complexity = from.__param_complexity;
140 template <
template <
typename >
class ALLOC >
144 __param_complexity = std::move(from.__param_complexity);
151 template <
template <
typename >
class ALLOC >
154 __param_complexity.clearCache();
159 template <
template <
typename >
class ALLOC >
162 __param_complexity.clearCache();
167 template <
template <
typename >
class ALLOC >
170 __param_complexity.useCache(on_off);
175 template <
template <
typename >
class ALLOC >
182 const bool informative_external_apriori = this->
_apriori->isInformative();
187 std::size_t r_x, r_y;
188 if (!node2cols.empty()) {
189 r_x = db.domainSize(node2cols.second(idset[0]));
190 r_y = db.domainSize(node2cols.second(idset[1]));
192 r_x = db.domainSize(idset[0]);
193 r_y = db.domainSize(idset[1]);
199 if (idset.hasConditioningSet()) {
201 IdSet< ALLOC > idset_xui = idset;
202 idset_xui.erase(idset[1]);
203 IdSet< ALLOC > idset_yui = idset;
204 idset_yui.erase(idset[0]);
206 std::vector< double, ALLOC< double > > N_ui =
207 this->
_counter.counts(idset.conditionalIdSet(),
false);
208 std::vector< double, ALLOC< double > > N_xui =
209 this->
_counter.counts(idset_xui,
false);
210 std::vector< double, ALLOC< double > > N_yui =
211 this->
_counter.counts(idset_yui,
false);
213 if (informative_external_apriori) {
214 this->
_apriori->addConditioningApriori(idset, N_ui);
215 this->
_apriori->addAllApriori(idset, N_xui);
216 this->
_apriori->addAllApriori(idset, N_yui);
224 for (
auto n_xui : N_xui)
225 score += __param_complexity.log2Cnr(r_y, n_xui);
226 for (
auto n_yui : N_yui)
227 score += __param_complexity.log2Cnr(r_x, n_yui);
228 for (
auto n_ui : N_ui) {
229 score -= __param_complexity.log2Cnr(r_y, n_ui);
230 score -= __param_complexity.log2Cnr(r_x, n_ui);
239 IdSet< ALLOC > idset_xui(idset[0], this->
_empty_ids,
true);
240 IdSet< ALLOC > idset_yui(idset[1], this->
_empty_ids,
true);
242 std::vector< double, ALLOC< double > > N_xui =
243 this->
_counter.counts(idset_xui,
false);
244 std::vector< double, ALLOC< double > > N_yui =
245 this->
_counter.counts(idset_yui,
false);
247 if (informative_external_apriori) {
248 this->
_apriori->addAllApriori(idset, N_xui);
249 this->
_apriori->addAllApriori(idset, N_yui);
258 for (
auto n_xui : N_xui) {
259 score += __param_complexity.log2Cnr(r_y, n_xui);
262 for (
auto n_yui : N_yui)
263 score += __param_complexity.log2Cnr(r_x, n_yui);
264 score -= __param_complexity.log2Cnr(r_y, N);
265 score -= __param_complexity.log2Cnr(r_x, N);
RecordCounter< ALLOC > _counter
the record counter used for the countings over discrete variables
ALLOC< NodeId > allocator_type
type for the allocators passed in arguments of methods
virtual void clearCache()
clears the current cache
const DatabaseTable< ALLOC > & database() const
return the database used by the score
const Bijection< NodeId, std::size_t, ALLOC< std::size_t > > & nodeId2Columns() const
return the mapping between the columns of the database and the node ids
gum is the global namespace for all aGrUM entities
const std::vector< NodeId, ALLOC< NodeId > > _empty_ids
an empty vector
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
virtual void clear()
clears all the data structures from memory, including the C_n^r cache
virtual ~KNML()
destructor
IndependenceTest< ALLOC > & operator=(const IndependenceTest< ALLOC > &from)
copy operator
virtual double _score(const IdSet< ALLOC > &idset) final
returns the score for a given IdSet
double score(const NodeId var1, const NodeId var2)
returns the score of a pair of nodes
virtual void clearCache()
clears the current C_n^r cache
KNML< ALLOC > & operator=(const KNML< ALLOC > &from)
copy operator
virtual void useCache(const bool on_off)
turn on/off the use of a cache of the previously computed score
virtual void useCache(const bool on_off)
turn on/off the use of the C_n^r cache
virtual KNML< ALLOC > * clone() const
virtual copy constructor
Size NodeId
Type for node ids.
virtual void clear()
clears all the data structures from memory, including the cache
KNML(const DBRowGeneratorParser< ALLOC > &parser, const Apriori< ALLOC > &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
Apriori< ALLOC > * _apriori
the expert knowledge a priori we add to the contongency tables