To estimate the parameters of various probability distribution functions (PDFs) for a given float vector in Python using NumPy and SciPy, you can follow these steps. The distributions considered here are Normal, Exponential, and Scaled Beta.
1. Normal Distribution
The normal distribution is characterized by two parameters: the mean (μ) and the standard deviation (σ).
- Mean (μ): The average of the data.
- Standard Deviation (σ): The square root of the variance of the data.
import numpy as np
# Example data
data = np.array([...]) # Replace with your actual data
# Estimate parameters for the normal distribution
mu_normal = np.mean(data)
sigma_normal = np.std(data, ddof=1) # Use ddof=1 for sample standard deviation
2. Exponential Distribution
The exponential distribution is defined for positive values and is characterized by a rate parameter (λ), which is the inverse of the mean.
- Rate (λ):
λ = 1 / mean(data)
# Check if data contains only positive values
if np.all(data > 0):
lambda_exp = 1.0 / np.mean(data)
else:
raise ValueError("Exponential distribution requires strictly positive data.")
3. Scaled Beta Distribution
The beta distribution is typically defined on the interval [0, 1], but it can be scaled to any interval [a, b] by introducing location (loc) and scale (scale) parameters. The parameters for the scaled beta distribution include:
- Shape parameters (α, β): Control the shape of the distribution.
- Location (loc): The lower bound of the distribution.
- Scale (scale): The upper bound of the distribution (i.e.,
scale = b - a).
To fit a scaled beta distribution, use scipy.stats.beta.fit, which automatically estimates all four parameters (α, β, loc, scale).
from scipy import stats
# Fit a scaled beta distribution
alpha_beta, beta_beta, loc_beta, scale_beta = stats.beta.fit(data)
Note: Ensure the data is within the range [loc, loc + scale]. If your data is not naturally in [0, 1], this method will still attempt to fit the distribution by adjusting loc and scale.
Putting It All Together
Here’s a complete example using all three distributions:
import numpy as np
from scipy import stats
# Sample data
data = np.random.exponential(scale=2, size=1000) # Example: Exponential data
# Normal distribution
mu_normal = np.mean(data)
sigma_normal = np.std(data, ddof=1)
# Exponential distribution
if np.all(data > 0):
lambda_exp = 1.0 / np.mean(data)
else:
lambda_exp = None # Handle invalid data
# Scaled Beta distribution
alpha_beta, beta_beta, loc_beta, scale_beta = stats.beta.fit(data)
print("Normal Parameters:")
print(f" Mean (μ): {mu_normal:.4f}, Standard Deviation (σ): {sigma_normal:.4f}")
print("Exponential Parameters:")
print(f" Rate (λ): {lambda_exp:.4f}" if lambda_exp is not None else " Invalid data for Exponential")
print("Scaled Beta Parameters:")
print(f" Shape (α): {alpha_beta:.4f}, Shape (β): {beta_beta:.4f}, Location (loc): {loc_beta:.4f}, Scale (scale): {scale_beta:.4f}")
Key Considerations
- Data Validity: Ensure the data matches the support of the distribution (e.g., exponential only for positive values, beta for [0, 1] or scaled range).
- Scipy Dependency: The
scipy.stats.beta.fit function requires SciPy. If not installed, install it via pip install scipy.
- Model Selection: Fitting parameters is just the first step. You may also want to assess which distribution fits your data best using goodness-of-fit tests (e.g., Kolmogorov-Smirnov, AIC, BIC).
This approach provides a robust way to estimate parameters for different distributions using NumPy and SciPy. Adjust the code based on your specific data and requirements.