Fitting Probability Distributions to Data with SciPy (Python)
Вставка
- Опубліковано 6 вер 2024
- UPDATE: I realized the method I used in this video, called fit() is only included for CONTINUOUS distributions (normal, gamma, exponential, etc) in SciPy. If you're trying to fit a DISCRETE distribution (Poisson, geometric, binomial, etc), I made a new video explaining how to do that:
• Fitting Discrete Distr...
UPDATE 2: After I made this video, SciPy 1.9 was released, which includes a new method that makes it easy to fit distributions to data, and can be used for both continuous and discrete distributions. Here is the link to the documentation for this method:
scipy.github.i...
Hi everyone! This video is about how to use the Python SciPy library to fit a probably distribution to data, using the normal distribution and gamma distribution as examples.
All the code from my videos is available on my Github:
github.com/mik...
Let me know if you have any questions! Thanks for watching!
Hi everyone, thanks for watching! Here's the link to the code on my Github in case you want to try it for yourself (also feel free to add me as a friend on Github if you want 🙂) :
github.com/mikesaint-antoine/Comp_Bio_Tutorials/tree/main/parameter_estimation/continuous_distributions
Python newbie here, emerging into the modern world from Fortran...77. I learned a lot. Thanks.
Thanks for watching James, and let me know if you have any questions! 🙂
very helpful! Thanks :)
Thanks for watching! 🙂
Excellent video.
Thanks Robert! Let me know if you have any questions 🙂
Dude, How are you so smart? Is it because your name is Michael and also a saint?
Thanks lol
Thank you very much for your helpful video! It was exactly what I was looking for. I just subscribed to your channel. Wishing you the best!
Thanks Andrew! 🙂
Very valuable explanation. Thank you
Thanks for watching Mohammad!
Thank you so much! Very helpful video!!
Thanks for watching Digant, and let me know if you have any questions! 🙂
Great video Mike. What method do I use to fit to discrete distributions. I looked under the scipy binom object and there is no fit method. Is it possible to compute measure of fit eg least squares, AIC or BIC?
Hi Nnamdi, hmmmm that's a good question. I didn't realize this until now, but you're right, when I looked up some discrete distributions they don't have the fit() method, and I couldn't find any built-in method to do it in scipy. I'm not sure why that is.
It can definitely still be done from scratch, but that is a bit more difficult. I think the way to do it is to just use the binomial probability mass function and then just treat it like you're fitting parameters to a regular function, varying the binomial distribution parameters. For example this is my video on fitting parameters to a logistic growth model:
ua-cam.com/video/GV3hAUO02kk/v-deo.html
I would recommend trying to do this, except instead of the logistic growth model just use the binomial probability mass function.
I'll try to make a video on this when I have more free time (I'm a bit busy with research and grad school at the moment).
Thanks for watching!
Hi again Nnamdi! Ok I made a new video explaining how to do the parameter estimation for discrete distributions:
ua-cam.com/video/TwbJCt36_DU/v-deo.html
Hope this helps! I used the Poisson distribution here, but I think the same method will work for any discrete distribution that's included in the scipy.stats library.
Let me know if you have any questions!
@@MikeSaintAntoine Been trying to use scipy to fit a multivariate t distribution to stock returns. It can be done with a line in Matlab but scipy doesnt have any methods for the multivariate_t object to do this. Any ideas?
@@nnamdiodozi7713 Hi Nnamdi, good question. Yes I saw that SciPy's new scipy.stats.fit() function doesn't work for the multivariate_t distribution yet, even though SciPy has a multivariate_t distribution object:
docs.scipy.org/doc/scipy/reference/generated/scipy.stats.multivariate_t.html
So my advice here would be to attempt to do the fitting from scratch -- by defining a loss function and then minimizing it to find the parameters that fit the best. This is basically what I tried to do in my previous video on fitting discrete distributions (although with the new version of SciPy many discrete distributions can be fit with scipy.stats.fit()). That video is here:
ua-cam.com/video/TwbJCt36_DU/v-deo.html
Good luck, and thanks for watching my videos!
great tut!
Thanks for watching Adebisi!
Great video! If you want to test U-shape distribution what test would you choose. I look at the documentation but couldn't find how to do it
Hi! That's a great question. I previously thought that all the scipy distributions had this fit() method to make the fitting easy, but apparently some of them don't have it (as another commenter pointed out). So for those, unfortunately I don't really have a good recommendation for how to easily do the fitting.
It's still possible to do the fitting from scratch though. For that I would recommend checking out my video on fitting to the logistic growth model:
ua-cam.com/video/GV3hAUO02kk/v-deo.html
And then just try to follow those steps, except instead of using the logistic growth function, just use the probability mass function for whichever distribution you're trying to fit to.
Maybe I will also try to make a video on how to do the distribution fits from scratch in the future, if I have more time.
Thanks for watching and good luck!
Hi again! I'm not sure which specific distribution you're talking about with the U-shape, but I made another video explaining how to fit discrete distributions in SciPy, which don't have the convenient fit() method. This can still be done by using their logpmf() function. Here is the video:
ua-cam.com/video/TwbJCt36_DU/v-deo.html
I used the Poisson distribution for an example, but I think this method should work for any discrete distribution included in SciPy.
Hope that helps! Let me know if you have any questions!
@@MikeSaintAntoine Amazing. Thank you!!