File: t_KrigingAlgorithm_std.cxx

package info (click to toggle)
openturns 1.26-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 67,708 kB
  • sloc: cpp: 261,605; python: 67,030; ansic: 4,378; javascript: 406; sh: 185; xml: 164; makefile: 101
file content (187 lines) | stat: -rw-r--r-- 6,294 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//                                               -*- C++ -*-
/**
 *  @brief The test file of KrigingAlgorithm class
 *
 *  Copyright 2005-2025 Airbus-EDF-IMACS-ONERA-Phimeca
 *
 *  This library is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this library.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
#include "openturns/OT.hxx"
#include "openturns/OTtestcode.hxx"

using namespace OT;
using namespace OT::Test;


int main(int, char *[])
{
  TESTPREAMBLE;
  OStream fullprint(std::cout);

  try
  {
    // Set Numerical precision to 3
    PlatformInfo::SetNumericalPrecision(3);
    {

      UnsignedInteger sampleSize = 6;
      UnsignedInteger dimension = 1;

      // Create the function to estimate
      Description input(dimension);
      input[0] = "x0";
      Description formulas(1);
      formulas[0] = "x0 * sin(x0)";
      SymbolicFunction model(input, formulas);

      Sample X(sampleSize, dimension);
      Sample X2(sampleSize, dimension);
      for ( UnsignedInteger i = 0; i < sampleSize; ++ i )
      {
        X(i, 0) = 3.0 + i;
        X2(i, 0) = 2.5 + i;
      }
      X(0, 0) = 1.0;
      X(1, 0) = 3.0;
      X2(0, 0) = 2.0;
      X2(1, 0) = 4.0;
      Sample Y(model(X));
      Sample Y2(model(X2));

      Basis basis(ConstantBasisFactory(dimension).build());
      SquaredExponential covarianceModel(Point(1, 1e-02), Point(1, 4.50736));
      KrigingAlgorithm algo(X, Y, covarianceModel, basis);

      // set sensible optimization bounds and estimate hyperparameters
      algo.setOptimizationBounds(Interval(X.getMin(), X.getMax()));
      algo.run();

      // perform an evaluation
      KrigingResult result(algo.getResult());

      assert_almost_equal(result.getMetaModel()(X), Y, 1e-3);

      // Evaluation of the covariance on the X dataset
      CovarianceMatrix covMatrix(result.getConditionalCovariance(X));

      // Validation of the covariance ==> should be null on the learning set
      assert_almost_equal(covMatrix, SquareMatrix(sampleSize), 0.0, 1e-13);

      // Covariance per marginal & extract variance component
      Collection<CovarianceMatrix> coll(result.getConditionalMarginalCovariance(X));

      for(UnsignedInteger k = 0; k < coll.getSize(); ++k)
        assert_almost_equal(coll[k](0, 0), 0.0, 1e-14, 1e-13);

      // Validation of marginal variance
      const Sample marginalVariance(result.getConditionalMarginalVariance(X));
      assert_almost_equal(marginalVariance, Sample(sampleSize, 1), 1e-14, 1e-13);

      // Prediction accuracy
      assert_almost_equal(Y2, result.getMetaModel()(X2), 0.3, 0.0);
    }

    {
      UnsignedInteger dimension = 2;
      UnsignedInteger sampleSize = 8;
      // Create the function to estimate
      Description input(dimension);
      input[0] = "x0";
      input[1] = "x1";
      Description formulas(1);
      formulas[0] = "5.-x1-0.5*(x0-0.1)^2";
      SymbolicFunction model(input, formulas);
      Sample X(sampleSize, dimension);
      X(0, 0) = -4.61611719;
      X(0, 1) = -6.00099547;
      X(1, 0) = 4.10469096;
      X(1, 1) = 5.32782448;
      X(2, 0) = 0.;
      X(2, 1) = -.5;
      X(3, 0) = -6.17289014;
      X(3, 1) = -4.6984743;
      X(4, 0) = 1.3109306;
      X(4, 1) = -6.93271427;
      X(5, 0) = -5.03823144;
      X(5, 1) = 3.10584743;
      X(6, 0) = -2.87600388;
      X(6, 1) = 6.74310541;
      X(7, 0) = 5.21301203;
      X(7, 1) = 4.26386883;
      Sample Y(model(X));

      // create algorithm
      Basis basis(ConstantBasisFactory(dimension).build());
      Point scale(2);
      scale[0] = 1e-05;
      scale[1] = 18.9;
      Point amplitude(1,  8.05);
      SquaredExponential covarianceModel(scale, amplitude);
      KrigingAlgorithm algo(X, Y, covarianceModel, basis);
      algo.run();

      // perform an evaluation
      KrigingResult result(algo.getResult());

      assert_almost_equal(result.getMetaModel()(X), Y, 1e-3);

      Function metaModel(result.getMetaModel());
      // Get the gradient computed by metamodel
      Matrix gradientKriging(metaModel.gradient(X[1]));

      // Set DF evaluation as gradient and validate
      metaModel.setGradient(new CenteredFiniteDifferenceGradient(ResourceMap::GetAsScalar( "CenteredFiniteDifferenceGradient-DefaultEpsilon" ), metaModel.getEvaluation()));

      // Get the gradient computed by metamodel using FD
      Matrix gradientKrigingFD(metaModel.gradient(X[1]));

      // Validation of the gradient
      assert_almost_equal(gradientKriging, gradientKrigingFD, 1e-3, 1e-3);

      // Covariance per marginal & extract variance component
      Collection<CovarianceMatrix> coll(result.getConditionalMarginalCovariance(X));

      for(UnsignedInteger k = 0; k < coll.getSize(); ++k)
        assert_almost_equal(coll[k](0, 0), 0.0, 1e-13, 1e-13);

      // Validation of marginal variance
      const Sample marginalVariance(result.getConditionalMarginalVariance(X));
      assert_almost_equal(marginalVariance, Sample(sampleSize, 1), 1e-13, 1e-13);
    }

    {
      // fix https: //github.com/openturns/openturns/issues/1861
      RandomGenerator::SetSeed(0);
      SymbolicFunction rho("tau", "exp(-abs(tau))*cos(2*pi_*abs(tau))");
      const Point scale = {1.0};
      StationaryFunctionalCovarianceModel model(scale, scale, rho);
      const Sample x(Normal(0, 1.0).getSample(20));
      const Sample y(x + Normal(0, 0.1).getSample(20));
      KrigingAlgorithm algo(x, y, model, LinearBasisFactory().build());
      algo.run();
      KrigingResult result(algo.getResult());
      assert_almost_equal(result.getConditionalMarginalVariance(x), Sample(x.getSize(), 1), 1e-16, 1e-16);
    }

  }
  catch (TestFailed & ex)
  {
    std::cerr << ex << std::endl;
    return ExitCode::Error;
  }


  return ExitCode::Success;
}