Laplace Distribution in Python [with Examples]

Laplace

Laplace distribution is a probability distribution used to model data with heavy tails, where extreme values are more likely than in a normal distribution. It has wider tails than the normal distribution, making it suitable for modeling financial asset returns, rainfall patterns, and other phenomena with occasional extreme values. The key parameters are the location (mean) and scale (related to spread or volatility).

In this article, we’ll look at what laplace distribution is and how it works. Let’s get started.

Recommended: Random Search in Machine Learning: Hyperparameter Tuning Technique

Recommended: Detecting Anomalies with Pycaret: A Step-by-Step Guide

What is Laplace distribution?

Laplace distribution is used in multiple fields, from Physics to Engineering. It is very similar to a normal distribution, but it has much wider tails, so it can model instances where the deviations are very large. Let us first look at the formula for the Laplace distribution curve.

Laplace Distribution Curve
Laplace Distribution Curve

We can see that it has a greater peak than normal distribution and fatter details. Let us look at the Python code for the same and plot it.

from scipy import stats

# Define location (mean) and scale parameters
loc = 2.5
scale = 1.0

# Create a Laplace distribution object
laplace_dist = stats.laplace(loc, scale)

# Calculate the PDF for a specific value (x = 3.0)
x = 3.0
pdf_value = laplace_dist.pdf(x)

print(f"PDF for x = {x}: {pdf_value}")

# Alternatively, calculate the PDF for an array of values
x_values = np.linspace(1.0, 4.0, 10)
pdf_values = laplace_dist.pdf(x_values)

# Plot the PDF (using matplotlib)
import matplotlib.pyplot as plt

plt.plot(x_values, pdf_values)
plt.xlabel("x")
plt.ylabel("PDF(x)")
plt.title("Laplace Distribution PDF")
plt.show()

Let us look at the output.

Laplace Distribution Output
Laplace Distribution Output

Examples of Laplace distribution

The Laplace distribution is utilized in various fields such as speech recognition, which models priors on DFT coefficients, and JPEG image compression for AC coefficients generated by a DCT. It also finds applications in finance, ocean engineering, and hydrology, primarily due to its ability to model deviations from a central value. Let’s look at some examples of the Laplace distribution in finance and hydrology

Laplace Distribution in Finance: Stock Price Simulation

In the field of finance, Laplace distribution is used heavily. We can approximate the stock prices of an asset daily. In the code below, we have calculated stock prices for 250 days. We have also defined the volatility of the asset as well. Let us look at the code for the same.

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

# Simulate daily stock returns
num_days = 250
drift = 0.001  # Daily average return
volatility = 0.01  # Daily volatility
scale = volatility / np.sqrt(2)  # Scale parameter for Laplace distribution

# Generate random Laplace distributed returns
returns = drift + scale * np.random.laplace(loc=0, scale=1, size=num_days)

# Calculate daily stock prices (assuming starting price of $100)
prices = np.cumsum(returns) + 100

# Simulate a market crash by randomly dropping the price on a specific day
crash_day = np.random.randint(1, num_days)
crash_severity = -0.2  # Proportionate price drop during crash
prices[crash_day] *= (1 - crash_severity)

# Plot the stock prices
plt.plot(prices)
plt.xlabel("Day")
plt.ylabel("Stock Price")
plt.title("Stock Price Simulation with Laplace Distribution (Crash on Day {})".format(crash_day))
plt.grid(True)
plt.show()

# Analyze daily returns with Laplace distribution
laplace_dist = stats.laplace(loc=drift, scale=scale)

# Calculate theoretical percentiles for returns (e.g., 1st, 5th, 95th, 99th)
percentiles = np.array([1, 5, 95, 99])
theoretical_percentiles = laplace_dist.ppf(percentiles / 100)

# Calculate actual percentiles from simulated returns
actual_percentiles = np.percentile(returns, percentiles)

# Print the comparison of theoretical vs. actual percentiles
print("Theoretical Percentiles (Daily Returns):")
print(theoretical_percentiles)
print("\nActual Percentiles (Simulated Returns):")
print(actual_percentiles)

Let us look at the output for the same.

Stock Price Laplace Distribution
Stock Price Laplace Distribution

According to the above code, it will crash on the 147th day.

Laplace Distribution in Hydrology: Rainfall Modeling

Laplace distribution is also used to predict the rainfall as well. Let us see this with the help of a Python code.

import numpy as np
import matplotlib.pyplot as plt

# Define parameters for the Laplace distribution (hydrology scenario)
loc = 1.5  # Average daily rainfall (mm)
scale = 0.8  # Controls spread (larger = more frequent extreme events)

# Simulate 365 days of rainfall data
daily_rainfall = np.random.laplace(loc, scale, size=365)

# Ensure non-negative rainfall values
daily_rainfall[daily_rainfall < 0] = 0  # Set negative values to zero

# Plot the daily rainfall data
days = np.arange(len(daily_rainfall))  # Days (x-axis)

plt.figure(figsize=(10, 6))
plt.plot(days, daily_rainfall, marker='o', linestyle='-', label='Daily Rainfall (mm)')

# Optional: Add labels and title
plt.xlabel('Day')
plt.ylabel('Rainfall (mm)')
plt.title('Simulated Daily Rainfall (Laplace Distribution)')
plt.grid(True)
plt.legend()

plt.show()

# Print some descriptive statistics (optional)
print(f"Average daily rainfall: {daily_rainfall.mean():.2f} mm")
print(f"Maximum daily rainfall: {daily_rainfall.max():.2f} mm")

Let us look at the output for the same.

Laplace Rainfall Output
Laplace Rainfall Output

Conclusion

Laplace distribution’s ability to capture extreme events makes it a powerful tool for modeling real-world phenomena. From finance to hydrology, its applications continue to grow. As data becomes more complex, how can we leverage Laplace and other heavy-tailed distributions to make better predictions?

Recommended: Non-Parametric Statistics in Python: Exploring Distributions and Hypothesis Testing

Recommended: Understanding Joint Probability Distribution with Python