542 KiB
542 KiB
In [1]:
%matplotlib inline
from math import erf, factorial
import torch
from IPython import display
from d2l import torch as d2l
torch.pi = torch.acos(torch.zeros(1)) * 2 # Define pi in torchIn [2]:
p = 0.3
d2l.set_figsize()
d2l.plt.stem([0, 1], [1 - p, p], use_line_collection=True)
d2l.plt.xlabel('x')
d2l.plt.ylabel('p.m.f.')
d2l.plt.show()In [3]:
x = torch.arange(-1, 2, 0.01)
def F(x):
return 0 if x < 0 else 1 if x > 1 else 1 - p
d2l.plot(x, torch.tensor([F(y) for y in x]), 'x', 'c.d.f.')In [4]:
1*(torch.rand(10, 10) < p)Out [4]:
tensor([[0, 1, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 1, 0, 0, 1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 1, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 1, 1, 0, 0],
[1, 1, 0, 0, 1, 1, 1, 1, 1, 0],
[1, 0, 0, 0, 1, 0, 1, 1, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[1, 0, 1, 1, 1, 1, 0, 1, 0, 0]])In [5]:
n = 5
d2l.plt.stem([i+1 for i in range(n)], n*[1 / n], use_line_collection=True)
d2l.plt.xlabel('x')
d2l.plt.ylabel('p.m.f.')
d2l.plt.show()In [6]:
x = torch.arange(-1, 6, 0.01)
def F(x):
return 0 if x < 1 else 1 if x > n else torch.floor(x) / n
d2l.plot(x, torch.tensor([F(y) for y in x]), 'x', 'c.d.f.')In [7]:
torch.randint(1, n, size=(10, 10))Out [7]:
tensor([[1, 4, 3, 2, 1, 1, 3, 1, 1, 4],
[4, 1, 1, 4, 4, 1, 4, 3, 2, 4],
[2, 4, 4, 1, 4, 2, 4, 3, 2, 1],
[1, 2, 3, 1, 1, 4, 2, 4, 1, 3],
[1, 2, 4, 1, 4, 3, 3, 2, 2, 1],
[1, 2, 2, 4, 1, 3, 2, 4, 2, 3],
[1, 2, 3, 4, 1, 3, 4, 1, 4, 3],
[3, 1, 1, 4, 4, 1, 3, 1, 1, 2],
[2, 2, 4, 3, 4, 2, 3, 4, 2, 4],
[1, 4, 3, 3, 2, 3, 3, 4, 1, 3]])In [8]:
a, b = 1, 3
x = torch.arange(0, 4, 0.01)
p = (x > a).type(torch.float32)*(x < b).type(torch.float32)/(b-a)
d2l.plot(x, p, 'x', 'p.d.f.')In [9]:
def F(x):
return 0 if x < a else 1 if x > b else (x - a) / (b - a)
d2l.plot(x, torch.tensor([F(y) for y in x]), 'x', 'c.d.f.')In [10]:
(b - a) * torch.rand(10, 10) + aOut [10]:
tensor([[2.4857, 2.2461, 1.6809, 2.7434, 2.7072, 2.6190, 1.4883, 1.2517, 1.3454,
2.4754],
[1.0974, 1.5680, 1.8788, 2.8231, 2.1695, 2.6461, 1.4914, 1.4887, 1.3860,
1.9090],
[1.3746, 1.7773, 1.2412, 1.1950, 2.7281, 2.8356, 1.2266, 2.4724, 2.4641,
2.8991],
[2.4018, 2.6727, 1.0308, 1.1951, 1.9390, 1.6486, 2.8314, 1.1025, 1.3354,
1.0130],
[1.1281, 1.8000, 2.3788, 2.6580, 1.6750, 2.2081, 1.2705, 1.0757, 2.3311,
2.6557],
[2.9912, 1.2263, 1.8115, 1.5940, 1.9321, 1.6469, 2.2990, 2.1473, 1.8165,
1.2806],
[1.1672, 1.1536, 1.9649, 2.1655, 1.7170, 1.0284, 1.3305, 2.1904, 1.4036,
2.1958],
[2.5891, 2.5840, 2.2679, 2.0687, 2.9249, 1.6741, 1.2238, 2.4463, 2.2235,
2.7038],
[1.8697, 2.4965, 1.5785, 2.7890, 2.3319, 2.1434, 2.3333, 1.0286, 1.9245,
1.7640],
[1.2504, 1.7558, 1.4322, 1.5226, 1.3380, 1.1388, 1.8707, 2.2330, 2.3818,
2.2087]])In [11]:
n, p = 10, 0.2
# Compute binomial coefficient
def binom(n, k):
comb = 1
for i in range(min(k, n - k)):
comb = comb * (n - i) // (i + 1)
return comb
pmf = torch.tensor([p**i * (1-p)**(n - i) * binom(n, i) for i in range(n + 1)])
d2l.plt.stem([i for i in range(n + 1)], pmf, use_line_collection=True)
d2l.plt.xlabel('x')
d2l.plt.ylabel('p.m.f.')
d2l.plt.show()In [12]:
x = torch.arange(-1, 11, 0.01)
cmf = torch.cumsum(pmf, dim=0)
def F(x):
return 0 if x < 0 else 1 if x > n else cmf[int(x)]
d2l.plot(x, torch.tensor([F(y) for y in x.tolist()]), 'x', 'c.d.f.')In [13]:
m = torch.distributions.binomial.Binomial(n, p)
m.sample(sample_shape=(10, 10))Out [13]:
tensor([[6., 3., 4., 3., 3., 1., 3., 3., 3., 3.],
[3., 1., 2., 2., 3., 2., 1., 3., 1., 4.],
[6., 1., 0., 3., 0., 3., 1., 0., 1., 1.],
[1., 2., 3., 1., 2., 2., 2., 2., 3., 2.],
[2., 2., 5., 4., 1., 3., 4., 3., 2., 0.],
[2., 0., 2., 2., 3., 1., 1., 4., 3., 1.],
[1., 1., 3., 2., 4., 2., 2., 2., 1., 0.],
[0., 3., 2., 1., 1., 3., 2., 1., 1., 3.],
[2., 3., 2., 3., 4., 3., 1., 2., 1., 2.],
[1., 2., 1., 1., 3., 2., 4., 3., 3., 2.]])In [14]:
lam = 5.0
xs = [i for i in range(20)]
pmf = torch.tensor([torch.exp(torch.tensor(-lam)) * lam**k
/ factorial(k) for k in xs])
d2l.plt.stem(xs, pmf, use_line_collection=True)
d2l.plt.xlabel('x')
d2l.plt.ylabel('p.m.f.')
d2l.plt.show()In [15]:
x = torch.arange(-1, 21, 0.01)
cmf = torch.cumsum(pmf, dim=0)
def F(x):
return 0 if x < 0 else 1 if x > n else cmf[int(x)]
d2l.plot(x, torch.tensor([F(y) for y in x.tolist()]), 'x', 'c.d.f.')In [16]:
m = torch.distributions.poisson.Poisson(lam)
m.sample((10, 10))Out [16]:
tensor([[ 1., 4., 6., 8., 4., 4., 4., 7., 6., 4.],
[ 3., 6., 7., 7., 5., 7., 7., 3., 5., 4.],
[ 4., 1., 3., 3., 10., 5., 5., 3., 7., 5.],
[ 4., 3., 4., 10., 8., 6., 4., 6., 5., 5.],
[ 5., 11., 1., 5., 7., 5., 2., 4., 3., 5.],
[ 6., 6., 4., 4., 3., 1., 5., 8., 4., 5.],
[ 2., 9., 7., 2., 6., 5., 2., 8., 6., 10.],
[ 1., 4., 3., 7., 3., 1., 7., 5., 3., 6.],
[ 5., 4., 6., 4., 9., 8., 3., 3., 1., 8.],
[ 3., 12., 9., 13., 2., 14., 3., 2., 0., 3.]])In [17]:
p = 0.2
ns = [1, 10, 100, 1000]
d2l.plt.figure(figsize=(10, 3))
for i in range(4):
n = ns[i]
pmf = torch.tensor([p**i * (1-p)**(n-i) * binom(n, i)
for i in range(n + 1)])
d2l.plt.subplot(1, 4, i + 1)
d2l.plt.stem([(i - n*p)/torch.sqrt(torch.tensor(n*p*(1 - p)))
for i in range(n + 1)], pmf,
use_line_collection=True)
d2l.plt.xlim([-4, 4])
d2l.plt.xlabel('x')
d2l.plt.ylabel('p.m.f.')
d2l.plt.title("n = {}".format(n))
d2l.plt.show()In [18]:
mu, sigma = 0, 1
x = torch.arange(-3, 3, 0.01)
p = 1 / torch.sqrt(2 * torch.pi * sigma**2) * torch.exp(
-(x - mu)**2 / (2 * sigma**2))
d2l.plot(x, p, 'x', 'p.d.f.')In [19]:
def phi(x):
return (1.0 + erf((x - mu) / (sigma * torch.sqrt(torch.tensor(2.))))) / 2.0
d2l.plot(x, torch.tensor([phi(y) for y in x.tolist()]), 'x', 'c.d.f.')In [20]:
torch.normal(mu, sigma, size=(10, 10))Out [20]:
tensor([[ 1.3588, 0.0473, -1.5805, -0.0108, 0.4253, 0.7924, -0.6547, 0.7313,
-0.3038, 1.1935],
[ 0.0089, 0.8951, 1.0055, 0.0956, -1.1109, -0.6342, 1.6772, 1.0314,
0.3819, -1.7822],
[-0.0604, -1.0318, 0.9113, 1.3118, -1.8370, -0.9023, 1.0365, 0.9052,
-0.6411, -0.8949],
[-0.1713, -0.2347, 0.0767, -0.6375, -0.4612, -1.6875, -0.1570, 1.0591,
0.8377, 0.5097],
[ 0.2762, -0.6213, -0.3422, 0.9449, -0.7544, -0.2150, 1.0240, 1.0253,
-0.9182, 1.1536],
[ 0.0614, 0.2758, -0.3610, -1.0577, -0.5513, -0.9158, 0.7539, 0.9204,
-0.5908, 0.9113],
[ 1.6190, -0.9213, -0.7944, -2.2621, 0.5826, -1.8287, 1.4097, -0.5744,
-0.0668, 1.2074],
[-0.0624, 0.1928, 1.3002, 0.6756, 1.1590, 1.0144, 1.1840, -0.5010,
0.6026, -0.7722],
[-2.0148, 0.6958, 0.9940, 0.8477, 1.0957, -0.5253, 0.2353, -0.2663,
1.2275, 0.5993],
[ 0.4651, -0.8218, -0.5441, -2.0338, -0.6930, -0.0674, -0.4448, -0.8397,
0.0360, -0.7089]])