def test_active_parameter(): # Define product of matern 1d cov_model_1d = ot.MaternModel([0.5], 2.5) print("1D Full parameter : ", cov_model_1d.getFullParameter()) print("1D active cov. param.: ", [cov_model_1d.getFullParameterDescription()[ i] for i in cov_model_1d.getActiveParameter()]) print("Activate nu parameter") cov_model_1d.setActiveParameter([0, 1, 2]) print("active cov. param.: ", [cov_model_1d.getFullParameterDescription()[ i] for i in cov_model_1d.getActiveParameter()]) print("Matern d-dimensional covariance as product") d = 3 cov_model = ot.ProductCovarianceModel([cov_model_1d]*d) marginal0 = cov_model.getMarginal(0) assert marginal0.getInputDimension() == d, "wrong marginal input dim" assert marginal0.getOutputDimension() == 1, "wrong marginal output dim" print("Full parameter : ", cov_model.getFullParameter()) print("active cov. param.: ", [cov_model.getFullParameterDescription()[ i] for i in cov_model.getActiveParameter()]) print("Disable nu for marginals 0 & 1 parameter : ", cov_model.getFullParameter()) cov_model.setActiveParameter([0, 1, 2, 3, 6]) print("active cov. param.: ", [cov_model.getFullParameterDescription()[ i] for i in cov_model.getActiveParameter()]) print("Check that active parameter is correctly propagated") for k in range(3): print("Model ", k, " : active cov. param.: ", [cov_model.getCollection()[ k].getFullParameterDescription()[i] for i in cov_model.getCollection()[k].getActiveParameter()])
def test_active_amplitude_parameter(): # Define product of matern 1d model1 = ot.MaternModel([1.0], 2.5) print("Model 1 : ", model1.getFullParameterDescription()) print("Activate nu parameter and disable sigma2") model1.setActiveParameter([0, 2]) print("model1 active parameter: ", [ model1.getFullParameterDescription()[i] for i in model1.getActiveParameter() ]) model2 = ot.ExponentiallyDampedCosineModel() print("Model 2 : ", model2.getFullParameterDescription()) print("Activate freq parameter") model2.setActiveParameter([0, 1, 2]) print("model2 active parameter: ", [ model2.getFullParameterDescription()[i] for i in model2.getActiveParameter() ]) print("Product covariance model") d = 3 cov_model = ot.ProductCovarianceModel([model1, model2]) print("Full parameter : ", cov_model.getFullParameter()) print("active cov. param.: ", [ cov_model.getFullParameterDescription()[i] for i in cov_model.getActiveParameter() ])
def _buildKrigingAlgo(self, inputSample, outputSample): """ Build the functional chaos algorithm without running it. """ if self._basis is None: # create linear basis only for the defect parameter (1st parameter), # constant otherwise input = ['x' + str(i) for i in range(self._dim)] functions = [] # constant functions.append(ot.NumericalMathFunction(input, ['y'], ['1'])) # linear for the first parameter only functions.append(ot.NumericalMathFunction(input, ['y'], [input[0]])) self._basis = ot.Basis(functions) if self._covarianceModel is None: # anisotropic squared exponential covariance model covColl = ot.CovarianceModelCollection(self._dim) for i in range(self._dim): if LooseVersion(ot.__version__) == '1.6': covColl[i] = ot.SquaredExponential(1, 1.) elif LooseVersion(ot.__version__) > '1.6': covColl[i] = ot.SquaredExponential([1], [1.]) self._covarianceModel = ot.ProductCovarianceModel(covColl) if LooseVersion(ot.__version__) == "1.9": algoKriging = ot.KrigingAlgorithm(inputSample, outputSample, self._covarianceModel, self._basis) else: algoKriging = ot.KrigingAlgorithm(inputSample, outputSample, self._basis, self._covarianceModel, True) algoKriging.run() return algoKriging
import openturns as ot from matplotlib import pyplot as plt from openturns.viewer import View if ot.MaternModel().__class__.__name__ == 'ExponentialModel': covarianceModel = ot.ExponentialModel([0.5], [5.0]) elif ot.MaternModel().__class__.__name__ == 'GeneralizedExponential': covarianceModel = ot.GeneralizedExponential([2.0], [3.0], 1.5) elif ot.MaternModel().__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.MaternModel().__class__.__name__ == 'RankMCovarianceModel': variance = [1.0, 2.0] basis = ot.LinearBasisFactory().build() covarianceModel = ot.RankMCovarianceModel(variance, basis) else: covarianceModel = ot.MaternModel() title = str(covarianceModel)[:100] if covarianceModel.getInputDimension() == 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)
def test_parameters_iso(): scale = [] amplitude = 1.0 extraParameter = [] # model 1 atom_ex = ot.IsotropicCovarianceModel(ot.MaternModel(), 2) atom_ex.setScale([5]) atom_ex.setAmplitude([1.5]) scale.append(5) amplitude *= 1.5 extraParameter.append(atom_ex.getKernel().getFullParameter()[-1]) # model2 m = ot.MaternModel() m.setNu(2.5) m.setScale([3]) m.setAmplitude([3]) scale.append(3) amplitude *= 3 extraParameter.append(m.getNu()) # model 3 atom = ot.IsotropicCovarianceModel(ot.AbsoluteExponential(), 2) atom.setScale([2]) atom.setAmplitude([2.5]) scale.append(2) amplitude *= 2.5 model = ot.ProductCovarianceModel([atom_ex, m, atom]) ott.assert_almost_equal(model.getScale(), scale, 1e-16, 1e-16) ott.assert_almost_equal(model.getAmplitude(), [amplitude], 1e-16, 1e-16) ott.assert_almost_equal(model.getFullParameter(), scale + [amplitude] + extraParameter, 1e-16, 1e-16) # active parameter should be scale + amplitude ott.assert_almost_equal(model.getActiveParameter(), [0, 1, 2, 3], 1e-16, 1e-16) # setting new parameters extraParameter = [2.5, 0.5] model.setFullParameter([6, 7, 8, 2] + extraParameter) ott.assert_almost_equal(model.getCollection()[ 0].getScale()[0], 6, 1e-16, 1e-16) ott.assert_almost_equal(model.getCollection()[ 1].getScale()[0], 7, 1e-16, 1e-16) ott.assert_almost_equal(model.getCollection()[ 2].getScale()[0], 8, 1e-16, 1e-16) ott.assert_almost_equal(model.getAmplitude()[0], 2, 1e-16, 1e-16) ott.assert_almost_equal(model.getCollection( )[0].getFullParameter()[-1], extraParameter[0], 1e-16, 1e-16) ott.assert_almost_equal(model.getCollection( )[1].getFullParameter()[-1], extraParameter[1], 1e-16, 1e-16) # checking active par setting model.setActiveParameter([0, 1, 2, 3, 5]) ott.assert_almost_equal(model.getParameter(), [ 6, 7, 8, 2, extraParameter[-1]], 1e-16, 1e-16)
import openturns as ot from matplotlib import pyplot as plt from openturns.viewer import View covarianceModel = ot.ProductCovarianceModel() 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)
myDefautModel = ot.DiracCovarianceModel() print('myDefautModel = ', myDefautModel) test_model(myDefautModel) amplitude = [1.5 + 2.0 * k for k in range(2)] dimension = 2 spatialCorrelation = ot.CorrelationMatrix(dimension) for j in range(dimension): for i in range(j + 1, dimension): spatialCorrelation[i, j] = (i + 1.0) / dimension - (j + 1.0) / dimension myModel = ot.DiracCovarianceModel(inputDimension, amplitude, spatialCorrelation) 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
XX_input = ot.Sample([[0.1, 0], [0.32, 0], [0.6, 0], [0.9, 0], [ 0.07, 1], [0.1, 1], [0.4, 1], [0.5, 1], [0.85, 1]]) y_output = ot.Sample(len(XX_input), 1) for i in range(len(XX_input)): y_output[i, 0] = fun_mixte(XX_input[i]) def C(s, t): return m.exp(-4.0 * abs(s - t) / (1 + (s * s + t * t))) N = 32 a = 4.0 myMesh = ot.IntervalMesher([N]).build(ot.Interval(-a, a)) myCovariance = ot.CovarianceMatrix(myMesh.getVerticesNumber()) for k in range(myMesh.getVerticesNumber()): t = myMesh.getVertices()[k] for l in range(k + 1): s = myMesh.getVertices()[l] myCovariance[k, l] = C(s[0], t[0]) covModel_discrete = ot.UserDefinedCovarianceModel(myMesh, myCovariance) f_ = ot.SymbolicFunction(["tau", "theta", "sigma"], [ "(tau!=0) * exp(-1/theta) * sigma * sigma + (tau==0) * exp(0) * sigma * sigma"]) rho = ot.ParametricFunction(f_, [1, 2], [0.2, 0.3]) covModel_discrete = ot.StationaryFunctionalCovarianceModel([1.0], [ 1.0], rho) covModel_continuous = ot.SquaredExponential([1.0], [1.0]) covarianceModel = ot.ProductCovarianceModel( [covModel_continuous, covModel_discrete]) covarianceModel.discretize(XX_input)
ot.RandomGenerator.SetSeed(0) ot.RandomGenerator.SetState(ot.RandomGeneratorState(ot.Indices([0] * 768), 0)) POD2 = otpod.KrigingPOD(inputSample, signals, detection, noiseThres, saturationThres, boxCox=False) if ot.__version__ == '1.6': covColl = ot.CovarianceModelCollection(4) scale = [5.03148, 13.9442, 20, 20] for i in range(4): c = ot.SquaredExponential(1, scale[i]) c.setAmplitude([15.1697]) covColl[i] = c covarianceModel = ot.ProductCovarianceModel(covColl) elif ot.__version__ > '1.6': covarianceModel = ot.SquaredExponential([5.03148, 13.9442, 20, 20], [15.1697]) POD2.setCovarianceModel(covarianceModel) POD2.setInitialStartSize(0) POD2.setSamplingSize(100) POD2.setSimulationSize(100) POD2.run() detectionSize2 = POD2.computeDetectionSize(0.6, 0.95) def test_2_a90(): np.testing.assert_almost_equal(detectionSize2[0], 4.423224253595858, decimal=4)
import openturns as ot from matplotlib import pyplot as plt from openturns.viewer import View if ot.ProductCovarianceModel().__class__.__name__ == 'ExponentialModel': covarianceModel = ot.ExponentialModel([0.5], [5.0]) elif ot.ProductCovarianceModel( ).__class__.__name__ == 'GeneralizedExponential': covarianceModel = ot.GeneralizedExponential([2.0], [3.0], 1.5) elif ot.ProductCovarianceModel( ).__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.ProductCovarianceModel().__class__.__name__ == 'RankMCovarianceModel': variance = [1.0, 2.0] basis = ot.LinearBasisFactory().build() covarianceModel = ot.RankMCovarianceModel(variance, basis) else: covarianceModel = ot.ProductCovarianceModel() title = str(covarianceModel)[:100] if covarianceModel.getInputDimension() == 1: scale = covarianceModel.getScale()[0] if covarianceModel.isStationary(): def f(x): return [covarianceModel(x)[0, 0]] func = ot.PythonFunction(1, 1, f)
myDefautModel = ot.AbsoluteExponential() print("myDefautModel = ", myDefautModel) myModel = ot.AbsoluteExponential(spatialDimension) test_model(myModel) myDefautModel = ot.MaternModel() print("myDefautModel = ", myDefautModel) myModel = ot.MaternModel(spatialDimension, 8.0, 2.0) test_model(myModel) myDefautModel = ot.ProductCovarianceModel() print("myDefautModel = ", myDefautModel) 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)