示例#1
0
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)
示例#2
0
    def _exec(self, X):
        f = ot.Function(
            ot.PiecewiseLinearEvaluation(
                [x[0] for x in self.getInputMesh().getVertices()], X))
        outputValues = ot.Sample(0, 1)
        for t in self.getOutputMesh().getVertices():
            kernel = ot.Normal(t[0], 0.05)

            def pdf(X):
                return [kernel.computePDF(X)]

            weight = ot.Function(ot.PythonFunction(1, 1, pdf))
            outputValues.add(
                self.algo_.integrate(weight * f, kernel.getRange()))
        return outputValues


N = 5
X = ot.GaussianProcess(ot.GeneralizedExponential([0.1], 1.0), mesh)
f = ot.FieldFunction(GaussianConvolution())
Y = ot.CompositeProcess(f, X)
x_graph = X.getSample(N).drawMarginal(0)
y_graph = Y.getSample(N).drawMarginal(0)
fig = plt.figure(figsize=(10, 4))
plt.suptitle("Composite process")
x_axis = fig.add_subplot(121)
y_axis = fig.add_subplot(122)
View(x_graph, figure=fig, axes=[x_axis], add_legend=False)
View(y_graph, figure=fig, axes=[y_axis], add_legend=False)
示例#3
0
        precision = ot.PlatformInfo.GetNumericalPrecision()
        ot.PlatformInfo.SetNumericalPrecision(4)
        print('dCov/dP=', pGrad)
        ot.PlatformInfo.SetNumericalPrecision(precision)


inputDimension = 2

myDefautModel = ot.SquaredExponential([2.0], [3.0])
print('myDefautModel = ', myDefautModel)
test_model(myDefautModel)

myModel = ot.SquaredExponential([2.0] * inputDimension, [3.0])
test_model(myModel)

myDefautModel = ot.GeneralizedExponential([2.0], [3.0], 1.5)
print('myDefautModel = ', myDefautModel)
test_model(myDefautModel)

myModel = ot.GeneralizedExponential([2.0] * inputDimension, [3.0], 1.5)
test_model(myModel)

myDefautModel = ot.AbsoluteExponential([2.0], [3.0])
print('myDefautModel = ', myDefautModel)
test_model(myDefautModel)

myModel = ot.AbsoluteExponential([2.0] * inputDimension, [3.0])
test_model(myModel)

myDefautModel = ot.MaternModel([2.0], [3.0], 1.5)
print('myDefautModel = ', myDefautModel)
示例#4
0
inputDimension = 2

# 1) SquaredExponential
myModel = ot.SquaredExponential([2.0], [3.0])
ott.assert_almost_equal(myModel.getScale(), [2], 0, 0)
ott.assert_almost_equal(myModel.getAmplitude(), [3], 0, 0)
test_model(myModel)

myModel = ot.SquaredExponential([2.0] * inputDimension, [3.0])
ott.assert_almost_equal(myModel.getScale(), [2, 2], 0, 0)
ott.assert_almost_equal(myModel.getAmplitude(), [3], 0, 0)
test_model(myModel)

# 2) GeneralizedExponential
myModel = ot.GeneralizedExponential([2.0], [3.0], 1.5)
ott.assert_almost_equal(myModel.getScale(), [2], 0, 0)
ott.assert_almost_equal(myModel.getAmplitude(), [3], 0, 0)
ott.assert_almost_equal(myModel.getP(), 1.5, 0, 0)
test_model(myModel)

myModel = ot.GeneralizedExponential([2.0] * inputDimension, [3.0], 1.5)
ott.assert_almost_equal(myModel.getScale(), [2, 2], 0, 0)
ott.assert_almost_equal(myModel.getAmplitude(), [3], 0, 0)
ott.assert_almost_equal(myModel.getP(), 1.5, 0, 0)
test_model(myModel)

