aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
multiDimLogit_tpl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40#pragma once
41
42
47
51
52namespace gum {
53
54 // Default constructor
55 template < typename GUM_SCALAR >
56 INLINE MultiDimLogit< GUM_SCALAR >::MultiDimLogit(GUM_SCALAR external_weight,
57 GUM_SCALAR default_weight) :
58 MultiDimICIModel< GUM_SCALAR >(external_weight, default_weight) {
59 GUM_CONSTRUCTOR(MultiDimLogit);
60 }
61
62 // Default constructor
63 template < typename GUM_SCALAR >
65 MultiDimICIModel< GUM_SCALAR >(from) {
66 GUM_CONS_CPY(MultiDimLogit);
67 }
68
69 // Copy constructor using a bijection to replace variables from source.
70 template < typename GUM_SCALAR >
76
77 // destructor
78 template < typename GUM_SCALAR >
82
83 template < typename GUM_SCALAR >
85 if (this->nbrDim() < 1) { GUM_ERROR(OperationNotAllowed, "Not enough variable for a Logit") }
86
87 const DiscreteVariable& C = this->variable((Idx)0);
88
89 if (i.val(C) > 1) return (GUM_SCALAR)0.0;
90
91 GUM_SCALAR fact = this->externalWeight();
92
93 for (Idx j = 1; j < this->nbrDim(); j++) {
94 const DiscreteVariable& v = this->variable(j);
95 fact += GUM_SCALAR(this->causalWeight(v) * this->variable(j).numerical(i.val(v)));
96 }
97
98 fact = 1 / (1 + std::exp(-fact)); // or std::exp(fact)/(1+std::exp(fact))
99 auto res = (i.val(C) == 1) ? fact : (GUM_SCALAR)1.0 - fact;
100
101 return res;
102 }
103
104 template < typename GUM_SCALAR >
106 std::stringstream s;
107 s << this->variable(0) << "=logit(" << this->externalWeight();
108
109 for (Idx i = 1; i < this->nbrDim(); i++) {
110 GUM_SCALAR c = this->causalWeight(this->variable(i));
111
112 if (c != GUM_SCALAR(0)) {
113 s << " ";
114
115 if (c > 0) s << "+";
116
117 s << this->causalWeight(this->variable(i)) << "*" << this->variable(i);
118 }
119 }
120
121 s << ")";
122
123 return s.str();
124 }
125
126 // For friendly displaying the content of the variable.
127 template < typename GUM_SCALAR >
128 INLINE std::ostream& operator<<(std::ostream& s, const MultiDimLogit< GUM_SCALAR >& ag) {
129 return s << ag.toString();
130 }
131
132 template < typename GUM_SCALAR >
136
137 // returns the name of the implementation
138 template < typename GUM_SCALAR >
139 INLINE const std::string& MultiDimLogit< GUM_SCALAR >::name() const {
140 static const std::string str = "MultiDimLogit";
141 return str;
142 }
143
144} /* namespace gum */
Set of pairs of elements with fast search for both elements.
Definition bijection.h:1594
Base class for discrete random variable.
Class for assigning/browsing values to tuples of discrete variables.
Idx val(Idx i) const
Returns the current value of the variable at position i.
Abstract base class for all multi dimensionnal containers.
GUM_SCALAR externalWeight() const
Copy of a multiDimICIModel.
GUM_SCALAR _default_weight_
in Henrion (89) in a hashtable with a default_value.
MultiDimICIModel(GUM_SCALAR external_weight, GUM_SCALAR default_weight=(GUM_SCALAR) 1.0)
Default constructor.
GUM_SCALAR causalWeight(const DiscreteVariable &v) const
Copy of a multiDimICIModel.
GUM_SCALAR _external_weight_
in Henrion (89).
virtual Idx nbrDim() const override
Returns the number of vars in the multidimensional container.
const DiscreteVariable & variable(Idx i) const override
Returns a const ref to the ith var.
Logit representation.
virtual ~MultiDimLogit()
Destructor.
virtual const std::string & name() const
Returns the real name of the multiDimArray.
virtual MultiDimContainer< GUM_SCALAR > * newFactory() const
This method creates a clone of this object, withouth its content (including variable),...
MultiDimLogit(GUM_SCALAR external_weight, GUM_SCALAR default_weight=(GUM_SCALAR) 0.0)
Default constructor.
std::string toString() const
Returns the real name of the multiDimArray.
virtual GUM_SCALAR get(const Instantiation &i) const
Returns the real name of the multiDimArray.
Exception : operation not allowed.
aGrUM's exceptions
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
Size Idx
Type for indexes.
Definition types.h:79
Headers of gum::MultiDimImplementation.
class for LOGIT implementation as multiDim
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
std::ostream & operator<<(std::ostream &stream, const AVLTree< Val, Cmp > &tree)
display the content of a tree
Definition AVLTree.h:913