# Matlab: Generating Data using a cubic equation with a specific variance

I'm wondering if it's possible to generate data (d) in Matlab using the equation

$$d = 1 + 0.5z + 0.25z^2 + 0.125z^3$$

where $$0 < z < 1$$ and the variance is

$$\sigma^2 = 0.01$$

I've generated guassian random variables with a defined variance/standard deviation, e.g.

z = normrnd(0,0.1,50,1);

but I'm unsure how to select values for z that will result in d having the required variance using the cubic equation.

Any help is greatly appreciated.

• Is $\sigma^2$ supposed to be the variance of $z$ or of $d$? – Robert Israel Sep 17 '15 at 15:08
• the way the question I am trying to solve is phrased, it is suggesting it is the variance of d, which is where I am having difficulty – user1707828 Sep 17 '15 at 19:15

A normal distribution will not be restricted to the interval $(0,1)$. You might try a truncated normal distribution (generate normally-distributed data and discard all data points that fall outside the given interval). Then you want to determine a mean and variance for the normal distribution that will result in the desired variance for $d$. Actually your problem is underdetermined in that you have two parameters (mean and variance of the normal distribution) to play with, and only one equation to satisfy (the variance of $d$ is $0.01$). You could, for example, make the mean of your normal distribution $0$. If the variance of the normal distribution is $s^2$, then the variance of the resulting $d$ is a rather complicated function: according to Maple

$$\sigma^2 = -{\frac {s}{64\,\pi } \left( 39\,\sqrt {\pi }{\rm erf} \left(1/2\,{ \frac {\sqrt {2}}{s}}\right)\sqrt {2}{{\rm e}^{-1/2\,{s}^{-2}}}{s}^{4} -15\, \left( {\rm erf} \left(1/2\,{\frac {\sqrt {2}}{s}}\right) \right) ^{2}\pi \,{s}^{5}-24\,\sqrt {2}\sqrt {\pi }{\rm erf} \left(1/ 2\,{\frac {\sqrt {2}}{s}}\right){s}^{4}+8\, \left( {{\rm e}^{-1/2\,{s} ^{-2}}} \right) ^{2}{s}^{5}+61\,\sqrt {\pi }{\rm erf} \left(1/2\,{ \frac {\sqrt {2}}{s}}\right)\sqrt {2}{{\rm e}^{-1/2\,{s}^{-2}}}{s}^{2} -32\, \left( {\rm erf} \left(1/2\,{\frac {\sqrt {2}}{s}}\right) \right) ^{2}\pi \,{s}^{3}-16\,{{\rm e}^{-1/2\,{s}^{-2}}}{s}^{5}-16\, \sqrt {2}\sqrt {\pi }{\rm erf} \left(1/2\,{\frac {\sqrt {2}}{s}} \right){s}^{2}+56\, \left( {{\rm e}^{-1/2\,{s}^{-2}}} \right) ^{2}{s}^ {3}+8\,{s}^{5}+49\,\sqrt {\pi }\sqrt {2}{\rm erf} \left(1/2\,{\frac { \sqrt {2}}{s}}\right){{\rm e}^{-1/2\,{s}^{-2}}}-16\, \left( {\rm erf} \left(1/2\,{\frac {\sqrt {2}}{s}}\right) \right) ^{2}\pi \,s-88\,{ {\rm e}^{-1/2\,{s}^{-2}}}{s}^{3}+98\,s \left( {{\rm e}^{-1/2\,{s}^{-2} }} \right) ^{2}+32\,{s}^{3}-112\,{{\rm e}^{-1/2\,{s}^{-2}}}s+32\,s \right) \left( {\rm erf} \left(1/2\,{\frac {\sqrt {2}}{s}}\right) \right) ^{-2}}$$

It's not possible to solve $\sigma^2 = 0.01$ in closed form, but it can be done numerically: the result is $s = 0.2460523638$ approximately.

Thus in Matlab

s = 0.2460523638;
X = s * randn(10^5, 1);
Z = X(X > 0 & X < 1);
D = 1 + 0.5*Z + 0.25 * Z .^ 2 + 0.125 * Z .^ 3;


generates a sample of $10^5$ normal random variates $X$ with mean $0$ and standard deviation $s$, discards those outside the interval $(0,1)$ (about half of them), and applies the transformation to those that remain. $D$ should then have variance close to $0.01$.