I'm trying to estimate the parameters of an inverse Gamma distribution given its 0.025 and 0.975 quantiles.
Currently, I've found rriskDistributions::get.gamma.par
which gives me the estimation of parameters given quantiles of a Gamma distribution. However, I cannot figure out the relationship between the quantiles of Gammas and inverse Gammas.
How should I continue, or is there a package that can do this for me?
You can write your own objective function that computes the squared deviation between the computed quantiles for a particular set of inverse-gamma parameters and the target quantiles:
library(invgamma)
objfun <- function(p,target) {
qq <- qinvgamma(c(0.025,0.975),shape=p[1],rate=p[2])
sum((qq-target)^2)
}
Then use optim()
to minimize:
## example
tt <- qinvgamma(c(0.025,0.975), shape=2,rate=2)
optim(par=c(1,4), ## starting values; must be sensible
fn=objfun,
target=tt)
$par
[1] 1.980279 1.948050
$value
[1] 9.0741e-05
$counts
function gradient
61 NA
$convergence
[1] 0
$message
NULL