Content and Objective

  • Showing distribution when mapping a random variable
  • Uniform and gaussian random variables are simulated and mapped according to a pre-defined mapping
  • Pmf and cdf are shown

Import


In [1]:
# importing
import numpy as np

import matplotlib.pyplot as plt
import matplotlib

# showing figures inline
%matplotlib inline

In [2]:
# plotting options 
font = {'size'   : 20}
plt.rc('font', **font)
plt.rc('text', usetex=True)

matplotlib.rc('figure', figsize=(18, 6) )

Simulation of Uniform and Mapping


In [3]:
# number of samples to be used by simulation
N_seq = int( 1e4 )

# sample uniform distribution on [0,1]
X = np.random.rand( N_seq )        

# map samples
a = 2
b = 1
Y = a * X**3 + b    


# theoretical values
delta_t = .01
t = np.arange( 0, 1 + delta_t, delta_t )
f_theo_x = np.ones( len( t ) ) 

t_y = np.arange( b+delta_t, a + b + delta_t, delta_t )
f_theo_y = 1 / ( 3 * a ) * ( ( t_y - b ) / a )**(-2/3)

In [4]:
# plotting
plt.figure()

plt.subplot(121)

plt.plot( t, f_theo_x, linewidth=2.0, label='theo.')    
plt.hist( X, 50, density=1, label='sim.', alpha=0.75)    


plt.xlabel('$x, n$')
plt.ylabel('$f_X(x), H_{{{}}}(n)$'.format(N_seq)) 
plt.grid( True )
plt.legend( loc = 'upper right' )


plt.subplot(122)

plt.plot( t_y, f_theo_y, linewidth=2.0, label='theo.')    
plt.hist( Y, 100, density=1, label='sim.', alpha=0.75)    

plt.xlabel('$y, n$')
plt.ylabel('$f_Y(y), H_{{{}}}(n)$'.format(N_seq)) 
plt.grid( True )
plt.legend( loc = 'upper right' )
plt.xlim( ( b - .5 , b + a ) )


Out[4]:
(0.5, 3)

Simulation of Gaussian and Mapping


In [5]:
# number of samples to be used by simulation
N_seq = int( 1e4 )

# sample from gaussian distribution
X = np.random.randn( N_seq )        

# mapping
a = 2
b = 5.
Y = a * X**2 + b    


# theoretical values
delta_t = .01
t = np.arange( -5, 5, delta_t )
f_theo_x = 1/np.sqrt( 2* np.pi ) * np.exp( -t**2 / 2 ) 

t_y = np.arange( b + delta_t, 2 * 5 + b, delta_t )
f_theo_y = 1/ ( 2 * a ) /np.sqrt( ( t_y - b ) / a ) * ( 
    1 / np.sqrt( 2 * np.pi ) * np.exp( - ( np.sqrt( ( t_y - b ) / a ) )**2 / 2 ) + 
    1 / np.sqrt( 2 * np.pi ) * np.exp( - ( - np.sqrt( ( t_y - b ) / a ) )**2 / 2 ) )

In [6]:
# plotting
plt.figure()

plt.subplot(121)

plt.plot( t, f_theo_x, linewidth=2.0, label='theo.')    
plt.hist( X, 50, density=1, label='sim.', alpha=0.75)    

plt.xlabel('$x, n$')
plt.ylabel('$f_X(x), H_{{{}}}(n)$'.format(N_seq)) 
plt.grid( True )
plt.legend( loc = 'upper right' )


plt.subplot(122)

plt.plot( t_y, f_theo_y, linewidth=2.0, label='theo.')    
plt.hist( Y, 100, density=1, label='sim.', alpha=0.75)    

plt.xlabel('$y, n$')
plt.ylabel('$f_Y(y), H_{{{}}}(n)$'.format(N_seq)) 
plt.grid( True )
plt.legend( loc = 'upper right' )
plt.xlim( ( 0, b + 3 * a**2 ) )


Out[6]:
(0, 17.0)

In [ ]: