51#ifndef DOXYGEN_SHOULD_SKIP_THIS
56 template < GUM_Numeric GUM_SCALAR >
58 const BayesNet< GUM_SCALAR >* priorbn) :
59 Prior(learning_db), _prior_bn_(priorbn) {
60 GUM_CONSTRUCTOR(DirichletPriorFromBN)
64 template < GUM_Numeric GUM_SCALAR >
65 DirichletPriorFromBN< GUM_SCALAR >::DirichletPriorFromBN(
const DirichletPriorFromBN& from) :
66 Prior(from), _prior_bn_(from._prior_bn_) {
67 GUM_CONS_CPY(DirichletPriorFromBN)
71 template < GUM_Numeric GUM_SCALAR >
72 DirichletPriorFromBN< GUM_SCALAR >::DirichletPriorFromBN(DirichletPriorFromBN&& from) noexcept :
73 Prior(std::move(from)), _prior_bn_(std::move(from._prior_bn_)) {
74 GUM_CONS_MOV(DirichletPriorFromBN)
78 template < GUM_Numeric GUM_SCALAR >
79 DirichletPriorFromBN< GUM_SCALAR >* DirichletPriorFromBN< GUM_SCALAR >::clone()
const {
80 return new DirichletPriorFromBN(*
this);
85 template < GUM_Numeric GUM_SCALAR >
86 DirichletPriorFromBN< GUM_SCALAR >::~DirichletPriorFromBN() {
87 GUM_DESTRUCTOR(DirichletPriorFromBN)
91 template < GUM_Numeric GUM_SCALAR >
92 DirichletPriorFromBN< GUM_SCALAR >&
93 DirichletPriorFromBN< GUM_SCALAR >::operator=(
const DirichletPriorFromBN& from) {
95 Prior::operator=(from);
96 _prior_bn_ = from._prior_bn_;
102 template < GUM_Numeric GUM_SCALAR >
103 DirichletPriorFromBN< GUM_SCALAR >&
104 DirichletPriorFromBN< GUM_SCALAR >::operator=(DirichletPriorFromBN&& from) {
106 Prior::operator=(std::move(from));
107 _prior_bn_ = from._prior_bn_;
114 template < GUM_Numeric GUM_SCALAR >
115 PriorType DirichletPriorFromBN< GUM_SCALAR >::getType()
const {
116 return PriorType::DirichletPriorType;
121 template < GUM_Numeric GUM_SCALAR >
122 bool DirichletPriorFromBN< GUM_SCALAR >::isInformative()
const {
123 return (this->weight_ != 0.0);
127 template < GUM_Numeric GUM_SCALAR >
128 void DirichletPriorFromBN< GUM_SCALAR >::setWeight(
const double weight) {
129 Prior::setWeight(weight);
133 template < GUM_Numeric GUM_SCALAR >
134 void DirichletPriorFromBN< GUM_SCALAR >::addJointPseudoCount(
const IdCondSet& idset,
135 std::vector< double >& counts) {
136 if (this->weight_ == 0.0)
return;
137 const auto [X, Y] = idset.toNodeSets();
140 for (
auto i = std::size_t(0); i < idset.size(); i++) {
141 Ijoint.
add(_prior_bn_->variable(idset.ids()[i]));
144 _addCountsForJoint_(Ijoint, X + Y, counts);
148 template < GUM_Numeric GUM_SCALAR >
149 void DirichletPriorFromBN< GUM_SCALAR >::addConditioningPseudoCount(
150 const IdCondSet& idset,
151 std::vector< double >& counts) {
152 if (this->weight_ == 0.0)
return;
153 const auto [X, Y] = idset.toNodeSets();
155 for (
auto i = idset.nbLHSIds(); i < idset.size(); i++)
156 Ijoint.
add(_prior_bn_->variable(idset.ids()[i]));
157 _addCountsForJoint_(Ijoint, Y, counts);
160 template < GUM_Numeric GUM_SCALAR >
161 void DirichletPriorFromBN< GUM_SCALAR >::_addCountsForJoint_(Instantiation& Ijoint,
162 const NodeSet& joint,
163 std::vector< double >& counts) {
164 const auto size = counts.size();
165 if (size != Ijoint.domainSize())
167 "The size of counts ("
168 << size <<
") does not match with the size of the asked prior (" << joint
169 <<
" : " << Ijoint.domainSize() <<
")")
172 counts[0] += weight_;
176 LazyPropagation lazy(_prior_bn_);
178 Tensor< GUM_SCALAR > p;
179 if (joint.size() == 1) {
180 const auto& target = *joint.begin();
181 lazy.addTarget(target);
182 lazy.makeInference();
183 p = lazy.posterior(target);
185 lazy.addJointTarget(joint);
186 lazy.makeInference();
187 p = lazy.jointPosterior(joint);
189 if (weight_ != 1) p.scale(weight_);
192 for (
auto i = std::size_t(0); i < size; ++i) {
193 counts[i] += p[Ijoint];
A dirichlet priori: computes its N'_ijk from a bayesian network.
Exception base for argument error.
Class for assigning/browsing values to tuples of discrete variables.
void add(const DiscreteVariable &v) final
Adds a new variable in the Instantiation.
The class representing a tabular database as used by learning tasks.
DirichletPriorFromBN(const DatabaseTable &learning_db, const BayesNet< GUM_SCALAR > *priorbn)
default constructor
the base class for all a priori
#define GUM_ERROR(type, msg)
include the inlined functions if necessary