Skip to content

Commit

Permalink
Restrict analytical C.V. to regression.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbaudin47 committed Jul 3, 2023
1 parent 6cc2d0c commit 9953dcb
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,10 @@ void FunctionalChaosAlgorithm::run()
Psi_k.add(basis.build(i));
}
// Build the result
result_ = FunctionalChaosResult(inputSample_, outputSample_, distribution_, transformation_, inverseTransformation_, basis, I_k, alpha_k, Psi_k, residuals, relativeErrors);
const Bool isRegression = projectionStrategy_.getIsRegression();
result_ = FunctionalChaosResult(inputSample_, outputSample_, distribution_, transformation_,
inverseTransformation_, basis, I_k, alpha_k, Psi_k,
residuals, relativeErrors, isRegression);
}

/* Marginal computation */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ FunctionalChaosResult::FunctionalChaosResult()
, I_(0)
, alpha_k_(0, 0)
, Psi_k_(0)
, isRegression_(true)
{
// Nothing to do
}
Expand All @@ -59,7 +60,8 @@ FunctionalChaosResult::FunctionalChaosResult(const Sample & inputSample,
const Sample & alpha_k,
const FunctionCollection & Psi_k,
const Point & residuals,
const Point & relativeErrors)
const Point & relativeErrors,
const Bool isRegression)
: MetaModelResult(inputSample, outputSample, Function(), residuals, relativeErrors)
, distribution_(distribution)
, transformation_(transformation)
Expand All @@ -68,6 +70,7 @@ FunctionalChaosResult::FunctionalChaosResult(const Sample & inputSample,
, I_(I)
, alpha_k_(alpha_k)
, Psi_k_(Psi_k)
, isRegression_(isRegression)
{
// The composed meta model will be a dual linear combination
composedMetaModel_ = DualLinearCombinationFunction(Psi_k, alpha_k);
Expand Down Expand Up @@ -270,14 +273,20 @@ Function FunctionalChaosResult::getComposedMetaModel() const
return composedMetaModel_;
}

/** Residuals accessor */
/* Residuals accessor */
Sample FunctionalChaosResult::getSampleResiduals() const
{
const Sample predictionsSample(metaModel_(inputSample_));
const Sample residualsSample(outputSample_ - predictionsSample);
return residualsSample;
}

/* IsRegression accessor */
Bool FunctionalChaosResult::getIsRegression() const
{
return isRegression_;
}

/* Method save() stores the object through the StorageManager */
void FunctionalChaosResult::save(Advocate & adv) const
{
Expand All @@ -290,6 +299,7 @@ void FunctionalChaosResult::save(Advocate & adv) const
adv.saveAttribute( "alpha_k_", alpha_k_ );
adv.saveAttribute( "Psi_k_", Psi_k_ );
adv.saveAttribute( "composedMetaModel_", composedMetaModel_ );
adv.saveAttribute( "isRegression_", isRegression_ );
}


Expand All @@ -304,9 +314,7 @@ void FunctionalChaosResult::load(Advocate & adv)
adv.loadAttribute( "I_", I_ );
adv.loadAttribute( "alpha_k_", alpha_k_ );
adv.loadAttribute( "Psi_k_", Psi_k_ );
adv.loadAttribute( "composedMetaModel_", composedMetaModel_ );
adv.loadAttribute( "isRegression_", isRegression_ );
}



END_NAMESPACE_OPENTURNS
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ Sample FunctionalChaosValidation::ComputeMetamodelCrossValidationPredictions(
const CrossValidationMethod cvMethod,
const UnsignedInteger & kParameter)
{
const Bool isRegression(functionalChaosResult.getIsRegression());
if (!isRegression)
throw InvalidArgumentException(HERE) << "Error: the polynomial chaos expansion was not computed from regression.";
const Sample residualsSample(functionalChaosResult.getSampleResiduals());
const Sample inputSample(functionalChaosResult.getInputSample());
const UnsignedInteger sampleSize = inputSample.getSize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ void IntegrationExpansion::run()
relativeErrors[j] = empiricalError / marginalOutputSample.computeVariance()[0];
}
// Build the result
result_ = FunctionalChaosResult(inputSample_, outputSample_, distribution_, transformation_, inverseTransformation_, basis_, activeFunctions_, coefficients, designProxy_.getBasis(activeFunctions_), residuals, relativeErrors);
const Bool isRegression = false;
result_ = FunctionalChaosResult(inputSample_, outputSample_, distribution_, transformation_, inverseTransformation_, basis_, activeFunctions_, coefficients, designProxy_.getBasis(activeFunctions_), residuals, relativeErrors, isRegression);
}

