How can I generate $r_i$ for $1 \leq i \leq n$, such that $\sum_{i=1}^n \left|\frac{r_i}{\sigma_i}\right|^2\leq\chi^2_{n,\alpha}$, where $\sigma_i^2$ is the variance of $r_i$ and $\chi^2_{n,\alpha}$ is a chi-squared value for $n$ degree of freedom and an $\alpha$ confidence level.

I truly appreciate your insights.

  • $\begingroup$ Two questions: are n and \alpha known or unknown? Are you asking a programming question or a math question? $\endgroup$
    – akash87
    Jun 28, 2016 at 18:57
  • $\begingroup$ @akash87, thanks for your comment. $\alpha$ and $n$ are both known. However, I am also keen on the relationships. I am looking more into the theory and mathematics, but any insights are certainly appreciated. $\endgroup$
    – Jolfaei
    Jun 28, 2016 at 19:01
  • $\begingroup$ Are you sure $r_i$ is not normal? $\endgroup$
    – akash87
    Jun 28, 2016 at 19:41
  • $\begingroup$ Not necessarily. $\endgroup$
    – Jolfaei
    Jun 28, 2016 at 20:03
  • 1
    $\begingroup$ In general, as $n \rightarrow \infty$, then $(\chi_n ^2 -n)/2n \rightarrow N(0,1) $ for $ \chi^2$. $\endgroup$
    – akash87
    Jun 28, 2016 at 20:29

2 Answers 2


Chi-squared distribution is defined in terms of normally distributed random variables. If $Z_1,\dots,Z_k$ are i.i.d. standard normal variables, then $ \sum_{i=1}^k Z_i^2 \sim \chi^2_k $. So to draw from chi-squared distribution with $k$ degrees of freedom, can use $k$ values drawn from standard normal. Alternatively, as in your case, you can draw $X_1,\dots,X_k$ from normal distribution with mean $0$ and standard deviation $\sigma^2$ and then take $Z_i = X_i / \sigma^2$ (so are you sure that you want to divide by variance..?).


f <- function() {
  n <- 20
  sigma <- 5
  r <- rnorm(n, 0, sigma)

x <- replicate(5000, f())
xx <- seq(0, 75, by = 0.01)

hist(x, 100, freq = FALSE)
lines(xx, dchisq(xx, df = n), col = "red")

enter image description here

If you need $100\alpha \% $ middle values, and $\alpha$ is big (say $0.95$), than the easiest way to go is to make multiple draws and then discard the draws that fall beyond the $100\alpha \% $ interval.

  • $\begingroup$ (+1) for your insight but the question still remains. I'm looking for a method to create $r_i$ that maintain the condition with probability 1. Your method seems like a trial and error. What is the best distribution and method of generation to ensure the condition? $\endgroup$
    – Jolfaei
    Jul 3, 2016 at 19:10
  • $\begingroup$ @Jolfaei if your alpha is big, e.g. $0.95$ then this means that your would have to reject only only 5% of the cases, that is not much. No better method comes to my mind. $\endgroup$
    – Tim
    Jul 3, 2016 at 19:54
  • $\begingroup$ I also have another concern, why did you use normal distribution? Can't this work with poison distribution (if you write the gof test then this might cross your mind) or even uniform distribution or ... . Why normal? $\endgroup$
    – Jolfaei
    Jul 3, 2016 at 21:31
  • $\begingroup$ @Jolfaei because, by definition, this is a distribution of sum of squared normals (check en.wikipedia.org/wiki/Chi-squared_distribution or statistics handbook). This is the distribution that is taken by sum of squared normals, not just squared things. $\endgroup$
    – Tim
    Jul 3, 2016 at 22:01
  • $\begingroup$ That's correct but $\chi^2$ is a constant, let's say $ C$, in here that defines an upper bound for the sum. Given this constant bound, can't we generate $r_i$ following any other distributions? $\endgroup$
    – Jolfaei
    Jul 3, 2016 at 22:21

In general, as $n \rightarrow \infty$, then $(\chi_n ^2 -n)/2n \rightarrow N(0,1) $ for $ \chi^2$. Now we can establish the function $\Sigma_{i=1} ^n (\frac {r_i}{\sigma_i})^2 \leq \chi^2 _{n,\alpha} $ is true for the normal distribution $R$ for $r_i$. This is kind of actually a normal to chi-square transformation, and what you can do to generate the $r_i$ is usually select $\sigma_i^2 = 1$ for all $i$ and then you can find the $r_i^2$ values that correspond to the $\chi^2_n$ distribution.

To consider $\alpha$ in this problem is really to determine if the value samples falls within the $1-\alpha$ confidence interval of $\chi^2_n$.

@Jolfaei The $r_i$ distribution is sampled from a normal distribution of mean $E(r_i)$ and variance $Var(r_i)$. It should come from $N(E(r_i), Var(r_i))$. If you know $n$, then you can sample from any normal distribution for a defined $E(r_i)$ and $Var(r_i)$. So lets say for example, you want to find the $\chi^2 _5$. You can actually pick the normal distribution parameters for each iteration. It should work because the tail end $\chi^2$ p-value increases as $n\rightarrow infty$ and by definition the $chi^2$ distribution is the sum of normal distributions.

  • $\begingroup$ I think the answer is somehow vague and not complete. It is still not clear to me how you generate $r_i$. What is the $E(r_i)$? My other concern is that I think $\chi^2$ is constant not a variable. $\endgroup$
    – Jolfaei
    Jul 3, 2016 at 6:33
  • $\begingroup$ @Jolfaei The $r_i$ distribution is sampled from a normal distribution of mean $E(r_i)$ and variance $Var(r_i)$. It should come from $N(E(r_i), Var(r_i))$ $\endgroup$
    – akash87
    Jul 4, 2016 at 12:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.