# Generating data with Gaussian distribution

We want to generate a Gaussian vector, (call it $\Delta$) - with an arbitrary size-, with zero mean and variance of $\alpha$. If $\alpha$ is chosen such that $\parallel\Delta\parallel_2 \leq 0.5$ with probability of e.g. $90\%$.

How can we do that in Matlab?

• Not sure of what you mean by norm(delta, 2). Is delta your mean and 2 your variance, as with the usual notation? – Florent Sep 15 '17 at 6:35
• The problem is in this part, we want a vector whose norm 2 of that be less than or equal to e.g. 0.5 with the specified probability e.g. 90%. I just know that norm 2 of Gaussian has Chi-square distribution! – Reza Mahjoob Sep 15 '17 at 6:40

## 2 Answers

Let $\Delta = [x_1, \dots, x_N]$ be your desired vector where $x_i$ are iid $\mathcal{N}(0,\sigma^2)$. Do you expect that $\mathrm{Pr} \left\lbrace ||\Delta||_2^2 < 0.5\right\rbrace = 0.9$ ?

By definition $y = ||\Delta||_2^2 / \sigma^2 = \sum_{i=1}^N \frac{x_i^2}{\sigma^2} \sim \chi^2_N(y)$ is Chi squared distribution of degree of freedom $N$.

Thus $z = ||\Delta||_2^2 = \sigma^2y \sim \frac{1}{\sigma^2} \chi^2_N(\frac{z}{\sigma^2})$

Denote the CDF $\mathrm{F}(z)$, you want something like $\mathrm{F}(z = 0.5) = 0.9$ with $\sigma^2$ is unknown variable.

A closed form expression of $\mathrm{F}(z)$ is available at wikipedia Chi squared distribution.

In MATLAB you can use the two functions chi2pdf() and chi2cdf() for numerical brute force solution for $\sigma^2$.

And after having the value of $\sigma^2$, just use the function randn() to generate your vector $\Delta$. For example with size N and $\sigma^2$ = sigma2, delta = randn(1,N) * sqrt(sigma2).

• I want Pr { ||Δ||_2<= 0.5} = 0.9 ; In other words the probability that the Euclidean norm of Δ be less than or equal to 0.5 be 90%. – Reza Mahjoob Sep 15 '17 at 10:16
• @RezaMahjoob So just replace 0.5 with 0.25 in the above... – Peter K. Sep 15 '17 at 12:05
• @RezaMahjoob to clarify what Peter said $\mathrm{Pr \left\lbrace ||\Delta||_2 < 0.25 \right\rbrace} = \mathrm{Pr \left\lbrace ||\Delta||_2^2 < 0.5 \right\rbrace}$ – AlexTP Sep 15 '17 at 12:36
• Sorry; how did you get the distribution of "z" in the last equation? The chi-square argument is not obvious for me. – Reza Mahjoob Sep 30 '17 at 20:43
• @RezaMahjoob $\mathrm{F}_Z(z) = \mathrm{Pr}(Z < z) = \mathrm{Pr}(\sigma^2 y < z) = \mathrm{Pr}(y < z / \sigma^2) = \mathrm{F}_Y(z/\sigma^2)$. Then $\mathrm{f}_Z(z) = \frac{\mathrm{dF}_Z(z)}{\mathrm{d}z} = \frac{1}{\sigma^2} \mathrm{f}_Y(z/\sigma^2)$. I did make a mistake. – AlexTP Oct 1 '17 at 8:57

Matlab's randn() function generates a vector of zero-mean normally distributed data.

You can proove that if you mutliply a random variable by a constant, then you will mutliply its variance by the square of this constant.

More generally :

$$\begin{split} \mathrm{Var}[a \times x + b] &= a^2\times\mathrm{Var}[x]\\ \mathrm{Mean}[a \times x + b] &= a\times\mathrm{Mean}[x] + b\\ \end{split}$$

Thus, to generate a random sequence of a specified variance (call it $\sigma_{\Delta}^2$) and 0-mean, you would first generate a 0 mean normally distributed vector with Matlab's randn()and then multpily by the square root of you new variance.

• The problem is in this part, we want a vector whose norm 2 of that be less than or equal to e.g. 0.5 with the specified probability e.g. 90%. I just know that norm 2 of Gaussian has Chi-square distribution! – Reza Mahjoob Sep 15 '17 at 6:55