"Matplotlib
is a comprehensive library for creating static, animated, and interactive visualizations in Python." There are several tutorials, where you can learn the usage. "matplotlib.pyplot
is a collection of functions that make matplotlib work like MATLAB. Each pyplot
function makes some change to a figure: e.g., creates a figure, creates a plotting area in a figure, plots some lines in a plotting area, decorates the plot with labels, etc."
import matplotlib.pyplot as plt
import random
list1 = range(5)
plt.plot(list1)
[<matplotlib.lines.Line2D at 0x7f49aac0fa90>]
list2 = [random.random() for _ in range(5)]
print(list2)
[0.24381195585535964, 0.33626227336549497, 0.9079249568014891, 0.43356144761342574, 0.28210875793957346]
plt.plot(list1, list2)
[<matplotlib.lines.Line2D at 0x7f49c6735fa0>]
plt.plot(list1, list2, 'gs') # try: 'r^' 'gs' 'k--' 'b:' 'ro-'
[<matplotlib.lines.Line2D at 0x7f49c6300280>]
plt.scatter(list1, list2)
<matplotlib.collections.PathCollection at 0x7f49c65d34c0>
plt.bar(list1, list2, .7)
<BarContainer object of 5 artists>
t = [i*0.01 for i in range(251)]
t2 = [(i*0.01)**2 for i in range(251)]
t3 = [(i*0.01)**3 for i in range(251)]
plt.plot(t, t2)
[<matplotlib.lines.Line2D at 0x7f49c64cd640>]
plt.plot(t, t2, 'r', t, t3, 'g')
[<matplotlib.lines.Line2D at 0x7f49c639c430>, <matplotlib.lines.Line2D at 0x7f49c639c460>]
plt.figure(figsize=(12, 3))
plt.subplot(131)
plt.bar(list1,list2)
plt.subplot(132)
plt.scatter(list1,list2)
plt.subplot(133)
plt.plot(list1,list2)
plt.suptitle('Functions')
plt.show()
plt.figure(figsize=(8, 12))
plt.subplot(311)
plt.bar(list1,list2)
plt.title("Bar")
plt.subplot(312)
plt.scatter(list1,list2)
plt.title("Scattered")
plt.subplot(313)
plt.plot(list1,list2)
plt.title("Plot")
plt.suptitle('Functions')
plt.tight_layout()
plt.show()
Exercise: Copy the next code into a file named 4test.py
and run it from a terminal with the command python3 4test.py 4
import matplotlib.pyplot as plt
import random
import sys
def my_plot():
"""
Plotting n random values.
n is read in from the terminal.
"""
n = int(sys.argv[1])
list1 = range(n)
list2 = [random.random() for _ in range(n)]
list3 = [i**2 for i in list2]
plt.subplot(311)
plt.bar(list1,list1)
plt.subplot(312)
plt.bar(list1,list2)
plt.subplot(313)
plt.bar(list1,list3)
plt.show()
def main():
my_plot()
if __name__ == "__main__":
main()
For calculating factorial, binomial coefficient, exponential function you may use the math
module:
math
module¶import math
math.factorial(5)
120
math.comb(25, 10)
3268760
This comb
function in math
module exists from Python 3.8 only, so refresh your Python if necessary, or calculate in an other way. If you have an older version (like the jupyter on our server) then you may use the next funcion instead comb
:
def comb(n, m):
return math.factorial(n)//math.factorial(m)//math.factorial(n-m)
comb(25, 10)
3268760
math.exp(1), math.exp(2) # the e^x function
(2.718281828459045, 7.38905609893065)
Exercise: Throw a die 10 times. What is the probability that you get 6 exactly 2 times. Calculate with the formula for the binomial distribution (the parameters will be $n=10$, $p=1/6$), and approximate it with Poisson-distribution, with the parameter $\lambda$, where $\lambda=n\cdot p$ is the expected value of the binomial distribution.
n = 10
p = 1/6
m = 2
l = n*p # lambda
binomial_dist = math.comb(n, m) * p**m * (1-p)**(n-m)
poison_dist = l**m * math.exp(-l) / math.factorial(m)
binomial_dist, poison_dist
(0.2907100492017224, 0.2623272261632803)
In this task you have to plot three diagrams in a figure. The first diagram shows the binomial distribution with paramteres $(n,p)$ (the title is "Binomial distribution"). The second diagram shows the result of the simulation of this distribution (the title is "Binomial distribution simulated with 1000 experiments"). The third picture shows the first $n+1$ columns of the diagram of the Poisson distribution with parameter $\lambda=n\cdot p$ which approximates our binomial distribution (the ttitle is "Binomial distribution approxiamted with Poisson-distribution").
The output of this program will look like this (the scale and color is not an essential part of the figure):
The inputs are $n$, $p$, $k$, where $n$ and $p$ are the parameters of the binomial distribution, and $k$ is the number of simulations. The program of Adrian Smith can be run by the next command:
python3 4AdrianSmith.py 12 0.3 1000
Do not forget to convert sys.argv[1]
,... into numbers (int
, float
).
Test your program with different parameters like 20 0.05 1000
, 40 0.02 1000
or 40 .5 1000
.
The simulation of this binomial distribution can be accomplished by executing $n$ times an event of probability $p$. The value of the random variable is $m$ if the event occurred $m$ times in the $n$ experiments. We repeat this $n$ experiment $k$ times so we get $k$ values of the random variable. The $m$-th column of the second diagram should be $i/k$ if the value of the random variable was $m$ exactly $i$ times from the $k$ values. (This part of the program can be written simply by loops using the random
function of the random
modul.)
The third diagram shows the first $n+1$ values of a random variable $X$ of Poisson distribution with parameter $\lambda=np$, where $$ \mathbb{P}(X=m)=\frac{\lambda^m}{m!}e^{-\lambda}, \quad m=0, 1, 2,\dots.$$
Please, use the filename convention (4AdrianSmith.py
) and the convention for the file structure. We have learnt it in the first lab, and an example can be seen above.
The aim of this exercise is to practice, simulate, approximate and better understand the binomial distribution.
Programming goal is to learn the command line running of programs, plotting diagrams as subfigures.