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')
Ejemplo n.º 2
0
 def test_NonZeroMean(self):
     # Create the KL result
     numberOfVertices = 10
     interval = ot.Interval(-1.0, 1.0)
     mesh = ot.IntervalMesher([numberOfVertices - 1]).build(interval)
     covariance = ot.SquaredExponential()
     zeroProcess = ot.GaussianProcess(covariance, mesh)
     # Define a trend function
     f = ot.SymbolicFunction(["t"], ["30 * t"])
     fTrend = ot.TrendTransform(f, mesh)
     # Add it to the process
     process = ot.CompositeProcess(fTrend, zeroProcess)
     # Sample
     sampleSize = 100
     processSample = process.getSample(sampleSize)
     threshold = 0.0
     algo = ot.KarhunenLoeveSVDAlgorithm(processSample, threshold)
     algo.run()
     klresult = algo.getResult()
     # Create the KL reduction
     meanField = processSample.computeMean()
     klreduce = ot.KarhunenLoeveReduction(klresult)
     # Generate a trajectory and reduce it
     field = process.getRealization()
     values = field.getValues()
     reducedValues = klreduce(values)
     ott.assert_almost_equal(values, reducedValues)
 def run(self):
     """
     Run high density region algorithm.
     """
     # KL decomposition
     threshold = 0.0
     algo = ot.KarhunenLoeveSVDAlgorithm(self.processSample, threshold)
     algo.setNbModes(self.numberOfComponents)
     algo.run()
     karhunenLoeveResult = algo.getResult()
     self.reducedComponents = karhunenLoeveResult.project(
         self.processSample)
     numberOfComponents = self.reducedComponents.getDimension()
     labels = ["C" + str(i) for i in range(numberOfComponents)]
     self.reducedComponents.setDescription(labels)
 def test_KarhunenLoeveValidation(self):
     # Create the KL result
     numberOfVertices = 20
     interval = ot.Interval(-1.0, 1.0)
     mesh = ot.IntervalMesher([numberOfVertices - 1]).build(interval)
     covariance = ot.SquaredExponential()
     process = ot.GaussianProcess(covariance, mesh)
     sampleSize = 100
     processSample = process.getSample(sampleSize)
     threshold = 1.0e-7
     algo = ot.KarhunenLoeveSVDAlgorithm(processSample, threshold)
     algo.run()
     klresult = algo.getResult()
     # Create validation
     validation = ot.KarhunenLoeveValidation(processSample, klresult)
     # Check residuals
     residualProcessSample = validation.computeResidual()
     assert (type(residualProcessSample) is ot.ProcessSample)
     # Check standard deviation
     residualSigmaField = validation.computeResidualStandardDeviation()
     exact = ot.Sample(numberOfVertices, 1)
     #ott.assert_almost_equal(residualSigmaField, exact)
     # Check mean
     residualMean = validation.computeResidualMean()
     exact = ot.Sample(numberOfVertices, 1)
     #ott.assert_almost_equal(residualMean, exact)
     # Check graph
     graph0 = validation.drawValidation()
     graph1 = residualProcessSample.drawMarginal(0)
     graph2 = residualMean.drawMarginal(0)
     graph3 = residualSigmaField.drawMarginal(0)
     graph4 = validation.drawObservationWeight(0)
     graph5 = validation.drawObservationQuality()
     if 0:
         from openturns.viewer import View
         View(graph0).save('validation1.png')
         View(graph1).save('validation1-residual.png')
         View(graph2).save('validation1-residual-mean.png')
         View(graph3).save('validation1-residual-stddev.png')
         View(graph4).save('validation1-indiv-weight.png')
         View(graph5).save('validation1-indiv-quality.png')
