09 Sample variance#

%%html
<iframe width="700" height="400" src="https://www.youtube.com/embed/iHXdzfF7UEs/" frameborder="0" allowfullscreen></iframe>
import numpy as np
import pandas as pd
from pandas import Series, DataFrame
import matplotlib.pyplot as plt
import seaborn as sns
from scipy import stats

khanacademy numpy.std

Sample variance fig 1

\[ \text{Population Variance } \sigma^2 = \frac{\sum_{i=1}^{N} (x_{i} - \mu)^2}{N} \]
\[ \text{Sample Variance } S^{2} = S_{n-1}^{2} = \frac{ \sum_{i=1}^{n} (x_{i} - \bar{x} ) ^ {2} }{n-1} \]
\[ \text{Population Standard Deviation } \sigma = \sqrt{\sigma} = \sqrt{ \frac{ \sum_{i=1}^{N} (x_{i} - \mu)^{2} }{N} } \]
\[ \text{Sample Standard Deviation } S = \sqrt{S} = \sqrt{\frac{ \sum_{i=1}^{n} (x_{i} - \bar{x}) ^ {2} }{n-1} } \]
x = np.array([1.5,2.5,4,2,1,1])
print(f'mean of x {np.mean(x)}')
print(f'variance of x {np.var(x, ddof=1)}')
print(f'standard deviativion of x {np.sqrt(np.var(x, ddof=1))}')
print(f'standard deviativion of x {np.std(x, ddof=1)}')
mean of x 2.0
variance of x 1.3
standard deviativion of x 1.140175425099138
standard deviativion of x 1.140175425099138
n = x.shape[0]
x_bar = x.mean()
s_sqrt = np.sum((x - x_bar)**2)/(n-1)
s = np.sqrt(s_sqrt)
print(f'Variance {s_sqrt}')
print(f'Standard Deviation {s}')
Variance 1.3
Standard Deviation 1.140175425099138