Plotting in Python

"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."

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:

Using the math module

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:

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.

4 Binomial distribution (deadline: 2021-10-09 20:00)

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):

title

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