Ejemplo n.º 5
0
 def test_ZeroMean(self):
     # Create the KL result
     numberOfVertices = 10
     interval = ot.Interval(-1.0, 1.0)
     mesh = ot.IntervalMesher([numberOfVertices - 1]).build(interval)
     covariance = ot.SquaredExponential()
     process = ot.GaussianProcess(covariance, mesh)
     sampleSize = 10
     processSample = process.getSample(sampleSize)
     threshold = 0.0
     algo = ot.KarhunenLoeveSVDAlgorithm(processSample, threshold)
     algo.run()
     klresult = algo.getResult()
     # Create the KL reduction
     meanField = processSample.computeMean()
     klreduce = ot.KarhunenLoeveReduction(klresult)
     # Generate a trajectory and reduce it
     field = process.getRealization()
     values = field.getValues()
     reducedValues = klreduce(values)
     ott.assert_almost_equal(values, reducedValues)
Ejemplo n.º 6
0
 def test_trend(self):
     N = 100
     M = 1000
     P = 10
     mean = ot.SymbolicFunction("x", "sign(x)")
     cov = ot.SquaredExponential([1.0], [0.1])
     mesh = ot.IntervalMesher([N]).build(ot.Interval(-2.0, 2.0))
     process = ot.GaussianProcess(ot.TrendTransform(mean, mesh), cov, mesh)
     sample = process.getSample(M)
     algo = ot.KarhunenLoeveSVDAlgorithm(sample, 1e-6)
     algo.run()
     result = algo.getResult()
     trend = ot.TrendTransform(
         ot.P1LagrangeEvaluation(sample.computeMean()), mesh)
     sample2 = process.getSample(P)
     sample2.setName('reduction of sign(x) w/o trend')
     reduced1 = ot.KarhunenLoeveReduction(result)(sample2)
     reduced2 = ot.KarhunenLoeveReduction(result, trend)(sample2)
     g = sample2.drawMarginal(0)
     g.setColors(["red"])
     g1 = reduced1.drawMarginal(0)
     g1.setColors(["blue"])
     drs = g1.getDrawables()
     for i, d in enumerate(drs):
         d.setLineStyle("dashed")
         drs[i] = d
     g1.setDrawables(drs)
     g.add(g1)
     g2 = reduced2.drawMarginal(0)
     g2.setColors(["green"])
     drs = g2.getDrawables()
     for i, d in enumerate(drs):
         d.setLineStyle("dotted")
         drs[i] = d
     g2.setDrawables(drs)
     g.add(g2)
     if 0:
         from openturns.viewer import View
         View(g).save('reduction.png')
Ejemplo n.º 7
0
ot.Log.Show(ot.Log.NONE)

# %%
# Create a Gaussian process
numberOfVertices = 20
interval = ot.Interval(-1.0, 1.0)
mesh = ot.IntervalMesher([numberOfVertices - 1]).build(interval)
covariance = ot.SquaredExponential()
process = ot.GaussianProcess(covariance, mesh)

# %%
# decompose it using KL-SVD
sampleSize = 100
processSample = process.getSample(sampleSize)
threshold = 1.0e-7
algo = ot.KarhunenLoeveSVDAlgorithm(processSample, threshold)
algo.run()
klresult = algo.getResult()

# %%
# Instanciate the validation service
validation = ot.KarhunenLoeveValidation(processSample, klresult)

# %%
# Plot the residual field
residualProcessSample = validation.computeResidual()
view = viewer.View(residualProcessSample.drawMarginal(0))

# %%
# Plot the residual mean field
residualMean = validation.computeResidualMean()
import openturns as ot
from matplotlib import pyplot as plt
from openturns.viewer import View
from math import sqrt

mesh = ot.IntervalMesher([128]).build(ot.Interval(-1.0, 1.0))
threshold = 0.001
model = ot.AbsoluteExponential([1.0])
sample = ot.GaussianProcess(model, mesh).getSample(100)
algo = ot.KarhunenLoeveSVDAlgorithm(sample, threshold)
algo.run()
ev = algo.getResult().getEigenValues()
modes = algo.getResult().getScaledModesAsProcessSample()
g = modes.drawMarginal(0)
g.setXTitle("$t$")
g.setYTitle("$\sqrt{\lambda_n}\phi_n$")

