forked from vongostev/202-Advanced-Python-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoisson_decimal.py
More file actions
57 lines (48 loc) · 1.12 KB
/
Poisson_decimal.py
File metadata and controls
57 lines (48 loc) · 1.12 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
import numpy as np
import matplotlib.pyplot as plt
from decimal import Decimal
def exp(x):
return x.exp()
@np.vectorize
def fact(n):
f = Decimal('1')
while n > Decimal('1'):
f = f * n
n = n - Decimal('1')
return f
def poisson(aver, N):
n = np.arange(N+1)
p = ((aver**n)*exp(-aver))/fact(n)
if aver >= 0:
return p
else:
return 0
def initial_moment(n, k):
arr = np.arange(np.size(n))
m = n*arr*k
if isinstance(k, int) == False or not(np.size(np.array(n)) > 1):
return 0
else:
return m.sum()
def average(n):
m = initial_moment(n, 1)
return m
def dispersion(n):
m = initial_moment(n, 2) - initial_moment(n, 1)**2
return m
def test(aver, N):
a = poisson(aver, N)
b = average(a)
c = dispersion(a)
plt.plot(a)
b1 = poisson(b, b)
plt.scatter(b, b1[np.size(b1)-1])
plt.show()
#test
a = poisson(Decimal('3'), Decimal('100'))
b = average(a)
c = dispersion(a)
print('a = ', Decimal('3'))
print('b = ', b)
print('c = ', c)
test(Decimal('3'), Decimal('100'))