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
|
#! /usr/bin/env python
import openturns as ot
import openturns.testing as ott
ot.TESTPREAMBLE()
inputDimension = 2
levels = [8, 6]
box = ot.Box(levels)
inputSample = box.generate()
inputSample *= 10.0
model = ot.SymbolicFunction(["x", "y"], ["cos(0.5*x) + sin(y)"])
outputSample = model(inputSample)
# Validation
sampleSize = 10
inputValidSample = ot.JointDistribution(2 * [ot.Uniform(1.0, 9.0)]).getSample(
sampleSize
)
outputValidSample = model(inputValidSample)
# Reimplement the squared exponential covariance model
rho = ot.SymbolicFunction(["x", "y"], ["exp(-0.5* (x * x + y * y))"])
covarianceModel = ot.StationaryFunctionalCovarianceModel([6.0, 2.0], [1.5], rho)
# Basis definition
basis = ot.LinearBasisFactory(inputDimension).build()
# Kriging algorithm
algo = ot.KrigingAlgorithm(inputSample, outputSample, covarianceModel, basis)
start = [50.0] * inputDimension
loglikelihood = algo.getReducedLogLikelihoodFunction()(start)
algo.setOptimizeParameters(False)
algo.run()
result = algo.getResult()
metaModel = result.getMetaModel()
variance = result.getConditionalMarginalVariance(inputSample)
ott.assert_almost_equal(variance, ot.Sample(inputSample.getSize(), 1), 1e-14, 1e-14)
# Consistency check: does the reimplementation fit the SquaredExponential class?
squaredExponential = ot.SquaredExponential(inputDimension)
squaredExponential.setParameter([6.0, 2.0, 1.5])
algoSE = ot.KrigingAlgorithm(inputSample, outputSample, squaredExponential, basis)
loglikelihoodSE = algoSE.getReducedLogLikelihoodFunction()(start)
ott.assert_almost_equal(loglikelihood, loglikelihoodSE, 1e-8, 1e-8)
# High level consistency check: does the prediction fit too?
algoSE.setOptimizeParameters(False)
algoSE.run()
resultSE = algoSE.getResult()
metaModelSE = resultSE.getMetaModel()
ott.assert_almost_equal(
metaModel(inputValidSample), metaModelSE(inputValidSample), 1e-8, 1e-8
)
# Validate the metamodel
ott.assert_almost_equal(outputValidSample, metaModel(inputValidSample), 5.0e-3, 5.0e-3)
|