import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
from scipy import stats # has lots of distribution functions in it
import numpy.random as random # contains uniform distribution random()
# and randrange() for integers
from scipy import stats # has lots of distribution functions in it
yvec = np.zeros(0) #empty array to hold randomly generated x values
# generate 1000 randomly generated x values and stick them in xvec
for i in range(2000):
y = random.random() # generate a random number y in [0,1), uniformly distributed
yvec = np.append(yvec,y) # append it to an array
plt.plot(yvec,'b.')
n,bin_edges,patches = plt.hist(yvec,ec='black') # plot a histogram
bin_edges # contains right edge also, left edge is not exactly 0, right edge not exactly 1
n,bin_edges,patches = plt.hist(yvec,bins=10,\
density=True,ec='black')
#normalize, 10 bins
len(bin_edges)
sum(n) # this was returned by plt.hist
# evenly spaced bins
bin_edges[1]-bin_edges[0]
sum(n)*(bin_edges[1]-bin_edges[0]) # should give 1 as is normalized
#We can set the bin edges to what we want exactly
mybins = np.linspace(0,1,11)
mybins
n,bin_edges,patches = plt.hist(yvec,bins=mybins,\
density=True,ec='black')
bin_edges
# integers
from random import randrange,seed
for i in range(10):
print(randrange(10))
# another set of integers [0,10), not the same as above
for i in range(10):
print( randrange(10))
seed(10001)
for i in range(10):
print( randrange(10))
seed(10001) # the same set of integers as before, but I comment this out it is not the same
for i in range(10):
print( randrange(10))
https://docs.scipy.org/doc/scipy/reference/stats.html
Has many handy distributions and routines for using them
# From a uniform distribution we generate a
# vector of random vectors all at once with
yvec2 = stats.uniform.rvs(size=2000) #rvs = random variable sequence
plt.plot(yvec2,'g.')
# stats package, normal distn
r = stats.norm.rvs(size=200)# random variable sequence of size 200, should be fast
print(np.mean(r),np.std(r)) # as we expect?
plt.plot(r,'r.') # the points cluster around y=0
x = stats.norm.rvs(size=10000) # normal distribution
x_shift = stats.norm.rvs(loc=2,scale=0.2,size=10000) # normal distribution shifted, scaled
#rv = random variable
j=plt.hist(x,bins=100,density=True, alpha=0.5)
j=plt.hist(x_shift,bins=100,density=True, alpha=0.5)
plt.hist(x,bins=50,density=True, alpha=0.3, color='orange')
plt.hist(x_shift,bins=50,density=True, alpha=0.3,color='blue')
xarr = np.linspace(-4,4,200)
# probability distributions in stats come with probability density functions
y =stats.norm.pdf(xarr) # the distribution function!
plt.plot(xarr,y,color='orange')
y_shift =stats.norm.pdf(xarr,loc=2,scale=0.2)
plt.plot(xarr,y_shift,color='blue')
plt.xlabel('x',fontsize=18)
j=plt.ylabel('p(x)',fontsize=18)
# compute the mean and standard deviation from
# the probability distributions
print(stats.norm.mean())
print(stats.norm.std())
Mention $$x_{n+1} = (ax_n + b) ~ \texttt{mod} ~ c$$
Mention Mersenne twister https://en.wikipedia.org/wiki/Mersenne_Twister
Show ps6 and discuss inverse method for sampling random variables from a desired PDF
What is a Markov chain? https://en.wikipedia.org/wiki/Markov_process
Show that sum of two random variables has distn that is a convolution.
Show that sum of two random variables has mean sum of means and variance sum of variances.
Mention central limit theorem https://en.wikipedia.org/wiki/Central_limit_theorem
Discuss diffusion