aGrUM  0.14.1
gum::learning::IndepTestChi2< ALLOC > Class Template Reference

the class for computing Chi2 independence test scores More...

#include <agrum/learning/scores_and_tests/indepTestChi2.h>

+ Inheritance diagram for gum::learning::IndepTestChi2< ALLOC >:
+ Collaboration diagram for gum::learning::IndepTestChi2< ALLOC >:

Public Member Functions

Constructors / Destructors
 IndepTestChi2 (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 More...
 
 IndepTestChi2 (const DBRowGeneratorParser< ALLOC > &parser, const Apriori< ALLOC > &apriori, 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 More...
 
 IndepTestChi2 (const IndepTestChi2< ALLOC > &from)
 copy constructor More...
 
 IndepTestChi2 (const IndepTestChi2< ALLOC > &from, const allocator_type &alloc)
 copy constructor with a given allocator More...
 
 IndepTestChi2 (IndepTestChi2< ALLOC > &&from)
 move constructor More...
 
 IndepTestChi2 (IndepTestChi2< ALLOC > &&from, const allocator_type &alloc)
 move constructor with a given allocator More...
 
virtual IndepTestChi2< ALLOC > * clone () const
 virtual copy constructor More...
 
virtual IndepTestChi2< ALLOC > * clone (const allocator_type &alloc) const
 virtual copy constructor with a given allocator More...
 
virtual ~IndepTestChi2 ()
 destructor More...
 
Operators
IndepTestChi2< ALLOC > & operator= (const IndepTestChi2< ALLOC > &from)
 copy operator More...
 
IndepTestChi2< ALLOC > & operator= (IndepTestChi2< ALLOC > &&from)
 move operator More...
 
Statistics
std::pair< double, doublestatistics (NodeId var1, NodeId var2, const std::vector< NodeId, ALLOC< NodeId > > &rhs_ids={})
 get the pair <chi2 statistic,pvalue> for a test var1 indep var2 given rhs_ids More...
 
Accessors / Modifiers
virtual void setMaxNbThreads (std::size_t nb) const
 changes the max number of threads used to parse the database More...
 
virtual std::size_t nbThreads () const
 returns the number of threads used to parse the database More...
 
virtual void setMinNbRowsPerThread (const std::size_t nb) const
 changes the number min of rows a thread should process in a multithreading context More...
 
virtual std::size_t minNbRowsPerThread () const
 returns the minimum of rows that each thread should process More...
 
template<template< typename > class XALLOC>
void setRanges (const std::vector< std::pair< std::size_t, std::size_t >, XALLOC< std::pair< std::size_t, std::size_t > > > &new_ranges)
 sets new ranges to perform the countings used by the independence test More...
 
void clearRanges ()
 reset the ranges to the one range corresponding to the whole database More...
 
const std::vector< std::pair< std::size_t, std::size_t >, ALLOC< std::pair< std::size_t, std::size_t > > > & ranges () const
 returns the current ranges More...
 
double score (const NodeId var1, const NodeId var2)
 returns the score of a pair of nodes More...
 
double score (const NodeId var1, const NodeId var2, const std::vector< NodeId, ALLOC< NodeId > > &rhs_ids)
 returns the score of a pair of nodes given some other nodes More...
 
virtual void clear ()
 clears all the data structures from memory, including the cache More...
 
virtual void clearCache ()
 clears the current cache More...
 
virtual void useCache (const bool on_off)
 turn on/off the use of a cache of the previously computed score More...
 
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 More...
 
const DatabaseTable< ALLOC > & database () const
 return the database used by the score More...
 
allocator_type getAllocator () const
 returns the allocator used by the score More...
 

Public Types

using allocator_type = ALLOC< NodeId >
 type for the allocators passed in arguments of methods More...
 

Protected Attributes

const double _1log2 {M_LOG2E}
 1 / log(2) More...
 
Apriori< ALLOC > * _apriori {nullptr}
 the expert knowledge a priori we add to the contongency tables More...
 
RecordCounter< ALLOC > _counter
 the record counter used for the countings over discrete variables More...
 
ScoringCache< ALLOC > _cache
 the scoring cache More...
 
bool _use_cache {true}
 a Boolean indicating whether we wish to use the cache More...
 
const std::vector< NodeId, ALLOC< NodeId > > _empty_ids
 an empty vector More...
 

Protected Member Functions

virtual double _score (const IdSet< ALLOC > &idset) final
 returns the score for a given IdSet More...
 
std::pair< double, double_statistics (const IdSet< ALLOC > &idset)
 compute the pair <chi2 statistic,pvalue> More...
 
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 More...
 

Detailed Description

template<template< typename > class ALLOC = std::allocator>
class gum::learning::IndepTestChi2< ALLOC >

the class for computing Chi2 independence test scores

Definition at line 45 of file indepTestChi2.h.

Member Typedef Documentation

◆ allocator_type

template<template< typename > class ALLOC = std::allocator>
using gum::learning::IndepTestChi2< ALLOC >::allocator_type = ALLOC< NodeId >

type for the allocators passed in arguments of methods

Definition at line 48 of file indepTestChi2.h.

Constructor & Destructor Documentation

◆ IndepTestChi2() [1/6]

template<template< typename > class ALLOC = std::allocator>
gum::learning::IndepTestChi2< ALLOC >::IndepTestChi2 ( 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 = BijectionNodeId, std::size_t, ALLOC< std::size_t > >(),
const allocator_type alloc = allocator_type() 
)

default constructor

Parameters
parserthe parser used to parse the database
external_aprioriAn apriori that we add to the computation of the score (this should come from expert knowledge): this consists in adding numbers to countings in the contingency tables
rangesa set of pairs {(X1,Y1),...,(Xn,Yn)} of database's rows indices. The countings are then performed only on the union of the rows [Xi,Yi), i in {1,...,n}. This is useful, e.g, when performing cross validation tasks, in which part of the database should be ignored. An empty set of ranges is equivalent to an interval [X,Y) ranging over the whole database.
nodeId2Columnsa mapping from the ids of the nodes in the graphical model to the corresponding column in the DatabaseTable parsed by the parser. This enables estimating from a database in which variable A corresponds to the 2nd column the parameters of a BN in which variable A has a NodeId of 5. An empty nodeId2Columns bijection means that the mapping is an identity, i.e., the value of a NodeId is equal to the index of the column in the DatabaseTable.
allocthe allocator used to allocate the structures within the Score.
Warning
If nodeId2columns is not empty, then only the scores over the ids belonging to this bijection can be computed: applying method score() over other ids will raise exception NotFound.

◆ IndepTestChi2() [2/6]

template<template< typename > class ALLOC = std::allocator>
gum::learning::IndepTestChi2< ALLOC >::IndepTestChi2 ( const DBRowGeneratorParser< ALLOC > &  parser,
const Apriori< ALLOC > &  apriori,
const Bijection< NodeId, std::size_t, ALLOC< std::size_t > > &  nodeId2columns = BijectionNodeId, std::size_t, ALLOC< std::size_t > >(),
const allocator_type alloc = allocator_type() 
)

default constructor

Parameters
parserthe parser used to parse the database
aprioriAn apriori that we add to the computation of the score
nodeId2Columnsa mapping from the ids of the nodes in the graphical model to the corresponding column in the DatabaseTable parsed by the parser. This enables estimating from a database in which variable A corresponds to the 2nd column the parameters of a BN in which variable A has a NodeId of 5. An empty nodeId2Columns bijection means that the mapping is an identity, i.e., the value of a NodeId is equal to the index of the column in the DatabaseTable.
allocthe allocator used to allocate the structures within the Score.
Warning
If nodeId2columns is not empty, then only the scores over the ids belonging to this bijection can be computed: applying method score() over other ids will raise exception NotFound.

◆ IndepTestChi2() [3/6]

template<template< typename > class ALLOC = std::allocator>
gum::learning::IndepTestChi2< ALLOC >::IndepTestChi2 ( const IndepTestChi2< ALLOC > &  from)

copy constructor

◆ IndepTestChi2() [4/6]

template<template< typename > class ALLOC = std::allocator>
gum::learning::IndepTestChi2< ALLOC >::IndepTestChi2 ( const IndepTestChi2< ALLOC > &  from,
const allocator_type alloc 
)

copy constructor with a given allocator

◆ IndepTestChi2() [5/6]

template<template< typename > class ALLOC = std::allocator>
gum::learning::IndepTestChi2< ALLOC >::IndepTestChi2 ( IndepTestChi2< ALLOC > &&  from)

move constructor

◆ IndepTestChi2() [6/6]

template<template< typename > class ALLOC = std::allocator>
gum::learning::IndepTestChi2< ALLOC >::IndepTestChi2 ( IndepTestChi2< ALLOC > &&  from,
const allocator_type alloc 
)

move constructor with a given allocator

◆ ~IndepTestChi2()

template<template< typename > class ALLOC = std::allocator>
virtual gum::learning::IndepTestChi2< ALLOC >::~IndepTestChi2 ( )
virtual

destructor

Member Function Documentation

◆ _marginalize()

template<template< typename > class ALLOC = std::allocator>
std::vector< double, ALLOC< double > > gum::learning::IndependenceTest< ALLOC >::_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
protectedinherited

returns a counting vector where variables are marginalized from N_xyz

Parameters
node_2_marginalizeindicates which node(s) shall be marginalized:
  • 0 means that X should be marginalized
  • 1 means that Y should be marginalized
  • 2 means that Z should be marginalized
X_sizethe domain size of variable X
Y_sizethe domain size of variable Y
Z_sizethe domain size of the set of conditioning variables Z
N_xyza counting vector of dimension X * Y * Z (in this order)

◆ _score()

template<template< typename > class ALLOC = std::allocator>
virtual double gum::learning::IndepTestChi2< ALLOC >::_score ( const IdSet< ALLOC > &  idset)
finalprotectedvirtual

returns the score for a given IdSet

Exceptions
OperationNotAllowedis raised if the score does not support calling method score such an idset (due to too many/too few variables in the left hand side or the right hand side of the idset).

Implements gum::learning::IndependenceTest< ALLOC >.

◆ _statistics()

template<template< typename > class ALLOC = std::allocator>
std::pair< double, double > gum::learning::IndepTestChi2< ALLOC >::_statistics ( const IdSet< ALLOC > &  idset)
protected

compute the pair <chi2 statistic,pvalue>

◆ clear()

template<template< typename > class ALLOC = std::allocator>
virtual void gum::learning::IndependenceTest< ALLOC >::clear ( )
virtualinherited

clears all the data structures from memory, including the cache

Reimplemented in gum::learning::KNML< ALLOC >.

◆ clearCache()

template<template< typename > class ALLOC = std::allocator>
virtual void gum::learning::IndependenceTest< ALLOC >::clearCache ( )
virtualinherited

clears the current cache

Reimplemented in gum::learning::KNML< ALLOC >.

◆ clearRanges()

template<template< typename > class ALLOC = std::allocator>
void gum::learning::IndependenceTest< ALLOC >::clearRanges ( )
inherited

reset the ranges to the one range corresponding to the whole database

◆ clone() [1/2]

template<template< typename > class ALLOC = std::allocator>
virtual IndepTestChi2< ALLOC >* gum::learning::IndepTestChi2< ALLOC >::clone ( ) const
virtual

virtual copy constructor

Implements gum::learning::IndependenceTest< ALLOC >.

◆ clone() [2/2]

template<template< typename > class ALLOC = std::allocator>
virtual IndepTestChi2< ALLOC >* gum::learning::IndepTestChi2< ALLOC >::clone ( const allocator_type alloc) const
virtual

virtual copy constructor with a given allocator

Implements gum::learning::IndependenceTest< ALLOC >.

◆ database()

template<template< typename > class ALLOC = std::allocator>
const DatabaseTable< ALLOC >& gum::learning::IndependenceTest< ALLOC >::database ( ) const
inherited

return the database used by the score

◆ getAllocator()

template<template< typename > class ALLOC = std::allocator>
allocator_type gum::learning::IndependenceTest< ALLOC >::getAllocator ( ) const
inherited

returns the allocator used by the score

◆ minNbRowsPerThread()

template<template< typename > class ALLOC = std::allocator>
virtual std::size_t gum::learning::IndependenceTest< ALLOC >::minNbRowsPerThread ( ) const
virtualinherited

returns the minimum of rows that each thread should process

◆ nbThreads()

template<template< typename > class ALLOC = std::allocator>
virtual std::size_t gum::learning::IndependenceTest< ALLOC >::nbThreads ( ) const
virtualinherited

returns the number of threads used to parse the database

◆ nodeId2Columns()

template<template< typename > class ALLOC = std::allocator>
const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >& gum::learning::IndependenceTest< ALLOC >::nodeId2Columns ( ) const
inherited

return the mapping between the columns of the database and the node ids

Warning
An empty nodeId2Columns bijection means that the mapping is an identity, i.e., the value of a NodeId is equal to the index of the column in the DatabaseTable.

◆ operator=() [1/2]

template<template< typename > class ALLOC = std::allocator>
IndepTestChi2< ALLOC >& gum::learning::IndepTestChi2< ALLOC >::operator= ( const IndepTestChi2< ALLOC > &  from)

copy operator

◆ operator=() [2/2]

template<template< typename > class ALLOC = std::allocator>
IndepTestChi2< ALLOC >& gum::learning::IndepTestChi2< ALLOC >::operator= ( IndepTestChi2< ALLOC > &&  from)

move operator

◆ ranges()

template<template< typename > class ALLOC = std::allocator>
const std::vector< std::pair< std::size_t, std::size_t >, ALLOC< std::pair< std::size_t, std::size_t > > >& gum::learning::IndependenceTest< ALLOC >::ranges ( ) const
inherited

returns the current ranges

◆ score() [1/2]

template<template< typename > class ALLOC = std::allocator>
double gum::learning::IndependenceTest< ALLOC >::score ( const NodeId  var1,
const NodeId  var2 
)
inherited

returns the score of a pair of nodes

◆ score() [2/2]

template<template< typename > class ALLOC = std::allocator>
double gum::learning::IndependenceTest< ALLOC >::score ( const NodeId  var1,
const NodeId  var2,
const std::vector< NodeId, ALLOC< NodeId > > &  rhs_ids 
)
inherited

returns the score of a pair of nodes given some other nodes

Parameters
var1the first variable on the left side of the conditioning bar
var2the second variable on the left side of the conditioning bar
rhs_idsthe set of variables on the right side of the conditioning bar

◆ setMaxNbThreads()

template<template< typename > class ALLOC = std::allocator>
virtual void gum::learning::IndependenceTest< ALLOC >::setMaxNbThreads ( std::size_t  nb) const
virtualinherited

changes the max number of threads used to parse the database

◆ setMinNbRowsPerThread()

template<template< typename > class ALLOC = std::allocator>
virtual void gum::learning::IndependenceTest< ALLOC >::setMinNbRowsPerThread ( const std::size_t  nb) const
virtualinherited

changes the number min of rows a thread should process in a multithreading context

When computing score, several threads are used by record counters to perform countings on the rows of the database, the MinNbRowsPerThread method indicates how many rows each thread should at least process. This is used to compute the number of threads actually run. This number is equal to the min between the max number of threads allowed and the number of records in the database divided by nb.

◆ setRanges()

template<template< typename > class ALLOC = std::allocator>
template<template< typename > class XALLOC>
void gum::learning::IndependenceTest< ALLOC >::setRanges ( const std::vector< std::pair< std::size_t, std::size_t >, XALLOC< std::pair< std::size_t, std::size_t > > > &  new_ranges)
inherited

sets new ranges to perform the countings used by the independence test

Parameters
rangesa set of pairs {(X1,Y1),...,(Xn,Yn)} of database's rows indices. The countings are then performed only on the union of the rows [Xi,Yi), i in {1,...,n}. This is useful, e.g, when performing cross validation tasks, in which part of the database should be ignored. An empty set of ranges is equivalent to an interval [X,Y) ranging over the whole database.

◆ statistics()

template<template< typename > class ALLOC = std::allocator>
std::pair< double, double > gum::learning::IndepTestChi2< ALLOC >::statistics ( NodeId  var1,
NodeId  var2,
const std::vector< NodeId, ALLOC< NodeId > > &  rhs_ids = {} 
)

get the pair <chi2 statistic,pvalue> for a test var1 indep var2 given rhs_ids

Referenced by gum::learning::genericBNLearner::chi2().

+ Here is the caller graph for this function:

◆ useCache()

template<template< typename > class ALLOC = std::allocator>
virtual void gum::learning::IndependenceTest< ALLOC >::useCache ( const bool  on_off)
virtualinherited

turn on/off the use of a cache of the previously computed score

Reimplemented in gum::learning::KNML< ALLOC >.

Member Data Documentation

◆ _1log2

template<template< typename > class ALLOC = std::allocator>
const double gum::learning::IndependenceTest< ALLOC >::_1log2 {M_LOG2E}
protectedinherited

1 / log(2)

Definition at line 218 of file independenceTest.h.

◆ _apriori

template<template< typename > class ALLOC = std::allocator>
Apriori< ALLOC >* gum::learning::IndependenceTest< ALLOC >::_apriori {nullptr}
protectedinherited

the expert knowledge a priori we add to the contongency tables

Definition at line 221 of file independenceTest.h.

◆ _cache

template<template< typename > class ALLOC = std::allocator>
ScoringCache< ALLOC > gum::learning::IndependenceTest< ALLOC >::_cache
protectedinherited

the scoring cache

Definition at line 227 of file independenceTest.h.

◆ _counter

template<template< typename > class ALLOC = std::allocator>
RecordCounter< ALLOC > gum::learning::IndependenceTest< ALLOC >::_counter
protectedinherited

the record counter used for the countings over discrete variables

Definition at line 224 of file independenceTest.h.

◆ _empty_ids

template<template< typename > class ALLOC = std::allocator>
const std::vector< NodeId, ALLOC< NodeId > > gum::learning::IndependenceTest< ALLOC >::_empty_ids
protectedinherited

an empty vector

Definition at line 233 of file independenceTest.h.

◆ _use_cache

template<template< typename > class ALLOC = std::allocator>
bool gum::learning::IndependenceTest< ALLOC >::_use_cache {true}
protectedinherited

a Boolean indicating whether we wish to use the cache

Definition at line 230 of file independenceTest.h.


The documentation for this class was generated from the following file: