Introduction to Copulas

Probability Review

Let’s start by reviewing some basic probability concepts.

We’ll focus specifically on continuous random variables, which is what the Copulas library is primarily intended to support.

Probability Density Function

A probability density function \(f(x)\) captures the likelihood that a random sample from the distribution is equal to \(x\). For example, the probability density function for the standard normal distribution is given by

\begin{equation} f(x) = \frac{1}{2 \pi} e^{-x^2/2} \end{equation}

Note that the probability density function does not return a probability but rather a “relative likelihood” which can take on values in the interval \([0, \infty)\); however, the integral over the probability density function from \(-\infty\) to \(\infty\) must be equal to one.

Cumulative Distribution Function

In many cases, the probability density function can be hard to work with directly. Instead, we will use the cumulative distribution function \(F(x)\) which is defined as the integral of the probability density function

\begin{equation} F(x) = \int_{-\infty}^x f(x) \end{equation}

The below figure shows the probability density function \(f(x)\) and the cumulative distribution function \(F(x)\) for a normal standard distribution with mean \(0.0\) and variance \(1\).

[2]:
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import numpy as np
import scipy.stats as stats

def plot_cdf_pdf_plotly():
    # Generate 10000 evenly distributed values from -4 to 4
    x = np.linspace(-4.0, 4.0, 10000)

    # Compute their Probability Densities and Cumulative Distributions
    pdf = stats.norm.pdf(x)
    cdf = stats.norm.cdf(x)

    fig = make_subplots(rows=1, cols=2, subplot_titles=("PDF", "CDF"))

    fig.add_trace(
        go.Scatter(x=x, y=pdf),
        row=1, col=1
    )
    fig.update_xaxes(title_text="x", row=1, col=1)
    fig.update_yaxes(title_text="f(x)", row=1, col=1)

    fig.add_trace(
        go.Scatter(x=x, y=cdf),
        row=1, col=2
    )
    fig.update_xaxes(title_text="x", row=1, col=2)
    fig.update_yaxes(title_text="F(x)", row=1, col=2)

    # Update yaxis properties

    fig.update_layout(height=400, width=900, showlegend=False)
    fig.show()

plot_cdf_pdf_plotly()