# 3) AbsoluteExponential
myModel = ot.AbsoluteExponential([2.0], [3.0])
ott.assert_almost_equal(myModel.getScale(), [2], 0, 0)
ott.assert_almost_equal(myModel.getAmplitude(), [3], 0, 0)
# MetaModelValidation - SPC
metaModelValidationSPC = ot.MetaModelValidation(
    inputValidation, outputValidation, result.getMetaModel())
print("")
print("Sparse chaos scoring")
print(
    "Q2 = ", round(metaModelValidationSPC.computePredictivityFactor(), 5))
print("Residual sample = ", repr(
    metaModelValidationSPC.getResidualSample()))

# 2) Kriging algorithm
# KrigingAlgorithm
basis = ot.QuadraticBasisFactory(dimension).build()
# model already computed, separately
covarianceModel = ot.GeneralizedExponential(
    [3.52, 2.15, 2.99], [11.41], 2.0)
algo2 = ot.KrigingAlgorithm(
    inputSample, outputSample, covarianceModel, basis)
algo2.setOptimizeParameters(False)
algo2.run()
result2 = algo2.getResult()

# MetaModelValidation - KG
metaModelValidationKG = ot.MetaModelValidation(
    inputValidation, outputValidation, result2.getMetaModel())
print("")
print("Kriging scoring")
print("Q2 = ", round(metaModelValidationKG.computePredictivityFactor(), 3))
ot.PlatformInfo.SetNumericalPrecision(2)
print("Residual sample = ", repr(
    metaModelValidationKG.getResidualSample()))
示例#6
0
import openturns as ot
from matplotlib import pyplot as plt
from openturns.viewer import View


# Create a bivariate normal process
myMesh = ot.IntervalMesher([39, 39]).build(ot.Interval([0.0] * 2, [1.0] * 2))
myCov = ot.GeneralizedExponential(2, 0.1, 1.3)
myProcess = ot.TemporalNormalProcess(myCov, myMesh)

myField = myProcess.getRealization()

graph = myField.drawMarginal(0, False)

fig = plt.figure(figsize=(8, 4))
plt.suptitle("A field")
axis = fig.add_subplot(111)
axis.set_xlim(auto=True)
View(graph, figure=fig, axes=[axis], add_legend=True)
import openturns as ot
import openturns.viewer as viewer
from matplotlib import pylab as plt
# ot.Log.Show(ot.Log.NONE)

# %%
# Definition of the covariance model
# ----------------------------------
#
# We define our Gaussian process from its covariance model. We consider a Gaussian kernel with the following parameters.

# %%
dimension = 1
amplitude = [1.0] * dimension
scale = [1] * dimension
covarianceModel = ot.GeneralizedExponential(scale, amplitude, 2)

# %%
# We define the time grid on which we want to sample the Gaussian process.

# %%
tmin = 0.0
step = 0.01
n = 10001
timeGrid = ot.RegularGrid(tmin, step, n)

# %%
# Finally we define the Gaussian process.
process = ot.GaussianProcess(covarianceModel, timeGrid)
print(process)
import openturns as ot
from matplotlib import pyplot as plt
from openturns.viewer import View

# Create a bivariate Gaussian process
myMesh = ot.IntervalMesher([39, 39]).build(ot.Interval([0.0] * 2, [1.0] * 2))
myCov = ot.GeneralizedExponential(2 * [0.1], 1.3)
myProcess = ot.GaussianProcess(myCov, myMesh)

myField = myProcess.getRealization()

graph = myField.drawMarginal(0, False)

fig = plt.figure(figsize=(8, 4))
plt.suptitle("A field")
axis = fig.add_subplot(111)
axis.set_xlim(auto=True)
View(graph, figure=fig, axes=[axis], add_legend=True)
import openturns as ot
from matplotlib import pyplot as plt
from openturns.viewer import View
covarianceModel = ot.GeneralizedExponential()
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)
        inputValues = X.getValues()
        f = ot.NumericalMathFunction(
            ot.PiecewiseLinearEvaluationImplementation(
                [x[0] for x in inputTG.getVertices()], inputValues))
        outputValues = ot.NumericalSample(0, 1)
        for t in self.outputGrid_.getVertices():
            kernel = ot.Normal(t[0], 0.05)

            def pdf(X):
                return [kernel.computePDF(X)]

            weight = ot.NumericalMathFunction(ot.PythonFunction(1, 1, pdf))
            outputValues.add(
                self.algo_.integrate(weight * f, kernel.getRange()))
        return ot.Field(self.outputGrid_, outputValues)


