-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab1.py
More file actions
78 lines (69 loc) · 2.87 KB
/
Copy pathlab1.py
File metadata and controls
78 lines (69 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import numpy as np
import math
import matplotlib.pyplot as plt
from scipy.special import factorial
countOfData = [10, 50, 1000]
plt.suptitle('Normal Distribution')
mu, sigma = 0, 1
for i in range(len(countOfData)):
plt.subplot(1, 3, i + 1)
N = np.random.normal(mu, sigma, countOfData[i])
count, bins, ignored = plt.hist(N, 30, normed=True, edgecolor='black')
plt.plot(bins, 1 / np.sqrt(2 * np.pi) * np.exp(-(bins ** 2) / 2), linewidth=2, color='r')
plt.title(r'Normal Distribution: $\mu=0$, $\sigma=1$, N=%i' % countOfData[i])
plt.xlabel('NormalNumbers')
plt.ylabel('Density')
plt.subplots_adjust(wspace=0.5)
plt.show()
plt.suptitle('Laplace Distribution')
mu, sigma = 0, math.sqrt(2)
for i in range(len(countOfData)):
plt.subplot(1, 3, i + 1)
L = np.random.laplace(mu, sigma, countOfData[i])
count, bins, ignored = plt.hist(L, 30, normed=True, edgecolor='black')
plt.plot(bins, 1 / np.sqrt(2) * np.exp(-np.sqrt(2) * np.fabs(bins)), linewidth=2, color='r')
plt.title(r'Laplace Distribution: $\mu=0$, $\sigma=1.41$, N=%i' % countOfData[i])
plt.xlabel('LaplaceNumbers')
plt.ylabel('Density')
plt.subplots_adjust(wspace=0.5)
plt.show()
plt.suptitle('Poisson Distribution')
base = 10
for i in range(len(countOfData)):
plt.subplot(1, 3, i + 1)
P = np.random.poisson(10, countOfData[i])
count, bins, ignored = plt.hist(P, 30, normed=True, edgecolor='black')
plt.plot(bins, np.exp(-10) * np.power(10, bins) / factorial(bins), linewidth=2, color='r')
plt.title(r'Poisson Distribution: $base=10$, N=%i' % countOfData[i])
plt.xlabel('PoissonNumbers')
plt.ylabel('Density')
plt.subplots_adjust(wspace=0.5)
plt.show()
plt.suptitle('Cauchy Distribution')
for i in range(len(countOfData)):
plt.subplot(1, 3, i + 1)
C = np.random.standard_cauchy(countOfData[i])
count, bins, ignored = plt.hist(C, 30, normed=True, edgecolor='black')
plt.plot(bins, 1 / (np.pi * (bins ** 2 + 1)), linewidth=2, color='r')
plt.title(r'Standard Cauchy Distribution: N=%i' % countOfData[i])
plt.xlabel('CauchyNumbers')
plt.ylabel('Density')
plt.subplots_adjust(wspace=0.5)
plt.show()
plt.suptitle('Uniform Distribution')
a = -np.sqrt(3)
b = np.sqrt(3)
for i in range(len(countOfData)):
plt.subplot(1, 3, i + 1)
U = np.random.uniform(-np.sqrt(3), np.sqrt(3), countOfData[i])
count, bins, ignored = plt.hist(U, 30, normed=True, edgecolor='black')
ar = np.arange(-3., 3., 0.01)
listU = []
for elem in ar:
listU.append(1 / (2 * np.sqrt(3))) if np.fabs(elem) <= np.sqrt(3) else listU.append(0)
plt.plot(ar, listU, linewidth=2, color='r')
plt.title(r'Uniform Distribution: $a=-1.73$, $b=1.73$, N=%i' % countOfData[i])
plt.xlabel('UniformNumbers')
plt.ylabel('Density')
plt.subplots_adjust(wspace=0.9)
plt.show()