SkillAgentSearch skills...

Pycop

Python library for multivariate dependence modeling with Copulas

Install / Use

/learn @maximenc/Pycop

README

<p align="center"> <img src="https://github.com/maximenc/pycop/raw/master/docs/images/logo_pycop.svg" width="40%" height="40%" /> </p>

PyPi version Downloads License DOI

How to cite

If you use pycop in a scientific publication

@article{nicolas2022pycop,
  title={pycop: a Python package for dependence modeling with copulas},
  author={Nicolas, Maxime LD},
  journal={Zenodo Software Package},
  volume={70},
  pages={7030034},
  year={2022}
}

Overview

Pycop 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 functions
  • Mixture model of multiple copula functions (up to 3 copula functions)
  • Multivariate random sample generation
  • Empirical copula method
  • Parametric and Non-parametric Tail Dependence Coefficient (TDC)

Available copula function

<p align="center">

| Copula | Bivariate <br /> Graph & Estimation | Multivariate <br /> Simulation | |--- | :-: | :-: | | Mixture | ✓ | ✓ | | Gaussian | ✓ | ✓ | | Student | ✓ | ✓ | | Clayton | ✓ | ✓ | | Rotated Clayton | ✓ | ✓ | | Gumbel | ✓ | ✓ | | Rotated Gumbel | ✓ | ✓ | | Frank | ✓ | ✓ | | Joe | ✓ | ✓ | | Rotated Joe | ✓ | ✓ | | Galambos | ✓ | ✗ | | Rotated Galambos | ✓ | ✗ | | BB1 | ✓ | ✗ | | BB2 | ✓ | ✗ | | FGM | ✓ | ✗ | | Plackett | ✓ | ✗ | | AMH | ✗ | ✓ |

</p>

Usage

Install pycop using pip

pip install pycop

Examples

Open In Colab Estimations on msci returns

Open In Colab Graphical Representations

Open In Colab Simulations

Table of Contents

Graphical Representation

We first create a copula object by specifying the copula familly

from pycop import archimedean
cop = archimedean(family="clayton")

Plot the cdf and pdf of the copula.

3d plot

cop = archimedean(family="gumbel")

cop.plot_cdf([2], plot_type="3d", Nsplit=100 )
cop.plot_pdf([2], plot_type="3d", Nsplit=100, cmap="cividis" )
<p align="center"> <img src="https://github.com/maximenc/pycop/raw/master/docs/images/plot/gumbel_3d_cdf.svg" width="45%" /> <img src="https://github.com/maximenc/pycop/raw/master/docs/images/plot/gumbel_3d_pdf.svg" width="45%" /> </p>

Contour plot

plot the contour

cop = archimedean(family="plackett")

cop.plot_cdf([2], plot_type="contour", Nsplit=100 )
cop.plot_pdf([2], plot_type="contour", Nsplit=100, )
<p align="center"> <img src="https://github.com/maximenc/pycop/raw/master/docs/images/plot/plackett_contour_cdf.svg" width="45%" /> <img src="https://github.com/maximenc/pycop/raw/master/docs/images/plot/plackett_contour_pdf.svg" width="45%" /> </p>

It is also possible to add specific marginals

cop = archimedean.archimedean(family="clayton")

from scipy.stats import norm


marginals = [
    {
        "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,  levels=lvls)
<p align="center"> <img src="https://github.com/maximenc/pycop/raw/master/docs/images/plot/clayton_3d_mpdf.svg" width="45%" /> <img src="https://github.com/maximenc/pycop/raw/master/docs/images/plot/clayton_contour_mpdf.svg" width="45%" /> </p>

Mixture plot

mixture of 2 copulas

from pycop import mixture

cop = mixture(["clayton", "gumbel"])
cop.plot_pdf([0.2, 2, 2],  plot_type="contour", Nsplit=40,  levels=[0.1,0.4,0.8,1.3,1.6] )
# plot with defined marginals
cop.plot_mpdf([0.2, 2, 2], marginals, plot_type="contour", Nsplit=50)
<p align="center"> <img src="https://github.com/maximenc/pycop/raw/master/docs/images/plot/2c_mixture_contour_pdf.svg" width="45%" /> <img src="https://github.com/maximenc/pycop/raw/master/docs/images/plot/2c_mixture_contour_mpdf.svg" width="45%" /> </p>

cop = mixture(["clayton", "gaussian", "gumbel"])
cop.plot_pdf([1/3, 1/3, 1/3, 2, 0.5, 4],  plot_type="contour", Nsplit=40,  levels=[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)
<p align="center"> <img src="https://github.com/maximenc/pycop/raw/master/docs/images/plot/3c_mixture_contour_pdf.svg" width="45%" /> <img src="https://github.com/maximenc/pycop/raw/master/docs/images/plot/3c_mixture_contour_mpdf.svg" width="45%" /> </p>

Simulation

Gaussian

from scipy.stats import norm
from pycop import simulation

n = 2 # dimension
m = 1000 # sample size

corrMatrix = 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)

Student

u1, u2 = simulation.simu_tstudent(n, m, corrMatrix, nu=1)

<p align="center"> <img src="https://github.com/maximenc/pycop/raw/master/docs/images/simu/gaussian_simu.svg" width="45%" /> <img src="https://github.com/maximenc/pycop/raw/master/docs/images/simu/student_simu.svg" width="45%" /> </p>

Archimedean

List of archimedean cop available

u1, u2 = simulation.simu_archimedean("gumbel", n, m, theta=2)
u1, u2 = 1 - u1, 1 - u2

Rotated

u1, u2 = 1 - u1, 1 - u2
<p align="center"> <img src="https://github.com/maximenc/pycop/raw/master/docs/images/simu/gumbel_simu.svg" width="45%" /> <img src="https://github.com/maximenc/pycop/raw/master/docs/images/simu/rgumbel_simu.svg" width="45%" /> </p>

High dimension


n = 3       # Dimension
m = 1000    # Sample size

corrMatrix = np.array([[1, 0.9, 0], [0.9, 1, 0], [0, 0, 1]])
u = simulation.simu_gaussian(n, m, corrMatrix)
u = norm.ppf(u)
<p align="center"> <img src="https://github.com/maximenc/pycop/raw/master/docs/images/simu/gaussian_simu_n3.svg" width="45%" /> </p>
u = simulation.simu_archimedean("clayton", n, m, theta=2)
u = norm.ppf(u)
<p align="center"> <img src="https://github.com/maximenc/pycop/raw/master/docs/images/simu/clayton_simu_n3.svg" width="45%" /> </p>

Mixture simulation

Simulation from a mixture of 2 copulas

n = 3
m = 2000

combination = [
    {"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)
<p align="center"> <img src="https://github.com/maximenc/pycop/raw/master/docs/images/simu/2c_mixture_simu.svg" width="45%" /> </p>

Simulation from a mixture of 3 copulas

corrMatrix = 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)
<p align="center"> <img src="https://github.com/maximenc/pycop/raw/master/docs/images/simu/3c_mixture_simu.svg" width="45%" /> </p>

Estimation

Estimation available : CMLE

Canonical Maximum Likelihood Estimation (CMLE)

Import a sample with pandas

import pandas as pd
import numpy as np

df = pd.read_csv("data/msci.csv")
df.index = pd.to_datetime(df["Date"], format="%m/%d/%Y

Related Skills

View on GitHub
GitHub Stars117
CategoryDevelopment
Updated1mo ago
Forks23

Languages

Python

Security Score

100/100

Audited on Feb 20, 2026

No findings