'Fitting empirical distributions using python

I have 255 monthly (~21 years) returns of financial asset that ranges from -22.25% to +18.09%. I am using the code from Fitting empirical distribution to theoretical ones with Scipy (Python)? to fit the data into distribution and generate random numbers.

enter image description here

This is the histogram of the data. I believe the code above tries to fit data into distribution using MLE (maximum likelihood estimation) and there are about 88 different distributions in the list. My question is that, for example, burr distribution is positive random variable only (https://en.wikipedia.org/wiki/Burr_distribution, https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.burr.html).

But when I fit the distribution, get the parameters and make PDF, I get the following result: enter image description here

which the distribution has both positive and negative values.

To be honest, I don't think I fully understand the code and the implications of fitting distributions. Why would a distribution that is supposed to fit positive values only also fit negative values?



Solution 1:[1]

Try the distfit library. It fits the best theoretical distribution based on your empirical data. It returns the loc/scale parameters. You can set the directionality to test for significance (upper/lower bounds). The fitted model can be used to generate new samples.

pip install distfit

# import library
from distfit import distfit

# Lets create some random data for demonstration purposes. Ssuppose that X is your data.
X = np.random.normal(0, 2, 10000)

# Initialize with default settings
model= distfit(bound='both')

# Fit to find the best theoretical distribution 
model.fit_transform(X)

# The fitted distribution can now be used to generate new samples.
Xgenerate = model.generate(n=1000)

Disclaimer: I am also the author of this repo.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1