forked from vongostev/202-Advanced-Python-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumpy_2.py
More file actions
37 lines (25 loc) · 794 Bytes
/
numpy_2.py
File metadata and controls
37 lines (25 loc) · 794 Bytes
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
import matplotlib.pyplot as plt
import numpy as np
import math
import scipy.special as SCS
def Poisson(Lambda, N):
assert Lambda >= 0
x = np.arange(0, N)
y = Lambda ** x * np.exp(-Lambda) / SCS.factorial(x)
return np.array([x, y])
def Moment(my_array, k):
assert isinstance(k, int)
assert isinstance(my_array, np.ndarray)
return (my_array[0] ** k * my_array[1]).sum()
def Average(my_array):
return Moment(my_array, 1)
def Dispersion(my_array):
my_array[0] = (my_array[0] - Average(my_array)) ** 2
return Average(my_array)
if __name__ == '__main__':
Lambda = 4
my_array = Poisson(Lambda, 40)
print(Average(my_array))
print(Dispersion(my_array))
plt.plot(my_array[1])
plt.show()