forked from openturns/openturns
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement FunctionalChaosValidation and LinearModelValidation
- Loading branch information
Showing
34 changed files
with
2,026 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
185 changes: 185 additions & 0 deletions
185
lib/src/Uncertainty/Algorithm/MetaModel/FunctionalChaos/FunctionalChaosValidation.cxx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
// -*- C++ -*- | ||
/** | ||
* @brief Validation of a functional chaos expansion | ||
* | ||
* Copyright 2005-2023 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 <algorithm> | ||
#include "openturns/PersistentObjectFactory.hxx" | ||
#include "openturns/FunctionalChaosValidation.hxx" | ||
#include "openturns/Os.hxx" | ||
#include "openturns/OSS.hxx" | ||
#include "openturns/DesignProxy.hxx" | ||
#include "openturns/LeastSquaresMethod.hxx" | ||
#include "openturns/KFoldSplitter.hxx" | ||
#include "openturns/MetaModelValidation.hxx" | ||
|
||
BEGIN_NAMESPACE_OPENTURNS | ||
|
||
CLASSNAMEINIT(FunctionalChaosValidation) | ||
|
||
static const Factory<FunctionalChaosValidation> Factory_FunctionalChaosValidation; | ||
|
||
/* Default constructor */ | ||
FunctionalChaosValidation::FunctionalChaosValidation() | ||
: MetaModelValidation() | ||
{ | ||
// Nothing to do | ||
} | ||
|
||
/* Default constructor */ | ||
|
||
FunctionalChaosValidation::FunctionalChaosValidation(const FunctionalChaosResult & functionalChaosResult, | ||
const CrossValidationMethod cvMethod, | ||
const UnsignedInteger & kParameter) | ||
: MetaModelValidation(functionalChaosResult.getOutputSample() | ||
, ComputeMetamodelCrossValidationPredictions(functionalChaosResult, cvMethod, kParameter)) | ||
, functionalChaosResult_(functionalChaosResult) | ||
{ | ||
if ((cvMethod != LEAVEONEOUT) && (cvMethod != KFOLD)) | ||
throw InvalidArgumentException(HERE) << "The method " << cvMethod << " is not available."; | ||
cvMethod_ = cvMethod; | ||
if (kParameter < 1) | ||
throw InvalidArgumentException(HERE) << "Cannot set k parameter of K-Fold method to " << kParameter << " which is lower than 1"; | ||
const UnsignedInteger sampleSize = functionalChaosResult_.getInputSample().getSize(); | ||
if (kParameter > sampleSize) | ||
throw InvalidArgumentException(HERE) << "Cannot set k parameter of K-Fold method to " << kParameter | ||
<< " which is larger than the sample size =" << sampleSize; | ||
kParameter_ = kParameter; | ||
if (!ResourceMap::GetAsBool("FunctionalChaosValidation-ModelSelection") && \ | ||
functionalChaosResult_.involvesModelSelection()) | ||
throw InvalidArgumentException(HERE) << "Cannot perform fast cross-validation " | ||
<< "with a polynomial chaos expansion involving model selection"; | ||
if (!functionalChaosResult.isLeastSquares()) | ||
throw InvalidArgumentException(HERE) << "Error: the polynomial chaos expansion was not computed from least squares."; | ||
} | ||
|
||
/* Virtual constructor */ | ||
FunctionalChaosValidation * FunctionalChaosValidation::clone() const | ||
{ | ||
return new FunctionalChaosValidation(*this); | ||
} | ||
|
||
/* String converter */ | ||
String FunctionalChaosValidation::__repr__() const | ||
{ | ||
OSS oss; | ||
oss << "class=" << FunctionalChaosValidation::GetClassName() | ||
<< " functional chaos result=" << functionalChaosResult_ | ||
<< " kParameter_=" << kParameter_ | ||
<< " cvMethod_=" << cvMethod_; | ||
return oss; | ||
} | ||
|
||
/* Get result*/ | ||
FunctionalChaosResult FunctionalChaosValidation::getFunctionalChaosResult() const | ||
{ | ||
return functionalChaosResult_; | ||
} | ||
|
||
/* Get the K parameter */ | ||
UnsignedInteger FunctionalChaosValidation::getKParameter() const | ||
{ | ||
return kParameter_; | ||
} | ||
|
||
/* Get the CV Method */ | ||
UnsignedInteger FunctionalChaosValidation::getMethod() const | ||
{ | ||
return cvMethod_; | ||
} | ||
|
||
/* Compute cross-validation metamodel predictions */ | ||
Sample FunctionalChaosValidation::ComputeMetamodelCrossValidationPredictions( | ||
const FunctionalChaosResult & functionalChaosResult, | ||
const CrossValidationMethod cvMethod, | ||
const UnsignedInteger & kParameter) | ||
{ | ||
const Sample residualsSample(functionalChaosResult.getSampleResiduals()); | ||
const Sample inputSample(functionalChaosResult.getInputSample()); | ||
const UnsignedInteger sampleSize = inputSample.getSize(); | ||
const FunctionCollection reducedBasis(functionalChaosResult.getReducedBasis()); | ||
const UnsignedInteger reducedBasisSize = reducedBasis.getSize(); | ||
if (reducedBasisSize > sampleSize) | ||
throw InvalidArgumentException(HERE) << "Error: the sample size is: " << sampleSize << | ||
" which is lower than the basis size: " << reducedBasisSize; | ||
const Sample outputSample(functionalChaosResult.getOutputSample()); | ||
const UnsignedInteger outputDimension = outputSample.getDimension(); | ||
const Function transformation(functionalChaosResult.getTransformation()); | ||
const Sample standardSample(transformation(inputSample)); | ||
DesignProxy designProxy(standardSample, reducedBasis); | ||
Indices allIndices(reducedBasisSize); | ||
allIndices.fill(); | ||
// The method name is set to the default one, given by ResourceMap | ||
const String methodName(ResourceMap::GetAsString("LeastSquaresExpansion-DecompositionMethod")); | ||
LeastSquaresMethod leastSquaresMethod(LeastSquaresMethod::Build(methodName, designProxy, allIndices)); | ||
leastSquaresMethod.update(Indices(0), allIndices, Indices(0)); | ||
Sample cvPredictions(sampleSize, outputDimension); | ||
if (cvMethod == LEAVEONEOUT) | ||
{ | ||
const Point diagonalH = leastSquaresMethod.getHDiag(); | ||
for (UnsignedInteger j = 0; j < outputDimension; ++j) | ||
for (UnsignedInteger i = 0; i < sampleSize; ++i) | ||
cvPredictions(i, j) = outputSample(i, j) - residualsSample(i, j) / (1.0 - diagonalH[i]); | ||
} | ||
else if (cvMethod == KFOLD) | ||
{ | ||
const SymmetricMatrix projectionMatrix(leastSquaresMethod.getH()); | ||
// Compute K-Fold error | ||
KFoldSplitter splitter(sampleSize, kParameter); | ||
Indices indicesTest; | ||
for (UnsignedInteger foldIndex = 0; foldIndex < kParameter; ++foldIndex) | ||
{ | ||
splitter.generate(indicesTest); | ||
const UnsignedInteger foldSize = indicesTest.getSize(); | ||
SymmetricMatrix projectionKFoldMatrix(foldSize); | ||
for (UnsignedInteger i1 = 0; i1 < foldSize; ++i1) | ||
for (UnsignedInteger i2 = 0; i2 < 1 + i1; ++i2) | ||
projectionKFoldMatrix(i1, i2) = projectionMatrix(indicesTest[i1], indicesTest[i2]); | ||
const IdentityMatrix identityMatrix(foldSize); | ||
const SymmetricMatrix reducedMatrix(identityMatrix - projectionKFoldMatrix); | ||
const Sample residualsSampleKFoldTest(residualsSample.select(indicesTest)); | ||
Matrix multipleRightHandSide(foldSize, outputDimension); | ||
for (UnsignedInteger j = 0; j < outputDimension; ++j) | ||
for (UnsignedInteger i = 0; i < foldSize; ++i) | ||
multipleRightHandSide(i, j) = residualsSampleKFoldTest(i, j); | ||
const Matrix residualsKFoldMatrix(reducedMatrix.solveLinearSystem(multipleRightHandSide)); | ||
for (UnsignedInteger j = 0; j < outputDimension; ++j) | ||
for (UnsignedInteger i = 0; i < foldSize; ++i) | ||
cvPredictions(indicesTest[i], j) = outputSample(indicesTest[i], j) - residualsKFoldMatrix(i, j); | ||
} // For fold indices | ||
} | ||
else | ||
throw InvalidArgumentException(HERE) << "The method " << cvMethod << " is not available."; | ||
return cvPredictions; | ||
} | ||
|
||
/* Method save() stores the object through the StorageManager */ | ||
void FunctionalChaosValidation::save(Advocate & adv) const | ||
{ | ||
PersistentObject::save(adv); | ||
adv.saveAttribute( "functionalChaosResult_", functionalChaosResult_ ); | ||
} | ||
|
||
/* Method load() reloads the object from the StorageManager */ | ||
void FunctionalChaosValidation::load(Advocate & adv) | ||
{ | ||
PersistentObject::load(adv); | ||
adv.loadAttribute( "functionalChaosResult_", functionalChaosResult_ ); | ||
} | ||
|
||
END_NAMESPACE_OPENTURNS |
97 changes: 97 additions & 0 deletions
97
...c/Uncertainty/Algorithm/MetaModel/FunctionalChaos/openturns/FunctionalChaosValidation.hxx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// -*- C++ -*- | ||
/** | ||
* @brief The validation of a chaos expansion | ||
* | ||
* Copyright 2005-2023 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/>. | ||
* | ||
*/ | ||
#ifndef OPENTURNS_FUNCTIONALCHAOSVALIDATION_HXX | ||
#define OPENTURNS_FUNCTIONALCHAOSVALIDATION_HXX | ||
|
||
#include "openturns/Sample.hxx" | ||
#include "openturns/FunctionalChaosResult.hxx" | ||
#include "openturns/MetaModelValidation.hxx" | ||
|
||
BEGIN_NAMESPACE_OPENTURNS | ||
|
||
/** | ||
* @class FunctionalChaosValidation | ||
* | ||
* The validation of a chaos expansion | ||
*/ | ||
|
||
class OT_API FunctionalChaosValidation | ||
: public MetaModelValidation | ||
{ | ||
CLASSNAME | ||
|
||
public: | ||
typedef Collection<Function> FunctionCollection; | ||
enum CrossValidationMethod {LEAVEONEOUT = 0, KFOLD = 1}; | ||
|
||
/** Default constructor */ | ||
FunctionalChaosValidation(); | ||
|
||
/** Parameter constructor */ | ||
FunctionalChaosValidation(const FunctionalChaosResult & chaosResult, | ||
const CrossValidationMethod method, | ||
const UnsignedInteger & kParameter = ResourceMap::GetAsUnsignedInteger("FunctionalChaosValidation-DefaultKFoldParameter")); | ||
|
||
/** Virtual constructor */ | ||
FunctionalChaosValidation * clone() const override; | ||
|
||
/** String converter */ | ||
String __repr__() const override; | ||
|
||
/** Result accessor */ | ||
FunctionalChaosResult getFunctionalChaosResult() const; | ||
|
||
/** Get the K-parameter */ | ||
UnsignedInteger getKParameter() const; | ||
|
||
/** Get the CV Method */ | ||
UnsignedInteger getMethod() const; | ||
|
||
/** Method save() stores the object through the StorageManager */ | ||
void save(Advocate & adv) const override; | ||
|
||
/** Method load() reloads the object from the StorageManager */ | ||
void load(Advocate & adv) override; | ||
|
||
protected: | ||
|
||
private: | ||
|
||
/** The functional chaos result */ | ||
FunctionalChaosResult functionalChaosResult_; | ||
|
||
/** K-parameter */ | ||
UnsignedInteger kParameter_; | ||
|
||
/** Cross-validation method */ | ||
CrossValidationMethod cvMethod_; | ||
|
||
/* Compute cross-validation metamodel predictions */ | ||
static Sample ComputeMetamodelCrossValidationPredictions( | ||
const FunctionalChaosResult & functionalChaosResult, const CrossValidationMethod cvMethod, | ||
const UnsignedInteger & kParameter); | ||
|
||
} ; /* class FunctionalChaosValidation */ | ||
|
||
|
||
END_NAMESPACE_OPENTURNS | ||
|
||
#endif /* OPENTURNS_FUNCTIONALCHAOSVALIDATION_HXX */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.