def test_two_outputs(): f = ot.SymbolicFunction(['x'], ['x * sin(x)', 'x * cos(x)']) sampleX = [[1.0], [2.0], [3.0], [4.0], [5.0], [6.0], [7.0], [8.0]] sampleY = f(sampleX) basis = ot.Basis([ ot.SymbolicFunction(['x'], ['x']), ot.SymbolicFunction(['x'], ['x^2']) ]) covarianceModel = ot.SquaredExponential([1.0]) covarianceModel.setActiveParameter([]) covarianceModel = ot.TensorizedCovarianceModel([covarianceModel] * 2) algo = ot.KrigingAlgorithm(sampleX, sampleY, covarianceModel, basis) algo.run() result = algo.getResult() mm = result.getMetaModel() assert mm.getOutputDimension() == 2, "wrong output dim" ott.assert_almost_equal(mm(sampleX), sampleY) # Check the conditional covariance reference_covariance = ot.Matrix([[4.4527, 0.0, 8.34404, 0.0], [0.0, 2.8883, 0.0, 5.41246], [8.34404, 0.0, 15.7824, 0.0], [0.0, 5.41246, 0.0, 10.2375]]) ott.assert_almost_equal( result([[9.5], [10.0]]).getCovariance() - reference_covariance, ot.Matrix(4, 4), 0.0, 2e-2)
def test_KarhunenLoeveValidationMultidimensional(self): # Create the KL result numberOfVertices = 20 interval = ot.Interval(-1.0, 1.0) mesh = ot.IntervalMesher([numberOfVertices - 1]).build(interval) outputDimension = 2 univariateCovariance = ot.SquaredExponential() covarianceCollection = [univariateCovariance] * outputDimension multivariateCovariance = ot.TensorizedCovarianceModel( covarianceCollection) process = ot.GaussianProcess(multivariateCovariance, mesh) sampleSize = 100 sampleSize = 10 processSample = process.getSample(sampleSize) threshold = 1.0e-7 algo = ot.KarhunenLoeveSVDAlgorithm(processSample, threshold) algo.run() klresult = algo.getResult() # Create the validation validation = ot.KarhunenLoeveValidation(processSample, klresult) # Check residuals residualProcessSample = validation.computeResidual() assert (type(residualProcessSample) is ot.ProcessSample) # Check standard deviation residualSigmaField = validation.computeResidualStandardDeviation() zeroSample = ot.Sample(numberOfVertices, outputDimension) ott.assert_almost_equal(residualSigmaField, zeroSample) # Check graph graph = validation.drawValidation() if False: from openturns.viewer import View View(graph).save('validation2.png')
import openturns as ot from matplotlib import pyplot as plt from openturns.viewer import View covarianceModel = ot.TensorizedCovarianceModel() if covarianceModel.getSpatialDimension() == 1: scale = covarianceModel.getScale()[0] if covarianceModel.isStationary(): def f(x): return [covarianceModel(x)[0, 0]] func = ot.PythonFunction(1, 1, f) func.setDescription(['$tau$', '$cov$']) cov_graph = func.draw(-3.0 * scale, 3.0 * scale, 129) fig = plt.figure(figsize=(10, 4)) plt.suptitle(str(covarianceModel)) cov_axis = fig.add_subplot(111) View(cov_graph, figure=fig, axes=[cov_axis], add_legend=False) else: def f(x): return [covarianceModel([x[0]], [x[1]])[0, 0]] func = ot.PythonFunction(2, 1, f) func.setDescription(['$s$', '$t$', '$cov$']) cov_graph = func.draw([-3.0 * scale] * 2, [3.0 * scale] * 2, [129] * 2) fig = plt.figure(figsize=(10, 4)) plt.suptitle(str(covarianceModel)) cov_axis = fig.add_subplot(111) View(cov_graph, figure=fig, axes=[cov_axis], add_legend=False)
test_model(myModel, x1=[0.5, 0.0], x2=[0.5, 0.0]) myDefautModel = ot.ProductCovarianceModel() print('myDefautModel = ', myDefautModel) test_model(myDefautModel) cov1 = ot.AbsoluteExponential([2.0], [3.0]) cov2 = ot.SquaredExponential([2.0], [3.0]) myModel = ot.ProductCovarianceModel([cov1, cov2]) test_model(myModel) # Collection ==> add covariance models # Add AbsoluteExponentialModel to the collection myAbsoluteExponential = ot.AbsoluteExponential([2.0] * inputDimension, [3.0]) mySquaredExponential = ot.SquaredExponential([2.0] * inputDimension, [3.0]) # Add exponentialModel to the collection amplitude = [4.0, 2.0] scale = [1.0] * inputDimension # Define a spatial correlation spatialCorrelation = ot.CorrelationMatrix(inputDimension) spatialCorrelation[1, 0] = 0.3 myExponentialModel = ot.ExponentialModel(scale, amplitude, spatialCorrelation) # Build TensorizedCovarianceModel with scale = [1,..,1] myModel = ot.TensorizedCovarianceModel( [myAbsoluteExponential, mySquaredExponential, myExponentialModel]) test_model(myModel, test_grad=False) # Define new scale scale = [2.5, 1.5] myModel.setScale(scale) test_model(myModel, test_grad=False)
import openturns as ot from matplotlib import pyplot as plt from openturns.viewer import View if ot.TensorizedCovarianceModel().__class__.__name__ == 'ExponentialModel': covarianceModel = ot.ExponentialModel([0.5], [5.0]) elif ot.TensorizedCovarianceModel( ).__class__.__name__ == 'GeneralizedExponential': covarianceModel = ot.GeneralizedExponential([2.0], [3.0], 1.5) elif ot.TensorizedCovarianceModel( ).__class__.__name__ == 'ProductCovarianceModel': amplitude = [1.0] scale1 = [4.0] scale2 = [4.0] cov1 = ot.ExponentialModel(scale1, amplitude) cov2 = ot.ExponentialModel(scale2, amplitude) covarianceModel = ot.ProductCovarianceModel([cov1, cov2]) elif ot.TensorizedCovarianceModel( ).__class__.__name__ == 'RankMCovarianceModel': variance = [1.0, 2.0] basis = ot.LinearBasisFactory().build() covarianceModel = ot.RankMCovarianceModel(variance, basis) else: covarianceModel = ot.TensorizedCovarianceModel() title = str(covarianceModel)[:100] if covarianceModel.getInputDimension() == 1: scale = covarianceModel.getScale()[0] if covarianceModel.isStationary(): def f(x): return [covarianceModel(x)[0, 0]]
def flow(X): Y0 = X[0] Y1 = X[1] dY0 = 0.5 * Y0 * (2.0 - Y1) dY1 = 0.5 * Y1 * (Y0 - 1.0) return [dY0, dY1] phi_func = ot.PythonFunction(2, 2, flow) # Create the mesh discretization = [N] * 2 mesh = ot.IntervalMesher(discretization).build(interval) # Covariance model covariance = ot.TensorizedCovarianceModel( [ot.SquaredExponential([0.2] * 2, [0.3])] * 2) process = ot.GaussianProcess(ot.TrendTransform(phi_func), covariance, mesh) field = process.getRealization() f = ot.Function(ot.P1LagrangeEvaluation(field)) ot.ResourceMap.SetAsUnsignedInteger("Field-LevelNumber", 64) ot.ResourceMap.SetAsScalar("Field-ArrowRatio", 0.01) ot.ResourceMap.SetAsScalar("Field-ArrowScaling", 0.03) graph = field.draw() print("f=", f.getInputDimension(), "->", f.getOutputDimension()) phi = ot.ValueFunction(f) solver = ot.RungeKutta(phi) initialState = [0.5, 1.0] timeGrid = ot.RegularGrid(0.0, 0.1, 10000) result = solver.solve(initialState, timeGrid) print(result) curve = ot.Curve(result)
coll = ot.CovarianceModelCollection() coll.add(ot.AbsoluteExponential(1, 3.0)) coll.add(ot.SquaredExponential(1, 2.0)) myModel = ot.ProductCovarianceModel(coll) test_model(myModel) collection = ot.CovarianceModelCollection() # Collection ==> add covariance models # Add AbsoluteExponentialModel to the collection myAbsoluteExponential = ot.AbsoluteExponential(spatialDimension, 3.0) collection.add(myAbsoluteExponential) # Add SquaredExponentialModel to the collection mySquaredExponential = ot.SquaredExponential(spatialDimension, 2.0) collection.add(mySquaredExponential) # Add exponentialModel to the collection amplitude = [4.0, 2.0] scale = [1.0] * spatialDimension # Define a spatial correlation spatialCorrelation = ot.CorrelationMatrix(spatialDimension) spatialCorrelation[1,0] = 0.3 myExponentialModel = ot.ExponentialModel(spatialDimension, amplitude, scale, spatialCorrelation) collection.add(myExponentialModel) # Build TensorizedCovarianceModel with scale = [1,..,1] myModel = ot.TensorizedCovarianceModel(collection) test_model(myModel) # Define new scale scale = [2.5, 1.5] myModel.setScale(scale) test_model(myModel)