1 //                                               -*- C++ -*-
2 /**
3  *  @brief The UserDefined distribution
4  *
5  *  Copyright 2005-2021 Airbus-EDF-IMACS-ONERA-Phimeca
6  *
7  *  This library is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU Lesser General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This library is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public License
18  *  along with this library.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 #ifndef OPENTURNS_USERDEFINED_HXX
22 #define OPENTURNS_USERDEFINED_HXX
23 
24 #include "openturns/OTprivate.hxx"
25 #include "openturns/DiscreteDistribution.hxx"
26 #include "openturns/PersistentCollection.hxx"
27 #include "openturns/PersistentObject.hxx"
28 #include "openturns/Indices.hxx"
29 #include "openturns/Distribution.hxx"
30 
31 BEGIN_NAMESPACE_OPENTURNS
32 
33 /**
34  * @class UserDefined
35  *
36  * The UserDefined distribution.
37  */
38 class OT_API UserDefined
39   : public DiscreteDistribution
40 {
41   CLASSNAME
42 public:
43 
44   /** Default constructor */
45   UserDefined();
46 
47   /** Constructor from a sample */
48   explicit UserDefined(const Sample & points);
49 
50   /** Constructor from a sample and associated weights */
51   UserDefined(const Sample & points,
52               const Point & weights);
53 
54 
55   /** Comparison operator */
56   Bool operator ==(const UserDefined & other) const;
57 protected:
58   Bool equals(const DistributionImplementation & other) const override;
59 public:
60 
61   /** String converter */
62   String __repr__() const override;
63   String __str__(const String & offset = "") const override;
64 
65 
66 
67   /* Interface inherited from Distribution */
68 
69   /** Virtual constructor */
70   UserDefined * clone() const override;
71 
72   /** Get one realization of the distribution */
73   Point getRealization() const override;
74 
75   /** Get a sample of the distribution */
76   Sample getSample(const UnsignedInteger size) const override;
77 
78   /** Get the PDF of the distribution */
79   using DiscreteDistribution::computePDF;
80   Scalar computePDF(const Point & point) const override;
81 
82   /** Get the CDF of the distribution */
83   using DiscreteDistribution::computeCDF;
84   Scalar computeCDF(const Point & point) const override;
85 
86   /** Get the PDFGradient of the distribution */
87   using DiscreteDistribution::computePDFGradient;
88   Point computePDFGradient(const Point & point) const override;
89 
90   /** Get the CDFGradient of the distribution */
91   using DiscreteDistribution::computeCDFGradient;
92   Point computeCDFGradient(const Point & point) const override;
93 
94   /** Compute the numerical range of the distribution given the parameters values */
95   void computeRange() override;
96 
97   /** Get the support of a discrete distribution that intersect a given interval */
98   using DistributionImplementation::getSupport;
99   Sample getSupport(const Interval & interval) const override;
100 
101   /** Tell if the distribution is integer valued */
102   Bool isIntegral() const override;
103 
104   /** Parameters value and description accessor */
105   PointWithDescriptionCollection getParametersCollection() const override;
106 
107   /** Parameters value accessors */
108   void setParameter(const Point & parameter) override;
109   Point getParameter() const override;
110 
111   /** Parameters description accessor */
112   Description getParameterDescription() const override;
113 
114   /* Interface specific to UserDefined */
115 
116   /** Data accessors */
117   void setData(const Sample & points,
118                const Point & weights);
119   Sample getX() const;
120   Point getP() const;
121 
122   /** Get the i-th marginal distribution */
123   Distribution getMarginal(const UnsignedInteger i) const override;
124 
125   /** Get the distribution of the marginal distribution corresponding to indices dimensions */
126   Distribution getMarginal(const Indices & indices) const override;
127 
128   /** Merge the identical points of the support */
129   void compactSupport(const Scalar epsilon = ResourceMap::GetAsScalar("DiscreteDistribution-SupportEpsilon"));
130 
131   /** Tell if the distribution has elliptical copula */
132   Bool hasEllipticalCopula() const override;
133 
134   /** Tell if the distribution has independent copula */
135   Bool hasIndependentCopula() const override;
136 
137   /** Method save() stores the object through the StorageManager */
138   void save(Advocate & adv) const override;
139 
140   /** Method load() reloads the object from the StorageManager */
141   void load(Advocate & adv) override;
142 
143 protected:
144 
145 private:
146 
147   /** Compute the mean of the distribution */
148   void computeMean() const override;
149 
150   /** Compute the covariance of the distribution */
151   void computeCovariance() const override;
152 
153   /** Quantile computation for dimension=1 */
154   Scalar computeScalarQuantile(const Scalar prob, const Bool tail = false) const override;
155 
156   /** The collection of couple (xi, pi) */
157   Sample points_;
158   Point probabilities_;
159 
160   /** The cumulative probabilities si = sum(pk, k=0..i) */
161   Point cumulativeProbabilities_;
162 
163   /** Flag to accelerate computations in case of uniform weights */
164   Bool hasUniformWeights_;
165 
166   /** Structures for the alias sampling method */
167   mutable Point base_;
168   mutable Indices alias_;
169 }; /* class UserDefined */
170 
171 END_NAMESPACE_OPENTURNS
172 
173 #endif /* OPENTURNS_USERDEFINED_HXX */
174