aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
indepTestG2.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /** @file
23  * @brief the class for computing G2 scores
24  *
25  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
26  */
27 
28 #ifndef GUM_LEARNING_INDEP_TEST_G2_H
29 #define GUM_LEARNING_INDEP_TEST_G2_H
30 
31 #include <agrum/tools/core/math/chi2.h>
32 #include <agrum/tools/stattests/independenceTest.h>
33 #include <agrum/BN/learning/aprioris/aprioriNoApriori.h>
34 
35 namespace gum {
36 
37  namespace learning {
38 
39  /**
40  * @class IndepTestG2
41  * @headerfile indepTestG2.h <agrum/BN/learning/scores_and_tests/indepTestG2.h>
42  * @brief the class for computing G2 independence test scores
43  * @ingroup learning_scores
44  */
45  // clang-format on
46  template < template < typename > class ALLOC = std::allocator >
47  class IndepTestG2: public IndependenceTest< ALLOC > {
48  public:
49  /// type for the allocators passed in arguments of methods
51 
52  // ##########################################################################
53  /// @name Constructors / Destructors
54  // ##########################################################################
55  /// @{
56 
57  /// default constructor
58  /** @param parser the parser used to parse the database
59  * @param external_apriori An apriori that we add to the computation
60  * of the score (this should come from expert knowledge): this consists in
61  * adding numbers to countings in the contingency tables
62  * @param ranges a set of pairs {(X1,Y1),...,(Xn,Yn)} of database's rows
63  * indices. The countings are then performed only on the union of the
64  * rows [Xi,Yi), i in {1,...,n}. This is useful, e.g, when performing
65  * cross validation tasks, in which part of the database should be ignored.
66  * An empty set of ranges is equivalent to an interval [X,Y) ranging over
67  * the whole database.
68  * @param nodeId2Columns a mapping from the ids of the nodes in the
69  * graphical model to the corresponding column in the DatabaseTable
70  * parsed by the parser. This enables estimating from a database in
71  * which variable A corresponds to the 2nd column the parameters of a BN
72  * in which variable A has a NodeId of 5. An empty nodeId2Columns
73  * bijection means that the mapping is an identity, i.e., the value of a
74  * NodeId is equal to the index of the column in the DatabaseTable.
75  * @param alloc the allocator used to allocate the structures within the
76  * Score.
77  * @warning If nodeId2columns is not empty, then only the scores over the
78  * ids belonging to this bijection can be computed: applying method
79  * score() over other ids will raise exception NotFound. */
80  IndepTestG2(const DBRowGeneratorParser< ALLOC >& parser,
81  const Apriori< ALLOC >& external_apriori,
82  const std::vector< std::pair< std::size_t, std::size_t >,
83  ALLOC< std::pair< std::size_t, std::size_t > > >& ranges,
85  = Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(),
87 
88 
89  /// default constructor
90  /** @param parser the parser used to parse the database
91  * @param apriori An apriori that we add to the computation of the score
92  * @param nodeId2Columns a mapping from the ids of the nodes in the
93  * graphical model to the corresponding column in the DatabaseTable
94  * parsed by the parser. This enables estimating from a database in
95  * which variable A corresponds to the 2nd column the parameters of a BN
96  * in which variable A has a NodeId of 5. An empty nodeId2Columns
97  * bijection means that the mapping is an identity, i.e., the value of a
98  * NodeId is equal to the index of the column in the DatabaseTable.
99  * @param alloc the allocator used to allocate the structures within the
100  * Score.
101  * @warning If nodeId2columns is not empty, then only the scores over the
102  * ids belonging to this bijection can be computed: applying method
103  * score() over other ids will raise exception NotFound. */
104  IndepTestG2(const DBRowGeneratorParser< ALLOC >& parser,
105  const Apriori< ALLOC >& apriori,
107  = Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(),
108  const allocator_type& alloc = allocator_type());
109 
110  /// copy constructor
111  IndepTestG2(const IndepTestG2< ALLOC >& from);
112 
113  /// copy constructor with a given allocator
114  IndepTestG2(const IndepTestG2< ALLOC >& from, const allocator_type& alloc);
115 
116  /// move constructor
117  IndepTestG2(IndepTestG2< ALLOC >&& from);
118 
119  /// move constructor with a given allocator
120  IndepTestG2(IndepTestG2< ALLOC >&& from, const allocator_type& alloc);
121 
122  /// virtual copy constructor
123  virtual IndepTestG2< ALLOC >* clone() const;
124 
125  /// virtual copy constructor with a given allocator
126  virtual IndepTestG2< ALLOC >* clone(const allocator_type& alloc) const;
127 
128  /// destructor
129  virtual ~IndepTestG2();
130 
131  /// @}
132 
133 
134  // ##########################################################################
135  /// @name Operators
136  // ##########################################################################
137 
138  /// @{
139 
140  /// copy operator
142 
143  /// move operator
145 
146  /// get the pair <G2statistic,pvalue> for a test var1 indep var2 given
147  /// rhs_ids
148  std::pair< double, double > statistics(NodeId var1,
149  NodeId var2,
150  const std::vector< NodeId, ALLOC< NodeId > >& rhs_ids
151  = {});
152 
153  /// @}
154 
155 
156  protected:
157  /// returns the score for a given IdCondSet
158  /** @throws OperationNotAllowed is raised if the score does not support
159  * calling method score such an idset (due to too many/too few variables
160  * in the left hand side or the right hand side of the idset). */
161  virtual double score_(const IdCondSet< ALLOC >& idset) final;
162 
163  /// compute the pair <G2 statistic,pvalue>
164  std::pair< double, double > statistics_(const IdCondSet< ALLOC >& idset);
165 
166 #ifndef DOXYGEN_SHOULD_SKIP_THIS
167 
168  private:
169  /// the domain sizes of the variables
171 
172  /// a chi2 distribution for computing critical values
173  Chi2 _chi2_;
174 
175  /// an empty vector of ids
176  const std::vector< Idx > _empty_set_;
177 
178 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
179  };
180 
181  } /* namespace learning */
182 
183 } /* namespace gum */
184 
185 
186 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
187 extern template class gum::learning::IndepTestG2<>;
188 #endif
189 
190 
191 // always include the template implementation
192 #include <agrum/tools/stattests/indepTestG2_tpl.h>
193 
194 #endif /* GUM_LEARNING_INDEP_TEST_G2_H */
IndepTestG2(IndepTestG2< ALLOC > &&from)
move constructor
IndepTestG2< ALLOC > & operator=(IndepTestG2< ALLOC > &&from)
move operator
IndepTestG2(IndepTestG2< ALLOC > &&from, const allocator_type &alloc)
move constructor with a given allocator
virtual IndepTestG2< ALLOC > * clone() const
virtual copy constructor
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
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
IndepTestG2(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
std::pair< double, double > statistics_(const IdCondSet< ALLOC > &idset)
compute the pair <G2 statistic,pvalue>
the class for computing G2 independence test scores
Definition: indepTestG2.h:47
virtual double score_(const IdCondSet< ALLOC > &idset) final
returns the score for a given IdCondSet
std::pair< double, double > statistics(NodeId var1, NodeId var2, const std::vector< NodeId, ALLOC< NodeId > > &rhs_ids={})
get the pair <G2statistic,pvalue> for a test var1 indep var2 given rhs_ids
IndepTestG2(const IndepTestG2< ALLOC > &from)
copy constructor
IndepTestG2(const IndepTestG2< ALLOC > &from, const allocator_type &alloc)
copy constructor with a given allocator
virtual ~IndepTestG2()
destructor
virtual IndepTestG2< ALLOC > * clone(const allocator_type &alloc) const
virtual copy constructor with a given allocator
IndepTestG2< ALLOC > & operator=(const IndepTestG2< ALLOC > &from)
copy operator
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)