It seems like invgamma::dinvgamma
gives a value that is both different from Wikipedia's and other package.
> shape = 1
> scale = 2
> x = 2
> invgamma::dinvgamma(x, shape = shape, scale = scale)
[1] 0.0973501
> scale ** shape / gamma(shape) * x ** (-shape - 1) * exp(-scale / x) # Wikipedia
[1] 0.1839397
> MCMCpack::dinvgamma(x, shape = shape, scale = scale)
[1] 0.1839397
Indeed, the scale
parameter in invgamma:dinvgamma
should have been called rate
instead.
> invgamma::dinvgamma(2, shape = shape, rate = scale)
[1] 0.1839397
What does the help on the density function say it implements?
Does it implement that?
Well, if I follow what you showed there, it seems to do what it says it does.
Should that be called a rate parameter?
No -- since the parameter (let's call it $\theta$ for now) appears in the form $t(x/\theta)$, that's clearly a scale parameter in the usual sense of the word.
In summary, strictly it's not calculationally wrong in the sense that it seems to implement what it says it implements.
It is arguably misusing the usual terminology in that it swaps I'd say was the conventional meaning of "scale" and "rate".
I'd say that's at least disturbing -- and a likely cause of error in use -- but if you pay close attention to what they say they implemented, you would not do an incorrect calculation.
[Both authors appear to have PhD's in statistics; it would be hoped that there would be a greater awareness of the broad convention in relation to what a scale parameter is, or at least of the existence of a widely used convention in relation to those terms that is exactly inverted from what they did (i.e. I'd hope to see a clear
**Warning**
note in the help so that people who think that the appearance of a term of the form $x/\theta$ implies that $\theta$ is a scale are not led to misuse their function). I have absolutely no reason to doubt the quality of the implementation work though.]Ultimately the sense of 'rate' here comes from the Poisson process - in which the gamma distribution is "time to k-th event" and the 'rate parameter' represents the rate at which events occur in the process, and appears in the form $\lambda x$ (or a multiple of it) in the density of that gamma. The inverse-Gamma arises as the inverse of that gamma (in effect, inverse of time to k-th event, a kind of speed), but the meaning of the rate parameter is unchanged; it's still a parameter that enters as $\lambda x$. (I'm not sure how they come to flip the two of those around. No doubt precedents exist though.)