/* Method to get/set the active functions */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,27 @@ static const Factory<IntegrationStrategy> Factory_IntegrationStrategy;
IntegrationStrategy::IntegrationStrategy()
: ProjectionStrategyImplementation()
{
// Nothing to do
isRegression_ = false;
}

/* Parameter constructor */
IntegrationStrategy::IntegrationStrategy(const Distribution & measure)
: ProjectionStrategyImplementation(measure)
: ProjectionStrategyImplementation(measure, false)
{
// Nothing to do
}

/* Parameter constructor */
IntegrationStrategy::IntegrationStrategy(const WeightedExperiment & weightedExperiment)
: ProjectionStrategyImplementation(weightedExperiment)
: ProjectionStrategyImplementation(weightedExperiment, false)
{
// Nothing to do
}

/* Parameter constructor */
IntegrationStrategy::IntegrationStrategy(const Distribution & measure,
const WeightedExperiment & weightedExperiment)
: ProjectionStrategyImplementation(measure, weightedExperiment)
: ProjectionStrategyImplementation(measure, weightedExperiment, false)
{
// Nothing to do
}
Expand All @@ -65,15 +65,15 @@ IntegrationStrategy::IntegrationStrategy(const Distribution & measure,
IntegrationStrategy::IntegrationStrategy(const Sample & inputSample,
const Point & weights,
const Sample & outputSample)
: ProjectionStrategyImplementation(inputSample, weights, outputSample)
: ProjectionStrategyImplementation(inputSample, weights, outputSample, false)
{
// Nothing to do
}

/* Parameter constructor */
IntegrationStrategy::IntegrationStrategy(const Sample & inputSample,
const Sample & outputSample)
: ProjectionStrategyImplementation(inputSample, Point(inputSample.getSize(), 1.0 / inputSample.getSize()), outputSample)
: ProjectionStrategyImplementation(inputSample, Point(inputSample.getSize(), 1.0 / inputSample.getSize()), outputSample, false)
{
// Nothing to do
}
Expand Down Expand Up @@ -186,6 +186,7 @@ void IntegrationStrategy::computeCoefficients(const Function & function,
}
residual_p_ = sqrt(residual_p_) / sampleSize;
relativeError_p_ = 0.0;
isRegression_ = false;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ void LeastSquaresExpansion::run()
relativeErrors[j] = empiricalError / marginalOutputSample.computeVariance()[0];
}
// Build the result
result_ = FunctionalChaosResult(inputSample_, outputSample_, distribution_, transformation_, inverseTransformation_, basis_, activeFunctions_, coefficients, designProxy_.getBasis(activeFunctions_), residuals, relativeErrors);
const Bool isRegression = true;
result_ = FunctionalChaosResult(inputSample_, outputSample_, distribution_, transformation_, inverseTransformation_, basis_, activeFunctions_, coefficients, designProxy_.getBasis(activeFunctions_), residuals, relativeErrors, isRegression);
}

