> [!tldr] Chi-Squared Distribution
>
> $\chi^{2}_{n}$ is the sum of $n$ iid. $N^{2}(0,1)$, where $n$ is the degree of freedom. It can construct a CI for the variance of $N(0,\sigma^{2})$.
>
> It is a special case of the [[Gamma Distribution|gamma distribution]]: $\chi^{2}_{n}=\mathrm{Gamma}(n / 2, 2)$.
The **chi-squared distribution** with $n$ degrees of freedom is denoted $\chi_{n}^{2}$, and it is *the sum of the square of iid. standard normals*: if $X_{1,\dots,n} \overset{iid.}{\sim} N(0,1)$, $\sum_{i=1}^{n}X_{i}^{2} \sim \chi_{n}^2,$and their pdf's under $k$ degrees of freedom are:
![[ChisqPDF.png#invert|w60|center]]
> [!exposition]- Deriving Confidence intervals with $\chi^{2}$
> How can we estimate the variance $\sigma^{2}$ in $N(0, \sigma^{2})$ from iid. samples $X_{1, \dots, n}$ with a CI?
> - The initial guess is to derive something from the CLT $\frac{\sum_{i}X_{i}}{\sigma\sqrt{ n }}\sim N( 0, 1)$, but this CI worsens with more samples.
>
> Standardizing $X_{1,\dots,n} \overset{iid.}{\sim} N(0,\sigma^{2})$ gives $\frac{\sum_{i}X_{i}^{2}}{\sigma^{2}} \sim \chi_{n}^{2}$ and the variance $\sigma^2$ has the CI: $
\mathbb{P}\left[ \frac{\sum_{i}X_{i}^{2}}{\sigma^{2}} \in (c_{1},c_{2}) \right]=\mathbb{P}\left[ \sigma^{2} \in \left( \frac{\sum_{i}X^{2}_{i}}{c_{1}},\frac{\sum_{i}X^{2}_{i}}{c_{2}}\right) \right]
=\,1-\alpha$by setting $c_{1,2}$ as appropriate percentiles of $\chi_{n}^{2}$.
>
> As $n$ increases, the distribution of $\chi_{n}^{2}$ flattens faster (i.e. $c_{1,2}$ grows faster) than the increase in $\sum_{i}X^{2}_{i}$, so the (expected) CI length improves with more samples:
>
> ```R
> Chisq_CI_length <- function(n,alpha=0.01){
lower = n / qchisq(1-alpha, df=n)
upper = n / qchisq(alpha, df=n)
return(upper-lower)
}
CI_lengths <- sapply(10:100, Chisq_CI_length)
plot(10:100, CI_lengths, xlab="n", ylab="CI length")
>```
>
>
> ![[ChiSquaredCILength.png#invert]]