import openturns.viewer as viewer from matplotlib import pylab as plt ot.Log.Show(ot.Log.NONE) # %% # Define a process myIndices = ot.Indices([10, 5]) myMesher = ot.IntervalMesher(myIndices) myInterval = ot.Interval([0.0, 0.0], [2.0, 1.0]) myMesh = myMesher.build(myInterval) amplitude = [1.0] scale = [0.2, 0.2] myCovModel = ot.ExponentialModel(scale, amplitude) myXproc = ot.GaussianProcess(myCovModel, myMesh) g = ot.SymbolicFunction(['x1'], ['exp(x1)']) myDynTransform = ot.ValueFunction(g, myMesh) myXtProcess = ot.CompositeProcess(myDynTransform, myXproc) # %% # Draw a field field = myXtProcess.getRealization() graph = field.drawMarginal(0) view = viewer.View(graph) # %% # Draw values marginal = ot.HistogramFactory().build(field.getValues()) graph = marginal.drawPDF() view = viewer.View(graph) # %%
myMesh = myMesher.build(myInterval) # Define a scalar temporal Gaussian process on the mesh # this process is stationary # myXproc R^2 --> R amplitude = [1.0] scale = [0.2, 0.2] myCovModel = ot.ExponentialModel(scale, amplitude) myXproc = ot.GaussianProcess(myCovModel, myMesh) # Transform myXproc to make its variance depend on the vertex (s,t) # and to get a positive process # thanks to the spatial function g # myXtProcess R --> R g = ot.SymbolicFunction(['x1'], ['exp(x1)']) myDynTransform = ot.ValueFunction(g, 2) myXtProcess = ot.CompositeProcess(myDynTransform, myXproc) myField = myXtProcess.getRealization() graphMarginal1 = ot.KernelSmoothing().build(myField.getValues()).drawPDF() graphMarginal1.setTitle("") graphMarginal1.setXTitle("X") graphMarginal1.setLegendPosition("") # Initiate a BoxCoxFactory myBoxCoxFactory = ot.BoxCoxFactory() graph = ot.Graph() shift = [0.0] # We estimate the lambda parameter from the field myField
#! /usr/bin/env python from __future__ import print_function import openturns as ot ot.TESTPREAMBLE() # Create a KarhunenLoeveResult mesh = ot.IntervalMesher([9]).build(ot.Interval(-1.0, 1.0)) cov1D = ot.AbsoluteExponential([1.0]) algo = ot.KarhunenLoeveP1Algorithm(mesh, cov1D, 0.0) algo.run() result = algo.getResult() projection = ot.KarhunenLoeveProjection(result) # Construction based on a FieldFunction followed by a FieldToPointFunction fieldFunction = ot.ValueFunction(ot.SymbolicFunction("x", "x"), mesh) # Create an instance myFunc = ot.FieldToPointConnection(projection, fieldFunction) print("myFunc=", myFunc) # Get the input and output description print("myFunc input description=", myFunc.getInputDescription()) print("myFunc output description=", myFunc.getOutputDescription()) # Get the input and output dimension print("myFunc input dimension=", myFunc.getInputDimension()) print("myFunc output dimension=", myFunc.getOutputDimension()) # Connection on a field field = result.getModesAsProcessSample().computeMean() print("field=", field) print("myFunc(field)=", myFunc(field.getValues())) print("called ", myFunc.getCallsNumber(), " times")
deltaT = 0.1 steps = 11 # Initialization of the TimeGrid timeGrid timeGrid = ot.RegularGrid(Tmin, deltaT, steps) # Creation of the Antecedent myARMAProcess = ot.ARMA() myARMAProcess.setTimeGrid(timeGrid) print('myAntecedentProcess = ', myARMAProcess) # Creation of a function f = ot.SymbolicFunction(['x'], ['2 * x + 5.0']) # We build a dynamical function myFieldFunction = ot.FieldFunction(ot.ValueFunction(f)) # finally we get the compsite process myCompositeProcess = ot.CompositeProcess(myFieldFunction, myARMAProcess) print('myCompositeProcess = ', repr(myCompositeProcess)) # Test realization print('One realization= ') print(myCompositeProcess.getRealization()) # # Create a spatial dynamical function # Create the function g : R^2 --> R^2 # (x1,x2) --> (x1^2, x1+x2) g = ot.SymbolicFunction(['x1', 'x2'], ['x1^2', 'x1+x2'])
# Test realization print('One realization= ') print(myCompositeProcess.getRealization()) # future print('future=', myCompositeProcess.getFuture(5)) # # Create a spatial dynamical function # Create the function g : R^2 --> R^2 # (x1,x2) --> (x1^2, x1+x2) g = ot.SymbolicFunction(['x1', 'x2'], ['x1^2', 'x1+x2']) # Convert g : R --> R into a spatial fucntion myDynFunc = ot.ValueFunction(g, ot.Mesh(2)) # Then g acts on processes X: Omega * R^nSpat --> R^2 # # Create a trend function fTrend: R^n --> R^q # for example for myXtProcess of dimension 2 # defined on a bidimensional mesh # fTrend : R^2 --> R^2 # (t1, t2) --> (1+2t1, 1+3t2) fTrend = ot.SymbolicFunction(['t1', 't2'], ['1+2*t1', '1+3*t2']) # # Create a Gaussian process of dimension 2 # which mesh is of box of dimension 2
# %% import openturns as ot import openturns.viewer as viewer from matplotlib import pylab as plt import math as m ot.Log.Show(ot.Log.NONE) # %% # Create a mesh N = 100 mesh = ot.RegularGrid(0.0, 1.0, N) # %% # Create the function that acts the values of the mesh g = ot.SymbolicFunction(['x1', 'x2'], ['x1^2', 'x1+x2']) # %% # Create the field function f = ot.ValueFunction(g, mesh) # %% # Evaluate f inF = ot.Normal(2).getSample(N) outF = f(inF) # print input/output at first mesh nodes xy = inF xy.stack(outF) xy[:5]
# Create the mesh discretization = [N] * 2 mesh = ot.IntervalMesher(discretization).build(interval) # Covariance model covariance = ot.TensorizedCovarianceModel( [ot.SquaredExponential([0.2] * 2, [0.3])] * 2) process = ot.GaussianProcess(ot.TrendTransform(phi_func), covariance, mesh) field = process.getRealization() f = ot.Function(ot.P1LagrangeEvaluation(field)) ot.ResourceMap.SetAsUnsignedInteger("Field-LevelNumber", 64) ot.ResourceMap.SetAsScalar("Field-ArrowRatio", 0.01) ot.ResourceMap.SetAsScalar("Field-ArrowScaling", 0.03) graph = field.draw() print("f=", f.getInputDimension(), "->", f.getOutputDimension()) phi = ot.ValueFunction(f) solver = ot.RungeKutta(phi) initialState = [0.5, 1.0] timeGrid = ot.RegularGrid(0.0, 0.1, 10000) result = solver.solve(initialState, timeGrid) print(result) curve = ot.Curve(result) curve.setColor("red") curve.setLineWidth(2) graph.add(curve) graph.setTitle("Perturbed Lotka-Voltera system") graph.setXTitle(r"$x$") graph.setYTitle(r"$y$") view = View(graph, (800, 600)) view.save("../plot_random_field.png") view.close()
deltaT = 0.1 steps = 11 # Initialization of the TimeGrid timeGrid timeGrid = ot.RegularGrid(Tmin, deltaT, steps) # Creation of the Antecedent myARMAProcess = ot.ARMA() myARMAProcess.setTimeGrid(timeGrid) print('myAntecedentProcess = ', myARMAProcess) # Creation of a function f = ot.SymbolicFunction(['x'], ['2 * x + 5.0']) # We build a dynamical function myFieldFunction = ot.FieldFunction(ot.ValueFunction(f, timeGrid)) # finally we get the compsite process myCompositeProcess = ot.CompositeProcess(myFieldFunction, myARMAProcess) print('myCompositeProcess = ', repr(myCompositeProcess)) # Test realization print('One realization= ') print(myCompositeProcess.getRealization()) # # Create a spatial dynamical function # Create the function g : R^2 --> R^2 # (x1,x2) --> (x1^2, x1+x2) g = ot.SymbolicFunction(['x1', 'x2'], ['x1^2', 'x1+x2'])
import openturns as ot from matplotlib import pyplot as plt from openturns.viewer import View def flow(X): Y0 = X[0] Y1 = X[1] dY0 = Y0 * (2.0 - Y1) dY1 = Y1 * (Y0 - 1.0) return [dY0, dY1] phi_func = ot.PythonFunction(2, 2, flow) phi = ot.ValueFunction(phi_func) solver = ot.Fehlberg(phi) initialState = [2.0, 2.0] nt = 47 dt = 0.1 timeGrid = ot.RegularGrid(0.0, dt, nt) result = solver.solve(initialState, timeGrid) xMin = result.getMin() xMax = result.getMax() delta = 0.2 * (xMax - xMin) mesh = ot.IntervalMesher([12] * 2).build( ot.Interval(xMin - delta, xMax + delta)) field = ot.Field(mesh, phi_func(mesh.getVertices())) ot.ResourceMap.SetAsScalar("Field-ArrowScaling", 0.1) graph = field.draw() cloud = ot.Cloud(mesh.getVertices())