import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from numpy.random import rand
plt.figure(figsize=(8,6))
npts = 10000
sums = np.sum(rand(2, npts), axis=0)
plt.hist(sums, bins=30, normed=True);
Note that as M increases, the distribution is getting wider in an absolute sense.
plt.figure(figsize=(15,3))
npts = 10000
for M in range(2, 7):
sums = np.sum(rand(M, npts), axis=0)
plt.subplot(1, 5, M-1)
plt.hist(sums, bins=30, normed=True)
plt.xlim(0, 6)
plt.figure(figsize=(15,3))
npts = 10000
for M in range(2, 7):
sums = np.sum(rand(M, npts), axis=0)
plt.subplot(1, 5, M-1)
plt.hist(sums, bins=30, normed=True, histtype='step')
plt.xlim(0, M)
u = rand(100000)
v = 2*u**(1/3)
plt.hist(v, normed=True, bins=50, fc='c')
x = np.linspace(0, 2)
y = 0.375*x**2
plt.plot(x, y, 'r', lw=2);
plt.figure(figsize=(10,5))
lam = 1/8267
tmax = 50000
t = np.linspace(0, tmax)
p = lam*np.exp(-lam*t)
plt.plot(t, p)
plt.xlim(0, tmax);
from numpy.random import randint
randint(6)
ngames = 10
winning = randint(1, 4, size=ngames)
print(winning)
chosen = np.ones(ngames, dtype=int)
print(chosen)
print(chosen == winning)
print(np.mean(chosen == winning))
switched = np.where(winning==chosen, randint(2,4, size=ngames), winning)
print(np.mean(switched == winning))
a = np.array([1, 3])
print(a)
print(np.mean(a))
print(np.amin(a))
c = np.array([[1,2],[3,4]])
np.amin(c)
np.amin(c, axis=1)
print(np.mean(c, axis=0))
from numpy.random import randn
mu = 0.0
sigma = 0.05
tmax = 366
s = 1
prices = np.empty(tmax)
prices[0] = s
for t in range(1, tmax):
s *= 1 + mu + sigma*randn()
prices[t] = s
plt.plot(prices);