4

We have qgamma in R and gamm.inv in excel, and I am not able to get the same results using invgamma function in python. For example in excel GAMMA.INV(0.99,35,0.08)=4.01, I can get the same value from R using qgamma with probability=0.99, alpha(scale)=35.0, shape=0.08.

But when I am using invgamma.pdf(0.99,35,0.8) it returns 1.61787636512e-15.

What is the right way of doing it? I have tried both gamma and invgamma with pdf, ppf, cdf but nothing is matching with the value I am getting from qgamma in R or GAMMA.INV in excel

iacob
  • 20,084
  • 6
  • 92
  • 119
SKD
  • 51
  • 3
  • [I can't reproduce the results you say you're getting in R.](https://ideone.com/EpT4OS) – user2357112 Jul 12 '18 at 05:16
  • Can you get the value in excel? I checked in R and it was matching with excel values. Will provide the exact arguments used in R by evening – SKD Jul 12 '18 at 07:24

1 Answers1

6

qgamma and GAMMA.INV give the inverse cumulative distribution function of a gamma distribution.

invgamma represents an inverse gamma distribution, which is a completely different thing. Explaining the difference is out of scope for this site, as it's a statistics issue, not a programming issue, but you should really get it solidly understood before you proceed.

The SciPy distribution object for a gamma distribution is scipy.stats.gamma, and the method for the inverse cumulative distribution function is ppf, short for "percentile point function" (another name for the inverse CDF). Thus, you should be using scipy.stats.gamma.ppf. Make sure to pass it the right arguments.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • 1
    Thanks for clarification on invgamma, but when I am using scipy.stats.gamma.ppf(0.99,35,0.08)=50.29 it's not matching with excel gammainv(0.99,35,0.08)=4.01 – SKD Jul 12 '18 at 07:20
  • 1
    @SKD: `gamma.ppf` doesn't take the same arguments as `gammainv`. You're passing `loc` instead of `scale`. You also appear to be mixing up scale and shape in general; alpha is the shape parameter, not the scale. – user2357112 Jul 12 '18 at 07:32
  • yes I am sure I am doing something wrong, can you guide me what should be the argument to get the value same as excel – SKD Jul 12 '18 at 07:39
  • 2
    Ok got the right argument it's gamma.ppf(0.99,35,0,0.08). I have to pass loc as 0 between shape and scale. Thanks a lot user2357112 – SKD Jul 12 '18 at 07:54
  • For some reason, beta.ppf and gamma.ppf have different argument orders; in gamma, the location comes in between the shape & scale, but it is after in beta. This threw me for a loop until I found this thread. – a11 Aug 27 '22 at 01:43