## Import relevant packages
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
Often, we will fine that we need to generate a regularly spaced array of numbers, such as when creating an x-axis, or a set of independent variables
The np.linspace
is really valuable in this regard. What does it do? Look up its syntax by running the following command
?np.linspace
Run the following commands
np.linspace(0,30,10)
and note what each varible does
Two possible functions that we can use for geneerating random numbers are
np.random.random.rand_int
and
np.random.random_sample
Look up the help file to determine the syntax of these two functions
#?np.random.random_sample
This is given by
$p(x) = \frac{1}{\sigma \sqrt{2\pi}}exp\left[-\frac{(x-\bar{x})^2}{2\sigma^2}\right]$
The snippet of code below generates 10 samples from a normal distribution
# Sampling from normal distribution
n = 10
mean = 10.
sigma = 2.
stats.norm.rvs(mean, sigma, size=n)
# Sampling from normal distribution
n = 1000
mean = 10.
sigma = 2.
ynorm = stats.norm.rvs(mean, sigma, size=n)
x = np.linspace(0,20,200) # make an array of 200 evenly spaced values from 0 to 20
y = stats.norm.pdf(x, mean, sigma) # determine the value of the pdf at each of the points in 'x'
plt.title("pdf of normal distribution")
plt.xlabel("$x$")
plt.ylabel("$p(x)$")
plt.grid()
plt.plot(x, y);
Consider the following snippet of code
n = 1000
mean = 10.
sigma = 2.
ynorm = stats.norm.rvs(mean, sigma, size=n)
Before runnint it, describe what it will do.
Now generate a histogram of using:
plt.hist(ynorm)
plt.xlabel("value")
plt.ylabel("occurences")
plt.title("Histogram; equal sized bins")
Now the plt.hist function has a number of optional variables. For example, you can normalize it using density = True, and you can change the relative width of the cells by using rwidth = ...
Try this snippet of code, and think about what each line an each optional variable does
plt.hist(ynorm, density = True,rwidth=0.8,label = 'samples')
plt.plot(x, y, label = 'pdf');
plt.legend()
How would you add labels on the x and y axes?
The stats module implements dozens of other distributions. For example, to generate random values from a binomial distribution, you may use
stats.binom.rvs(n,p,size = 100)
where n
and p
are the two required variables for the binomial distribution.
stats.poisson.rvs(mean, size=100)
generates a sample of 100 random variables form the poisson distribution. Note that the poisson distribution is a function of a single variable. Namely the mean number of counts.
Experiment with generating random numbers and histograms of these distributions.