Physics 310¶

Class 2¶

Tuesday January 23, 2024¶

Goals:

  • Introduce tools and functions for plotting
  • Introduce tools for generating random numbers and histograms
  • Experiment with PDFs (probability distribution functions)
In [3]:
## Import relevant packages
import numpy as np
import matplotlib.pyplot as plt

from scipy import stats

The linspace function¶

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

In [ ]:
 
In [ ]:
 

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

  • What do the functions take as input variables
  • Do the functions have any optional variables? How do you know
  • Use the function to generate an array of 10 random numbers
In [18]:
#?np.random.random_sample

Gaussian Distribution¶

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)
In [27]:
# Sampling from normal distribution
n = 1000
mean = 10.
sigma = 2.
ynorm = stats.norm.rvs(mean, sigma, size=n)  
In [ ]:
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")
In [ ]:
 

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?

In [ ]:
 
In [ ]:
 

Using other distributions¶

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.

In [ ]: