Week 5-6: Continuous random variables

Back to main page

Definition (cdf)

Let $X$ be any random variable (does not have to be discrete). Define the cdf of $X$ as before $$F(x)=P(X\leq x).$$

Definition (Continuous random variable)

A function $F:\mathbb{R}\to [0,1]$ is a cdf of some random variable, if and only if 1. $F$ is non-decreasing: $x\leq y\Rightarrow F(x)\leq F(y)$ 2. $\lim\limits_{x\to -\infty}F(x)=0$ 3. $\lim\limits_{x\to \infty}F(x)=1$ 4. $F$ is right continuous: for any $x\in \mathbb{R}$, $\lim\limits_{y\to x^+}f(y)=f(x).$

An example of a pdf is given by

$$f(x)=\frac{1}{\pi}\frac{\sin^2x}{ x^2}, \quad x\in\mathbb{R}.$$

This is indeed a pdf, because it is non-negative and, as it can be checked,

$$\int\limits_\mathbb{R}\frac{\sin^2x}{ x^2}=\pi.$$

Below we plot the pdf of this distribution.

In [1]:
# nbi:hide_in
import matplotlib.pyplot as plt
import numpy as np
from ipywidgets import interact, FloatSlider

x=0.5
def cdf_pdf(x):
    xdata = np.delete(np.linspace(-10, 10, 1000), [0])
    def pdf_func(x):
        y = np.divide(np.sin(x)**2,x**2)/np.pi
        return y


    plt.plot(xdata, pdf_func(xdata))
    xshade = xdata[xdata<=x]
    plt.fill_between(xshade, pdf_func(xshade), alpha=0.3)
    plt.scatter(x,0, s=30)
    plt.rcParams['figure.figsize'] = (8, 4)
    plt.axhline(y=0, color='k', linewidth=0.5)
    plt.xlim(-11,11)
    plt.ylim(-0.01,0.35)
    plt.xticks([x],["x={}".format(x)])
    plt.yticks(np.arange(0,1,2))
    plt.figtext(0.6,0.6, r"$f(x)=\frac{\sin^2x}{ \pi x^2}$", ha="left", va="top",
            backgroundcolor=(0, 0, 0, 0), fontsize="large")
    plt.box(on=None)
    plt.show();

# create interactive variables
x = FloatSlider(min=-10.0, max=10.0, step=0.1, value=0.6, readout_format='')

# display the interactive plot
interact(cdf_pdf, x=x);

Logistic function

$$F(X)=\frac{e^x}{1+e^x}\quad \Rightarrow \quad f(x)=\frac{e^x}{(1+e^x)^2}.$$
In [21]:
# nbi:hide_in
import matplotlib.pyplot as plt
import numpy as np
from ipywidgets import interact, FloatSlider
plt.rcParams['figure.figsize'] = (16, 5)


def logistic_cdf_pdf(x):
    xdata = np.linspace(-10, 10, 1000)
    def cdf_func(xdata):
        val = np.divide(np.exp(xdata), 1+np.exp(xdata))
        return val
    def pdf_func(xdata):
        val = np.divide(np.exp(xdata), (1+np.exp(xdata)**2))
        return val
    
    fig, [ax1, ax2] = plt.subplots(1, 2)
    
    ax1.plot(xdata, pdf_func(xdata))
    xshade = xdata[xdata<=x]
    ax1.fill_between(xshade, pdf_func(xshade), alpha=0.3)
    ax1.scatter(x,0, s=30)
    ax1.axhline(y=0, color='k', linewidth=0.5)
    ax1.set_xlim(-10, 10)
    ax1.set_ylim(-0.06,0.6)
    ax1.set_xticks([x])
    ax1.set_xticklabels(["x={}".format(x)])
    ax1.set_title("pdf")
    
    ax2.plot(xdata, cdf_func(xdata))
    ax2.vlines(x, 0, cdf_func(x), linestyle="dashed", alpha=0.4)
    ax2.scatter(x,0, s=30)
    ax2.axhline(y=0, color='k', linewidth=0.5)
    ax2.set_xlim(-10, 10)
    ax2.set_ylim(-0.1,1.1)
    ax2.set_xticks([x])
    ax2.set_xticklabels(["x={}".format(x)])
    ax2.set_title("cdf")
    
    plt.show();
    
# create interactive variables
x = FloatSlider(min=-10, max=10, step=0.1, value=0.6, readout_format='')

# display the interactive plot
interact(logistic_cdf_pdf, x=x);    
        
        
    
In [1]:
# nbi:hide_in
import matplotlib.pyplot as plt
import numpy as np
from ipywidgets import interact, FloatSlider
plt.rcParams['figure.figsize'] = (16, 5)

a=0
b=1

def uniform_cdf_pdf(x, a=a, b=b):
    xdata = np.linspace(a-1, b+1, 1000)
    def cdf_func(xdata):
        f0 = lambda y: (y-a)/(b-a)
        val = np.piecewise(xdata, [xdata<a, (xdata>=a) & (xdata<=b), xdata>b], [0, f0, 1])
        return val
    def pdf_func(y):
        val = np.piecewise(y, [y<a, (y>=a) & (y<=b), y>b], [0, 1, 0])
        return val
    
    fig, [ax2, ax1] = plt.subplots(1, 2)
    
    ax1.plot(xdata, pdf_func(xdata))
    xshade = xdata[xdata<=x]
    ax1.fill_between(xshade, pdf_func(xshade), alpha=0.3)
    ax1.scatter(x,0, s=30)
    ax1.axhline(y=0, color='k', linewidth=0.5)
    ax1.set_xlim(a-1, b+1)
    ax1.set_ylim(-0.01,1)
    ax1.set_xticks([a, x, b])
    ax1.set_xticklabels(["a={}".format(a), "x={}".format(x), "b={}".format(b)])
    ax1.set_frame_on(False)
    ax1.set_title("pdf")
    
    ax2.plot(xdata, cdf_func(xdata))
    ax2.vlines(x, 0, cdf_func(x), linestyle="dashed", alpha=0.4)
    ax2.scatter(x,0, s=30)
    ax2.axhline(y=0, color='k', linewidth=0.5)
    ax2.set_xlim(a-1, b+1)
    ax2.set_ylim(-0.01,1)
    ax2.set_xticks([a, x, b])
    ax2.set_xticklabels(["a={}".format(a), "x={}".format(x), "b={}".format(b)])
    ax2.set_frame_on(False)
    ax2.set_title("cdf")
    
    plt.show();
    
# create interactive variables
x = FloatSlider(min=-a, max=b, step=0.1, value=0.6, readout_format='')

# display the interactive plot
interact(uniform_cdf_pdf, x=x);    
        
        
    

The mean of the uniform distribution is

$$\mu=\frac{a+b}{2}.$$
In [3]:
# nbi:hide_in
import matplotlib.pyplot as plt
import numpy as np
from ipywidgets import interact, FloatSlider
plt.rcParams['figure.figsize'] = (12, 8)
import matplotlib as mpl
mpl.rcParams.update(mpl.rcParamsDefault)
lmbd = 0.8
x=1

xdata = np.linspace(-1, 10, 1000)
def pdf_func(xdata):
    f1 = lambda y: np.exp(-lmbd *(y + np.abs(y))/2)
    val = np.piecewise(xdata, [xdata<0, xdata>=0], [0, f1])
    return val


plt.plot(xdata, pdf_func(xdata))
xshade = xdata[xdata<=x]
plt.fill_between(xshade, pdf_func(xshade), alpha=0.2)
plt.xticks([x],["x={}".format(x)])
plt.axhline(y=0, color='k', linewidth=0.5)
plt.box(on=None)
plt.figtext(0.5,0.5, r" $\lambda=${}".format(lmbd), ha="left", va="top",
            backgroundcolor=(0.1, 0.1, 1, 0.15), fontsize="large")
plt.show();
    
In [4]:
# nbi:hide_in
import matplotlib.pyplot as plt
import numpy as np
from scipy.special import gamma 
from ipywidgets import interact, FloatSlider
plt.rcParams['figure.figsize'] = (12, 8)
import matplotlib as mpl
mpl.rcParams.update(mpl.rcParamsDefault)

Alpha = [1, 1.5,  2, 3]
Theta = [3.5, 3.5, 3.5, 3.5 ]

def pdf_func(xdata, alpha, theta):
    f1 = lambda y: np.power(y, alpha-1)*np.exp(-y/2)/(np.power(theta,alpha)*gamma(alpha))
    val = np.piecewise(xdata, [xdata<0, xdata>=0], [0, f1])
    return val

fix, ax = plt.subplots()

def pplot_gamma(theta, alpha, ax):
    xdata = np.linspace(-1, 20, 1000)
    ax.plot(xdata, pdf_func(xdata, alpha, theta), label=r"$\theta=${}, $\alpha=${}".format(theta, alpha))
    ax.axhline(y=0, color='k', linewidth=0.5)
    ax.set_frame_on(False)


for t,a in zip(Theta, Alpha):
    pplot_gamma(t, a, ax)
  
ax.set_title("pdf of Gamma distribution")
plt.legend()  
plt.show();
In [183]:
# nbi:hide_in
import matplotlib.pyplot as plt
import numpy as np
from scipy.special import gamma 
from ipywidgets import interact, FloatSlider
plt.rcParams['figure.figsize'] = (12, 8)
import matplotlib as mpl
mpl.rcParams.update(mpl.rcParamsDefault)

r_values = [2, 3, 4, 5, 10]

def pdf_func(xdata, alpha, theta):
    f1 = lambda y: np.power(y, alpha-1)*np.exp(-y/2)/(np.power(theta,alpha)*gamma(alpha))
    val = np.piecewise(xdata, [xdata<0, xdata>=0], [0, f1])
    return val

fix, ax = plt.subplots()

def pplot_chi(r):
    alpha = r/2
    xdata = np.linspace(-1, 30, 1000)
    ax.plot(xdata, pdf_func(xdata, alpha, theta=2), label="r={}".format(r))
    ax.axhline(y=0, color='k', linewidth=0.5)
    ax.set_frame_on(False)


for r in r_values:
    pplot_chi(r)
  
ax.set_title(r"pdf of $\chi^2$ distribution")
plt.legend()  
plt.show();
In [1]:
# nbi:hide_in
import matplotlib.pyplot as plt
import numpy as np
from scipy.special import gamma 
from ipywidgets import interact, FloatSlider
plt.rcParams['figure.figsize'] = (12, 8)
import matplotlib as mpl
mpl.rcParams.update(mpl.rcParamsDefault)

Mu = [0,  -15, 10]
Sigma = [1, 7, 5 ]

def pdf_func(xdata, mu, sigma):
    val = np.exp(-np.power(xdata-mu,2)/(2*sigma**2))/(sigma *np.sqrt(2*np.pi))
    return val

fix, ax = plt.subplots()

def pplot_gamma(mu, sigma, ax):
    xdata = np.linspace(-40, 40, 1000)
    ax.plot(xdata, pdf_func(xdata, mu, sigma), label=r"$\mu=${},   $\sigma=${}".format(mu, sigma))
    ax.axhline(y=0, color='k', linewidth=0.5)
    ax.set_frame_on(False)


for mu,sigma in zip(Mu, Sigma):
    pplot_gamma(mu, sigma, ax)
  
ax.set_title("pdf of normal distribution")
plt.xticks(Mu)
plt.legend()  
plt.show();
In [46]:
# nbi:hide_in
import matplotlib.pyplot as plt
import numpy as np
from scipy.special import gamma 
from ipywidgets import interact, FloatSlider
plt.rcParams['figure.figsize'] = (12, 8)
import matplotlib as mpl
mpl.rcParams.update(mpl.rcParamsDefault)

mu=0
sigma =1
alpha = 0.05
z  = 1.645

def pdf_func(xdata, mu, sigma):
    val = np.exp(-np.power(xdata-mu,2)/(2*sigma**2))/(sigma *np.sqrt(2*np.pi))
    return val

fix, ax = plt.subplots()

def pplot_gamma(mu, sigma, ax, z):
    xdata = np.linspace(-5, 5, 1000)
    xshade = xdata[xdata-mu/sigma>=-z]
    ax.fill_between(xshade, pdf_func(xshade, mu, sigma), alpha=0.3 )
    #xshade = xdata[xdata-mu/sigma<=-z]
    #ax.fill_between(xshade, pdf_func(xshade, mu, sigma), alpha=0.3, color="blue")
    ax.plot(xdata, pdf_func(xdata, mu, sigma), label=r"$\mu=${},   $\sigma=${}".format(mu, sigma))
    ax.axhline(y=0, color='k', linewidth=0.5)


pplot_gamma(mu, sigma, ax, z)
  
ax.set_title("pdf of standard normal distribution")
plt.xticks([-4,0,4])
plt.legend()  
plt.show();
In [2]:
# # nbi:hide_in


##### These are for the homeworkproblems #####


# import matplotlib.pyplot as plt
# import numpy as np
# from ipywidgets import interact, FloatSlider
# plt.rcParams['figure.figsize'] = (12, 8)
# import matplotlib as mpl
# mpl.rcParams.update(mpl.rcParamsDefault)
# lmbd = 0.8
# x=1

# xdata = np.linspace(-1, 10, 1000)
# def pdf_func(xdata):
#     f1 = lambda y: np.exp(-lmbd *(y + np.abs(y))/2)
#     val = np.piecewise(xdata, [xdata<0, xdata>=0], [0, f1])
#     return val


# plt.plot(xdata, pdf_func(xdata))
# xshade = xdata[xdata<=x]
# plt.fill_between(xshade, pdf_func(xshade), alpha=0.2)
# plt.xticks([x],["x={}".format(x)])
# plt.axhline(y=0, color='k', linewidth=0.5)
# plt.box(on=None)
# plt.figtext(0.5,0.5, r" $\lambda=${}".format(lmbd), ha="left", va="top",
#             backgroundcolor=(0.1, 0.1, 1, 0.15), fontsize="large")
# plt.show();
    
In [3]:
# # nbi:hide_in
# import matplotlib.pyplot as plt
# import numpy as np
# from ipywidgets import interact, FloatSlider
# plt.rcParams['figure.figsize'] = (12, 8)
# import matplotlib as mpl
# mpl.rcParams.update(mpl.rcParamsDefault)
# lmbd = 0.8
# x=1

# xdata = np.linspace(-1, 3, 1000)
# def pdf_func(xdata):
#     f1 = lambda y: np.power(y,4)/16
#     val = np.piecewise(xdata, [xdata<0, (xdata>=0) & (xdata<=2), xdata>=2], [0, f1, 1])
#     return val


# plt.plot(xdata, pdf_func(xdata))
# xshade = xdata[xdata<=x]
# plt.axhline(y=0, color='k', linewidth=0.5)
# plt.title("cdf")
# plt.show();
    
In [4]:
# # nbi:hide_in
# import matplotlib.pyplot as plt
# import numpy as np
# from ipywidgets import interact, FloatSlider
# plt.rcParams['figure.figsize'] = (12, 8)
# import matplotlib as mpl
# mpl.rcParams.update(mpl.rcParamsDefault)
# lmbd = 0.8
# x=1

# xdata = np.linspace(-1, 3, 1000)
# def pdf_func(xdata):
#     f1 = lambda y: np.power(y,3)/4
#     val = np.piecewise(xdata, [xdata<0, (xdata>=0) & (xdata<=2), xdata>=2], [0, f1, 0])
#     return val


# plt.plot(xdata, pdf_func(xdata))
# xshade = xdata[xdata<=x]
# plt.axhline(y=0, color='k', linewidth=0.5)
# plt.title("pdf")
# plt.show();
    
In [5]:
# # nbi:hide_in
# import matplotlib.pyplot as plt
# import numpy as np
# from ipywidgets import interact, FloatSlider
# plt.rcParams['figure.figsize'] = (12, 8)
# import matplotlib as mpl
# mpl.rcParams.update(mpl.rcParamsDefault)



# xdata = np.linspace(-3,3, 1000)
# def pdf_func(xdata):
#     f1 = lambda y: np.power(y,3)/16+0.5
#     val = np.piecewise(xdata, [xdata<-2, (xdata>=-2) & (xdata<=2), xdata>=2], [0, f1, 1])
#     return val


# plt.plot(xdata, pdf_func(xdata))
# xshade = xdata[xdata<=x]
# plt.axhline(y=0, color='k', linewidth=0.5)
# plt.title("cdf")
# plt.show();
In [6]:
# # nbi:hide_in
# import matplotlib.pyplot as plt
# import numpy as np
# from ipywidgets import interact, FloatSlider
# plt.rcParams['figure.figsize'] = (12, 8)
# import matplotlib as mpl
# mpl.rcParams.update(mpl.rcParamsDefault)
# lmbd = 0.8
# x=1

# xdata = np.linspace(-3, 3, 1000)
# def pdf_func(xdata):
#     f1 = lambda y: np.power(y,2)/16
#     val = np.piecewise(xdata, [xdata<-2, (xdata>=-2) & (xdata<=2), xdata>=2], [0, f1, 0])
#     return val


# plt.plot(xdata, pdf_func(xdata))
# xshade = xdata[xdata<=x]
# plt.axhline(y=0, color='k', linewidth=0.5)
# plt.title("pdf")
# plt.show();
    
In [7]:
# # nbi:hide_in
# import matplotlib.pyplot as plt
# import numpy as np
# from ipywidgets import interact, FloatSlider
# plt.rcParams['figure.figsize'] = (12, 8)
# import matplotlib as mpl
# mpl.rcParams.update(mpl.rcParamsDefault)
# lmbd = 0.8
# x=1

# xdata = np.linspace(-1, 2, 1001)
# def pdf_func(xdata):
#     f1 = lambda y: np.power(y,0.5)
#     val = np.piecewise(xdata, [xdata<0, (xdata>=0) & (xdata<=1), xdata>=1], [0, f1, 1])
#     return val


# plt.plot(xdata, pdf_func(xdata))
# xshade = xdata[xdata<=x]
# plt.axhline(y=0, color='k', linewidth=0.5)
# plt.title("cdf")
# plt.show();
    
In [8]:
# # nbi:hide_in
# import matplotlib.pyplot as plt
# import numpy as np
# from ipywidgets import interact, FloatSlider
# plt.rcParams['figure.figsize'] = (12, 8)
# import matplotlib as mpl
# mpl.rcParams.update(mpl.rcParamsDefault)
# lmbd = 0.8
# x=1

# xdata = np.linspace(-1, 2, 1001)
# def pdf_func(xdata):
#     f1 = lambda y: np.reciprocal(np.power(y,0.5))/2
#     val = np.piecewise(xdata, [xdata<0, (xdata>=0) & (xdata<=1), xdata>=1], [0, f1, 0])
#     return val


