import openturns as ot from openturns.viewer import View f = ot.SymbolicFunction(['x'], ['x + x * sin(x)']) inputSample = ot.Sample([[1.0], [3.0], [5.0], [6.0], [7.0], [8.0]]) outputSample = f(inputSample) f1 = ot.SymbolicFunction(['x'], ['sin(x)']) f2 = ot.SymbolicFunction(['x'], ['x']) f3 = ot.SymbolicFunction(['x'], ['cos(x)']) basis = ot.Basis([f1, f2, f3]) covarianceModel = ot.SquaredExponential([1.0]) covarianceModel.setActiveParameter([]) algo = ot.GeneralLinearModelAlgorithm(inputSample, outputSample, covarianceModel, basis) algo.run() result = algo.getResult() metamodel = result.getMetaModel() graph = f.draw(0.0, 10.0) graph.add(metamodel.draw(0.0, 10.0)) graph.add(ot.Cloud(inputSample, outputSample)) graph.setColors(['blue', 'red', 'black']) graph.setLegends(['model', 'meta model', 'sample']) graph.setLegendPosition('topleft') graph.setTitle('y(x)=x + x * sin(x)') View(graph, figure_kw={'figsize': (8, 4)})
model = ot.SymbolicFunction(["x0"], ["x0"]) X = ot.Sample(sampleSize, inputDimension) for i in range(sampleSize): X[i, 0] = 3.0 + (8.0 * i) / sampleSize Y = model(X) # Add a small noise to data Y += ot.GaussianProcess(ot.AbsoluteExponential([0.1], [0.2]), ot.Mesh(X)).getRealization().getValues() basis = ot.LinearBasisFactory(inputDimension).build() # Case of a misspecified covariance model covarianceModel = ot.DiracCovarianceModel(inputDimension) print("===================================================\n") algo = ot.GeneralLinearModelAlgorithm(X, Y, covarianceModel, basis) algo.run() result = algo.getResult() print("\ncovariance (dirac, optimized)=", result.getCovarianceModel()) print("trend (dirac, optimized)=", result.getTrendCoefficients()) print("===================================================\n") # Now without estimating covariance parameters basis = ot.LinearBasisFactory(inputDimension).build() covarianceModel = ot.DiracCovarianceModel(inputDimension) algo = ot.GeneralLinearModelAlgorithm(X, Y, covarianceModel, basis, True) algo.setOptimizeParameters(False) algo.run() result = algo.getResult() print("\ncovariance (dirac, not optimized)=", result.getCovarianceModel())
ot.RandomGenerator.SetSeed(0) dimension = 2 input_names = ['x1', 'x2'] formulas = ['cos(x1 + x2)'] model = ot.SymbolicFunction(input_names, formulas) distribution = ot.Normal(dimension) x = distribution.getSample(100) y = model(x) # %% # We create a `GeneralLinearModelAlgorithm` based on a linear basis. The `run` method estimats the coefficients of the trend and the hyperparameters of the covariance model. # %% basis = ot.LinearBasisFactory(dimension).build() covarianceModel = ot.SquaredExponential([1] * dimension, [1.0]) algo = ot.GeneralLinearModelAlgorithm(x, y, covarianceModel, basis) algo.run() result = algo.getResult() # %% # We see that the trend coefficients have been estimated. # %% result.getTrendCoefficients() # %% # The parameters of the covariance models also have been estimated. # %% result.getCovarianceModel()