from __future__ import print_function import openturns as ot from openturns.viewer import View from time import time size = 250 copula1 = ot.ClaytonCopula(3.0) tau = copula1.getKendallTau()[0, 1] print("tau=", tau) copula2 = ot.GumbelCopula(1.0 / (1.0 - tau)) sample = copula1.getSample(size) graph = ot.VisualTest.DrawKendallPlot(sample, copula1) graph.add(ot.VisualTest.DrawKendallPlot(sample, copula2).getDrawable(1)) graph.setColors(["red", "green", "blue"]) graph.setLegends( ["", "sample vs " + str(copula1), "sample vs " + str(copula2)]) graph.setLegendPosition("bottomright") graph.setTitle("Copula assessment using Kendall plot") graph.setXTitle(r"$x$") graph.setYTitle(r"$y$") view = View(graph, (800, 600)) view._ax[0].axis("equal") view.save("../plot_kendall_plot.png") view.close()
distribution = ot.KernelSmoothing().build(sample) algo = ot.MinimumVolumeClassifier(distribution, 0.8) threshold = algo.getThreshold() print("threshold=", threshold) assert m.fabs(threshold - 0.0012555) < 1e-3, "wrong threshold" cls_ref = [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1 ] for i in range(35): x = sample[i] cls = algo.classify(x) pdf = mixture.computePDF(x) print(i, x, cls, pdf - threshold) assert cls == cls_ref[i], "wrong class" graph1 = algo.drawSample(sample, [0]) graph2 = algo.drawSample(sample, [1]) graph3 = algo.drawSample(sample, [0, 1]) contour_alpha = [0.1, 0.5, 0.8] graph4 = algo.drawContour(contour_alpha) graph5 = algo.drawContourAndSample(contour_alpha, sample, [0, 1]) if len(sys.argv) > 1: from openturns.viewer import View # View(graph3).show() # View(graph4).show() view = View(graph5) view.save('mvc.png') view.ShowAll()
inSample, weights = weightedExperiment.generateWithWeights() print("Sample model") t0 = time() outSample = model(inSample) t1 = time() print("t=", t1 - t0, "s, speed=", inSample.getSize() / (t1 - t0), "evals/s") basis = ot.OrthogonalProductPolynomialFactory([ot.HermiteFactory()] * dim) adaptive = ot.FixedStrategy(basis, dim + 1) projection = ot.LeastSquaresStrategy(weightedExperiment) algo = ot.FunctionalChaosAlgorithm(inSample, outSample, distribution, adaptive, projection) algo.run() vector = ot.FunctionalChaosRandomVector(algo.getResult()) # Field of Sobol indices #for i in range(dim): for i in range(15, 16): print("i=", i) sobol = [ vector.getSobolIndex(i, j) for j in range(mesh.getVerticesNumber()) ] field = ot.Field(mesh, [[x] for x in sobol]) graph = field.draw() graph.setTitle("Sobol index field - component " + str(i)) #graph.add(field.drawMarginal(0)) view = View(graph, (800, 600)) view._ax[0].axis("equal") view.save("../plot_sobol_field_" + str(i).zfill(4) + ".png") view.close()
N = 10000 distribution = ot.Normal(2) X = ot.RandomVector(distribution) f = ot.SymbolicFunction(["x", "y"], ["x^2+y^2"]) Y = ot.RandomVector(f, X) event = ot.Event(Y, ot.Greater(), threshold) algo = ot.ProbabilitySimulationAlgorithm(event, ot.MonteCarloExperiment(1)) algo.setConvergenceStrategy(ot.Full()) algo.setMaximumOuterSampling(N) algo.setMaximumCoefficientOfVariation(0.0) algo.setMaximumStandardDeviation(0.0) algo.run() pRef = ot.ChiSquare(2).computeComplementaryCDF(threshold) # Draw convergence graph = algo.drawProbabilityConvergence() graph.setXMargin(0.0) graph.setLogScale(1) graph.setLegendPosition("topright") graph.setXTitle(r"n") graph.setYTitle(r"$\hat{p}_n$") graph.setTitle("Monte Carlo simulation - convergence history") ref = ot.Curve([[1, pRef], [N, pRef]]) ref.setColor("black") ref.setLineStyle("dashed") ref.setLegend(r"$p_{ref}$") graph.add(ref) view = View(graph, (800, 600)) view.save("../plot_monte_carlo.png") view.close()
for i in range(len(data))] bounds = ot.PolygonArray([ ot.Polygon( [dataLower[i], dataLower[i + 1], dataUpper[i + 1], dataUpper[i]], "green", "green") for i in range(len(dataLower) - 1) ]) graph = ot.Graph() graph.setLegendPosition("bottomright") graph.setAxes(True) graph.setGrid(True) graph.add(c) graph.add(bounds) d = f.draw(xMin, xMax).getDrawable(0) d.setLineStyle("dashed") d.setColor("magenta") d.setLineWidth(2) graph.add(d) graph.add(graph_meta) cloud = ot.Cloud(X, Y) cloud.setPointStyle("circle") cloud.setColor("red") graph.add(cloud) graph.setTitle("Kriging meta-modeling") graph.setXTitle(r"$x$") graph.setYTitle(r"$f$") graph.setLegends(["95% conf. bounds", "true function", "meta-model", "data"]) view = View(graph, (800, 600)) view.save("../plot_kriging.png") view.close()
from __future__ import print_function import openturns as ot from openturns.viewer import View from time import time size = 20000 randomWalk = ot.RandomWalk([0.0] * 2, ot.Normal(2), ot.RegularGrid(0, 1, size)) graph = randomWalk.getRealization().draw() graph.setColors(["red"]) graph.setTitle("2D random walk") graph.setXTitle(r"$x$") graph.setYTitle(r"$y$") view = View(graph, (800, 600)) view._ax[0].axis("equal") view.save("../plot_random_walk.png") view.close()
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()
mesh2 = ot.LevelSetMesher([3 * N] * 3).build( ls, ot.Interval([-2.5] * 3, [2.5] * 3), True, True) print("t (mesh1+mesh2)=", time() - t0, "s") print("mesh1=", mesh1.getVerticesNumber(), "vertices and", mesh1.getSimplicesNumber(), "simplices") print("mesh2=", mesh2.getVerticesNumber(), "vertices and", mesh2.getSimplicesNumber(), "simplices") print("draw mesh") t0 = time() graph = mesh1.draw3D(True, pi / 16, 0, pi / 4, False, 1.0) mesh2.setVertices(mesh2.getVertices() + ot.Point([-4.0, 0.0, 0.0])) ot.ResourceMap.SetAsScalar("Mesh-AmbientFactor", 0.1) ot.ResourceMap.SetAsScalar("Mesh-DiffuseFactor", 0.6) ot.ResourceMap.SetAsScalar("Mesh-SpecularFactor", 0.3) ot.ResourceMap.SetAsScalar("Mesh-Shininess", 100.0) graph.add(mesh2.draw3D(False, pi / 16, 0, pi / 4, True, 1.0)) graph.setTitle("3D mesh of implicit domains (with and without shading)") graph.setXTitle(r"") graph.setYTitle(r"") graph.setGrid(False) graph.setAxes(False) print("t=", time() - t0, "s") print("export") t0 = time() view = View(graph, (1200, 600)) view._ax[0].axis("equal") view.save("../plot_3d_mesh.png") view.close() print("Matplotlib=", time() - t0, "s")
from __future__ import print_function import openturns as ot from openturns.viewer import View # Generate some data size = 250 sample = ot.LogNormal(0.0, 0.4).getSample(size) # Estimate the distribution parametric_estimate = ot.LogNormalFactory().build(sample) nonparametric_estimate = ot.KernelSmoothing().build(sample) # Draw a non parametric estimate and the parametric estimate graph = parametric_estimate.drawPDF(0.0, 4.0) graph.add(nonparametric_estimate.drawPDF(0.0, 4.0)) graph.add(ot.Cloud(sample, ot.Sample(size, 1))) graph.setLegendPosition("topright") graph.setXTitle(r"$x$") graph.setYTitle(r"$p_X$") graph.setTitle(r"Parametric vs nonparametric estimation") graph.setColors(["red", "blue", "green"]) graph.setLegends(["parametric", "nonparametric", "data"]) view = View(graph, (800, 600)) view.save("../plot_distribution_fitting.png") view.close()