Skip to content

Instantly share code, notes, and snippets.

@SirmaXX
Last active January 25, 2024 00:02
Show Gist options
  • Save SirmaXX/9b0677c1dc39e514ec1f21f698d9c513 to your computer and use it in GitHub Desktop.
Save SirmaXX/9b0677c1dc39e514ec1f21f698d9c513 to your computer and use it in GitHub Desktop.
code snippets for inverse chi-square distribution
from scipy.stats.distributions import chi2
chi2.ppf(0.95, df=5) # 11.07
function mean_of_inversechi(v) {
if (v > 2) {
return 1/(v-2);
}else{
return 0;
}
}
function variance_of_inversechi(v) {
if (v > 2) {
return 2/(Math.pow(v-2,2) * (v-4));
}else{
return 0;
}
}
function inverseChiSquarePDF(x, v) {
if (x <= 0 || v <= 0) {
throw new Error("Both x and degrees of freedom (v) must be greater than 0");
}
const numerator = Math.pow(2, -v / 2);
const denominator = jStat.gammafn(v / 2);
const exponent = -1 / (2 * x);
const pdfValue = (numerator / denominator) * Math.pow(x, -(v / 2) - 1) * Math.exp(exponent);
return pdfValue;
}
library(VGAMextra)
xx <- seq(0, 3.0, len = 301)
yy <- dinv.chisq(xx, df = df)
qtl <- seq(0.1, 0.9, by = 0.1)
d.qtl <- qinv.chisq(qtl, df = df)
plot(xx, yy, type = "l", col = "orange",
main = "Orange is density, blue is cumulative distribution function",
sub = "Brown dashed lines represent the 10th, ... 90th percentiles",
las = 1, xlab = "x", ylab = "", ylim = c(0, 1))
abline(h = 0, col= "navy", lty = 2)
lines(xx, pinv.chisq(xx, df = df), col = "blue")
lines(d.qtl, dinv.chisq(d.qtl, df = df), type ="h", col = "brown", lty = 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment