import openturns.viewer as viewer from matplotlib import pylab as plt ot.Log.Show(ot.Log.NONE) # %% # Copula fitting test using Kendall plot # -------------------------------------- # # We first perform a visual goodness-of-fit test for a copula with the Kendall plot test. # %% # We create two samples of size 500. N = 500 # %% dist1 = ot.ComposedDistribution([ot.Normal()] * 2, ot.GumbelCopula(3.0)) sample1 = dist1.getSample(N) sample1.setName('sample1') # %% dist2 = ot.ComposedDistribution([ot.Normal()] * 2, ot.ClaytonCopula(0.2)) sample2 = dist2.getSample(N) sample2.setName('sample2') # %% # We change the parameter for the evaluation of E(Wi) thanks to the `ResourceMap` : ot.ResourceMap.SetAsUnsignedInteger('VisualTest-KendallPlot-MonteCarloSize', 25) # %% # We can test a specific copula model for a given sample,
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()
def check_bernstein_copula(est_copula): """ Check if an estimated distribution of kind EmpiricalBernstein is of type copula and all marginals are also. """ print("Is estimation a copula ? --> ", est_copula.isCopula()) print("Maginal checking") dimension = est_copula.getDimension() for d in range(dimension): print("Is marginal %d a copula ? --> %s" % (d, est_copula.isCopula())) try: coll = [ot.GumbelCopula(3.0), ot.ClaytonCopula(3.0), ot.FrankCopula(3.0)] size = 100 for i, ref_copula in enumerate(coll): ref_copula = coll[i] print("Reference copula", str(ref_copula)) sample = ref_copula.getSample(size) # Default method: log-likelihood m = ot.BernsteinCopulaFactory.ComputeLogLikelihoodBinNumber(sample) print("Log-likelihood m=", m) est_copula = ot.BernsteinCopulaFactory().build(sample, m) max_error = compute_max_error(ref_copula, est_copula) print("Max. error=%.5f" % max_error) check_bernstein_copula(est_copula) # AMISE method m = ot.BernsteinCopulaFactory.ComputeAMISEBinNumber(sample) print("AMISE m=", m)
# ot.Log.Show(ot.Log.ALL) coll = [] # case 1: no transformation coll.append([ot.Normal(), ot.Normal()]) # case 2: same copula left = ot.ComposedDistribution([ot.Normal(), ot.Gumbel()], ot.IndependentCopula(2)) right = ot.ComposedDistribution([ot.Triangular()] * 2, ot.IndependentCopula(2)) coll.append([left, right]) # case 3: same standard space left = ot.ComposedDistribution([ot.Normal(), ot.Gumbel()], ot.IndependentCopula(2)) right = ot.ComposedDistribution([ot.Triangular()] * 2, ot.GumbelCopula()) coll.append([left, right]) # TODO case 4: different standard space for left, right in coll: transformation = ot.DistributionTransformation(left, right) print('left=', left) print('right=', right) print('transformation=', transformation) inverseTransformation = transformation.inverse() print('inverseTransformation=', inverseTransformation) print('-' * 100)
import openturns as ot from matplotlib import pyplot as plt from openturns.viewer import View if (ot.GumbelCopula().__class__.__name__ == 'SklarCopula'): myStudent = ot.Student(3.0, [1.0] * 2, [3.0] * 2, ot.CorrelationMatrix(2)) copula = ot.SklarCopula(myStudent) else: copula = ot.GumbelCopula() if copula.getDimension() == 1: copula = ot.GumbelCopula(2) copula.setDescription(['$u_1$', '$u_2$']) pdf_graph = copula.drawPDF() cdf_graph = copula.drawCDF() fig = plt.figure(figsize=(10, 4)) plt.suptitle(str(copula)) pdf_axis = fig.add_subplot(121) cdf_axis = fig.add_subplot(122) View(pdf_graph, figure=fig, axes=[pdf_axis], add_legend=False) View(cdf_graph, figure=fig, axes=[cdf_axis], add_legend=False) pdf_axis.set_aspect('equal') cdf_axis.set_aspect('equal')
expression += "cos(" + str(i + 1) + "*" + inputVar[i] + ")" formula[0] = expression model = ot.SymbolicFunction(inputVar, formula) outputSample = model(inputSample) cobwebValue = ot.VisualTest.DrawParallelCoordinates( inputSample, outputSample, 2.5, 3.0, "red", False) print("cobwebValue = ", cobwebValue) cobwebQuantile = ot.VisualTest.DrawParallelCoordinates( inputSample, outputSample, 0.7, 0.9, "red", False) print("cobwebQuantile = ", cobwebQuantile) # KendallPlot tests size = 100 copula1 = ot.FrankCopula(1.5) copula2 = ot.GumbelCopula(4.5) sample1 = copula1.getSample(size) sample1.setName("data 1") sample2 = copula2.getSample(size) sample2.setName("data 2") kendallPlot1 = ot.VisualTest.DrawKendallPlot(sample1, copula2) print("KendallPlot1 = ", kendallPlot1) kendallPlot2 = ot.VisualTest.DrawKendallPlot(sample2, sample1) print("KendallPlot2 = ", kendallPlot2) # Clouds sample = ot.Normal(4).getSample(200) clouds = ot.VisualTest.DrawPairs(sample) print("Clouds = ", clouds) distribution = ot.ComposedDistribution(
import openturns as ot from matplotlib import pyplot as plt from openturns.viewer import View myCop1 = ot.GumbelCopula(2) myCop2 = ot.NormalCopula(2) alpha = 0.3 myOrdSumCop = ot.OrdinalSumCopula([myCop1, myCop2], [alpha]) myOrdSumCop.setDescription(['$u_1$', '$u_2$']) graphPDF = myOrdSumCop.drawPDF() graphCDF = myOrdSumCop.drawCDF() fig = plt.figure(figsize=(8, 4)) plt.suptitle("Ordinal Sum of Copulas: Gumbel(2) and Normal(2): pdf and cdf") pdf_axis = fig.add_subplot(121) cdf_axis = fig.add_subplot(122) pdf_axis.set_xlim(auto=True) cdf_axis.set_xlim(auto=True) View(graphPDF, figure=fig, axes=[pdf_axis], add_legend=True) View(graphCDF, figure=fig, axes=[cdf_axis], add_legend=True)
formula_fake_var = 'x1' formula_y0 = 'cos(0.5*x1) + sin(x2)' formula_y1 = 'cos(0.5*x1) + sin(x2) + x3' symbolicModel = persalys.SymbolicPhysicalModel('symbolicModel', [x1, x2, x3], [fake_var, y0, fake_y0, y1], [ formula_fake_var, formula_y0, formula_y0, formula_y1]) myStudy.add(symbolicModel) # python model ## code = 'from math import cos, sin, sqrt\n\ndef _exec(x1, x2, x3):\n y0 = cos(0.5*x1) + sin(x2) + sqrt(x3)\n return y0\n' pythonModel = persalys.PythonPhysicalModel('pythonModel', [x1, x2, x3], [y0], code) myStudy.add(pythonModel) filename = 'data.csv' cDist = ot.ComposedDistribution([ot.Normal(), ot.Gumbel(), ot.Normal(), ot.Uniform()], ot.ComposedCopula([ot.IndependentCopula(2), ot.GumbelCopula()])) sample = cDist.getSample(200) sample.exportToCSVFile(filename, ' ') # Designs of Experiment ## # fixed design ## ot.RandomGenerator.SetSeed(0) fixedDesign = persalys.FixedDesignOfExperiment('fixedDesign', symbolicModel) inputSample = ot.LHSExperiment(ot.ComposedDistribution([ot.Uniform(0., 10.), ot.Uniform(0., 10.)]), 10).generate() inputSample.stack(ot.Sample(10, [0.5])) fixedDesign.setOriginalInputSample(inputSample) fixedDesign.run() myStudy.add(fixedDesign) # grid ##
import openturns as ot from matplotlib import pyplot as plt from openturns.viewer import View if ot.GumbelCopula().__class__.__name__ == 'EmpiricalBernsteinCopula': sample = ot.Dirichlet([1.0, 2.0, 3.0]).getSample(100) copula = ot.EmpiricalBernsteinCopula(sample, 4) elif ot.GumbelCopula().__class__.__name__ == 'ExtremeValueCopula': copula = ot.ExtremeValueCopula(ot.SymbolicFunction("t", "t^3/2-t/2+1")) elif ot.GumbelCopula( ).__class__.__name__ == 'MaximumEntropyOrderStatisticsCopula': marginals = [ot.Beta(1.5, 3.2, 0.0, 1.0), ot.Beta(2.0, 4.3, 0.5, 1.2)] copula = ot.MaximumEntropyOrderStatisticsCopula(marginals) elif ot.GumbelCopula().__class__.__name__ == 'NormalCopula': R = ot.CorrelationMatrix(2) R[1, 0] = 0.8 copula = ot.NormalCopula(R) elif ot.GumbelCopula().__class__.__name__ == 'SklarCopula': student = ot.Student(3.0, [1.0] * 2, [3.0] * 2, ot.CorrelationMatrix(2)) copula = ot.SklarCopula(student) else: copula = ot.GumbelCopula() if copula.getDimension() == 1: copula = ot.GumbelCopula(2) copula.setDescription(['$u_1$', '$u_2$']) pdf_graph = copula.drawPDF() cdf_graph = copula.drawCDF() fig = plt.figure(figsize=(10, 4)) pdf_axis = fig.add_subplot(121) cdf_axis = fig.add_subplot(122) View(pdf_graph, figure=fig,
""" Create the ordinal sum of copulas ================================= """ # %% # In this example we are going to create an ordinal sum of copulas. # %% import openturns as ot import openturns.viewer as viewer from matplotlib import pylab as plt ot.Log.Show(ot.Log.NONE) # %% # Create a collection of copulas collection = [ot.GumbelCopula(2), ot.NormalCopula(2)] # %% # Merge the copulas bounds = [0.3] copula = ot.OrdinalSumCopula(collection, bounds) print(copula) # %% # Draw PDF graph = copula.drawPDF([512]*2) graph.setXTitle('x') graph.setYTitle('y') graph.setLegendPosition('') view = viewer.View(graph) plt.show()
ot.Normal(-1.0, 1.0), ot.Uniform(5.0, 6.0) ] weights = [0.4, 1.0, 0.2] # %% # create the mixture distribution = ot.Mixture(distributions, weights) print(distribution) # %% # draw PDF graph = distribution.drawPDF() view = viewer.View(graph) # %% # define a list of copulas and the associated weights copulas = [ot.GumbelCopula(4.5), ot.ClaytonCopula(2.3)] weights = [0.2, 0.8] # %% # create a mixture of copulas distribution = ot.Mixture(copulas, weights) print(distribution) # %% # draw PDF graph = distribution.drawPDF() view = viewer.View(graph) plt.show()
formula_fake_var = 'x1+' formula_y0 = 'cos(0.5*x1) + sin(x2)' formula_y1 = 'cos(0.5*x1) + sin(x2) + x3' model1 = persalys.SymbolicPhysicalModel( 'model1', [x1, x2, x3], [fake_var, y0, fake_y0, y1], [formula_fake_var, formula_y0, formula_y0, formula_y1]) myStudy.add(model1) # model 3 ## filename = 'data.csv' cDist = ot.ComposedDistribution( [ot.Normal(), ot.Gumbel(), ot.Normal(), ot.Uniform()], ot.ComposedCopula([ot.IndependentCopula(2), ot.GumbelCopula()])) sample = cDist.getSample(20) sample.exportToCSVFile(filename, ' ') model3 = persalys.DataModel('model3', 'data.csv', [0, 2, 3], [1], ['x_0', 'x_2', 'x_3'], ['x_1']) myStudy.add(model3) # Design of Experiment ## probaDesign = persalys.ProbabilisticDesignOfExperiment('probaDesign', model1, 20, "MONTE_CARLO") probaDesign.run() myStudy.add(probaDesign) # 1- meta model1 ##