# plt.plot(xdata, pdf_func(xdata))
# xshade = xdata[xdata<=x]
# plt.axhline(y=0, color='k', linewidth=0.5)
# plt.title("pdf")
# plt.show();
In [10]:
# # nbi:hide_in
# import matplotlib.pyplot as plt
# import numpy as np
# from ipywidgets import interact, FloatSlider
# plt.rcParams['figure.figsize'] = (12, 8)
# import matplotlib as mpl
# mpl.rcParams.update(mpl.rcParamsDefault)
# lmbd = 0.8
# x=1

# xdata = np.linspace(-1, 3, 1001)
# def cdf_func(xdata):
#     f1 = lambda y: np.power(y,2)/4
#     f2 = lambda y: (y+1)/4
#     val = np.piecewise(xdata, [xdata<0, (xdata>=0) & (xdata<1),(xdata>=1) & (xdata<2), xdata>=2], [0, f1, f2, 1])
#     return val


# plt.plot(xdata, cdf_func(xdata))
# xshade = xdata[xdata<=x]
# plt.axhline(y=0, color='k', linewidth=0.5)
# plt.title("cdf")
# plt.show();
In [13]:
# nbi:hide_in
import matplotlib.pyplot as plt
import numpy as np
from ipywidgets import interact, FloatSlider
plt.rcParams['figure.figsize'] = (12, 8)
import matplotlib as mpl
mpl.rcParams.update(mpl.rcParamsDefault)
lmbd = 0.8
x=1

xdata = np.linspace(-2, 7, 1001)
def cdf_func1(xdata):
    f = lambda y: y
    val = np.piecewise(xdata, [xdata<0, (xdata>=0) & (xdata<1), xdata>=1], [0, f, 1])
    return val

def cdf_func2(xdata):
    val = np.piecewise(xdata, [xdata<2, (xdata>=2) & (xdata<4),(xdata>=4) & (xdata<6), xdata>=6], [0, 1/3, 2/3, 1])
    return val

plt.plot(xdata, cdf_func1(xdata)/2+cdf_func2(xdata)/2)
xshade = xdata[xdata<=x]
plt.axhline(y=0, color='k', linewidth=0.5)
plt.title("cdf")
plt.show();
In [11]:
# nbi:hide_in
import matplotlib.pyplot as plt
import numpy as np
from ipywidgets import interact, FloatSlider
plt.rcParams['figure.figsize'] = (12, 8)
import matplotlib as mpl
mpl.rcParams.update(mpl.rcParamsDefault)
lmbd = 0.8
x=1

xdata = np.linspace(-5, 5, 1001)
def cdf_func1(xdata):
    f = lambda y: np.exp(y)/(1+np.exp(y))
    return f(xdata)

def cdf_func2(xdata):
    val = np.piecewise(xdata, [xdata<0, xdata>=0], [0, 1])
    return val

plt.plot(xdata, cdf_func1(xdata)/2+cdf_func2(xdata)/2)
xshade = xdata[xdata<=x]
plt.axhline(y=0, color='k', linewidth=0.5)
plt.title("cdf of mixture")
plt.show();
In [13]:
# nbi:hide_in
import matplotlib.pyplot as plt
import numpy as np
from ipywidgets import interact, FloatSlider
plt.rcParams['figure.figsize'] = (12, 8)
import matplotlib as mpl
mpl.rcParams.update(mpl.rcParamsDefault)
lmbd = 0.8
x=1

xdata = np.linspace(-5, 5, 1001)
def cdf_func1(xdata):
    f = lambda y: np.exp(y)/(1+np.exp(y))
    return f(xdata)

def cdf_func2(xdata):
    val = np.piecewise(xdata, [xdata<0, xdata>=0], [0, 1])
    return val

plt.plot(xdata, cdf_func1(xdata))
xshade = xdata[xdata<=x]
plt.axhline(y=0, color='k', linewidth=0.5)
plt.title("cdf of continuous")
plt.show();
In [15]:
# nbi:hide_in
import matplotlib.pyplot as plt
import numpy as np
from ipywidgets import interact, FloatSlider
plt.rcParams['figure.figsize'] = (12, 8)
import matplotlib as mpl
mpl.rcParams.update(mpl.rcParamsDefault)
lmbd = 0.8
x=1

xdata = np.linspace(-5, 5, 1001)
def cdf_func1(xdata):
    f = lambda y: np.exp(y)/(1+np.exp(y))
    return f(xdata)

def cdf_func2(xdata):
    val = np.piecewise(xdata, [xdata<0, xdata>=0], [0, 1])
    return val

plt.plot(xdata,  cdf_func2(xdata)/2)
xshade = xdata[xdata<=x]
plt.axhline(y=0, color='k', linewidth=0.5)
plt.title("cdf of discrete")
plt.show();
In [45]:
# nbi:hide_in
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = (10, 5)

N=1000
num = 1000
a=2
b=4
intsize = 50
plot_width = 2

data =np.random.rand(N, num)*(b-a)+a
length=(b-a)/(intsize-1)
mu = (b+a)/2

def pdf_func(xdata, mu, sigma):
    val = np.exp(-np.power(xdata-mu,2)/(2*sigma**2))/(sigma *np.sqrt(2*np.pi))
    return val

def epmf(x, inter):
    epmf_values = np.zeros(intsize-1)
    for i in range(intsize-1): 
        length = inter[i+1]-inter[i]
        epmf_values[i] = np.sum((inter[i]<=x) & (x<inter[i+1]))/(x.size*length)
    return epmf_values 

def mean_hist(n):
    sigma = np.sqrt((b-a)**2/12)/np.sqrt(n)
    xvalues = np.linspace(a,b, 1000)
    plt.plot(xvalues, pdf_func(xvalues, mu, sigma), linewidth=2, color="red")
    x = np.sum(data[0:n,:], axis=0)/n
    inter = np.linspace(a,b,intsize)
    epmf_values = epmf(x, inter)
    plt.bar(inter[:intsize-1], epmf_values, width=length, 
            color='#039be5', edgecolor='black', linewidth=1, 
            align="edge", label="True histogran")
    plt.figtext(0.8,0.8, "n = {}".format(n), ha="left", va="top",
        backgroundcolor=(0.1, 0.1, 1, 0.15), fontsize="large")
    plt.show();

def mean_hist_std(n):
    sigma = np.sqrt((b-a)**2/12)/np.sqrt(n)
    xvalues = np.linspace(-3,3, 1000)
    plt.plot(xvalues, pdf_func(xvalues, 0, 1), linewidth=2, color="red")
    x = np.sum(data[0:n,:], axis=0)/n
    x = (x - mu)/sigma
    inter = np.linspace(-10,10,100)
#     inter = (inter - mu)/sigma
    length = inter[1]-inter[0]
    epmf_values = epmf(x, inter)
    print(x)
    plt.bar(inter[:intsize-1], epmf_values, width=length, 
            color='#039be5', edgecolor='black', linewidth=1, 
            align="edge", label="True histogran")
    plt.figtext(0.8,0.8, "n = {}".format(n), ha="left", va="top",
        backgroundcolor=(0.1, 0.1, 1, 0.15), fontsize="large")
    plt.xlim(-5, 5)
    plt.show();
In [46]:
mean_hist_std(100)
[ 7.07732629e-02 -1.07745892e-01 -4.90864550e-01  3.33684114e-01
 -6.35670876e-01  9.05525862e-02  4.97468299e-02  1.39725580e+00
 -1.22948822e+00  4.05072869e-01  9.21873153e-02 -8.37684610e-01
  1.18603825e+00  3.97054509e-01  2.50914097e+00 -1.29041834e+00
 -1.52857033e-01  2.21878861e-01 -9.93104100e-01  1.48011780e+00
 -1.73721739e+00  4.69496993e-01 -4.04414438e-01 -1.66013624e+00
 -1.62108914e-01 -7.98708794e-01  3.26109054e-01  9.38645023e-01
 -1.14852364e+00  1.41646368e+00  1.53890060e+00  5.21180529e-01
 -6.02021665e-01 -2.47624414e+00 -1.24557010e+00 -8.00116655e-01
  1.54221371e+00 -4.28920698e-01  1.74110793e+00 -2.75199121e-01
 -6.03358535e-02 -1.66501268e+00  1.54862189e+00  1.40361737e+00
 -1.35610953e+00 -1.82962430e-01 -6.21618058e-01  7.02490725e-02
 -7.32128067e-01 -2.60076471e-02 -1.14321241e+00  4.95956604e-01
 -2.66473627e-01 -8.97402028e-01 -1.82532838e-01 -4.45259140e-01
 -5.02927449e-01  9.13072318e-02  8.05841756e-01 -2.04984384e+00
  6.85395457e-01 -7.54190373e-02 -1.28100942e+00 -1.09816169e+00
  5.73051418e-01 -1.30776948e+00  1.03108295e+00  1.35367858e+00
 -1.77389110e+00 -7.49683084e-01  8.55435663e-01 -8.60343492e-01
  8.15656283e-01  7.74938314e-01 -3.54540683e-01 -1.18882061e+00
  7.57962102e-01 -1.37384398e+00  2.34152701e-01  1.48585634e+00
 -1.36213097e+00  6.95499462e-01  6.69341115e-01 -2.70481729e+00
  3.27392635e-02  8.84872113e-01 -9.58324219e-02  1.78701975e+00
  5.58502106e-01  5.09003493e-01 -7.25759660e-01 -5.15217430e-01
 -8.04497330e-01 -3.58827044e-01  1.73932301e+00 -1.40326112e-01
 -1.11674376e+00 -5.06940485e-01 -1.60590435e+00 -4.63293372e-01
 -5.76073114e-01  7.89677767e-01  4.31709969e-01 -1.99715319e+00
 -5.37797220e-01 -7.27853355e-01  5.42512576e-02  6.25539396e-01
  6.11260996e-01  8.23078647e-01 -7.13630237e-02  1.21218879e+00
  5.40417118e-01  9.59382805e-01  2.37187732e+00  1.51794390e+00
  8.10665322e-01 -1.51924736e+00 -6.03845696e-01 -3.42120456e-01
 -1.78568440e-01  3.88603552e-01  8.52742069e-02 -1.35724386e+00
  1.29156680e+00 -2.07264319e-01 -1.19226044e+00  1.11320043e+00
  5.79310779e-01  7.88002765e-01  2.01461689e+00  1.72785360e+00
 -4.43405211e-01 -9.94699208e-01  6.81605181e-01  8.50396267e-01
  1.01318763e+00  6.68754220e-02 -3.33212099e-02 -2.99660230e+00
  4.71619033e-01 -2.30507634e+00 -1.09249538e+00  8.77841468e-01
  4.07299945e-01 -6.00673599e-01  1.81904290e+00  9.28494931e-01
  5.33040111e-01  9.29297700e-01 -1.03604366e+00 -6.75261479e-02
  1.49649698e+00  1.76413231e+00 -1.75049763e+00 -8.51263725e-01
 -2.33905072e+00 -1.29070704e+00 -1.03069094e+00 -7.43059600e-02
 -8.95681064e-02  1.03046202e+00  8.50025618e-01 -7.73219163e-02
 -1.46046628e+00  1.71638596e+00  5.64475394e-01  5.26341421e-01
  8.95782016e-01  7.24992314e-01 -7.20500000e-02 -9.27644878e-01
  1.11278871e+00  7.93204133e-02 -9.78449025e-01  1.40795062e+00
 -7.31725910e-02 -1.39430138e+00  4.12167291e-01 -6.17054790e-01
  2.70849258e-02  7.93139960e-01 -2.03354389e+00  1.45126443e+00
  7.26346456e-01  1.10886094e+00  2.48190921e-01 -1.58197718e+00
 -5.89162575e-01  1.20679885e+00 -3.80798645e-01  1.01275077e+00
  1.93506193e-02 -7.35669850e-01  4.04807129e-01 -1.70290455e+00
  3.00826566e-02 -1.51533787e+00 -4.76193952e-01  4.83291436e-01
  1.13615061e+00 -1.54636283e+00 -1.24401705e+00  5.92194845e-01
 -6.39027124e-02  1.02288148e+00 -1.57356750e+00  5.20518148e-01
  3.56887063e-01  8.17551999e-02 -1.69883416e-01  2.36848409e-01
 -1.65070150e+00  7.23083785e-01  3.58544382e-01 -8.82088990e-01
 -1.41046157e+00  1.80184142e-01 -3.78385397e-01 -6.23047000e-01
  2.85678828e+00 -1.29349216e+00  8.83132545e-01 -2.05964874e+00
 -5.09571793e-01  2.06966539e-01 -6.39182709e-01  1.10290176e+00
  9.53600812e-01 -6.67526104e-01 -1.80705327e-01 -3.86016167e-01
 -8.09368918e-01  8.65541075e-01 -4.36019371e-01  3.21050107e-01
 -1.69649388e-01  7.76823694e-01  9.27528998e-02  9.79935765e-01
 -2.64817964e-01 -1.10617584e-01 -1.81507405e+00 -1.33049902e+00
  7.16765773e-02  6.43451328e-01  5.38287601e-01  7.11850942e-01
 -7.32021943e-01  1.02965695e+00  6.56027868e-01 -1.06788819e+00
 -1.52691860e+00 -1.43074313e+00  1.04817898e+00 -5.48584465e-01
 -5.77912191e-01 -1.45767854e-01 -3.14709022e-01  4.58951062e-01
 -1.08443497e+00 -1.54815811e+00  8.94837039e-01  2.71936966e-01
 -7.86246387e-01  2.07480905e-01  7.12681825e-01 -3.49134988e-01
  2.03749210e+00 -4.10306434e-01  1.75606602e-01 -1.71602353e+00
 -3.75836151e-01  6.90123384e-01  1.05037610e+00  1.19182218e+00
  8.43082300e-02  1.42930340e+00  5.35167876e-01  2.93973451e-01
 -4.43904614e-01  1.49905748e+00  8.18536429e-01  1.33969173e+00
  2.85887674e-02  1.18211421e+00 -1.71629474e+00  6.99765612e-01
 -4.10802008e-01 -8.56756829e-03  5.07990652e-01 -5.96770829e-01
  7.62562834e-01 -1.96983112e-01  1.79371133e+00 -1.99064959e+00
  5.36907192e-01 -3.08359748e-01 -5.16946769e-01  5.87541702e-01
 -6.37437743e-01 -8.74399717e-01  9.28795706e-01  6.99043611e-01
  7.69418261e-01  1.02587700e+00  7.05489343e-01  6.76793018e-01
  1.05042356e+00 -2.81937624e-01 -1.04752002e+00 -1.37210550e-01
 -1.14312418e+00 -1.43044331e+00 -6.67274336e-01 -8.19181536e-01
  5.25049123e-01  3.66381705e-01 -3.36494575e-01 -1.11112767e+00
  4.60303862e-01  1.00975168e+00 -4.12261471e-01 -1.32246781e-02
 -1.14370989e+00 -1.68010308e+00 -5.63481771e-01  2.56882427e+00
  9.61720594e-01  1.80966934e-01 -4.29893791e-01 -1.23478746e+00
 -3.77618816e-01 -1.05457814e+00  4.11184255e-01 -4.30967806e-01
 -1.41985374e-01 -3.24945905e-01 -1.33758533e-01 -1.41194047e+00
 -2.15520312e+00  1.64135723e+00  4.10471883e-02  9.40794981e-01
 -5.37576687e-01  1.25164358e+00  1.81157470e+00  5.42311625e-01
  1.05599177e+00 -1.81870939e+00  1.00194181e+00  9.69437982e-01
 -2.39154863e-01  4.04781872e-02 -8.49014900e-01  4.46041601e-01
 -2.96223323e-01  1.57014679e-01  1.59522670e+00 -1.32094440e+00
  1.65243459e+00 -2.01847335e+00  1.20151102e+00  1.08594969e+00
  5.08477339e-01  1.36363988e+00  2.12242539e+00 -1.40440830e+00
 -9.12659559e-01  1.17141563e+00 -1.83609824e-01 -1.56451540e-01
 -1.06687115e+00  2.80149795e-01 -9.07071096e-01 -1.62270412e-01
 -1.74224248e+00  9.99924911e-01 -2.59549450e+00  2.32757697e-01
  2.46229725e+00 -3.85944966e-01 -7.89634832e-01  3.41871792e-01
  1.34433944e-01 -5.05862179e-01 -1.77780648e-01 -1.16238185e+00
 -1.32605743e+00  5.56433813e-01 -5.85711643e-01 -1.30749832e-01
 -1.44254380e+00  1.12551845e+00 -1.30213258e-02 -3.60546636e-01
  6.12870111e-02 -4.41502320e-01 -9.19188314e-01 -6.40525836e-01
  1.06511059e+00  1.62104651e+00 -1.09190333e+00 -8.16205345e-01
 -4.33488714e-01  2.14746156e+00 -3.07629151e-01  3.19253504e-01
 -1.98497280e+00  6.56297533e-01 -4.61910807e-01 -4.46745737e-01
  7.40740958e-02  1.46487337e+00 -2.26648975e-02  1.33114747e+00
 -1.41476472e-01  7.65518303e-01 -2.31753956e+00  4.41478561e-01
 -7.16688842e-02  1.73234784e+00 -1.40345558e+00 -1.35597209e+00
 -1.17758611e+00  4.74171655e-01  4.16247821e-03  1.01660058e-01
  2.30042543e-01  7.63893084e-01  1.30458277e+00 -9.23627385e-01
  1.00603518e+00  4.07690230e-01  5.87621563e-01  8.83447740e-01
  1.32138941e+00 -1.91745755e+00  5.45933042e-01 -7.57944499e-01
  9.57537440e-01  1.07219333e+00 -1.45431563e+00 -1.07599753e+00
  1.17286433e+00 -3.72842169e-01  1.39858342e+00 -1.34851256e-01
  4.48296522e-01  1.02153953e-01 -4.47000358e-01  2.49317888e-01
 -1.31811039e-01  5.55054937e-01  6.21588027e-01 -3.81246188e-01
 -2.10200199e-02 -1.09265818e+00  3.17376512e-01 -8.10689920e-01
  8.19120896e-02 -1.19708051e+00 -1.63527054e+00 -5.04848621e-01
 -5.64280770e-01 -8.41915583e-01 -6.61537628e-01  8.58528535e-01
 -7.62097293e-01 -1.77771199e+00 -1.01960076e+00  1.02397585e+00
  8.88654636e-02  1.01922543e+00  3.32955906e-01 -5.15718917e-01
 -3.16198753e-01 -3.39321199e-01 -1.94593936e+00 -5.53721747e-01
 -6.34919623e-02  1.29764854e+00 -6.30160553e-01  6.51075059e-01
  3.84608188e-01 -5.61042933e-01 -5.33491032e-01 -5.87210797e-01
 -1.24107612e+00  2.28866364e+00  4.52396800e-01  7.51373821e-01
  7.70895786e-01 -1.86733059e-01  9.44596624e-01  1.05632642e+00
 -8.06481378e-01 -1.16786763e+00  8.80189289e-01 -1.30356488e-01
 -4.29964128e-01 -2.53683861e+00 -4.78238791e-01  3.76279445e-01
 -1.05988289e-01  8.07824235e-01 -1.09265251e+00  5.58901368e-01
 -1.11844268e-01 -2.16901497e+00  1.10060123e-01  8.78911839e-01
 -1.26647615e+00 -1.68893232e-03 -2.32471431e-02 -3.25355912e-03
 -1.10334416e+00  1.33016776e+00 -8.91831503e-01  6.21966280e-01
 -1.07857398e+00 -1.25787724e-01 -9.97901708e-01  6.47189483e-01
 -1.84359844e-01  5.49139202e-01 -4.58412991e-01  7.15817567e-01
  2.64015739e-01  1.18528843e+00  3.80473507e-01  1.05176073e+00
  9.96302934e-01 -1.62342390e+00  1.36509073e+00 -4.77762398e-01
  4.33746080e-01  1.21712458e+00 -6.33186032e-01  1.69796485e+00
 -2.66534877e+00 -5.97072644e-01  1.57140621e-01  2.84692990e-01
 -2.68913867e-01 -1.08377013e+00 -7.38752709e-01  3.83511663e-01
  9.83198731e-01 -4.35577629e-01  1.75042523e+00  2.64354582e+00
  7.39618341e-01 -1.17143287e+00 -8.89402439e-01  6.30924614e-01
 -6.99836873e-01  2.06810908e+00 -1.77981333e-01  1.39335760e-01
  9.10688959e-01 -1.15538949e+00 -2.23779729e-01 -7.77076832e-01
  8.53545484e-02 -1.59666471e+00  1.72175227e+00 -1.60343578e+00
  7.07694424e-01 -4.80248825e-01  6.74299077e-01  2.09767939e+00
  8.76259775e-01  1.24162503e+00 -1.08226761e+00 -8.64561108e-01
 -2.02730055e+00  5.72612429e-01 -6.07164856e-01  8.60241535e-01
  4.73106406e-01  2.36722652e-01 -3.60661557e-01  6.38222365e-01
  7.57266906e-01  9.79874660e-01 -2.19073816e+00 -1.09216586e+00
 -7.47034858e-01  6.32374840e-01  1.30283604e+00  7.44929169e-01
  9.88058523e-03 -1.16399068e+00  3.49052440e-01 -6.79671245e-01
  1.32709642e+00 -8.61887266e-03  2.72982624e-01  1.42681714e+00
  1.40795369e+00 -3.98358742e-01 -9.67219407e-01  1.76002748e+00
 -1.43494744e-01 -4.96522270e-01 -2.55723267e+00  2.57104869e-01
 -8.79925480e-01 -1.43573364e+00  2.94949728e-02 -9.11282283e-01
 -2.02504477e-01 -1.24200796e-01  1.25210952e+00 -9.84097674e-01
 -4.15474685e-01  1.84557484e+00 -1.33512841e-01 -9.74060680e-01
  1.05366924e+00 -2.90807226e+00 -1.18522859e+00 -9.46895461e-01
 -1.23028913e+00  6.60790698e-01 -3.22041006e-01  6.66135088e-01
 -2.25481978e-01  9.60305100e-01  1.39014659e-01  8.87833447e-01
  1.79196289e-01  1.69635751e+00  8.28204682e-01 -9.63120289e-01
 -1.76388037e-01  6.69034648e-02 -3.72626227e-01 -1.31688041e+00
  2.18171060e+00 -2.19015868e-01  4.95516987e-01  8.85237608e-01
  1.84597291e+00 -5.83019474e-01 -9.90982756e-01 -3.14004007e-02
 -2.64115865e+00 -4.83510239e-01 -4.76177070e-01 -5.72058743e-01
  2.91979062e-01  1.83223927e+00  1.33581911e+00 -6.06478080e-01
 -8.62073467e-01  8.91695176e-01 -4.64562734e-01  9.10535581e-01
 -5.90792344e-01 -3.07237365e-02  6.74582158e-01 -1.35062663e-05
  3.44213516e-01  7.48837496e-01  1.13116275e-01 -2.89371006e-01
 -1.61596302e-01 -1.04745114e+00  6.09068822e-01 -1.89412437e-01
  4.70987891e-01 -9.59356137e-01  1.95697726e+00 -8.02682646e-01
 -1.35237195e+00  1.03347604e+00 -1.76718576e+00  6.44656931e-01
  1.32855246e+00  4.93140757e-01  4.14041160e-01  9.85705958e-01
 -4.88825779e-01 -6.56301285e-01  4.30233316e-01  4.54260990e-01
 -4.64113674e-01 -5.78859521e-01 -5.46250414e-01  7.13527909e-01
 -9.19854372e-01 -9.33526697e-01  8.40709614e-01 -5.17375404e-01
 -1.47552499e+00  5.70511244e-01 -1.52473085e+00  1.96661209e+00
 -1.06314577e+00 -1.64769825e-01  1.33432973e+00  1.52294228e+00
  7.17083150e-01 -3.29956282e-01 -4.21784698e-01  7.89324513e-01
  1.18516016e+00 -5.59226909e-01  1.72337383e+00  2.20326087e-01
  4.17948181e-01  1.60650660e+00  4.10328206e-01 -1.82058223e+00
 -7.99030170e-01  1.16312102e+00 -4.61357779e-01  3.20091379e-01
  7.66874010e-01  1.74704955e+00  1.33928552e+00  4.09836199e-01
  2.77951635e-01 -1.00432825e+00  7.64806386e-01 -2.48449486e+00
  9.08946510e-01  1.50800216e+00 -6.85845585e-01 -9.55540522e-01
 -6.05172882e-01  6.56278639e-01  1.47400319e+00 -1.62322196e-01
 -5.05903697e-01  8.46190538e-01  4.38305197e-01  4.90500632e-01
 -1.54426525e-01 -8.78285168e-01  6.40287131e-01 -1.76367750e-02
 -2.36042006e+00 -5.10672046e-02  1.57230009e+00 -2.72472870e+00
 -5.90441520e-01 -1.56192345e+00  4.71696143e-01 -1.82231166e+00
 -3.15268551e-01 -5.58229768e-02 -2.59981834e-01  1.99043651e+00
  1.25272260e+00  6.32669308e-01  4.21284093e-01  6.32265808e-03
  3.76039775e-01  2.01974610e-01  7.41321559e-01  3.78584227e-01
  3.83251297e-01  3.90703976e-01  2.11369204e-01  1.24600139e+00
 -1.63539762e+00 -4.92909490e-01 -9.99952088e-01 -2.74108572e-01
 -3.06168888e-01  4.93890598e-01 -7.20023952e-01 -1.23845315e-02
 -1.81835302e+00  3.92172450e-01 -1.28347738e+00  7.79126724e-01
  3.76761482e-01 -4.04457803e-01  1.21274004e+00 -1.06344333e+00
 -3.20338734e-01  3.09205580e-01  1.69320331e-01 -1.77417879e+00
  1.52635661e+00  1.28464767e+00  9.47980529e-01  9.54499041e-01
 -5.17395079e-01  6.10335637e-01 -3.68245581e-01  6.11560165e-01
  8.35888060e-01  1.16129799e+00  1.55733101e+00 -1.51666021e+00
 -1.91487831e+00  1.60993900e+00  4.31471566e-01 -5.35433586e-01
  3.69334903e-01 -2.71901543e-01 -2.38172848e-02 -3.19222154e-01
  7.89979713e-01 -1.34622219e+00 -1.12357499e+00 -3.67864680e-01
 -1.11075770e-02  1.20633386e+00  1.26527650e+00 -1.45788294e+00
  1.52236876e+00  1.87591313e-01  1.87660005e+00  4.16721054e-01
 -1.50356854e-01 -3.01311755e-01 -5.33367459e-01  1.38879551e+00
 -6.63024508e-01 -7.85307666e-01  9.97575942e-01 -1.62500638e+00
 -2.56111129e-01 -8.03436161e-02  1.78669960e+00  2.47845634e-01
  1.51844222e+00  3.55118743e-01  1.18550967e+00  1.07541150e+00
 -3.82183692e-01  7.93776354e-01  8.43851776e-01  5.81754563e-01
  7.83870187e-01 -9.53019383e-01  1.43656076e+00 -2.55055583e+00
 -6.27015102e-01 -1.16408974e+00 -1.06541744e+00  1.52315478e+00
  1.38516234e+00  3.08380943e-01  1.30676081e-02 -5.68163169e-01
  4.93063150e-03  1.94606245e-01 -8.40616185e-01 -1.26646281e-01
 -3.32937566e-01  1.51202545e+00 -5.28864954e-01  2.10182251e+00
 -6.81539748e-01  5.13030978e-01  6.31001362e-01 -1.51247199e-02
 -1.10748695e+00  2.15389145e+00 -6.84987060e-01  1.08289333e+00
 -2.90346669e-01  6.81627207e-01  1.44249307e+00  1.22500372e+00
  6.68752388e-01  1.31309486e+00  6.24094416e-01  9.22509648e-01
 -6.88747066e-01  3.89414624e-01  7.09130071e-01 -3.40115329e-01
  1.50914048e+00  8.67679934e-01 -3.21247601e-01  1.62229287e-01
  1.06215050e-01  3.71175759e-01  1.98313229e-02 -6.10737606e-03
  1.44296526e-01 -7.07243759e-02 -1.10070898e+00  1.26401930e+00
  1.50153035e-01  7.01802394e-01  8.60629452e-01 -1.89818712e+00
  7.47823634e-01 -1.35038160e+00 -2.11770263e-01 -1.95260478e+00
  3.72265658e-01 -1.08299535e+00 -1.66012908e-01  9.98537085e-02
 -2.76670646e-01  1.55773111e+00  2.69623919e-01  8.10287186e-01
  9.54288671e-01  9.08802894e-01  1.87324254e+00  1.18472608e+00
 -5.81888385e-01  3.09683317e-01 -2.90336411e-01 -1.58095858e-01
 -1.04697888e+00  1.42356407e+00 -2.64079286e-01  4.99607638e-02
 -1.25314723e+00 -5.02839190e-03 -1.72604359e-01 -4.10591790e-01
 -7.15710246e-01  7.35213994e-01 -1.20741837e+00 -1.06598587e+00
  8.66007777e-01  1.04665282e+00  3.43643966e-01 -1.06042586e+00
 -8.81580840e-01  2.90883141e-01 -1.47908741e-01 -1.26600908e+00
 -1.16833471e+00  2.31619776e+00  1.52845388e+00 -5.26060569e-01
 -1.70305950e+00 -1.06099756e+00 -1.94494876e+00  3.83855524e-01
 -1.39753411e+00  6.92770765e-01 -1.89703197e+00 -1.11688718e+00
 -1.73545821e+00  8.67578829e-02 -2.45557132e-01 -3.84049407e-01
 -6.84457468e-01  3.48147172e-01 -1.81429782e-01  1.98576095e-01
  1.68567350e-01 -5.24573365e-01 -1.73113666e-01 -1.47462059e-01
 -1.49955514e+00 -7.94161932e-01  1.35738436e+00 -1.43995614e+00
 -1.95248608e-01  1.06455080e+00 -5.52946279e-01  6.11070341e-02
 -9.48283058e-02  1.61626131e+00 -1.61086816e+00  6.83524738e-02
 -7.63411812e-01 -7.75848182e-01  2.23134162e-01  7.53923587e-01
  8.99569476e-01 -1.14354559e+00  2.35574015e-01  3.00357450e-01
 -6.28131986e-01 -1.00521103e+00 -1.68286606e+00 -7.76278044e-01
 -3.12949741e-01  4.20845375e-01 -4.32053393e-01 -4.51291975e-01
  4.79755689e-01 -1.00680268e+00  2.60441365e-01  3.95931704e-01
  3.54148622e-01  3.70378415e-01 -8.67973020e-01 -3.40440671e-01
  1.68073007e+00 -5.89094096e-01 -4.43185213e-01  3.62317406e-01]
In [25]:
mean_hist_std(50)
In [ ]: