3
3
* @file LeastSquaresStrategy.cxx
4
4
* @brief An implementation of projection strategy as a leastSquares
6
* (C) Copyright 2005-2007 EDF-EADS-Phimeca
6
* (C) Copyright 2005-2010 EDF-EADS-Phimeca
8
8
* This library is free software; you can redistribute it and/or
9
9
* modify it under the terms of the GNU Lesser General Public
26
26
#include "LeastSquaresStrategy.hxx"
28
28
#include "PersistentObjectFactory.hxx"
29
#include "MonteCarloExperiment.hxx"
30
#include "LeastSquaresAlgorithm.hxx"
29
#include "PenalizedLeastSquaresAlgorithm.hxx"
32
31
namespace OpenTURNS
43
42
static Base::Common::Factory<LeastSquaresStrategy> RegisteredFactory("LeastSquaresStrategy");
45
typedef Base::Optim::LeastSquaresAlgorithm LeastSquaresAlgorithm;
44
typedef Base::Optim::PenalizedLeastSquaresAlgorithm PenalizedLeastSquaresAlgorithm;
47
46
/* Default constructor */
48
47
LeastSquaresStrategy::LeastSquaresStrategy()
49
: ProjectionStrategyImplementation(),
50
p_weightedExperiment_(MonteCarloExperiment().clone()),
48
: ProjectionStrategyImplementation()
54
// The leastSquares strategy impose its distribution to the weighted experiment
55
p_weightedExperiment_->setDistribution(getMeasure());
58
53
/* Parameter constructor */
59
54
LeastSquaresStrategy::LeastSquaresStrategy(const Distribution & measure)
60
: ProjectionStrategyImplementation(measure),
61
p_weightedExperiment_(MonteCarloExperiment().clone()),
55
: ProjectionStrategyImplementation(measure)
65
// The leastSquares strategy impose its distribution to the weighted experiment
66
p_weightedExperiment_->setDistribution(getMeasure());
69
60
/* Parameter constructor */
70
61
LeastSquaresStrategy::LeastSquaresStrategy(const WeightedExperiment & weightedExperiment)
71
: ProjectionStrategyImplementation(weightedExperiment.getDistribution()),
72
p_weightedExperiment_(weightedExperiment.clone()),
62
: ProjectionStrategyImplementation(weightedExperiment)
76
// The leastSquares strategy get its measure from the weighted experiment, nothing special to do
79
67
/* Parameter constructor */
80
68
LeastSquaresStrategy::LeastSquaresStrategy(const Distribution & measure,
81
69
const WeightedExperiment & weightedExperiment)
82
: ProjectionStrategyImplementation(measure),
83
p_weightedExperiment_(weightedExperiment.clone()),
70
: ProjectionStrategyImplementation(measure, weightedExperiment)
87
// The leastSquares strategy impose the distribution of the weighted experiment
88
p_weightedExperiment_->setDistribution(getMeasure());
95
79
return new LeastSquaresStrategy(*this);
98
/* Measure accessor */
99
void LeastSquaresStrategy::setMeasure(const Distribution & measure)
101
ProjectionStrategyImplementation::setMeasure(measure);
102
// Set the measure as the distribution of the weighted experiment
103
p_weightedExperiment_->setDistribution(getMeasure());
106
/* Experiment accessors */
107
void LeastSquaresStrategy::setExperiment(const WeightedExperiment & weightedExperiment)
109
p_weightedExperiment_ = weightedExperiment.clone();
110
p_weightedExperiment_->setDistribution(getMeasure());
113
WeightedExperiment LeastSquaresStrategy::getExperiment() const
115
return *p_weightedExperiment_;
118
82
/* String converter */
119
83
String LeastSquaresStrategy::__repr__() const
130
94
void LeastSquaresStrategy::computeCoefficients(const NumericalMathFunction & function,
131
95
const NumericalMathFunctionCollection & partialBasis,
132
96
const Indices & indices,
133
const Indices & addedIndices,
134
const Indices & removedIndices)
97
const Indices & addedRanks,
98
const Indices & conservedRanks,
99
const Indices & removedRanks)
136
101
// If the input sample has not yet been generated
137
102
// we have to generate it and to evaluate the model
138
103
// upon this sample
139
104
if (inputSample_.getSize() == 0)
141
inputSample_ = p_weightedExperiment_->generate();
106
inputSample_ = p_weightedExperiment_->generate(weights_);
142
107
outputSample_ = function(inputSample_);
144
109
// Compute the coefficients and the residual using the least square algorithm
145
LeastSquaresAlgorithm algo(inputSample_, outputSample_, p_weightedExperiment_->getWeight(), partialBasis);
110
PenalizedLeastSquaresAlgorithm algo(inputSample_, outputSample_, weights_, partialBasis);
146
111
alpha_k_p_ = algo.getCoefficients();
147
112
residual_p_ = algo.getResidual();
151
116
/* Method save() stores the object through the StorageManager */
152
void LeastSquaresStrategy::save(const StorageManager::Advocate & adv) const
117
void LeastSquaresStrategy::save(StorageManager::Advocate & adv) const
154
119
ProjectionStrategyImplementation::save(adv);
158
123
/* Method load() reloads the object from the StorageManager */
159
void LeastSquaresStrategy::load(const StorageManager::Advocate & adv)
124
void LeastSquaresStrategy::load(StorageManager::Advocate & adv)
161
126
ProjectionStrategyImplementation::load(adv);