How to Use the Cauchy Distribution in Python

fimg-cauchy

The Cauchy distribution, also known as the Lorentz distribution, is a continuous probability distribution that differs from many familiar distributions due to its heavy tails and undefined mean and variance.

Unlike the normal distribution, where values tend to cluster around a central mean, the Cauchy distribution has no well-defined mean, variance, or higher moments. It’s often used to model processes with extreme outcomes or outliers—making it useful in physics, finance, and other fields where extreme values are common.

In this tutorial, we’ll learn more about the Cauchy distribution, visualize its probability density function, and learn how to use it in Python (yes, using the SciPy stats module).

▶️ Link to the Google Colab notebook

Understanding the Cauchy Distribution

The Cauchy distribution is characterized by the following two parameters:

  • x0: the location parameter (mode and median)
  • γ: the scale parameter, which controls the width of the peak.

The PDF of the Cauchy distribution is as follows:

cauchy-dist

Key characteristics of the Cauchy distribution:

  • Heavy tails: The probability of extreme values is higher than in a normal distribution, making it heavy-tailed.
  • Undefined mean and variance: The mean, variance, and higher moments are undefined because the tails do not decay fast enough for these quantities to converge.
  • Symmetry: The Cauchy distribution is symmetric around x0.

Working with the Cauchy Distribution in Python

You can use cauchy from the Scipy stats module to work with the Cauchy distribution. Let’s go over the steps.

Importing Required Libraries

Let’s start by importing the necessary libraries:

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import cauchy

Visualizing the Probability Density Function (PDF)

The Cauchy distribution’s PDF demonstrates its characteristic heavy tails. Let’s visualize it with different location and scale parameters:

# Set location and scale parameters
x0, gamma = 0, 1

# Define the x-axis range for the plot
x = np.linspace(-10, 10, 1000)

# Calculate the PDF
pdf_values = cauchy.pdf(x, loc=x0, scale=gamma)

# Plot the PDF
plt.plot(x, pdf_values, color='blue')
plt.title('Cauchy Distribution PDF (x0=0, γ=1)')
plt.xlabel('x')
plt.ylabel('Probability Density')
plt.show()

This plot shows the PDFs of the Cauchy distribution for different x0 and γ.
cauchy-distplot

Generating Cauchy-Distributed Random Numbers

Generating random samples from a Cauchy distribution is useful for simulating events with potential extreme outliers. Here’s how to generate and visualize these samples in Python:

# Set location and scale parameters
x0, gamma = 0, 1

# Get random samples from the Cauchy distribution
samples = cauchy.rvs(loc=x0, scale=gamma, size=1000)

Example: Robust Modeling with Cauchy Noise

You’ll often use the Cauchy distribution in robust statistical modeling where you expect extreme outliers. Here’s a practical example: adding Cauchy-distributed noise to a dataset:

# Generate a simple linear dataset
x_values = np.linspace(-5, 5, 100)
y_values = 2 * x_values + 1  # True linear relationship

# Add Cauchy noise to simulate outliers
cauchy_noise = cauchy.rvs(loc=0, scale=1, size=100)
y_noisy = y_values + cauchy_noise

# Plot the data
plt.scatter(x_values, y_noisy, color='red', label='Data with Cauchy Noise')
plt.plot(x_values, y_values, color='blue', label='True Line')
plt.title('Linear Data with Cauchy-distributed Noise')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()

Here’s the sample output plot.
cauchy-noise

Conclusion

We learned more about the Cauchy distribution, visualized its PDF, generated Cauchy-distributed random samples, and applied it to simulate noise with extreme values in a dataset. 

As discussed, the Cauchy distribution is useful in situations where outliers are expected or when we need to model processes with extreme variability.

Cover image: Created by Author | Segmind SSD-1B Model

Leave a Reply

Your email address will not be published. Required fields are marked *