def generateDataForSpecificInstance(size): R = ot.CorrelationMatrix(3) R[0, 1] = 0.5 R[0, 2] = 0.45 collection = [ot.FrankCopula(3.0), ot.NormalCopula(R), ot.ClaytonCopula(2.0)] copula = ot.ComposedCopula(collection) return copula.getSample(size)
import openturns as ot from matplotlib import pyplot as plt from openturns.viewer import View if ot.FrankCopula().__class__.__name__ == 'EmpiricalBernsteinCopula': sample = ot.Dirichlet([1.0, 2.0, 3.0]).getSample(100) copula = ot.EmpiricalBernsteinCopula(sample, 4) elif ot.FrankCopula().__class__.__name__ == 'ExtremeValueCopula': copula = ot.ExtremeValueCopula(ot.SymbolicFunction("t", "t^3/2-t/2+1")) elif ot.FrankCopula( ).__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.FrankCopula().__class__.__name__ == 'NormalCopula': R = ot.CorrelationMatrix(2) R[1, 0] = 0.8 copula = ot.NormalCopula(R) elif ot.FrankCopula().__class__.__name__ == 'SklarCopula': student = ot.Student(3.0, [1.0] * 2, [3.0] * 2, ot.CorrelationMatrix(2)) copula = ot.SklarCopula(student) else: copula = ot.FrankCopula() if copula.getDimension() == 1: copula = ot.FrankCopula(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,
""" Assemble copulas ================ """ # %% # In this example we are going to merge a collection of independent copulas into one. # # %% from __future__ import print_function 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 R = ot.CorrelationMatrix(3) R[0, 1] = 0.5 R[0, 2] = 0.25 collection = [ot.FrankCopula(3.0), ot.NormalCopula(R), ot.ClaytonCopula(2.0)] # %% # merge the copulas copula = ot.ComposedCopula(collection) print(copula)
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)
import openturns as ot from openturns.viewer import View ot.RandomGenerator.SetSeed(0) copula = ot.FrankCopula(1.5) sample = copula.getSample(100) graph = ot.VisualTest.DrawKendallPlot(sample, copula) View(graph)
# #Lakach # muLog = 7.43459;sigmaLog = 0.555439;gamma = 4977.04 # marginal1 =ot.LogNormal(muLog, sigmaLog, gamma) # mu = 0.165352;beta = 0.0193547; # marginal2 =ot.Logistic(mu, beta) # theta = -4.2364 # copula = ot.FrankCopula(theta) #BF3 beta1 = 2458.48 gamma1 = 28953.5 marginal1 = ot.Gumbel(beta1, gamma1) beta2 = 0.0489963 gamma2 = 0.156505 marginal2 = ot.Gumbel(beta2, gamma2) theta = -5.21511 copula = ot.FrankCopula(theta) bivariate_distribution_data = ot.ComposedDistribution([marginal1, marginal2], copula) marginal_data = [bivariate_distribution_data.getMarginal(i) for i in [0, 1]] copula_data = bivariate_distribution_data.getCopula() #Weights# w1 = [1, 1] w = [w0[0] * w1[0], w0[1] * w1[1]] ##Objective function## F = fo.funcobj_cond(w, Data_Grid_nc, Data_Conditioning, svt, lag_tolerance,
expression += "+" 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)
import openturns as ot from matplotlib import pyplot as plt from openturns.viewer import View if (ot.FrankCopula().__class__.__name__ == 'SklarCopula'): myStudent = ot.Student(3.0, [1.0] * 2, [3.0] * 2, ot.CorrelationMatrix(2)) copula = ot.SklarCopula(myStudent) else: copula = ot.FrankCopula() if copula.getDimension() == 1: copula = ot.FrankCopula(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, square_axes=True) View(cdf_graph, figure=fig, axes=[cdf_axis], add_legend=False, square_axes=True)
print("Inverse survival (ref)=", ref.computeInverseSurvivalFunction(0.95)) print("Survival(inverse survival)=%.5f" % distribution.computeSurvivalFunction(InverseSurvival)) # Get 50% quantile quantile = distribution.computeQuantile(0.5) print("Quantile =", quantile) print("Quantile (ref)=", ref.computeQuantile(0.5)) print("CDF(quantile) =%.5f" % distribution.computeCDF(quantile)) # Instantiate one distribution object R = ot.CorrelationMatrix(3) R[0, 1] = 0.5 R[0, 2] = 0.25 collection = [ot.ComposedDistribution([ot.Normal()]*2, ot.AliMikhailHaqCopula(0.5)), ot.Normal([1.0]*3, [2.0]*3, R), ot.ComposedDistribution([ot.Exponential()]*2, ot.FrankCopula(0.5))] distribution = ot.BlockIndependentDistribution(collection) print("Distribution ", distribution) # Is this distribution elliptical ? print("Elliptical distribution= ", distribution.isElliptical()) # Is this distribution continuous ? print("Continuous = ", distribution.isContinuous()) # Has this distribution an elliptical copula ? print("Elliptical = ", distribution.hasEllipticalCopula()) # Has this distribution an independent copula ? print("Independent = ", distribution.hasIndependentCopula())