0
$\begingroup$

Am I correct to say that following the formula

$ f(x) = \frac{1}{\sigma \sqrt{2\pi} } e^{-\frac{1}{2}\left(\frac{x-\mu}{\sigma}\right)^2} $

represents a distribution with mean $\mu$ and standard deviation $\sigma$? I tried to verify the case with a short Python code

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
%matplotlib inline
mu  = 0 # mean
var = 1.0 #variance
sigma = np.sqrt(var) #standard deviation",
x = np.linspace(mu-3*var,mu+3*var, 100)
y = norm.pdf(x, mu, sigma)
plt.plot(x, y);

and found np.mean(y) = 0.1645975096425618 and n.std(y) = 0.13947206450268224. I did notice that y is non-negative.

Pardon me for my lack of understanding here, but neither does mean seem to be 0 nor does sigma seem to be 1. Is this how $\mu$ and $\sigma$ calculated suppose to be calculated for $f(x)$? How does one calculate the area under this curve?

enter image description here

Nonetheless, I was able to make the transformation z = (y -np.mean(y))/np.std(y) and found np.mean(z) = 2.220446049250313e-16 and n.std(z) = 0.9999999999999999. So is $z$ the normal distribution with $\mu \approx 0$ and $\sigma \approx 1 $? I am confused.

Update 1: As per gunes answer, "The formula for the normal distribution PDF is correct. But, np.mean(y) takes the mean of the PDF's y-axis values. From the plot, you can see that these values are all non-negative (as it should be because these are PDF values) and between 0 and 0.4. Same logic applies to deviation."

I wonder if one can literally apply the same logic? The PDF values for z has to be negative for some half of its part for the mean to be zero. That's why i was confused else these are not PDF values.

plt.plot(x, z);

enter image description here

Update 2: I learnt about new updates in numpy and tried to sample the new variate(s) as follow

s = np.random.default_rng().normal(mu, sigma, 10000)

which gives, not to my satisfaction,

>>> abs(mu - np.mean(s))
0.0026358298651454506
>>> abs(sigma - np.std(s, ddof=1))
0.004024714057919487

I am unhappy and would like to know what's going on when the data is sampled or how is it done? Any help in this regard is appreciated.

$\endgroup$

2 Answers 2

2
$\begingroup$
  • Yes that’s the correct expression for a normal density
  • You in effect computed $\frac16 \int_{-3}^3 p(x) dx\approx \frac16$ for the mean. You should have computed $\int p(x) x dx$ which you may do numerically by np.dot(x, y) / y.sum()
  • The quantity z = (y -np.mean(y))/np.std(y) has mean 0 and variance 1 by definition. Just try to compute it. But the fact that it has mean 0 and variance 1 does not mean it is distributed as a standard normal $N(0,1)$.
  • You computed the sample mean and sample variance. They are estimates of the mean and variance of the underlying normal distribution that don’t agree exactly with 0 and 1 due to the finite number of samples (10000) that you used. Try increasing that number; the differences should decrease.

More generally,

The PDF values for z has to be negative for some half of its part for the mean to be zero. That's why i was confused else these are not PDF values.

Here and throughout you are confusing the random variable $x$ with the pdf $p(x)$. The PDF is non-negative. The PDF must be non-zero at some negative $x$ in order for the mean of $x$ to be zero.

$\endgroup$
3
  • $\begingroup$ Since z is obtained from shifting and scaling f(x) i.e. y which is normal. I would like to say that z is a normal distribution too. $\endgroup$
    – Sowmya
    May 29, 2020 at 13:24
  • $\begingroup$ I am wondering where did I set +/- 6 as the limits? $\endgroup$
    – Sowmya
    May 29, 2020 at 13:31
  • $\begingroup$ Oops that should have said -3,3 and you set them in your np.linespace call $\endgroup$
    – innisfree
    May 29, 2020 at 21:55
1
$\begingroup$

The formula for the normal distribution PDF is correct. But, np.mean(y) takes the mean of the PDF's y-axis values. From the plot, you can see that these values are all non-negative (as it should be because these are PDF values) and between 0 and $0.4$. Same logic applies to deviation.

For example: let a RV be either $5$ or $6$ with probabilities $1/2$. If you take the mean of $f(x)$, you'll have $1/2$, but that has nothing to do with the actual mean $5.5$.

For actually calculating the mean of normal random variables, you need to sample from $f(x)$. You can use numpy.random.normal.

$\endgroup$
8
  • $\begingroup$ The non-negativity is obvious to me. I didn't put it there to keep the write-up short. Will check out the rest. Thanks. $\endgroup$
    – Sowmya
    May 29, 2020 at 9:08
  • $\begingroup$ I have a question now, instead of using np.random.normal(mu, sigma, 1000), all I need to do is sample from f(x) to get the correct mean and std.deviation, right? $\endgroup$
    – Sowmya
    May 29, 2020 at 9:10
  • $\begingroup$ @Sowmya Yes, you won't calculate the mean & std of f(x). Those are irrelevant. You're going to sample from f(x). Your update 2 is the correct way to sample, and the difference between the sample mean/std and true mean/std is normal. It'll decrease as your sample size increases. $\endgroup$
    – gunes
    May 29, 2020 at 9:55
  • 1
    $\begingroup$ He doesn’t need to sample: with his setup, np.dot(x, y) / y.sum() should give mean $\endgroup$
    – innisfree
    May 29, 2020 at 10:39
  • 1
    $\begingroup$ @innisfree Yes :) that is the discretised approximation of the theoretical expectation, $\int x f(x) dx$. I suggested sampling because I thought he tries to do so, he wasn't trying to consciously approximate the integral. $\endgroup$
    – gunes
    May 29, 2020 at 10:42

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.