In [1]:
from scipy.stats import norm
Assume $X$ is a random variable which is normally distributed:
</P> $X \sim N(\mu_X, \sigma_X^2),$
where $\mu_X \in \mathbb{R} $ is the mean (or location) and $\sigma_X^2 > 0$ is the variance (squared scale)
In [2]:
# create a normally distributed random variable with mu and sigma
mu = 28.74
sigma = 8.33 # standard deviation!
rv_X = norm(loc = mu, scale = sigma)
In [3]:
# plot the theoretical and empirical distributions
x = np.linspace(start = rv_X.ppf(0.001), stop = rv_X.ppf(0.999), num = 100)
plt.plot(x, rv_X.pdf(x), color = 'r', lw=2, label='theoretical');
plt.hist( rv_X.rvs(size = 1000), rwidth=.85, facecolor='k', normed=1, label='empirical');
plt.legend(frameon=0);
Assume $X$ and $Y$ are independent random variables and are both normally distributed, then the sum of the two random variables $X+Y$ will be also a random variable $Z$ that has a normal distribution with the following properties:
</P> $Z \sim N(\mu_Z, \sigma_Z^2),$
The resulting mean is simply sum of the two means: $\mu_Z= \mu_X+\mu_Y,$
and the variance is the sum of the two variances: $\sigma_Z^2 = \sigma_X^2 + \sigma_Y^2,$
or alternatively, the standard deviation: $\sigma_Z = \sqrt{\sigma_X^2 + \sigma_Y^2}$
In [4]:
# create a second normally distributed random variable with mu and sigma
mu = 28.74
sigma = 8.33 # standard deviation!
rv_Y = norm(loc = mu, scale = sigma)
In [5]:
# The theoretical distrubution of Z
rv_Z = norm(loc = mu+mu, scale = sqrt(sigma**2+sigma**2))
# The empirical distribution of Z based on the sum of two random variables
data = rv_X.rvs(100) + rv_Y.rvs(100)
In [6]:
# Plot resulting distributions
x = np.linspace(start = rv_Z.ppf(0.001), stop = rv_Z.ppf(0.999), num = 100)
plt.plot(x, rv_Z.pdf(x), color = 'r', lw=2, label='theoretical');
plt.hist( data, rwidth=.85, facecolor='k', normed=1, label='empirical');
plt.legend(frameon=0);
In [7]:
# Location and scale from data
print('Location = %f, scale = %f'%norm.fit(data))
In [9]:
# Theoretical location and scale
mu_Z = mu + mu
sigma_Z = sqrt(sigma**2 + sigma**2)
print('Location = %f, scale =%f'%(mu_Z , sigma_Z))