N = 5
X = ot.TemporalNormalProcess(ot.GeneralizedExponential([0.1], 1.0),
                             ot.RegularGrid(-5.0, 0.1, 101))
f = ot.DynamicalFunction(GaussianConvolution())
Y = ot.CompositeProcess(f, X)
x_graph = X.getSample(N).drawMarginal(0)
y_graph = Y.getSample(N).drawMarginal(0)
fig = plt.figure(figsize=(10, 4))
plt.suptitle("Composite process")
x_axis = fig.add_subplot(121)
y_axis = fig.add_subplot(122)
View(x_graph, figure=fig, axes=[x_axis], add_legend=False)
View(y_graph, figure=fig, axes=[y_axis], add_legend=False)
    # MetaModelValidation - SPC
    metaModelValidationSPC = ot.MetaModelValidation(inputValidation,
                                                    outputValidation,
                                                    result.getMetaModel())
    print("")
    print("Sparse chaos scoring")
    print("Q2 = ", round(metaModelValidationSPC.computePredictivityFactor(),
                         5))
    print("Residual sample = ",
          repr(metaModelValidationSPC.getResidualSample()))

    # 2) Kriging algorithm
    # KrigingAlgorithm
    basis = ot.QuadraticBasisFactory(dimension).build()
    # model already computed, separatly
    covarianceModel = ot.GeneralizedExponential([1.933, 1.18, 1.644], [10.85],
                                                2.0)
    algo2 = ot.KrigingAlgorithm(inputSample, outputSample, basis,
                                covarianceModel, True, True)
    algo2.run()
    result2 = algo2.getResult()

    # MetaModelValidation - KG
    metaModelValidationKG = ot.MetaModelValidation(inputValidation,
                                                   outputValidation,
                                                   result2.getMetaModel())
    print("")
    print("Kriging scoring")
    print("Q2 = ", round(metaModelValidationKG.computePredictivityFactor(), 3))
    ot.PlatformInfo.SetNumericalPrecision(2)
    print("Residual sample = ",
          repr(metaModelValidationKG.getResidualSample()))
示例#12
0
import openturns as ot
from matplotlib import pyplot as plt
from openturns.viewer import View
if ot.GeneralizedExponential().__class__.__name__ == 'ExponentialModel':
    covarianceModel = ot.ExponentialModel([0.5], [5.0])
elif ot.GeneralizedExponential(
).__class__.__name__ == 'GeneralizedExponential':
    covarianceModel = ot.GeneralizedExponential([2.0], [3.0], 1.5)
elif ot.GeneralizedExponential(
).__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.GeneralizedExponential().__class__.__name__ == 'RankMCovarianceModel':
    variance = [1.0, 2.0]
    basis = ot.LinearBasisFactory().build()
    covarianceModel = ot.RankMCovarianceModel(variance, basis)
else:
    covarianceModel = ot.GeneralizedExponential()
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)
示例#13
0
    def _exec(self, X):
        inputTG = X.getTimeGrid()
        inputValues = X.getValues()
        f = ot.NumericalMathFunction(ot.PiecewiseLinearEvaluationImplementation(
            [x[0] for x in inputTG.getVertices()], inputValues))
        outputValues = ot.NumericalSample(0, 1)
        for t in self.outputGrid_.getVertices():
            kernel = ot.Normal(t[0], 0.05)

            def pdf(X):
                return [kernel.computePDF(X)]
            weight = ot.NumericalMathFunction(ot.PythonFunction(1, 1, pdf))
            outputValues.add(self.algo_.integrate(
                weight * f, kernel.getRange()))
        return ot.Field(self.outputGrid_, outputValues)

