bpp-core3  3.0.0
InvariantMixedDiscreteDistribution.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #include "../../Utils/MapTools.h"
7 
8 using namespace bpp;
9 using namespace std;
10 
11 /******************************************************************************/
12 
14  unique_ptr<DiscreteDistributionInterface> dist, double p, double invariant) :
15  AbstractDiscreteDistribution(1, "Invariant."),
16  dist_(std::move(dist)),
17  invariant_(invariant),
18  p_(p),
19  nestedPrefix_(dist_->getNamespace())
20 {
21  // We first change the namespace of the nested distribution:
22  dist_->setNamespace("Invariant." + nestedPrefix_);
23  addParameters_(dist_->getIndependentParameters());
25 
27 }
28 
29 /******************************************************************************/
30 
32 {
34  p_ = getParameterValue("p");
35  dist_->matchParametersValues(parameters);
36 
38 }
39 
40 /******************************************************************************/
41 
43 {
44  distribution_.clear();
45  bounds_.clear();
46 
47  size_t distNCat = dist_->getNumberOfCategories();
48  vector<double> probs = dist_->getProbabilities();
49  vector<double> cats = dist_->getCategories();
50 
52  for (size_t i = 0; i < distNCat; i++)
53  {
54  if (cats[i] == invariant_)
55  distribution_[invariant_] += (1. - p_) * probs[i];
56  else
57  distribution_[cats[i]] = (1. - p_) * probs[i];
58  }
59 
60  intMinMax_->setLowerBound(dist_->getLowerBound(), !dist_->strictLowerBound());
61  intMinMax_->setUpperBound(dist_->getUpperBound(), !dist_->strictUpperBound());
62 
63  if (invariant_ <= intMinMax_->getLowerBound())
64  intMinMax_->setLowerBound(invariant_, true);
65  if (invariant_ >= intMinMax_->getUpperBound())
66  intMinMax_->setUpperBound(invariant_, true);
67 
69 
70  // bounds_
71 
72  // if invariant_ is between 2 values of dist_, bounds_ are set in the
73  // middle of the 3 values
74 
75  bool nv = true;
76 
77  double a = dist_->getCategory(0), b;
78  if (nv && (invariant_ < a))
79  {
80  bounds_.push_back((a + invariant_) / 2);
81  nv = false;
82  }
83 
84  for (size_t i = 1; i < distNCat; i++)
85  {
86  b = dist_->getCategory(i);
87  if (nv && (invariant_ < b))
88  {
89  bounds_.push_back((a + invariant_) / 2);
90  bounds_.push_back((invariant_ + b) / 2);
91  nv = false;
92  }
93  else
94  bounds_.push_back(dist_->getBound(i - 1));
95  a = b;
96  }
97 
98  if (nv)
99  bounds_.push_back((a + invariant_) / 2);
100 }
101 
102 /******************************************************************************/
103 
105 {
107  // We also need to update the namespace of the nested distribution:
108  dist_->setNamespace(prefix + nestedPrefix_);
109 }
110 
111 /******************************************************************************/
112 
114 {
115  if (!c.isCorrect(invariant_))
116  throw ConstraintException("Impossible to restrict to Constraint", &getParameter_("p"), invariant_);
117 
118  dist_->restrictToConstraint(c);
120 }
InvariantMixedDiscreteDistribution(std::unique_ptr< DiscreteDistributionInterface > dist, double p, double invariant=0.)
Build a new InvariantMixedDiscreteDistribution object.
Partial implementation of the DiscreteDistribution interface.
The constraint interface.
Definition: Constraints.h:28
STL namespace.
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:97
void addParameter_(Parameter *parameter)
void setNamespace(const std::string &prefix)
Set the namespace for the parameter names.
std::unique_ptr< DiscreteDistributionInterface > dist_
double getParameterValue(const std::string &name) const override
Get the value for parameter of name &#39;name&#39;.
The parameter list object.
Definition: ParameterList.h:27
virtual void fireParameterChanged(const ParameterList &parameters)
Notify the class when one or several parameters have changed.
virtual bool isCorrect(double value) const =0
Tell if a given value is correct.
std::shared_ptr< IntervalConstraint > intMinMax_
the interval where the distribution is defined/restricted.
void addParameters_(const ParameterList &parameters)
void restrictToConstraint(const ConstraintInterface &c)
Restricts the distribution to the domain where the constraint is respected, in addition of other pred...
void fireParameterChanged(const ParameterList &parameters)
Notify the class when one or several parameters have changed.
double getLowerBound() const
methods about the range of the definition
std::map< double, double, Order > distribution_
static const std::shared_ptr< IntervalConstraint > PROP_CONSTRAINT_IN
Definition: Parameter.h:311
void setNamespace(const std::string &prefix)
Set the namespace for the parameter names.
Exception thrown when a value do not match a given constraint.
Parameter & getParameter_(const std::string &name)