aGrUM 3.3.2
a C++ library for (probabilistic) graphical models
multiDimNoisyORCompound_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-2026 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-2026 *
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
41#pragma once
42
43
51
52namespace gum {
53
55
56 template < GUM_Numeric GUM_SCALAR >
58 GUM_SCALAR default_weight) :
59 MultiDimICIModel< GUM_SCALAR >(external_weight, default_weight) {
60 // virtual calls are not dispatched in the base constructor
61 checkCausalWeight_(external_weight, default_weight);
62 GUM_CONSTRUCTOR(MultiDimNoisyORCompound);
63 }
64
65 template < GUM_Numeric GUM_SCALAR >
67 GUM_SCALAR causal_weight) const {
68 this->checkWeightsAreProbabilities_(external_weight, causal_weight);
69 if (causal_weight < external_weight) {
71 "In a NoisyOR compound (Henrion), a causal weight ("
72 << causal_weight << ") cannot be lower than the leak (" << external_weight
73 << ")")
74 }
75 }
76
78
79 template < GUM_Numeric GUM_SCALAR >
84
86
87 template < GUM_Numeric GUM_SCALAR >
94
96
97 template < GUM_Numeric GUM_SCALAR >
101
102 template < GUM_Numeric GUM_SCALAR >
104 if (this->nbrDim() < 1) { GUM_ERROR(OperationNotAllowed, "Not enough variable for a NoisyOr ") }
105
106 const DiscreteVariable& C = this->variable((Idx)0);
107
108 if (i.val(C) > 1) return (GUM_SCALAR)0.0;
109
110 GUM_SCALAR ratio = (GUM_SCALAR)1.0 - this->externalWeight();
111
112 GUM_SCALAR fact = (GUM_SCALAR)ratio;
113
114 if (fact != (GUM_SCALAR)0) {
115 for (Idx j = 1; j < this->nbrDim(); j++) {
116 const DiscreteVariable& v = this->variable(j);
117
118 if (i.val(v) == 1) {
119 GUM_SCALAR pr = (1 - this->causalWeight(v)) / ratio;
120
121 if (pr == (GUM_SCALAR)0.0) {
122 fact = (GUM_SCALAR)0.0;
123 break;
124 } else {
125 fact *= pr;
126 }
127 }
128 }
129 }
130
131 return (i.val(C) != 1) ? fact : (GUM_SCALAR)1.0 - fact;
132 }
133
134 template < GUM_Numeric GUM_SCALAR >
136 std::stringstream s;
137 s << MultiDimImplementation< GUM_SCALAR >::variable(0) << "=noisyORCompound(["
138 << this->externalWeight() << "]";
139
140 for (Idx i = 1; i < MultiDimImplementation< GUM_SCALAR >::nbrDim(); i++)
143
144 s << ")";
145 return s.str();
146 }
147
148 // For friendly displaying the content of the variable.
149
150 template < GUM_Numeric GUM_SCALAR >
151 std::ostream& operator<<(std::ostream& s, const MultiDimNoisyORCompound< GUM_SCALAR >& ag) {
152 return s << ag.toString();
153 }
154
155 template < GUM_Numeric GUM_SCALAR >
160
161 // returns the name of the implementation
162 template < GUM_Numeric GUM_SCALAR >
164 static const std::string str = "MultiDimNoisyORCompound";
165 return str;
166 }
167
168 // ==================================================
169} /* namespace gum */
Set of pairs of elements with fast search for both elements.
Definition bijection.h:1640
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.
static void checkWeightsAreProbabilities_(GUM_SCALAR external_weight, GUM_SCALAR causal_weight)
MultiDimICIModel(GUM_SCALAR external_weight, GUM_SCALAR default_weight=(GUM_SCALAR) 1.0)
GUM_SCALAR causalWeight(const DiscreteVariable &v) const
const DiscreteVariable & variable(Idx i) const override
MultiDimNoisyORCompound(GUM_SCALAR external_weight, GUM_SCALAR default_weight=(GUM_SCALAR) 1.0)
Default constructor.
const std::string & name() const override
returns the real name of the multiDimArray
MultiDimContainer< GUM_SCALAR > * newFactory() const override
This method creates a clone of this object, withouth its content (including variable),...
void checkCausalWeight_(GUM_SCALAR external_weight, GUM_SCALAR causal_weight) const override
In Henrion's parametrization, a causal weight is the probability of the effect when only this cause i...
GUM_SCALAR get(const Instantiation &i) const override
returns the real name of the multiDimArray
std::string toString() const override
returns the real name of the multiDimArray
Exception : operation not allowed.
Exception : out of bound.
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
Size Idx
Type for indexes.
Definition types.h:79
class for multiDimNoisyORCompound
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