aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
indepTestG2.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 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. */
81  const DBRowGeneratorParser< ALLOC >& parser,
82  const Apriori< ALLOC >& external_apriori,
83  const std::vector< std::pair< std::size_t, std::size_t >,
84  ALLOC< std::pair< std::size_t, std::size_t > > >&
85  ranges,
86  const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
88  = Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(),
90 
91 
92  /// default constructor
93  /** @param parser the parser used to parse the database
94  * @param apriori An apriori that we add to the computation of the score
95  * @param nodeId2Columns a mapping from the ids of the nodes in the
96  * graphical model to the corresponding column in the DatabaseTable
97  * parsed by the parser. This enables estimating from a database in
98  * which variable A corresponds to the 2nd column the parameters of a BN
99  * in which variable A has a NodeId of 5. An empty nodeId2Columns
100  * bijection means that the mapping is an identity, i.e., the value of a
101  * NodeId is equal to the index of the column in the DatabaseTable.
102  * @param alloc the allocator used to allocate the structures within the
103  * Score.
104  * @warning If nodeId2columns is not empty, then only the scores over the
105  * ids belonging to this bijection can be computed: applying method
106  * score() over other ids will raise exception NotFound. */
107  IndepTestG2(const DBRowGeneratorParser< ALLOC >& parser,
108  const Apriori< ALLOC >& apriori,
109  const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
111  = Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(),
112  const allocator_type& alloc = allocator_type());
113 
114  /// copy constructor
115  IndepTestG2(const IndepTestG2< ALLOC >& from);
116 
117  /// copy constructor with a given allocator
118  IndepTestG2(const IndepTestG2< ALLOC >& from, const allocator_type& alloc);
119 
120  /// move constructor
121  IndepTestG2(IndepTestG2< ALLOC >&& from);
122 
123  /// move constructor with a given allocator
124  IndepTestG2(IndepTestG2< ALLOC >&& from, const allocator_type& alloc);
125 
126  /// virtual copy constructor
127  virtual IndepTestG2< ALLOC >* clone() const;
128 
129  /// virtual copy constructor with a given allocator
130  virtual IndepTestG2< ALLOC >* clone(const allocator_type& alloc) const;
131 
132  /// destructor
133  virtual ~IndepTestG2();
134 
135  /// @}
136 
137 
138  // ##########################################################################
139  /// @name Operators
140  // ##########################################################################
141 
142  /// @{
143 
144  /// copy operator
146 
147  /// move operator
149 
150  /// get the pair <G2statistic,pvalue> for a test var1 indep var2 given
151  /// rhs_ids
152  std::pair< double, double >
154  NodeId var2,
155  const std::vector< NodeId, ALLOC< NodeId > >& rhs_ids = {});
156 
157  /// @}
158 
159 
160  protected:
161  /// returns the score for a given IdCondSet
162  /** @throws OperationNotAllowed is raised if the score does not support
163  * calling method score such an idset (due to too many/too few variables
164  * in the left hand side or the right hand side of the idset). */
165  virtual double score_(const IdCondSet< ALLOC >& idset) final;
166 
167  /// compute the pair <G2 statistic,pvalue>
168  std::pair< double, double > statistics_(const IdCondSet< ALLOC >& idset);
169 
170 #ifndef DOXYGEN_SHOULD_SKIP_THIS
171 
172  private:
173  /// the domain sizes of the variables
175 
176  /// a chi2 distribution for computing critical values
177  Chi2 chi2__;
178 
179  /// an empty vector of ids
180  const std::vector< Idx > empty_set__;
181 
182 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
183  };
184 
185  } /* namespace learning */
186 
187 } /* namespace gum */
188 
189 
190 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
191 extern template class gum::learning::IndepTestG2<>;
192 #endif
193 
194 
195 // always include the template implementation
196 #include <agrum/tools/stattests/indepTestG2_tpl.h>
197 
198 #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:669
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)