<jupyter_start><jupyter_text><NAME>, 2018320192  Basic probability theory<jupyter_code>import torch
from torch.distributions.multinomial import Multinomial

from IPython import display

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
probabilities = torch.ones([6], dtype = torch.float64) / 6
distribution = Multinomial(total_count = 1, probs = probabilities)
distribution.sample()
print((Multinomial(total_count = 10, probs = probabilities)).sample())
print((Multinomial(total_count = 10000, probs = probabilities)).sample())
num_rolls = 1000
rolls = Multinomial(total_count = 1, probs = probabilities)
counts = torch.zeros(6, num_rolls, dtype = torch.double)
totals = torch.zeros(6, dtype = torch.double)
for i in range(num_rolls):
   totals = totals + rolls.sample()
   counts[:, i] = totals
totals / num_rolls
counts
x = torch.arange(num_rolls, dtype = torch.double).reshape((1, num_rolls)) + 1
estimates = counts / x
print(estimates[:, 0])
print(estimates[:, 1])
print(estimates[:, 100])
display.set_matplotlib_formats('jpg')

plt.figure(figsize=(8, 6))
for i in range(6):
   plt.plot(estimates[i, :].numpy(), label = ("P(die=" + str(i) + ")"))

plt.axhline(y=0.16666, color='black', linestyle='dashed')
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>Sampling<jupyter_code>import random

for i in range(10):
   print(random.random())<jupyter_output>0.7774286595896818
0.30315020377594315
0.9012779998907656
0.8801049468798324
0.40633672424709144
0.2201065921135158
0.7920311401607091
0.6326210024203199
0.9744995434907887
0.44876163284275983<jupyter_text>Uniform Distribution<jupyter_code>for i in range(10):
   print(random.randint(1, 100))
import math

counts = np.zeros(100)
fig, axes = plt.subplots(2, 3, figsize=(11, 6), sharex=True)
axes = axes.reshape(6)

for i in range(1, 1000001):
   counts[random.randint(0, 99)] += 1
   if i in [10, 100, 1000, 10000, 100000, 1000000]:
      axes[int(math.log10(i)) - 1].bar(np.arange(1, 101), counts)
plt.show()<jupyter_output><empty_output><jupyter_text>The categorical distribution<jupyter_code>n = 1000000
y = np.random.uniform(0, 1, n)
x = np.arange(1, n + 1)

p0 = np.cumsum(y < 0.35) / x
p1 = np.cumsum(y >= 0.35) / x

plt.figure(figsize=(11, 6))
plt.semilogx(x, p0)
plt.semilogx(x, p1)
plt.show()
n = 1000000
y = np.random.uniform(0, 1, n)
x = np.arange(1, n + 1)

p0 = np.cumsum(y < 0.35) / x
p1 = np.cumsum(y >= 0.35) / x

plt.figure(figsize=(11, 6))
plt.semilogx(x, p0)
plt.semilogx(x, p1)
plt.show()<jupyter_output><empty_output><jupyter_text>The Normal distribution<jupyter_code>x = np.arange(-10, 10, 0.01)
p = (1/math.sqrt(2 * math.pi)) * np.exp(-0.5 * x**2)
plt.figure(figsize=(10, 5))
plt.plot(x, p)
plt.show()
tmp = np.random.uniform(size=(10000,10))
x = 1.0 * (tmp > 0.3) + 1.0 * (tmp > 0.8)
mean = 1 * 0.5 + 2 * 0.2
variance = 1 * 0.5 + 4 * 0.2 - mean**2
print('mean {}, variance{}'.format(mean, variance))

y = np.arange(1, 10001).reshape(10000,1)
z = np.cumsum(x,axis=0) / y

plt.figure(figsize=(10,5))
for i in range(10):
   plt.semilogx(y,z[:,i])

plt.semilogx(y,(variance**0.5) * np.power(y,-0.5) + mean,'r')
plt.semilogx(y,-(variance**0.5) * np.power(y, -0.5) + mean,'r')
plt.show()<jupyter_output>mean 0.9, variance0.49
{'fraction_non_alphanumeric': 0.17603458925262508, 'fraction_numerical': 0.12445954292773317, 'mean_word_length': 5.240847784200385, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '254308', 'license': 'Apache-2.0', 'n_tokens_mistral': 1650, 'n_tokens_neox': 1368, 'n_words': 271, 'path': 'homework/Ch4.4 Probability and Statistics.ipynb', 'repo_name': 'jasson31/d2l-pytorch', 'size': '3242'}