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)
""" 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)
import pyAgrum.lib.notebook as gnb gnb.showInference(model, evs=evs, size=size) except ImportError: pass # **Probabilistic model** # Marginal distributions Torque = ot.LogNormal(0.0, 0.25) Angle = ot.TruncatedNormal(0.0, 2.0, -8.0, 8.0) Joint = ot.Uniform(1.8, 2.2) # Dependence rho = 0.5 TorqueAngleCopula = ot.NormalCopula(ot.CorrelationMatrix(2, [1.0, rho, rho, 1.0])) copula = ot.ComposedCopula([TorqueAngleCopula, ot.IndependentCopula(1)]) # Joint distribution if needed TorqueAngle = ot.ComposedDistribution([Torque, Angle], TorqueAngleCopula) fullDistribution = ot.ComposedDistribution([Torque, Angle, Joint], copula) # Leakage angle (rd) angleMax = 5.0 # Leakage joint (mm) jointMin = 2.0 jointSpread = 0.1 # Vibration torque (kN.m) torqueSpread = 2.0
# - find the dependent components # - estimate a copula on each dependent bloc # - assemble the estimated copulas # # %% from __future__ import print_function import openturns as ot import math as m ot.Log.Show(ot.Log.NONE) # %% # generate some multivariate data to estimate, with correlation cop1 = ot.AliMikhailHaqCopula(0.6) cop2 = ot.ClaytonCopula(2.5) copula = ot.ComposedCopula([cop1, cop2]) marginals = [ ot.Uniform(5.0, 6.0), ot.Arcsine(), ot.Normal(-40.0, 3.0), ot.Triangular(100.0, 150.0, 300.0) ] distribution = ot.ComposedDistribution(marginals, copula) sample = distribution.getSample(10000).getMarginal([0, 2, 3, 1]) # %% # estimate marginals dimension = sample.getDimension() marginalFactories = [] for factory in ot.DistributionFactory.GetContinuousUniVariateFactories(): if str(factory).startswith('Histogram'):
# 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) # with marginaltransformation copula = ot.ComposedCopula([ot.IndependentCopula(2), ot.IndependentCopula(2)]) distribution = ot.ComposedDistribution([ot.Normal()]*4, copula) print(ot.DistributionTransformation(ot.IndependentCopula(4), distribution.getMarginal([3,1,2,0])))
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 ##
#! /usr/bin/env python from __future__ import print_function import openturns as ot ot.TESTPREAMBLE() model = ot.SymbolicFunction(['x0', 'x1', 'x2', 'x3'], ['-(6+x0^2-x1+x2+3*x3)']) dim = model.getInputDimension() marginals = [ot.Normal(5.0, 3.0) for i in range(dim)] distribution = ot.ComposedDistribution( marginals, ot.ComposedCopula([ot.ClaytonCopula(), ot.NormalCopula()])) #distribution = ot.Normal([5]*dim, [3]*dim, ot.CorrelationMatrix(dim)) #distribution = ot.ComposedDistribution(marginals, ot.IndependentCopula(dim)) distribution.setDescription(['marginal' + str(i) for i in range(dim)]) vect = ot.RandomVector(distribution) output = ot.CompositeRandomVector(model, vect) event = ot.Event(output, ot.Greater(), 0.0) solver = ot.Cobyla() solver.setMaximumEvaluationNumber(200) solver.setMaximumAbsoluteError(1.0e-10) solver.setMaximumRelativeError(1.0e-10) solver.setMaximumResidualError(1.0e-10) solver.setMaximumConstraintError(1.0e-10) algo = ot.FORM(solver, event, distribution.getMean()) algo.run() result = algo.getResult() hasoferReliabilityIndexSensitivity = result.getHasoferReliabilityIndexSensitivity( ) print(hasoferReliabilityIndexSensitivity)
import openturns as ot from matplotlib import pyplot as plt from openturns.viewer import View myColl = [ot.ClaytonCopula(0.3), ot.NormalCopula(3)] myMergedCop = ot.ComposedCopula(myColl) myMergedCop.setDescription(['$u_1$', '$u_2$', '$u_3$', '$u_4$', '$u_5$']) graphPDF = myMergedCop.drawMarginal2DPDF(0, 1, [0.0] * 2, [1.0] * 2, [100] * 2) graphPDF.setXTitle('$u_1$') graphPDF.setYTitle('$u_2$') graphCDF = myMergedCop.drawMarginal2DCDF(0, 1, [0.0] * 2, [1.0] * 2, [100] * 2) graphCDF.setXTitle('$u_1$') graphCDF.setYTitle('$u_2$') fig = plt.figure(figsize=(10, 4)) 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) fig.suptitle("ComposedCopula(Clayton(0.3), NormalCopula(3)): pdf and cdf")