fig = plt.figure(figsize=(6, 4))
plt.suptitle("SVD approx. of KL expansion for $C(s,t)=e^{-|s-t|}$")
axis = fig.add_subplot(111)
axis.set_xlim(auto=True)
View(g, figure=fig, axes=[axis], add_legend=False)
Ejemplo n.º 9
0
    graph_ev.setLogScale(2)
    bb = graph_ev.getBoundingBox()
    lower = bb.getLowerBound()
    lower[1] = 1.0e-7
    bb = ot.Interval(lower, bb.getUpperBound())
    graph_ev.setBoundingBox(bb)
    return graph_modes, graph_ev


# %%
# We compute the Karhunen-Loeve decomposition of the model outputs.
# The underlying assumption is that these outputs are realizations of a
# stochastic process.

threshold = 0.0001
algoKL = ot.KarhunenLoeveSVDAlgorithm(outputFMUSample, threshold)
algoKL.run()
resultKL = algoKL.getResult()

# %%
# Let be curious and plot the Karhunen-Loeve modes:
phi_Y = resultKL.getScaledModesAsProcessSample()
lambda_Y = resultKL.getEigenvalues()
graph_modes_Y, graph_ev_Y = drawKL(phi_Y, lambda_Y, mesh, "Y")
view = viewer.View(graph_modes_Y)
view.ShowAll()

# %%
# Now that Karhunen-Loeve algorithm is trained, we can project them
# in the smaller-dimension space:
projectionSample = resultKL.project(outputFMUSample)
# %%
# Dynamical model: convolution wrt kernel p
print("Create the convolution function")
p = ot.SymbolicFunction("x", "exp(-(x/" + str(h) + ")^2)")
myConvolution = ot.FieldFunction(ConvolutionP1(p, mesh))

# %%
# Output database generation
print("Sample the output process")
sample_Y = myConvolution(sample_X)

# %%
# Karhunen-Loeve decomposition of the output process
print("Compute the decomposition of the output process")
algo_Y = ot.KarhunenLoeveSVDAlgorithm(sample_Y, threshold)
algo_Y.run()
result_Y = algo_Y.getResult()
phi_Y = result_Y.getScaledModesAsProcessSample()
lambda_Y = result_Y.getEigenvalues()
graph_modes_Y, graph_ev_Y = drawKL(phi_Y, lambda_Y, mesh, "Y")
view = viewer.View(graph_modes_Y)

# %%
# Compare eigenvalues of X and Y
graph_ev_X.add(graph_ev_Y)
graph_ev_X.setTitle("Input/output eigenvalues comparison")
graph_ev_X.setYTitle(r"$\lambda_X, \lambda_Y$")
graph_ev_X.setColors(["blue", "blue", "red", "red"])
graph_ev_X.setLegends([r"$\lambda_X$", "", r"$\lambda_Y$", ""])
graph_ev_X.setLegendPosition("topright")
#! /usr/bin/env python

from __future__ import print_function
import openturns as ot

ot.TESTPREAMBLE()

mesh = ot.IntervalMesher([9]).build(ot.Interval(-1.0, 1.0))
# 1D mesh, 1D covariance, uniform weight, automatic centering, more samples
# than vertices
cov1D = ot.AbsoluteExponential([1.0])
sample = ot.GaussianProcess(cov1D, mesh).getSample(16)
algo = ot.KarhunenLoeveSVDAlgorithm(sample, 0.0)
algo.run()
result = algo.getResult()
lambd = result.getEigenvalues()
KLModes = result.getModesAsProcessSample()
print("KL modes=", KLModes)
print("KL eigenvalues=", lambd)
coefficients = result.project(sample)
print("KL coefficients=", coefficients)
KLFunctions = result.getModes()
print("KL functions=", KLFunctions)
print("KL lift=", result.lift(coefficients[0]))
print("KL lift as field=", result.liftAsField(coefficients[0]))
# 1D mesh, 1D covariance, uniform weight, automatic centering
sample = ot.GaussianProcess(cov1D, mesh).getSample(6)
algo = ot.KarhunenLoeveSVDAlgorithm(sample, 0.0)
algo.run()
result = algo.getResult()
lambd = result.getEigenvalues()
Ejemplo n.º 12
0
import openturns as ot
from openturns.viewer import View