N = 5
X = ot.TemporalNormalProcess(ot.GeneralizedExponential(
    1, 0.1, 1.0), ot.RegularGrid(-5.0, 0.1, 101))
f = ot.DynamicalFunction(GaussianConvolution())
Y = ot.CompositeProcess(f, X)
x_graph = X.getSample(N).drawMarginal(0)
y_graph = Y.getSample(N).drawMarginal(0)
fig = plt.figure(figsize=(10, 4))
plt.suptitle("Composite process")
x_axis = fig.add_subplot(121)
y_axis = fig.add_subplot(122)
View(x_graph, figure=fig, axes=[x_axis], add_legend=False)
View(y_graph, figure=fig, axes=[y_axis], add_legend=False)
# The :class:`~openturns.GeneralizedExponential` class implements a generalized exponential with a
# parameter :math:`p < 0 \leq 2` exponent. The case :math:`p=1` is the standard exponential model
# while :math:`p=2` is the squared exponential.
#

# %%
# Various parameters p and a fixed correlation length of 0.1
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#
# In this part we set the correlation length to :math:`\theta = 0.1` and study three different models
# with parameters :math:`p=0.25`, :math:`p=1` and :math:`p=2` and trajectories from gaussian processes
# based on these models.

# %%
# We define the :math:`p = 0.25` generalized exponential model :
covarianceModel = ot.GeneralizedExponential([0.1], 0.25)

# %%
# We define the :math:`p = 1` generalized exponential model :
covarianceModel2 = ot.GeneralizedExponential([0.1], 1.0)

# %%
# We define the :math:`p = 2` generalized exponential model :
covarianceModel3 = ot.GeneralizedExponential([0.1], 2.0)

# %%
# We draw the covariance models :
graphModel = covarianceModel.draw()
graphModel.add(covarianceModel2.draw())
graphModel.add(covarianceModel3.draw())
graphModel.setColors(["green", "orange", "blue"])
示例#15
0
            for j in range(currentValue.getSize()):
                gradfd[i, j] = (currentValue[j] - centralValue[j]) / eps;
        print("dCov (FD)=", repr(gradfd))

spatialDimension = 2


myDefautModel = ot.SquaredExponential()
print("myDefautModel = ",  myDefautModel)


myModel = ot.SquaredExponential(spatialDimension)
test_model(myModel)


myDefautModel = ot.GeneralizedExponential()
print("myDefautModel = ",  myDefautModel)

myModel = ot.GeneralizedExponential(spatialDimension, 10.0, 1.5)
test_model(myModel)


myDefautModel = ot.AbsoluteExponential()
print("myDefautModel = ",  myDefautModel)

myModel = ot.AbsoluteExponential(spatialDimension)
test_model(myModel)


myDefautModel = ot.MaternModel()
print("myDefautModel = ",  myDefautModel)
        x = ot.Point(hmat.getNbColumns())
        x[i] = 1.0
        y = ot.Point(hmat.getNbRows())
        hmat.gemv('N', 1.0, x, 0.0, y)
        for j in range(hmat.getNbColumns()):
            res[i, j] = y[j]
    return res


if __name__ == "__main__":

    models = []
    refModels = []
    # rho correlation
    scale = [4, 5]
    rho = ot.GeneralizedExponential(scale, 1)

    # Amplitude values
    amplitude = [1, 2]
    myModel = ot.KroneckerCovarianceModel(rho, amplitude)
    models.append(myModel)

    spatialCorrelation = ot.CorrelationMatrix(2)
    spatialCorrelation[0, 1] = 0.8
    myModel = ot.KroneckerCovarianceModel(rho, amplitude, spatialCorrelation)
    models.append(myModel)

    spatialCovariance = ot.CovarianceMatrix(2)
    spatialCovariance[0, 0] = 4.0
    spatialCovariance[1, 1] = 5.0
    spatialCovariance[1, 0] = 1.2