aGrUM  0.14.2
leafPair.cpp
Go to the documentation of this file.
1 /*********************************************************************************
2  * Copyright (C) 2005 by Pierre-Henri WUILLEMIN et Christophe GONZALES *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  *********************************************************************************/
26 // =======================================================
27 #include <agrum/core/math/math.h>
30 // =======================================================
31 
32 
33 namespace gum {
34 
35  // ############################################################################
36  // Miscelleanous Methods
37  // ############################################################################
38 
40  __likelyhood1 = 0.0;
41  __likelyhood2 = 0.0;
42 
43  if (!__l1->total() || !__l2->total()) {
44  __likelyhood1 = std::numeric_limits< double >::max();
45  __likelyhood2 = std::numeric_limits< double >::max();
46  return;
47  }
48 
49  double scaleFactor1 = __l1->total() / (__l1->total() + __l2->total());
50  double scaleFactor2 = __l2->total() / (__l1->total() + __l2->total());
51  for (Idx moda = 0; moda < __l1->nbModa(); ++moda) {
52  if (__l1->effectif(moda)) {
53  double add =
54  __l1->effectif(moda)
55  * std::log(
56  __l1->effectif(moda)
57  / (scaleFactor1 * (__l1->effectif(moda) + __l2->effectif(moda))));
58  __likelyhood1 += add;
59  }
60  if (__l2->effectif(moda)) {
61  double add =
62  __l2->effectif(moda)
63  * std::log(
64  __l2->effectif(moda)
65  / (scaleFactor2 * (__l1->effectif(moda) + __l2->effectif(moda))));
66  __likelyhood2 += add;
67  }
68  }
69 
70  __likelyhood1 *= 2;
71  __likelyhood2 *= 2;
72  }
73 
75  // updateLikelyhood();
76  return 1
78  : __likelyhood2,
79  (__l1->nbModa() - 1));
80  }
81 
82  std::string LeafPair::toString() {
83  std::stringstream ss;
84  ss << "\t[ Leaf1 : " << __l1->toString() << " - Leaf2 : " << __l2->toString();
85  // ss << " - L1 Total : " << __l1->total() << " - L2 Total : " <<
86  // __l2->total();
87  // for( Idx moda = 0; moda < __l1->nbModa(); ++moda )
88  // ss << "~ M=" << moda << ".L1=" << __l1->effectif(moda) << ".L2="
89  // << __l2->effectif(moda) << " ~";
90  ss << " - GStat : " << this->likelyhood() << " ]";
91  return ss.str();
92  }
93 
94 } // namespace gum
Useful macros for maths.
virtual double effectif(Idx) const =0
Gaves the leaf effectif for given modality.
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
Headers of the ChiSquare class.
virtual std::string toString()=0
virtual Idx nbModa() const =0
double __likelyhood2
Definition: leafPair.h:127
static double probaChi2(double x, Size df)
computes the probability of chi2 value (used by the cache)
Definition: chiSquare.cpp:115
AbstractLeaf * __l2
Definition: leafPair.h:124
Size Idx
Type for indexes.
Definition: types.h:50
double __likelyhood1
Definition: leafPair.h:126
Headers of the leaf pair class.
std::string toString()
Definition: leafPair.cpp:82
virtual double total() const =0
double likelyhood()
Updates GStatistic.
Definition: leafPair.cpp:74
AbstractLeaf * __l1
Definition: leafPair.h:123
void updateLikelyhood()
Updates GStatistic.
Definition: leafPair.cpp:39