File: t_LinearModelAlgorithm_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 (113 lines) | stat: -rw-r--r-- 4,570 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
//                                               -*- C++ -*-
/**
 *  @brief The test file of class LinearModelAlgorithm
 *
 *  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
  {

    {
      fullprint << "Fit y ~ 3 - 2 x + 0.05 * sin(x) model using 20 points (sin(x) ~ noise)" << std::endl;
      UnsignedInteger size = 20;
      Sample oneSample(size, 1);
      Sample twoSample(size, 1);
      for (UnsignedInteger i = 0; i < size; ++i)
      {
        oneSample[i][0] = 7.0 * sin(-3.5 + (6.5 * i) / (size - 1.0)) + 2.0;
        twoSample[i][0] = -2.0 * oneSample[i][0] + 3.0 + 0.05 * sin(oneSample[i][0]);
      }
      LinearModelAlgorithm test(oneSample, twoSample);
      LinearModelResult result(test.getResult());
      fullprint << "result = " << std::endl;
      fullprint << result << std::endl;
      fullprint << result.__str__() << std::endl;
      fullprint << "trend coefficients = " << result.getCoefficients() << std::endl;
    }

    {
      fullprint << "Fit y ~ 1 + 0.1 x + 10 x^2 model using 100 points" << std::endl;
      UnsignedInteger size = 100;
      // Define a linear grid from 0 to 10 with size points
      // We use a Box experiment ==> remove 0 & 1 points
      const Box experiment(Indices(1, size - 2));
      Sample X(experiment.generate());
      // X is defined in [0,1]
      const Point scale(1, 10.0);
      X *= scale;
      // Stack X2
      Sample X2(X);
      for (UnsignedInteger i = 0; i < size; ++i)
        X2(i, 0) = X(i, 0) * X(i, 0);
      // Stack
      X.stack(X2);
      // Define y = 1 + 0.1 * x + 10  x^2 + e with e a gaussian noise
      Sample Y(size, 1);
      for (UnsignedInteger i = 0; i < size; ++i)
        Y(i, 0) = 1.0 + 0.1 * X(i, 0) + 10.0 * X(i, 0) * X(i, 0) + 0.1 * DistFunc::rNormal();
      LinearModelAlgorithm test(X, Y);
      LinearModelResult result(test.getResult());
      fullprint << "trend coefficients = " << result.getCoefficients() << std::endl;

      // Test various attributes
      const Point cook(result.getCookDistances());
      const Point cook_reference = {0.0233296, 0.0360369, 0.00178903, 0.0502183, 0.0966701, 0.00562596};
      Point cookFirstElements(cook_reference.getSize());
      for (UnsignedInteger i = 0; i < cookFirstElements.getSize(); ++i)
        cookFirstElements[i] = cook[i];
      assert_almost_equal(cookFirstElements, cook_reference, 1e-5, 0.0);

      const Point leverages(result.getLeverages());
      const Point leverages_reference = {0.0864939, 0.0797831, 0.0735447, 0.0677578, 0.0624023, 0.0574582};
      Point leverageFirstElements(leverages_reference.getSize());
      for (UnsignedInteger i = 0; i < leverageFirstElements.getSize(); ++i)
        leverageFirstElements[i] = leverages[i];
      assert_almost_equal(leverageFirstElements, leverages_reference, 1e-6, 0.0);

      LeastSquaresMethod lsMethod(result.buildMethod());
      SymmetricMatrix projectionMatrix(lsMethod.getH());
      assert_equal(projectionMatrix.getNbRows(), size);
      assert_equal(projectionMatrix.getNbColumns(), size);
      Point predictionsReference(result.getFittedSample().asPoint());
      Point predictionsFromProjection(projectionMatrix * Y.asPoint());
      assert_almost_equal(predictionsFromProjection, predictionsReference);

      Sample residuals(result.getSampleResiduals());
      assert_equal(residuals.getSize(), size);
      assert_equal(residuals.getDimension(), (UnsignedInteger) 1);
      Point residualsReference(Y.asPoint() - predictionsReference);
      assert_almost_equal(residuals.asPoint(), residualsReference);
    }
  }
  catch (TestFailed &ex)
  {
    std::cerr << ex << std::endl;
    return ExitCode::Error;
  }
  return ExitCode::Success;
}