1 // -*- C++ -*-
2 /**
3 * @brief The test file of class Laplace for standard methods
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 #include "openturns/OT.hxx"
22 #include "openturns/OTtestcode.hxx"
23
24 using namespace OT;
25 using namespace OT::Test;
26
27 class TestObject : public Laplace
28 {
29 public:
TestObject()30 TestObject() : Laplace(0.5, 1.5) {}
~TestObject()31 virtual ~TestObject() {}
32 };
33
34
main(int,char * [])35 int main(int, char *[])
36 {
37 TESTPREAMBLE;
38 OStream fullprint(std::cout);
39 setRandomGenerator();
40
41 try
42 {
43 // Test basic functionnalities
44 checkClassWithClassName<TestObject>();
45
46 // Instanciate one distribution object
47 Laplace distribution(0.5, 1.0 / 1.5);
48 fullprint << "Distribution " << distribution << std::endl;
49 std::cout << "Distribution " << distribution << std::endl;
50
51 // Is this distribution elliptical ?
52 fullprint << "Elliptical = " << (distribution.isElliptical() ? "true" : "false") << std::endl;
53
54 // Is this distribution continuous ?
55 fullprint << "Continuous = " << (distribution.isContinuous() ? "true" : "false") << std::endl;
56
57 // Test for realization of distribution
58 Point oneRealization = distribution.getRealization();
59 fullprint << "oneRealization=" << oneRealization << std::endl;
60
61 // Test for sampling
62 UnsignedInteger size = 10000;
63 Sample oneSample = distribution.getSample( size );
64 fullprint << "oneSample first=" << oneSample[0] << " last=" << oneSample[size - 1] << std::endl;
65 fullprint << "mean=" << oneSample.computeMean() << std::endl;
66 fullprint << "covariance=" << oneSample.computeCovariance() << std::endl;
67 size = 100;
68 for (UnsignedInteger i = 0; i < 2; ++i)
69 {
70 fullprint << "Kolmogorov test for the generator, sample size=" << size << " is " << (FittingTest::Kolmogorov(distribution.getSample(size), distribution).getBinaryQualityMeasure() ? "accepted" : "rejected") << std::endl;
71 size *= 10;
72 }
73
74 // Define a point
75 Point point( distribution.getDimension(), 1.0 );
76 fullprint << "Point= " << point << std::endl;
77
78 // Show PDF and CDF of point
79 Scalar eps = 1e-5;
80 Point DDF = distribution.computeDDF( point );
81 fullprint << "ddf =" << DDF << std::endl;
82 Scalar LPDF = distribution.computeLogPDF( point );
83 fullprint << "log pdf=" << LPDF << std::endl;
84 Scalar PDF = distribution.computePDF( point );
85 fullprint << "pdf =" << PDF << std::endl;
86 fullprint << "pdf (FD)=" << (distribution.computeCDF( point + Point(1, eps) ) - distribution.computeCDF( point + Point(1, -eps) )) / (2.0 * eps) << std::endl;
87 Scalar CDF = distribution.computeCDF( point );
88 fullprint << "cdf=" << CDF << std::endl;
89 Scalar CCDF = distribution.computeComplementaryCDF( point );
90 fullprint << "ccdf=" << CCDF << std::endl;
91 Scalar Survival = distribution.computeSurvivalFunction( point );
92 fullprint << "survival=" << Survival << std::endl;
93 Point InverseSurvival = distribution.computeInverseSurvivalFunction(0.95);
94 fullprint << "Inverse survival=" << InverseSurvival << std::endl;
95 fullprint << "Survival(inverse survival)=" << distribution.computeSurvivalFunction(InverseSurvival) << std::endl;
96 Complex CF = distribution.computeCharacteristicFunction( point[0] );
97 fullprint << "characteristic function=" << CF << std::endl;
98 Complex LCF = distribution.computeLogCharacteristicFunction( point[0] );
99 fullprint << "log characteristic function=" << LCF << std::endl;
100 Point PDFgr = distribution.computePDFGradient( point );
101 fullprint << "pdf gradient =" << PDFgr << std::endl;
102 Point PDFgrFD(2);
103 PDFgrFD[0] = (Laplace(distribution.getMu() + eps, distribution.getLambda()).computePDF(point) -
104 Laplace(distribution.getMu() - eps, distribution.getLambda()).computePDF(point)) / (2.0 * eps);
105 PDFgrFD[1] = (Laplace(distribution.getMu(), distribution.getLambda() + eps).computePDF(point) -
106 Laplace(distribution.getMu(), distribution.getLambda() - eps).computePDF(point)) / (2.0 * eps);
107 fullprint << "pdf gradient (FD)=" << PDFgrFD << std::endl;
108 Point CDFgr = distribution.computeCDFGradient( point );
109 fullprint << "cdf gradient =" << CDFgr << std::endl;
110 Point CDFgrFD(2);
111 CDFgrFD[0] = (Laplace(distribution.getMu() + eps, distribution.getLambda()).computeCDF(point) -
112 Laplace(distribution.getMu() - eps, distribution.getLambda()).computeCDF(point)) / (2.0 * eps);
113 CDFgrFD[1] = (Laplace(distribution.getMu(), distribution.getLambda() + eps).computeCDF(point) -
114 Laplace(distribution.getMu(), distribution.getLambda() - eps).computeCDF(point)) / (2.0 * eps);
115 fullprint << "cdf gradient (FD)=" << CDFgrFD << std::endl;
116 Point quantile = distribution.computeQuantile( 0.95 );
117 fullprint << "quantile=" << quantile << std::endl;
118 fullprint << "cdf(quantile)=" << distribution.computeCDF(quantile) << std::endl;
119 // Confidence regions
120 Scalar threshold;
121 fullprint << "Minimum volume interval=" << distribution.computeMinimumVolumeIntervalWithMarginalProbability(0.95, threshold) << std::endl;
122 fullprint << "threshold=" << threshold << std::endl;
123 Scalar beta;
124 LevelSet levelSet(distribution.computeMinimumVolumeLevelSetWithThreshold(0.95, beta));
125 fullprint << "Minimum volume level set=" << levelSet << std::endl;
126 fullprint << "beta=" << beta << std::endl;
127 fullprint << "Bilateral confidence interval=" << distribution.computeBilateralConfidenceIntervalWithMarginalProbability(0.95, beta) << std::endl;
128 fullprint << "beta=" << beta << std::endl;
129 fullprint << "Unilateral confidence interval (lower tail)=" << distribution.computeUnilateralConfidenceIntervalWithMarginalProbability(0.95, false, beta) << std::endl;
130 fullprint << "beta=" << beta << std::endl;
131 fullprint << "Unilateral confidence interval (upper tail)=" << distribution.computeUnilateralConfidenceIntervalWithMarginalProbability(0.95, true, beta) << std::endl;
132 fullprint << "beta=" << beta << std::endl;
133 fullprint << "entropy=" << distribution.computeEntropy() << std::endl;
134 fullprint << "entropy (MC)=" << -distribution.computeLogPDF(distribution.getSample(1000000)).computeMean()[0] << std::endl;
135 Point mean = distribution.getMean();
136 fullprint << "mean=" << mean << std::endl;
137 CovarianceMatrix covariance = distribution.getCovariance();
138 fullprint << "covariance=" << covariance << std::endl;
139 CovarianceMatrix correlation = distribution.getCorrelation();
140 fullprint << "correlation=" << correlation << std::endl;
141 CovarianceMatrix spearman = distribution.getSpearmanCorrelation();
142 fullprint << "spearman=" << spearman << std::endl;
143 CovarianceMatrix kendall = distribution.getKendallTau();
144 fullprint << "kendall=" << kendall << std::endl;
145 Laplace::PointWithDescriptionCollection parameters = distribution.getParametersCollection();
146 fullprint << "parameters=" << parameters << std::endl;
147 for (UnsignedInteger i = 0; i < 6; ++i) fullprint << "standard moment n=" << i << ", value=" << distribution.getStandardMoment(i) << std::endl;
148 fullprint << "Standard representative=" << distribution.getStandardRepresentative().__str__() << std::endl;
149
150 // Specific to this distribution
151 Scalar lambda = distribution.getLambda();
152 fullprint << "lambda=" << lambda << std::endl;
153 Scalar mu = distribution.getMu();
154 fullprint << "mu=" << mu << std::endl;
155 Point standardDeviation = distribution.getStandardDeviation();
156 fullprint << "standard deviation=" << standardDeviation << std::endl;
157 Point skewness = distribution.getSkewness();
158 fullprint << "skewness=" << skewness << std::endl;
159 Point kurtosis = distribution.getKurtosis();
160 fullprint << "kurtosis=" << kurtosis << std::endl;
161 }
162 catch (TestFailed & ex)
163 {
164 std::cerr << ex << std::endl;
165 return ExitCode::Error;
166 }
167
168
169 return ExitCode::Success;
170 }
171