Skip to main content

A python library for building different types of copulas and using them for sampling.

Project description

“Copulas” An open source project from Data to AI Lab at MIT.

Copulas

Overview

A python library for building different types of copulas and using them for sampling.

Supported Copulas

Bivariate

  • Clayton
  • Frank
  • Gumbel

Accesible from copulas.bivariate.copulas.Copula

Multivariate

Accesible from copulas.multivariate.models.CopulaModel

Installation

Install with pip

The easiest way to install Copulas is using pip

pip install copulas

Install from sources

You can also clone the repository and install it from sources

git clone git@github.com:DAI-Lab/Copulas.git
cd Copulas
python setup.py install

Data Requirements

This package works under the assumption that the data is perfectly clean, that means that:

  • There are no missing values.
  • All values are numerical

Usage

In this library you can model univariate distributions and create copulas from a numeric dataset. For this example, we will use the iris dataset in the data folder.

Creating Univariate Distribution

First we will retrieve the data from the data folder and create a univariate distribution. For this example, we will create a normal distribution. First type the following commands on a python terminal.

>>> from copulas.univariate.gaussian import GaussianUnivariate
>>> import pandas as pd
>>> data = pd.read_csv('data/iris.data.csv')
>>> data.head()
   feature_01  feature_02  feature_03  feature_04
0         5.1         3.5         1.4         0.2
1         4.9         3.0         1.4         0.2
2         4.7         3.2         1.3         0.2
3         4.6         3.1         1.5         0.2
4         5.0         3.6         1.4         0.2

Once we have the data, we can pass it into the GaussianUnivariate class.

>>> feature1 = data['feature_01']
>>> gu = GaussianUnivariate()
>>> gu.fit(feature1)
>>> print(gu)
Distribution Type: Gaussian
Variable name: feature_01
Mean: 5.843333333333334
Standard deviation: 0.8253012917851409

Once you fit the distribution, you can get the pdf or cdf of data points and you can sample from the distribution.

>>> gu.probability_density(5)
0.2867858505472377
>>> gu.cumulative_distribution(5)
0.15342617720079227
>>> gu.sample(1)
array([6.14745446])

Creating a Gaussian Copula

When you have a numeric data table, you can also create a copula and use it to sample from the multivariate distribution. In this example, we will use a Gaussian Copula.

>>> from copulas.multivariate.gaussian import GaussianMultivariate
>>> gc = GaussianMultivariate()
>>> gc.fit(data)
>>> print(gc)
feature_01
===============
Distribution Type: Gaussian
Variable name: feature_01
Mean: 5.843333333333334
Standard deviation: 0.8253012917851409

feature_02
===============
Distribution Type: Gaussian
Variable name: feature_02
Mean: 3.0540000000000003
Standard deviation: 0.4321465800705435

feature_03
===============
Distribution Type: Gaussian
Variable name: feature_03
Mean: 3.758666666666666
Standard deviation: 1.7585291834055212

feature_04
===============
Distribution Type: Gaussian
Variable name: feature_04
Mean: 1.1986666666666668
Standard deviation: 0.7606126185881716

Covariance matrix:
[[ 1.26935536  0.64987728  0.94166734 ... -0.57458312 -0.14548004
  -0.43589371]
 [ 0.64987728  0.33302068  0.4849735  ... -0.29401609 -0.06772633
  -0.21867228]
 [ 0.94166734  0.4849735   0.72674568 ... -0.42778472 -0.04608618
  -0.27836438]
 ...
 [-0.57458312 -0.29401609 -0.42778472 ...  0.2708685   0.0786054
   0.19208669]
 [-0.14548004 -0.06772633 -0.04608618 ...  0.0786054   0.17668562
   0.14455133]
 [-0.43589371 -0.21867228 -0.27836438 ...  0.19208669  0.14455133
   0.22229033]]

Once you have fit the copula, you can sample from it.

gc.sample(5)
   feature_01  feature_02  feature_03  feature_04
0    5.529610    2.966947    3.162891    0.974260
1    5.708827    3.011078    3.407812    1.149803
2    4.623795    2.712284    1.283194    0.213796
3    5.952688    3.086259    4.088219    1.382523
4    5.360256    2.920929    2.844729    0.826919

History

0.2.0 - Unified API

  • New API for stats methods.
  • Standarize input and output to numpy.ndarray.
  • Increase unittest coverage to 90%.
  • Add methods to load/save copulas.
  • Improve Gaussian copula sampling accuracy.

0.1.1 - Minor Improvements

  • Different Copula types separated in subclasses
  • Extensive Unit Testing
  • More pythonic names in the public API.
  • Stop using third party elements that will be deprected soon.
  • Add methods to sample new data on bivariate copulas.
  • New KDE Univariate copula
  • Improved examples with additional demo data.

0.1.0 - First Release

  • First release on PyPI.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

copulas-0.2.0.tar.gz (38.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

copulas-0.2.0-py2.py3-none-any.whl (24.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file copulas-0.2.0.tar.gz.

File metadata

  • Download URL: copulas-0.2.0.tar.gz
  • Upload date:
  • Size: 38.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.5

File hashes

Hashes for copulas-0.2.0.tar.gz
Algorithm Hash digest
SHA256 4fca87d8a57265b19954412a0faa438ff83b9dad346f4ee495893ae33452ad3f
MD5 f7129c915aa26a1acf150862c2292307
BLAKE2b-256 9d291e530bec44c80ccd381f50ff449a7c59ac758814def49caae5be43f64e4c

See more details on using hashes here.

File details

Details for the file copulas-0.2.0-py2.py3-none-any.whl.

File metadata

  • Download URL: copulas-0.2.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 24.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.5

File hashes

Hashes for copulas-0.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 8f77e35d181c61d10e59aed750a9178eb6df575547079a2c2d0dc101c1ad7a80
MD5 00ba9db4fce0721f72f3f91d2c61f383
BLAKE2b-256 1f12cd798e51fc433a387e58cc51f3ab180833d3abb84760782b50a1ececf6e3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page