FunctionalChaosValidation¶
(Source code, svg)
- class FunctionalChaosValidation(*args)¶
Validate a functional chaos metamodel.
The scores of the validation, the R2 score and the mean squared error, are weighted by the design weights stored in the result, uniform by default for studies saved before the weights were stored. The predictions sit on the training points, so the design weights are the right validation weights: they are applied automatically, and
and MSE converge to their continuous counterparts when the design is a quadrature of the input distribution. An explicit
setWeights()call overrides them. A FunctionalChaosValidation object is used for the validation of a functional chaos expansion. It is based on the fast (analytical) leave-one-out and fast K-Fold cross-validation methods presented in Validation and cross validation of surrogate models.Analytical cross-validation can only be performed accurately if some conditions are met.
This can only be done if the coefficients of the expansion are estimated using least squares regression: if the expansion is computed from integration, then an exception is produced.
This can only be done if the coefficients of the expansion are estimated using full expansion, without model selection: if the expansion is computed with model selection, then an exception is produced by default. This is because model selection leads to supposedly improved coefficients, so that the hypotheses required to estimate the mean squared error using the cross-validation method are not satisfied anymore. As a consequence, using the analytical formula without taking model selection into account leads to a biased, overly optimistic, mean squared error. More precisely, the analytical formula produces a MSE which is lower than the true one on average. Model selection is involved in the expansion if the
LeastSquaresMetaModelSelectionFactoryorCleaningStrategyclasses are involved. If thePenalizedLeastSquaresAlgorithmFactoryclass is used, then no model selection is involved and the analytical cross-validation methods can be used. If model selection is involved, the naive methods based on theLeaveOneOutSplitterandKFoldSplitterclasses can be used, but this can be much slower than the analytical methods implemented here. In many cases, however, the order of magnitude of the estimate from the analytical formula applied to a sparse model is correct: the estimate of the MSE is only slightly lower than the true value. In order to enable the calculation of the analytical MSE estimator on a sparse model, please set the FunctionalChaosValidation-ModelSelection key of theResourceMapto True: use this option at your own risks.
We suggest to use leave-one-out (LOO) cross validation when possible, because it produces a more accurate estimate of the error than K-Fold does. If K-Fold is required, we suggest to use the largest possible value of
.
The predictions of the leave-one-one or K-Fold surrogate models are based on the hold-out method. For example, if we use the leave-one-out cross-validation method, the
-th prediction is the prediction of the linear model trained using the hold-out sample where the
-th observation was removed. This produces a sample of residuals which can be retrieved using the
getResidualSample()method. ThedrawValidation()method performs similarly.The exactness of the two analytical methods differs when the design carries non-uniform weights, for instance the quadrature weights of a density. Both rebuild a weighted least-squares method from the design weights stored in the
FunctionalChaosResult, so both are exact for any positive weights:the leave-one-out residual is
with the weighted leverage
returned by
getHDiag()(PRESS statistic of Allen, 1974);the K-Fold prediction on a test block
solves
with
and
, the downdate of the weighted normal equations, which reduces to the
formula for uniform weights.
Results saved before the design weights were stored validate as uniform designs.
If the sample was not produced from Monte Carlo, then the leave-one-out cross-validation method may not necessarily provide an accurate estimator of the MSE. Indeed, one assumption for cross validation using LOO or K-Fold is to have independent observations, which is required to state that the LOO sample MSE is an estimator of the true MSE. For example, using cross-validation on a quasi-Monte Carlo (QMC) experiment may not necessarily provide an accurate estimate of the MSE, because the internal structure of the QMC is broken by the different splits: the elementary volumes are not filled as expected anymore and the space-filling properties of the sequence are lost.
- Parameters:
- result
FunctionalChaosResult A functional chaos result obtained from a polynomial chaos expansion.
- splitter
SplitterImplementation, optional The cross-validation method. The default is
LeaveOneOutSplitter.
- result
Methods
Accessor to the mean absolute error.
Accessor to the mean squared error.
Compute the R2 score.
Accessor to the root mean squared error.
Plot the residuals depending on the model observations.
Plot a model vs metamodel graph for visual validation.
Accessor to the object's name.
Result accessor.
Accessor to the output predictions from the metamodel.
getName()Accessor to the object's name.
Accessor to the output sample.
getResidualDistribution([smooth])Compute the non parametric distribution of the residual sample.
Compute the residual sample.
Get the cross-validation method.
Accessor to the weights of the validation sample.
hasName()Test if the object is named.
setName(name)Accessor to the object's name.
setWeights(weights)Accessor to the weights of the validation sample.
Examples
Create a polynomial chaos for the Ishigami function. We use the
PenalizedLeastSquaresAlgorithmFactoryclass to specify the method to compute the coefficients. We may use theLeastSquaresMetaModelSelectionFactoryclass in order to create a sparse polynomial chaos expansion, but this prevents us from using the fast analytical formulas and get an accurate estimator of the MSE.>>> import openturns as ot >>> from math import pi >>> from openturns.usecases import ishigami_function >>> im = ishigami_function.IshigamiModel() >>> sampleSize = 500 >>> inputTrain = im.distribution.getSample(sampleSize) >>> outputTrain = im.model(inputTrain) >>> multivariateBasis = ot.OrthogonalProductPolynomialFactory([im.X1, im.X2, im.X3]) >>> selectionAlgorithm = ot.PenalizedLeastSquaresAlgorithmFactory() >>> projectionStrategy = ot.LeastSquaresStrategy(inputTrain, outputTrain, selectionAlgorithm) >>> totalDegree = 8 >>> enumerateFunction = multivariateBasis.getEnumerateFunction() >>> basisSize = enumerateFunction.getStrataCumulatedCardinal(totalDegree) >>> adaptiveStrategy = ot.FixedStrategy(multivariateBasis, basisSize) >>> chaosalgo = ot.FunctionalChaosAlgorithm( ... inputTrain, outputTrain, im.distribution, adaptiveStrategy, projectionStrategy ... ) >>> chaosalgo.run() >>> result = chaosalgo.getResult()
Validate the polynomial chaos expansion using leave-one-out cross-validation.
>>> splitterLOO = ot.LeaveOneOutSplitter(sampleSize) >>> validation = ot.FunctionalChaosValidation(result, splitterLOO) >>> r2Score = validation.computeR2Score() >>> print('R2 = ', r2Score[0]) R2 = 0.99...
Validate the polynomial chaos expansion using K-Fold cross-validation and set the
parameter.
>>> kParameter = 10 >>> splitterKF = ot.KFoldSplitter(sampleSize, kParameter) >>> validation = ot.FunctionalChaosValidation(result, splitterKF)
Draw the validation graph.
>>> graph = validation.drawValidation()
- __init__(*args)¶
- computeMeanAbsoluteError()¶
Accessor to the mean absolute error.
The sample mean absolute error is:
where
is the sample size,
is the metamodel,
is the input experimental design,
is the output of the model and
are the validation weights, uniform by default. Contrary to the mean squared error, it is robust to outliers.
If the output is multi-dimensional, the same calculations are repeated separately for each output marginal
for
where
is the output dimension.
- Returns:
- meanAbsoluteError
Point The mean absolute error of each marginal output dimension.
- meanAbsoluteError
- computeMeanSquaredError()¶
Accessor to the mean squared error.
The sample mean squared error is:
where
is the sample size,
is the metamodel,
is the input experimental design,
is the output of the model and
are the validation weights, uniform by default.
If the output is multi-dimensional, the same calculations are repeated separately for each output marginal
for
where
is the output dimension.
- Returns:
- meanSquaredError
Point The mean squared error of each marginal output dimension.
- meanSquaredError
- computeR2Score()¶
Compute the R2 score.
The coefficient of determination
is the fraction of the variance of the output explained by the metamodel. It is defined as:
where
is the fraction of unexplained variance:
where
is the output of the physical model
,
is the variance of the output and
is the mean squared error of the metamodel:
The sample
is:
where
is the sample size,
is the metamodel,
is the input experimental design,
is the output of the model,
are the validation weights, uniform by default, and
is the sample variance of the output:
where
is the weighted output sample mean:
- Returns:
- r2Score
Point The coefficient of determination R2
- r2Score
- computeRootMeanSquaredError()¶
Accessor to the root mean squared error.
The sample root mean squared error is the square root of the
computeMeanSquaredError(), expressed in the unit of the output.If the output is multi-dimensional, the same calculations are repeated separately for each output marginal
for
where
is the output dimension.
- Returns:
- rootMeanSquaredError
Point The root mean squared error of each marginal output dimension.
- rootMeanSquaredError
- drawResiduals()¶
Plot the residuals depending on the model observations.
If the points are scattered around the horizontal zero line of the plot, then the metamodel validation is satisfactory. A trend in the cloud reveals a bias of the metamodel.
If the output is multi-dimensional, the graph has 1 row and
columns, where
is the output dimension.
- Returns:
- graph
GridLayout The residuals graph.
- graph
- drawValidation()¶
Plot a model vs metamodel graph for visual validation.
The plot presents the metamodel predictions depending on the model observations. If the points are close to the diagonal line of the plot, then the metamodel validation is satisfactory. Points which are far away from the diagonal represent outputs for which the metamodel is not accurate.
If the output is multi-dimensional, the graph has 1 row and
columns, where
is the output dimension.
- Returns:
- graph
GridLayout The visual validation graph.
- graph
- getClassName()¶
Accessor to the object’s name.
- Returns:
- class_namestr
The object class name (object.__class__.__name__).
- getFunctionalChaosResult()¶
Result accessor.
- Returns:
- result
FunctionalChaosResult The result provided.
- result
- getMetamodelPredictions()¶
Accessor to the output predictions from the metamodel.
- Returns:
- outputMetamodelSample
Sample Output sample of the metamodel.
- outputMetamodelSample
- getName()¶
Accessor to the object’s name.
- Returns:
- namestr
The name of the object.
- getOutputSample()¶
Accessor to the output sample.
- Returns:
- outputSample
Sample Output sample of a model evaluated apart.
- outputSample
- getResidualDistribution(smooth=True)¶
Compute the non parametric distribution of the residual sample.
The residual distribution is built thanks to
KernelSmoothingif smooth argument is true. Otherwise, an histogram distribution is returned, thanks toHistogramFactory.- Parameters:
- smoothbool
Tells if distribution is smooth (true) or not. Default argument is true.
- Returns:
- residualDistribution
Distribution The residual distribution.
- residualDistribution
- getResidualSample()¶
Compute the residual sample.
The residual sample is given by :
for
where
is the sample size,
is the model observation,
is the metamodel and
is the
-th input observation.
If the output is multi-dimensional, the residual sample has dimension
, where
is the output dimension.
- Returns:
- residual
Sample The residual sample.
- residual
- getSplitter()¶
Get the cross-validation method.
- Returns:
- splitter
SplitterImplementation The cross-validation method.
- splitter
- getWeights()¶
Accessor to the weights of the validation sample.
- Returns:
- weights
Point The validation weights, stored as a single value when uniform.
- weights
- hasName()¶
Test if the object is named.
- Returns:
- hasNamebool
True if the name is not empty.
- setName(name)¶
Accessor to the object’s name.
- Parameters:
- namestr
The name of the object.
- setWeights(weights)¶
Accessor to the weights of the validation sample.
The weights define the discrete inner product of the scores: use the quadrature weights of the validation design when they are known, so that
computeR2Score()andcomputeMeanSquaredError()converge to the continuousand MSE.
- Parameters:
- weightssequence of float
The validation weights, either a single uniform value or one non-negative value per validation point with a positive sum.
OpenTURNS