Generate random number from inverse gamma distribution
Show older comments
Hi, I want to get the sample from the inverse gamma distribution with two parameter a and b and I am wondering how I can do that in matlab? Please help me ! Thanks
Answers (1)
Tom Lane
on 27 May 2012
1 vote
If I understand the inverse gamma correctly, you can generate from x=gamrnd(a,1./b) and then take y=1./x. This would require the Statistics Toolbox.
3 Comments
The conversion you mention I believe is correct.
Discussion here:
Some more details here:
But when using this approach I don't seem to get the correct distribution.
Set a=0.5, b=0.7, and check the maximum value for 1000 samples, it is approximately 7-12, but there should be a sufficient probability to attain much higher values over 1000.
As a comparison you can use the R function "rinvgamma(1000, shape=a, rate=g)" or the online tool http://homepage.divms.uiowa.edu/~mbognar/applets/inversegamma.html
The issue seems to be in the gamrnd function not sampling small enough values to produce those large inverse-gamma samples.
Is there another way to sample from an (inverse) gamma in MATLAB?
Zhiyong Yang
on 27 Nov 2018
Hi, Jonathan
Have you solved this problem?I have compared the pdf curves obtained by ksdensity method of samples draw by the mentioned way using gamrnd function and by analytical solution using the equation of inverse-gamma pdf. It is shown as the alpha is smaller than 2, the mentioned way cannot sample well. Please see the attached figure where symbols represent pdf from ksdensity method and lines represent pdf from analytical solutoin. 

Kasi Vemalaiah
on 8 Mar 2021
Thank you.
Categories
Find more on Gamma Distribution in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!