N = 100
M = 1000
P = 10
mean = ot.SymbolicFunction("x", "sign(x)")
cov = ot.SquaredExponential([1.0], [0.1])
mesh = ot.IntervalMesher([N]).build(ot.Interval(-2.0, 2.0))
process = ot.GaussianProcess(ot.TrendTransform(mean, mesh), cov, mesh)
sample = process.getSample(M)
algo = ot.KarhunenLoeveSVDAlgorithm(sample, 1e-6)
algo.run()
result = algo.getResult()
trend = ot.TrendTransform(ot.P1LagrangeEvaluation(sample.computeMean()), mesh)
sample2 = process.getSample(P)
sample2.setName('reduction of sign(x) w/o trend')
reduced1 = ot.KarhunenLoeveReduction(result)(sample2)
reduced2 = ot.KarhunenLoeveReduction(result, trend)(sample2)
g = sample2.drawMarginal(0)
g.setColors(["red"])
g1 = reduced1.drawMarginal(0)
g1.setColors(["blue"])
drs = g1.getDrawables()
for i, d in enumerate(drs):
    d.setLineStyle("dashed")
    drs[i] = d
g1.setDrawables(drs)
g.add(g1)
g2 = reduced2.drawMarginal(0)
g2.setColors(["green"])
Ejemplo n.º 13
0
graphInf.setColors(["red"])
graphMoy.add(graphSup)
graphMoy.add(graphInf)
graphMoy.setTitle('Trajectoire')
graphMoy.setXTitle(parameterIndexName)
graphMoy.setYTitle(fieldName)
graphMoy.setLegends(["Moyenne","Quantile %.2f%%" % (100-alphaInf*100),"Quantile %.2f%%" % (alphaInf*100)])
graphMoy.setLegendPosition("topleft")
View(graphMoy)

# Compute the KL decomposition of the output
size = 100
inputSample = distX.getSample(size)
outputSample = maFonctionChamp(inputSample)
threshold = 1.e-5 # Seuil pour la troncature des valeurs propres
algo = ot.KarhunenLoeveSVDAlgorithm(outputSample, threshold)
algo.run()
KLResult = algo.getResult()
scaledModes = KLResult.getScaledModesAsProcessSample()
nbModes = scaledModes.getSize()

# Plot the KL decomposition
graph = scaledModes.drawMarginal(0)
graph.setTitle('%s, seuil=%.2e, %d modes de KL' % (modeleName,threshold,nbModes))
graph.setXTitle(parameterIndexName)
graph.setYTitle(fieldName)
modesStr = ["Mode "+str(i) for i in range(nbModes)]
graph.setLegends(modesStr)
graph.setLegendPosition("topleft")
View(graph)
# %%
# Compute a training sample.

# %%
size = 2000
ot.RandomGenerator.SetSeed(0)
inputSample = distribution.getSample(size)
outputSample = alti(inputSample)

# %%
# Compute the KL decomposition of the output
# ------------------------------------------

# %%
algo = ot.KarhunenLoeveSVDAlgorithm(outputSample, 1.0e-6)
algo.run()
KLResult = algo.getResult()
scaledModes = KLResult.getScaledModesAsProcessSample()

# %%
graph = scaledModes.drawMarginal(0)
graph.setTitle('KL modes')
graph.setXTitle(r'$t$')
graph.setYTitle(r'$z$')
view = viewer.View(graph)

# %%
# We create the `postProcessingKL` function which takes coefficients of the the K.-L. modes as inputs and returns the trajectories.

# %%