I want to check if the probability density of my data follows power-law, exponential or stretched exponential function.
I have the following code, and I am using scipy's powerlaw.fit and expon.fit() but I am not sure how to check for stretched exponential -
pip install powerlaw
import numpy as np
import scipy.stats as stats
from scipy.stats import ks_2samp
from scipy.stats import powerlaw, expon
from scipy.stats import kstest
from scipy.optimize import curve_fit
from powerlaw import Fit
#Check if the pdf follows power law or exponential function
data = [120.0, 120.0, 100.0, 25.0, 20.0, 30.0, 28.0, 60.0, 90.0, 30.0, 24.0, 30.0, 30.0]
# Fit data to power-law distribution
params_powerlaw = powerlaw.fit(data)
# Fit data to exponential distribution
params_exponential = expon.fit(data)
# Perform Kolmogorov-Smirnov test to compare distributions
D_powerlaw, p_powerlaw = kstest(data, powerlaw.cdf, args=params_powerlaw)
D_exponential, p_exponential = kstest(data, expon.cdf, args=params_exponential)
# Print test statistics and p-values
print('Kolmogorov-Smirnov test statistics:')
print('Power-law:', D_powerlaw)
print('Exponential:', D_exponential)
print('\nKolmogorov-Smirnov test p-values:')
print('Power-law:', p_powerlaw)
print('Exponential:', p_exponential)
#printing parameters
print("\nPowerlaw alpha =", params_powerlaw[0])
print("\nExponential loc =", params_exponential[0], "scale =", params_exponential[1])
# Compare p-values to determine which distribution fits better
if p_powerlaw > p_exponential:
print('\nThe data follows a power-law distribution.')
else:
print('\nThe data follows an exponential distribution.')