1 //                                               -*- C++ -*-
2 /**
3  *  @brief The test file of class SobolIndicesAlgorithm
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 
main(int,char * [])27 int main(int, char *[])
28 {
29   TESTPREAMBLE;
30   OStream fullprint(std::cout);
31   try
32   {
33 
34     RandomGenerator::SetSeed(0);
35 
36     UnsignedInteger inputDimension = 3;
37     //UnsignedInteger outputDimension = 1;
38 
39     Description inputName(inputDimension);
40     inputName[0] = "X1";
41     inputName[1] = "X2";
42     inputName[2] = "X3";
43     Description formula(1);
44     formula[0] = "sin(pi_*X1)+7*sin(pi_*X2)*sin(pi_*X2)+0.1*((pi_*X3)*(pi_*X3)*(pi_*X3)*(pi_*X3))*sin(pi_*X1)";
45 
46     SymbolicFunction model(inputName, formula);
47 
48     ComposedDistribution::DistributionCollection marginals(inputDimension);
49     marginals[0] = Uniform(-1.0, 1.0);
50     //     marginals[0].setDescription("Marginal 1");
51     marginals[1] = Uniform(-1.0, 1.0);
52     //     marginals[1].setDescription("Marginal 2");
53     marginals[2] = Uniform(-1.0, 1.0);
54     //     marginals[2].setDescription("Marginal 3");
55     ComposedDistribution maDistribution(ComposedDistribution(marginals, IndependentCopula(inputDimension)));
56 
57 
58     const UnsignedInteger size = 10000;
59     const UnsignedInteger nr_bootstrap = 100;
60     const Scalar confidence_level = 0.95;
61 
62     Description methods;
63     methods.add("MonteCarlo");
64     methods.add("LHS");
65     methods.add("QMC");
66     for (UnsignedInteger i = 0; i < methods.getSize(); ++i)
67     {
68       ResourceMap::SetAsString("SobolIndicesExperiment-SamplingMethod", methods[i]);
69       fullprint << "Sampling method=" << methods[i] << std::endl;
70       const SobolIndicesExperiment sobolExperiment(maDistribution, size, true);
71       const Sample inputDesign(sobolExperiment.generate());
72       const Sample outputDesign(model(inputDesign));
73 
74       {
75         SaltelliSensitivityAlgorithm sensitivitySobol(inputDesign, outputDesign, size);
76 
77         const SymmetricMatrix secondOrderIndices( sensitivitySobol.getSecondOrderIndices() );
78         const Point firstOrderIndices(sensitivitySobol.getFirstOrderIndices());
79         const Point totalOrderIndices(sensitivitySobol.getTotalOrderIndices());
80 
81         fullprint << "Method = " << sensitivitySobol.getClassName() << std::endl;
82         fullprint << "First order Sobol indice of Y|X1 = " << firstOrderIndices[0] << std::endl;
83         fullprint << "Total order Sobol indice of Y|X3 = " << totalOrderIndices[2] << std::endl;
84         fullprint << "Second order Sobol indice of Y|X1,X3 = " << secondOrderIndices(0, 2) << std::endl;
85         // Confidence interval
86         sensitivitySobol.setBootstrapSize(nr_bootstrap);
87         sensitivitySobol.setConfidenceLevel(confidence_level);
88 
89         const Interval confidenceIntervalFirstOrder(sensitivitySobol.getFirstOrderIndicesInterval());
90         const Interval confidenceIntervalTotalOrder(sensitivitySobol.getTotalOrderIndicesInterval());
91         fullprint << "Confidence interval of first order Y|X1 = [" << confidenceIntervalFirstOrder.getLowerBound()[0]
92                   << ", " << confidenceIntervalFirstOrder.getUpperBound()[0] << "]" << std::endl;
93         fullprint << "Confidence interval of total order Y|X3 = [" << confidenceIntervalTotalOrder.getLowerBound()[2]
94                   << ", " << confidenceIntervalTotalOrder.getUpperBound()[2] << "]" << std::endl;
95       }
96 
97       {
98         JansenSensitivityAlgorithm sensitivitySobol(inputDesign, outputDesign, size);
99 
100         const SymmetricMatrix secondOrderIndices( sensitivitySobol.getSecondOrderIndices() );
101         const Point firstOrderIndices(sensitivitySobol.getFirstOrderIndices());
102         const Point totalOrderIndices(sensitivitySobol.getTotalOrderIndices());
103 
104         fullprint << "Method = " << sensitivitySobol.getClassName() << std::endl;
105         fullprint << "First order Sobol indice of Y|X1 = " << firstOrderIndices[0] << std::endl;
106         fullprint << "Total order Sobol indice of Y|X3 = " << totalOrderIndices[2] << std::endl;
107         fullprint << "Second order Sobol indice of Y|X1,X3 = " << secondOrderIndices(0, 2) << std::endl;
108         // Confidence interval
109         sensitivitySobol.setBootstrapSize(nr_bootstrap);
110         sensitivitySobol.setConfidenceLevel(confidence_level);
111 
112         const Interval confidenceIntervalFirstOrder(sensitivitySobol.getFirstOrderIndicesInterval());
113         const Interval confidenceIntervalTotalOrder(sensitivitySobol.getTotalOrderIndicesInterval());
114         fullprint << "Confidence interval of first order Y|X1 = [" << confidenceIntervalFirstOrder.getLowerBound()[0]
115                   << ", " << confidenceIntervalFirstOrder.getUpperBound()[0] << "]" << std::endl;
116         fullprint << "Confidence interval of total order Y|X3 = [" << confidenceIntervalTotalOrder.getLowerBound()[2]
117                   << ", " << confidenceIntervalTotalOrder.getUpperBound()[2] << "]" << std::endl;
118       }
119       {
120         MauntzKucherenkoSensitivityAlgorithm sensitivitySobol(inputDesign, outputDesign, size);
121 
122         const SymmetricMatrix secondOrderIndices( sensitivitySobol.getSecondOrderIndices() );
123         const Point firstOrderIndices(sensitivitySobol.getFirstOrderIndices());
124         const Point totalOrderIndices(sensitivitySobol.getTotalOrderIndices());
125 
126         fullprint << "Method = " << sensitivitySobol.getClassName() << std::endl;
127         fullprint << "First order Sobol indice of Y|X1 = " << firstOrderIndices[0] << std::endl;
128         fullprint << "Total order Sobol indice of Y|X3 = " << totalOrderIndices[2] << std::endl;
129         fullprint << "Second order Sobol indice of Y|X1,X3 = " << secondOrderIndices(0, 2) << std::endl;
130         // Confidence interval
131         sensitivitySobol.setBootstrapSize(nr_bootstrap);
132         sensitivitySobol.setConfidenceLevel(confidence_level);
133 
134         const Interval confidenceIntervalFirstOrder(sensitivitySobol.getFirstOrderIndicesInterval());
135         const Interval confidenceIntervalTotalOrder(sensitivitySobol.getTotalOrderIndicesInterval());
136         fullprint << "Confidence interval of first order Y|X1 = [" << confidenceIntervalFirstOrder.getLowerBound()[0]
137                   << ", " << confidenceIntervalFirstOrder.getUpperBound()[0] << "]" << std::endl;
138         fullprint << "Confidence interval of total order Y|X3 = [" << confidenceIntervalTotalOrder.getLowerBound()[2]
139                   << ", " << confidenceIntervalTotalOrder.getUpperBound()[2] << "]" << std::endl;
140       }
141       {
142         MartinezSensitivityAlgorithm sensitivitySobol(inputDesign, outputDesign, size);
143         const SymmetricMatrix secondOrderIndices( sensitivitySobol.getSecondOrderIndices() );
144         const Point firstOrderIndices(sensitivitySobol.getFirstOrderIndices());
145         const Point totalOrderIndices(sensitivitySobol.getTotalOrderIndices());
146 
147         fullprint << "Method = " << sensitivitySobol.getClassName() << std::endl;
148         fullprint << "First order Sobol indice of Y|X1 = " << firstOrderIndices[0] << std::endl;
149         fullprint << "Total order Sobol indice of Y|X3 = " << totalOrderIndices[2] << std::endl;
150         fullprint << "Second order Sobol indice of Y|X1,X3 = " << secondOrderIndices(0, 2) << std::endl;
151         // Confidence interval
152         sensitivitySobol.setBootstrapSize(nr_bootstrap);
153         sensitivitySobol.setConfidenceLevel(confidence_level);
154 
155         const Interval confidenceIntervalFirstOrder(sensitivitySobol.getFirstOrderIndicesInterval());
156         const Interval confidenceIntervalTotalOrder(sensitivitySobol.getTotalOrderIndicesInterval());
157         fullprint << "Confidence interval of first order Y|X1 = [" << confidenceIntervalFirstOrder.getLowerBound()[0]
158                   << ", " << confidenceIntervalFirstOrder.getUpperBound()[0] << "]" << std::endl;
159         fullprint << "Confidence interval of total order Y|X3 = [" << confidenceIntervalTotalOrder.getLowerBound()[2]
160                   << ", " << confidenceIntervalTotalOrder.getUpperBound()[2] << "]" << std::endl;
161 
162 
163         fullprint << "Asymptotic estimate" << std::endl;
164         sensitivitySobol.setUseAsymptoticDistribution(true);
165         const Interval asymptoticConfidenceIntervalFirstOrder(sensitivitySobol.getFirstOrderIndicesInterval());
166         const Interval asymptoticConfidenceIntervalTotalOrder(sensitivitySobol.getTotalOrderIndicesInterval());
167         fullprint << "Confidence interval of first order Y|X1 = [" << asymptoticConfidenceIntervalFirstOrder.getLowerBound()[0]
168                   << ", " << asymptoticConfidenceIntervalFirstOrder.getUpperBound()[0] << "]" << std::endl;
169         fullprint << "Confidence interval of total order Y|X3 = [" << asymptoticConfidenceIntervalTotalOrder.getLowerBound()[2]
170                   << ", " << asymptoticConfidenceIntervalTotalOrder.getUpperBound()[2] << "]" << std::endl;
171         fullprint << "First order indices distribution = " << sensitivitySobol.getFirstOrderIndicesDistribution() << std::endl;
172         fullprint << "Total order indices distribution = " << sensitivitySobol.getTotalOrderIndicesDistribution() << std::endl;
173       }
174     } // Sampling method
175   }
176   catch (TestFailed & ex)
177   {
178     std::cerr << ex << std::endl;
179     return ExitCode::Error;
180   }
181 
182   return ExitCode::Success;
183 }
184