Exemple #1
0
# Create the function to estimate
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()
    for i in range(sampleSize):
        X[i, 0] = 3.0 + i
        X2[i, 0] = 2.5 + i
    X[0, 0] = 1.0
    X[1, 0] = 3.0
    X2[0, 0] = 2.0
    X2[1, 0] = 4.0
    Y = model(X)
    # Data validation
    Y2 = model(X2)
    for i in range(sampleSize):
        # Add a small noise to data
        Y[i, 0] += 0.01 * ot.DistFunc.rNormal()

    basis = ot.LinearBasisFactory(spatialDimension).build()
    covarianceModel = ot.DiracCovarianceModel(spatialDimension)
    algo = ot.GeneralizedLinearModelAlgorithm(X, Y, covarianceModel, basis)
    algo.run()

    # perform an evaluation
    result = algo.getResult()
    metaModel = result.getMetaModel()
    conditionalCovariance = result.getCovarianceModel()
    residual = metaModel(X) - Y
    assert_almost_equal(residual.computeCenteredMoment(2), [0.00013144], 1e-5,
                        1e-5)
    assert_almost_equal(conditionalCovariance.getParameter(),
                        [0.011464782674211804], 1e-5, 1e-3)
    print("Test Ok")

except:
Exemple #3
0
myDefautModel = ot.SphericalModel([2.0], [3.0], 4.5)
print('myDefautModel = ', myDefautModel)
test_model(myDefautModel)

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

myDefautModel = ot.FractionalBrownianMotionModel(2.0, 3.0, 0.25)
print('myDefautModel = ', myDefautModel)
test_model(myDefautModel)

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

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()
import openturns as ot
from matplotlib import pyplot as plt
from openturns.viewer import View
if ot.DiracCovarianceModel().__class__.__name__ == 'ExponentialModel':
    covarianceModel = ot.ExponentialModel([0.5], [5.0])
elif ot.DiracCovarianceModel().__class__.__name__ == 'GeneralizedExponential':
    covarianceModel = ot.GeneralizedExponential([2.0], [3.0], 1.5)
elif ot.DiracCovarianceModel().__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.DiracCovarianceModel().__class__.__name__ == 'RankMCovarianceModel':
    variance = [1.0, 2.0]
    basis = ot.LinearBasisFactory().build()
    covarianceModel = ot.RankMCovarianceModel(variance, basis)
else:
    covarianceModel = ot.DiracCovarianceModel()
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)
        cov_graph.setTitle(title)
        fig = plt.figure(figsize=(10, 4))
myDefautModel = ot.ExponentiallyDampedCosineModel([2.0], [3.0], 1.5)
print('myDefautModel = ', myDefautModel)
test_model(myDefautModel)

myModel = ot.ExponentiallyDampedCosineModel([2.0] * spatialDimension, [3.0],
                                            1.5)
test_model(myModel)

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

myModel = ot.SphericalModel([2.0] * spatialDimension, [3.0], 4.5)
test_model(myModel)

myDefautModel = ot.DiracCovarianceModel()
print('myDefautModel = ', myDefautModel)
test_model(myDefautModel)

amplitude = list(map(lambda k: 1.5 + 2.0 * k, range(2)))
myModel = ot.DiracCovarianceModel(spatialDimension, amplitude)
test_model(myModel)

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)
outputSample *= scale

# translate sample
translate = [3.1]
outputSample += translate

# Finally inverse transform using an arbitrary lambda
lamb = [1.8]
boxCoxFunction = ot.InverseBoxCoxEvaluationImplementation(lamb)

# transform y using BoxCox function
outputSample = boxCoxFunction(outputSample)

# Add small noise
epsilon = ot.Normal(0, 1.0e-2).getSample(size)
outputSample +=  epsilon

# Now we build the factory
factory = ot.BoxCoxFactory()

# Creation of the BoxCoxTransform
result = ot.GeneralizedLinearModelResult()
basis = ot.LinearBasisFactory(1).build()
covarianceModel = ot.DiracCovarianceModel()
shift = [1.0e-1]
myBoxCox = factory.build(inputSample, outputSample, covarianceModel, basis, shift, result)

print("myBoxCox (GLM) =", myBoxCox)
print("GLM result     =", result)