/* Method to get/set the active functions */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ LeastSquaresStrategy::LeastSquaresStrategy(const ApproximationAlgorithmImplement
: ProjectionStrategyImplementation()
, p_approximationAlgorithmImplementationFactory_( factory.clone() )
{
// Nothing to do
isRegression_ = true;
}

/* Parameter constructor */
LeastSquaresStrategy::LeastSquaresStrategy(const Distribution & measure,
const ApproximationAlgorithmImplementationFactory & factory)
: ProjectionStrategyImplementation(measure)
: ProjectionStrategyImplementation(measure, true)
, p_approximationAlgorithmImplementationFactory_( factory.clone() )
{
// Nothing to do
Expand All @@ -53,7 +53,7 @@ LeastSquaresStrategy::LeastSquaresStrategy(const Distribution & measure,
/* Parameter constructor */
LeastSquaresStrategy::LeastSquaresStrategy(const WeightedExperiment & weightedExperiment,
const ApproximationAlgorithmImplementationFactory & factory)
: ProjectionStrategyImplementation(weightedExperiment)
: ProjectionStrategyImplementation(weightedExperiment, true)
, p_approximationAlgorithmImplementationFactory_( factory.clone() )
{
// Nothing to do
Expand All @@ -63,7 +63,7 @@ LeastSquaresStrategy::LeastSquaresStrategy(const WeightedExperiment & weightedEx
LeastSquaresStrategy::LeastSquaresStrategy(const Distribution & measure,
const WeightedExperiment & weightedExperiment,
const ApproximationAlgorithmImplementationFactory & factory)
: ProjectionStrategyImplementation(measure, weightedExperiment)
: ProjectionStrategyImplementation(measure, weightedExperiment, true)
, p_approximationAlgorithmImplementationFactory_( factory.clone() )
{
// Nothing to do
Expand All @@ -74,7 +74,7 @@ LeastSquaresStrategy::LeastSquaresStrategy(const Sample & inputSample,
const Point & weights,
const Sample & outputSample,
const ApproximationAlgorithmImplementationFactory & factory)
: ProjectionStrategyImplementation(inputSample, weights, outputSample)
: ProjectionStrategyImplementation(inputSample, weights, outputSample, true)
, p_approximationAlgorithmImplementationFactory_( factory.clone() )
{
// Nothing to do
Expand All @@ -84,7 +84,7 @@ LeastSquaresStrategy::LeastSquaresStrategy(const Sample & inputSample,
LeastSquaresStrategy::LeastSquaresStrategy(const Sample & inputSample,
const Sample & outputSample,
const ApproximationAlgorithmImplementationFactory & factory)
: ProjectionStrategyImplementation(inputSample, Point(inputSample.getSize(), 1.0 / inputSample.getSize()), outputSample)
: ProjectionStrategyImplementation(inputSample, Point(inputSample.getSize(), 1.0 / inputSample.getSize()), outputSample, true)
, p_approximationAlgorithmImplementationFactory_( factory.clone() )
{
// Nothing to do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ ProjectionStrategy::ProjectionStrategy()


/* Parameter constructor */
ProjectionStrategy::ProjectionStrategy(const Distribution & measure)
: TypedInterfaceObject<ProjectionStrategyImplementation>(new ProjectionStrategyImplementation(measure))
ProjectionStrategy::ProjectionStrategy(const Distribution & measure, const Bool isRegression)
: TypedInterfaceObject<ProjectionStrategyImplementation>(new ProjectionStrategyImplementation(measure, isRegression))
{
// Nothing to do
}
Expand Down Expand Up @@ -130,6 +130,11 @@ Point ProjectionStrategy::getCoefficients() const
return getImplementation()->getCoefficients();
}

/* IsRegression accessor */
Bool ProjectionStrategy::getIsRegression() const
{
return getImplementation()->getIsRegression();
}

/* Compute the components alpha_k_p_ by projecting the model on the partial L2 basis */
void ProjectionStrategy::computeCoefficients(const Function & function,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ ProjectionStrategyImplementation::ProjectionStrategyImplementation()
, inputSample_(0, 0)
, weights_(0)
, outputSample_(0, 0)
, isRegression_(false)
{
// The ProjectionStrategyImplementation imposes its distribution to the weighted experiment
weightedExperiment_.setDistribution(measure_);
}


/* Parameter constructor */
ProjectionStrategyImplementation::ProjectionStrategyImplementation(const Distribution & measure)
ProjectionStrategyImplementation::ProjectionStrategyImplementation(const Distribution & measure, const Bool isRegression)
: PersistentObject()
, alpha_k_p_(0)
, residual_p_(0.0)
Expand All @@ -59,14 +60,15 @@ ProjectionStrategyImplementation::ProjectionStrategyImplementation(const Distrib
, inputSample_(0, 0)
, weights_(0)
, outputSample_(0, 0)
, isRegression_(isRegression)
{
// The ProjectionStrategyImplementation imposes the distribution of the weighted experiment
weightedExperiment_.setDistribution(measure_);
}


/* Parameter constructor */
ProjectionStrategyImplementation::ProjectionStrategyImplementation(const WeightedExperiment & weightedExperiment)
ProjectionStrategyImplementation::ProjectionStrategyImplementation(const WeightedExperiment & weightedExperiment, const Bool isRegression)
: PersistentObject()
, alpha_k_p_(0)
, residual_p_(0.0)
Expand All @@ -76,14 +78,15 @@ ProjectionStrategyImplementation::ProjectionStrategyImplementation(const Weighte
, inputSample_(0, 0)
, weights_(0)
, outputSample_(0, 0)
, isRegression_(isRegression)
{
// Nothing to do
}

/* Parameter constructor */
ProjectionStrategyImplementation::ProjectionStrategyImplementation(const Sample & inputSample,
const Point & weights,
const Sample & outputSample)
const Sample & outputSample, const Bool isRegression)
: PersistentObject()
, alpha_k_p_(0)
, residual_p_(0.0)
Expand All @@ -92,6 +95,7 @@ ProjectionStrategyImplementation::ProjectionStrategyImplementation(const Sample
, inputSample_(0, 0)
, weights_(0)
, outputSample_(0, 0)
, isRegression_(isRegression)
{
if (inputSample.getSize() != weights.getSize()) throw InvalidArgumentException(HERE) << "Error: cannot build a ProjectionStrategyImplementation with an input sample and weights of different size. Here, input sample size=" << inputSample.getSize() << ", weights size=" << weights.getSize();
if (inputSample.getSize() != outputSample.getSize()) throw InvalidArgumentException(HERE) << "Error: cannot build a ProjectionStrategyImplementation with samples of different size. Here, input sample size=" << inputSample.getSize() << ", output sample size=" << outputSample.getSize();
Expand All @@ -104,7 +108,7 @@ ProjectionStrategyImplementation::ProjectionStrategyImplementation(const Sample

/* Parameter constructor */
ProjectionStrategyImplementation::ProjectionStrategyImplementation(const Distribution & measure,
const WeightedExperiment & weightedExperiment)
const WeightedExperiment & weightedExperiment, const Bool isRegression)
: PersistentObject()
, alpha_k_p_(0)
, residual_p_(0.0)
Expand All @@ -114,6 +118,7 @@ ProjectionStrategyImplementation::ProjectionStrategyImplementation(const Distrib
, inputSample_(0, 0)
, weights_(0)
, outputSample_(0, 0)
, isRegression_(isRegression)
{
// The ProjectionStrategyImplementation imposes the distribution of the weighted experiment
weightedExperiment_.setDistribution(measure_);
Expand Down Expand Up @@ -219,6 +224,12 @@ Point ProjectionStrategyImplementation::getCoefficients() const
return alpha_k_p_;
}

/* IsRegression accessor */
Bool ProjectionStrategyImplementation::getIsRegression() const
{
return isRegression_;
}

/* Compute the components alpha_k_p_ by projecting the model on the partial L2 basis */
void ProjectionStrategyImplementation::computeCoefficients(const Function &,
const FunctionCollection &,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ public:
const Sample & alpha_k,
const FunctionCollection & Psi_k,
const Point & residuals,
const Point & relativeErrors);
const Point & relativeErrors,
const Bool isRegression
);

/** Virtual constructor */
FunctionalChaosResult * clone() const override;
Expand Down Expand Up @@ -103,6 +105,9 @@ public:
/** Residuals accessor */
virtual Sample getSampleResiduals() const;

/** IsRegression accessor */
virtual Bool getIsRegression() const;

/** Method save() stores the object through the StorageManager */
void save(Advocate & adv) const override;

Expand Down Expand Up @@ -137,6 +142,9 @@ private:
/** Composed meta model */
Function composedMetaModel_;

/** Is regression? */
Bool isRegression_;

} ; /* class FunctionalChaosResult */


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public:
ProjectionStrategy();

/** Parameter constructor */
ProjectionStrategy(const Distribution & measure);
ProjectionStrategy(const Distribution & measure, const Bool isRegression);

/** Constructor from implementation */
ProjectionStrategy(const ProjectionStrategyImplementation & implementation);
Expand Down Expand Up @@ -84,6 +84,9 @@ public:
virtual void setExperiment(const WeightedExperiment & weightedExperiment);
virtual WeightedExperiment getExperiment() const;

/** IsRegression accessor */
virtual Bool getIsRegression() const;

/** String converter */
String __repr__() const override;

Expand Down
Loading

0 comments on commit 9953dcb

Please sign in to comment.