Package: pycop
OverviewPycop is the most complete tool for modeling multivariate dependence with Python. The package provides methods such as estimation, random sample generation, and graphical representation for commonly used copula functions. The package supports the use of mixture models defined as convex combinations of copulas. Other methods based on the empirical copula such as the non-parametric Tail Dependence Coefficient are given.Some of the features covered:Elliptical copulas (Gaussian & Student) and common Archimedean Copulas functionsMixture model of multiple copula functions (up to 3 copula functions)Multivariate random sample generationEmpirical copula methodParametric and Non-parametric Tail Dependence Coefficient (TDC)Available copula functionCopulaBivariateGraph &  EstimationMultivariateSimulationMixture✓✓Gaussian✓✓Student✓✓Clayton✓✓Rotated Clayton✓✓Gumbel✓✓Rotated Gumbel✓✓Frank✓✓Joe✓✓Rotated Joe✓✓Galambos✓✗Rotated Galambos✓✗BB1✓✗BB2✓✗FGM✓✗Plackett✓✗AMH✗✓UsageInstall pycop using pippip install pycopExamplesEstimations on msci returnsGraphical RepresentationsSimulationsTable of ContentsGraphical Representation3d plotContour plotMixture plotSimulationGaussianStudentArchimedeanHigh dimensionMixture simulationEstimationCanonical Maximum Likelihood EstimationTail Dependence CoefficientTheoretical TDCNon-parametric TDCOptimal Empirical TDCGraphical RepresentationWe first create a copula object by specifying the copula famillyfrompycopimportarchimedeancop=archimedean(family="clayton")Plot the cdf and pdf of the copula.3d plotcop=archimedean(family="gumbel")cop.plot_cdf([2],plot_type="3d",Nsplit=100)cop.plot_pdf([2],plot_type="3d",Nsplit=100,cmap="cividis")Contour plotplot the contourcop=archimedean(family="plackett")cop.plot_cdf([2],plot_type="contour",Nsplit=100)cop.plot_pdf([2],plot_type="contour",Nsplit=100,)It is also possible to add specific marginalscop=archimedean.archimedean(family="clayton")fromscipy.statsimportnormmarginals=[{"distribution":norm,"loc":0,"scale":0.8,},{"distribution":norm,"loc":0,"scale":0.6,}]cop.plot_mpdf([2],marginals,plot_type="3d",Nsplit=100,rstride=1,cstride=1,antialiased=True,cmap="cividis",edgecolor='black',linewidth=0.1,zorder=1,alpha=1)lvls=[0.02,0.05,0.1,0.2,0.3]cop.plot_mpdf([2],marginals,plot_type="contour",Nsplit=100,lvls=lvls)Mixture plotmixture of 2 copulasfrompycop.mixtureimportmixturecop=mixture(["clayton","gumbel"])cop.plot_pdf([0.2,2,2],plot_type="contour",Nsplit=40,lvls=[0.1,0.4,0.8,1.3,1.6])# plot with defined marginalscop.plot_mpdf([0.2,2,2],marginals,plot_type="contour",Nsplit=50)cop=mixture(["clayton","gaussian","gumbel"])cop.plot_pdf([1/3,1/3,1/3,2,0.5,4],plot_type="contour",Nsplit=40,lvls=[0.1,0.4,0.8,1.3,1.6])cop.plot_mpdf([1/3,1/3,1/3,2,0.5,2],marginals,plot_type="contour",Nsplit=50)SimulationGaussianfromscipy.statsimportnormfrompycopimportsimulationn=2# dimensionm=1000# sample sizecorrMatrix=np.array([[1,0.8],[0.8,1]])u1,u2=simulation.simu_gaussian(n,m,corrMatrix)Adding gaussian marginals, (using distribution.ppf from scipy.statsto transform uniform margin to the desired distribution)u1=norm.ppf(u1)u2=norm.ppf(u2)Studentu1,u2=simulation.simu_tstudent(n,m,corrMatrix,nu=1)ArchimedeanList of archimedean cop availableu1,u2=simulation.simu_archimedean("gumbel",n,m,theta=2)u1,u2=1-u1,1-u2Rotatedu1,u2=1-u1,1-u2High dimensionn=3# Dimensionm=1000# Sample sizecorrMatrix=np.array([[1,0.9,0],[0.9,1,0],[0,0,1]])u=simulation.simu_gaussian(n,m,corrMatrix)u=norm.ppf(u)u=simulation.simu_archimedean("clayton",n,m,theta=2)u=norm.ppf(u)Mixture simulationSimulation from a mixture of 2 copulasn=3m=2000combination=[{"type":"clayton","weight":1/3,"theta":2},{"type":"gumbel","weight":1/3,"theta":3}]u=simulation.simu_mixture(n,m,combination)u=norm.ppf(u)Simulation from a mixture of 3 copulascorrMatrix=np.array([[1,0.8,0],[0.8,1,0],[0,0,1]])combination=[{"type":"clayton","weight":1/3,"theta":2},{"type":"student","weight":1/3,"corrMatrix":corrMatrix,"nu":2},{"type":"gumbel","weight":1/3,"theta":3}]u=simulation.simu_mixture(n,m,combination)u=norm.ppf(u)EstimationEstimation available :
CMLECanonical Maximum Likelihood Estimation (CMLE)Import a sample with pandasimportpandasaspdimportnumpyasnpdf=pd.read_csv("data/msci.csv")df.index=pd.to_datetime(df["Date"],format="%m/%d/%Y")df=df.drop(["Date"],axis=1)forcolindf.columns.values:df[col]=np.log(df[col])-np.log(df[col].shift(1))df=df.dropna()frompycopimportestimation,archimedeancop=archimedean.archimedean("clayton")param,cmle=estimation.fit_cmle(cop,df[["US","UK"]])clayton  estim:  0.8025977727691012Tail Dependence coefficientTheoretical TDCcop.LTDC(theta=param)cop.UTDC(theta=param)Non-parametric TDCCreate an empirical copula objectfrompycopimportempiricalcop=empirical(df[["US","UK"]])Compute the non-parametric Upper TDC (UTDC) or the Lower TDC (LTDC) for a given threshold:cop.LTDC(0.01)# i/n = 1%cop.UTDC(0.99)# i/n = 99%Optimal Empirical TDCReturns the optimal non-parametric TDC based on the heuristic plateau-finding algorithm from Frahm et al (2005) "Estimating the tail-dependence coefficient: properties and pitfalls"cop.optimal_tdc("upper")cop.optimal_tdc("lower")