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) )
In [3]:
# number of realizations
N_trials = int( 1e4 )
# sample two uniform random variables and add them up
X = np.random.rand( N_trials )
Y = np.random.rand( N_trials )
Z = X + Y
# sample two gaussian random variables and add them up
X2 = np.random.randn( N_trials )
Y2 = np.random.randn( N_trials )
Z2 = X2 + Y2
In [4]:
# sample x regime
delta_x = 0.01
# pdf of addition of uniform distribution with according parameters
x = np.arange( 0, 2, delta_x )
f_theo_add_uniform = 1 - np.abs( x - 1 )
# pdf of addition of gaussian distribution with according parameters
x2 = np.arange( -5, 5, delta_x )
f_theo_add_gaussian = 1. / np.sqrt( 2* np.pi * 2 ) * np.exp( - x2**2 / 2 / 2 )
In [5]:
# subplot 121 for uniform
plt.subplot(121)
plt.plot( x, f_theo_add_uniform, linewidth=2.0, label='theo.')
plt.hist( Z, bins=50, density=1, label='sim.' )
plt.xlabel('$x, n$')
plt.ylabel('$f(x), H_{{{}}}(n)$'.format( N_trials))
plt.grid( True )
plt.legend( loc = 'upper right' )
plt.title('Addition of Uniform Distributions')
# subplot 122 for gaussian
plt.subplot(122)
plt.plot( x2, f_theo_add_gaussian, linewidth=2.0, label='theo.')
plt.hist( Z2, bins=50, density=1, label='sim.' )
plt.xlabel('$x, n$')
plt.ylabel('$f(x), H_{{{}}}(n)$'.format( N_trials))
plt.grid( True )
plt.legend( loc = 'upper right' )
plt.title('Addition of Gaussian Distributions')
Out[5]:
In [ ]: