Generating random sample from an inverse chi-square distribtution
Ellen1 asked me if it was possible to draw samples from an inverse chi-square distribution in R. It can't get anymore straightforward: By definition a random variable is distributed according an inverse chi-square law, if its inverse is distributed according to a chi-square law. Hence,
# 1000000 samples from an inverse chi-square distributed random
# variable with 30 degrees of freedom
x = 1/rchisq(1000000,30)
hist(x, 300, freq=FALSE) # density not frequencies
is the simplest way. The density is given by simple change of variable y=1/x, or
# variable with 30 degrees of freedom
x = 1/rchisq(1000000,30)
hist(x, 300, freq=FALSE) # density not frequencies
# density of inverse chi-square
dichisq = function(x, df) dchisq(1/x, df)/x^2
plot(function(t) dichisq(t, 30), 0.0, 0.14, col=2, add=TRUE)
dichisq = function(x, df) dchisq(1/x, df)/x^2
plot(function(t) dichisq(t, 30), 0.0, 0.14, col=2, add=TRUE)
1 Comments:
For those who may find this searching. The scaled inverse chi2 distribution can be calculated by multiplying the above code by v*s2: the degrees of freedom times the scale.
Post a Comment
<< Home