• Active Topics 

Visualizing random samples from a normal (Gaussian) distribution

Post Reply
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5214
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#1

The normal (Gaussian) distribution occurs often in nature, especially as the sample becomes large.

The probability density for the Gaussian distribution is

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

where \(\mu\) is the mean and \(\sigma\) the standard deviation. The square of the standard deviation, \(\sigma^2\), is called the variance (see variance).

The probability density function of the normal distribution, is often called the bell curve because of its characteristic shape:
Image

The function has its peak at the mean, and its "spread" increases with the standard deviation.

Below is the Python program that visualizes the normal distribution from 1200 random samples, with \(\mu = 0, \ \sigma = 0.1\) and outputs the normal distribution curve including a histogram (see diagram below):
  1. """Visualize normal distribution curve, including histogram"""
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. import pylab
  5.  
  6. mu, sigma = 0, 0.1 # mean and standard deviation
  7. gauss = np.random.normal(mu, sigma, 1200) #Generate 1200 random samples
  8.  
  9. count, x, ignored = plt.hist(gauss, 60, normed=True) # x is the number of bins
  10. plt.plot(x, 1/(sigma * np.sqrt(2 * np.pi)) * np.exp( - (x - mu)**2 / (2 * sigma**2) ), linewidth=2, color='magenta', label = "mu = 0.0, sigma = 0.1")
  11. plt.legend(loc = 1 )
  12. plt.title("Normal Distribution")
  13. pylab.savefig('Norm_Distr', bbox_inches='tight') #Save the plot
  14. plt.show()

Image
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5214
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#2

PDF (probability density function) for the normal distribution:

Image

The red curve is the standard normal distribution.

What does the variance signify, from the graphs?
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5214
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#3

Normal distribution curve for 786432 random samples, 100 bins:

Image

This means as sample becomes larger and larger, distribution behaves more normally!
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5214
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#4

More examples:

Image

Image
Attachments
spectral_index_gauss_distr.png
(33.18 KiB) Not downloaded yet
spectral_index_gauss_distr.png
(33.18 KiB) Not downloaded yet
Bell_curve.png
(22.13 KiB) Not downloaded yet
Bell_curve.png
(22.13 KiB) Not downloaded yet
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5214
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#5

Here are a further examples of visualizing the Normal/Gaussian distributions using Python (Matplotlib and Numpy), this time is not by using random samples but from Gaussian distribution formula.

Example code1:
  1. import math
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4.  
  5. def normal(mu,sigma):
  6.     def f(x):
  7.         z = 1.0*(x-mu)/sigma
  8.         e = math.e**(-0.5*z**2)
  9.         C = math.sqrt(2*math.pi)*sigma
  10.         return 1.0*e/C
  11.     return f
  12.  
  13. X = 2
  14. dx = 0.1
  15. R = np.arange(-X,X+dx,dx)
  16.  
  17. L = list()
  18. sdL = (0.5,1,2,3)
  19. for sd in sdL:
  20.     f = normal(mu=0,sigma=sd)
  21.     L.append([f(x) for x in R])
  22. colors = ['purple','b','r']
  23.  
  24. for c,P in zip(colors,L):
  25.     plt.plot(R,P,zorder=1,color='0.2',lw=1.5)
  26.     plt.scatter(R,P,zorder=2,s=50,color=c)
  27.    
  28. ax = plt.axes()
  29. ax.set_xlim(-2.0,2.0)
  30. #You can set y limits as well
  31. plt.title('Normal Distribution')
  32. plt.savefig('Normal_distribution_example.png')
  33. plt.show()

Output:
Image
Example code 2:
  1. import numpy as np
  2. from scipy.stats import norm
  3. from matplotlib import pyplot as plt
  4.  
  5. # You must have LaTeX installed on your system, otherwise you will get an error
  6. # You can set usetex to False.
  7. from astroML.plotting import setup_text_plots
  8. setup_text_plots(fontsize=8, usetex=True)
  9.  
  10. #Define the distributions to be plotted
  11. sigma_values = [0.5, 1.0, 2.0]
  12. linestyles = ['-', '--', ':']
  13. colors = ['r','g','purple']
  14. mu = 0
  15. x = np.linspace(-10, 10, 1000)
  16.  
  17. #Plot the distributions
  18. fig, ax = plt.subplots(figsize=(5, 3.75))
  19.  
  20. for sigma, ls, c in zip(sigma_values, linestyles, colors):
  21.     # Generate a Gaussian / Normal distribution
  22.     dist = norm(mu, sigma)
  23.  
  24.     plt.plot(x, dist.pdf(x), ls=ls, color= c,lw=2,
  25.              label=r'$\mu=%i,\ \sigma=%.1f$' % (mu, sigma))
  26.  
  27. plt.xlim(-5, 5)
  28. plt.ylim(0, 0.85)
  29.  
  30. plt.xlabel('$x$')
  31. plt.ylabel(r'$p(x|\mu,\sigma)$')
  32. plt.title('Gaussian Distribution')
  33. plt.savefig('Gauss_distr_example.png')
  34. plt.legend()
  35. plt.show()

Output:
gauss_distr_example.png
or more compact plot
Gauss_distr_example.png
Gauss_distr_example.png (21.35 KiB) Viewed 4837 times
Gauss_distr_example.png
Gauss_distr_example.png (21.35 KiB) Viewed 4837 times
Attachments
Normal_distribution_example.png
(43.12 KiB) Not downloaded yet
Normal_distribution_example.png
(43.12 KiB) Not downloaded yet
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
Post Reply
  • Similar Topics
    Replies
    Views
    Last post

Return to “Statistics and Probability”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 0 guests