Practice Assignment: Understanding Distributions Through Sampling

This assignment is optional, and I encourage you to share your solutions with me and your peers in the discussion forums!

To complete this assignment, create a code cell that:

  • Creates a number of subplots using the pyplot subplots or matplotlib gridspec functionality.
  • Creates an animation, pulling between 100 and 1000 samples from each of the random variables (x1, x2, x3, x4) for each plot and plotting this as we did in the lecture on animation.
  • Bonus: Go above and beyond and "wow" your classmates (and me!) by looking into matplotlib widgets and adding a widget which allows for parameterization of the distributions behind the sampling animations.

Tips:

  • Before you start, think about the different ways you can create this visualization to be as interesting and effective as possible.
  • Take a look at the histograms below to get an idea of what the random variables look like, as well as their positioning with respect to one another. This is just a guide, so be creative in how you lay things out!
  • Try to keep the length of your animation reasonable (roughly between 10 and 30 seconds).

In [42]:
import matplotlib.pyplot as plt
import numpy as np

%matplotlib notebook

# generate 4 random variables from the random, gamma, exponential, and uniform distributions
x1 = np.random.normal(-2.5, 1, 10000)
x2 = np.random.gamma(2, 1.5, 10000)
x3 = np.random.exponential(2, 10000)+7
x4 = np.random.uniform(14,20, 10000)

# plot the histograms
plt.figure(figsize=(9,3))
plt.hist(x1, normed=True, bins=20, alpha=0.5)
plt.hist(x2, normed=True, bins=20, alpha=0.5)
plt.hist(x3, normed=True, bins=20, alpha=0.5)
plt.hist(x4, normed=True, bins=20, alpha=0.5);
plt.axis([-7,21,0,0.6])

plt.text(x1.mean()-1.5, 0.5, 'x1\nNormal')
plt.text(x2.mean()-1.5, 0.5, 'x2\nGamma')
plt.text(x3.mean()-1.5, 0.5, 'x3\nExponential')
plt.text(x4.mean()-1.5, 0.5, 'x4\nUniform')


Out[42]:
<matplotlib.text.Text at 0x7f3fc8c39780>

In [1]:
import matplotlib.animation as animation
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.gridspec as gridspec
import mpl_toolkits.axes_grid1.inset_locator as mpl_il
%matplotlib notebook

#fig= plt.figure(figsize=(10,9))
#plt.gca().set_title('Four Distributions')
#plt.subplots_adjust(hspace=0.4)
n=1000

x1 = np.random.normal(-2.5, 1, 10000)
x2 = np.random.gamma(2, 1.5,10000)
x3 = np.random.exponential(2, 10000)+7
x4 = np.random.uniform(14,20, 10000)
x = np.random.random(size=10000)
sample=len(list(x1))
bin_size=30
#n=10000
fig=plt.figure(figsize=(10,10))
g1=gridspec.GridSpec(4,4)
#fig,((ax1,ax2),(ax3,ax4),(ax5,ax6),(ax7,ax8))=plt.subplots(4,2)
#plt.tight_layout()
ax1=plt.subplot(g1[0,0:])
ax3=plt.subplot(g1[1,0:])
ax5=plt.subplot(g1[2,0:])
ax7=plt.subplot(g1[3,0:])
def br(curr):
    if curr==n:
        a.event_source.stop()
    #print(curr,n)
    ax1.cla()
    ax1.hist(x1[:curr],normed=True,bins=bin_size,alpha=0.5,orientation='horizontal')
    ax1.invert_xaxis()
    ax1.set_title("Normal Distribution")
    ax2.cla()
    ax2.scatter(x[:curr],x1[:curr])
    #ax2.invert_xaxis()
    ax3.cla()
    ax3.hist(x2[:curr],normed=True,bins=bin_size,bin_sizealpha=0.5,orientation='horizontal')
    ax3.invert_xaxis()
    ax4.cla()
    ax4.scatter(x[:curr],x2[:curr])
    ax5.cla()
    ax5.hist(x3[:curr],normed=True,bins=bin_size,alpha=0.5,orientation='horizontal')
    ax5.invert_xaxis()
    ax6.cla()
    ax6.scatter(x[:curr],x3[:curr])
    ax7.cla()
    ax7.hist(x4[:curr],normed=True,bins=bin_size,alpha=0.5,orientation='horizontal')
    ax7.invert_xaxis()
    ax8.cla()
    ax8.scatter(x[:curr],x4[:curr])  

def br2(curr):
    if curr==n:
        a.event_source.stop()
    ax1.cla()
    ax1.hist(x1[:curr],bins=bin_size,color='lightblue')
    ax1.set_title("Normal Distribution of sample {}".format(sample))
    ax2=mpl_il.inset_axes(ax1,width='30%',height='30%',loc=1)
    ax2.cla()
    ax2.scatter(x[:curr],x1[:curr],color='lightblue')
    ax2.axis([np.min(x),np.max(x),np.min(x1),np.max(x1)])
    ax3.cla()
    ax3.hist(x2[:curr],bins=bin_size,color='#F5C822')
    ax3.set_title("Gamma Distribution of sample {}".format(sample))
    ax4=mpl_il.inset_axes(ax3,width='30%',height='30%',loc=1)
    ax4.cla()
    ax4.scatter(x[:curr],x2[:curr],color='#F5C822')
    ax4.axis([np.min(x),np.max(x),np.min(x2),np.max(x2)])
    ax5.cla()
    ax5.hist(x3[:curr],bins=bin_size,color='#A2F522')
    ax5.set_title("Exponential Distribution of sample {}".format(sample))
    ax6=mpl_il.inset_axes(ax5,width='30%',height='30%',loc=1)
    ax6.cla()
    ax6.scatter(x[:curr],x3[:curr],color='#A2F522')
    ax6.axis([np.min(x),np.max(x),np.min(x3),np.max(x3)])
    ax7.cla()
    ax7.hist(x4[:curr],bins=bin_size,color='#22F2F5')
    ax7.set_title("Exponential Distribution of sample {}".format(sample))
    ax8=mpl_il.inset_axes(ax7,width='30%',height='30%',loc=1)
    ax8.cla()
    ax8.scatter(x[:curr],x4[:curr],color='#22F2F5')
    ax8.axis([np.min(x),np.max(x),np.min(x4),np.max(x4)])
#plt.figure()
    
plt.subplots_adjust(hspace=0.4)
a = animation.FuncAnimation(fig, br2, interval=10)



In [ ]: