forked from vongostev/202-Advanced-Python-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecimal_numpy.py
More file actions
76 lines (63 loc) · 2.02 KB
/
decimal_numpy.py
File metadata and controls
76 lines (63 loc) · 2.02 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
import numpy as np
import scipy.special as s
import matplotlib.pyplot as plt
from decimal import Decimal, getcontext
getcontext().prec = 8
def toDecimalArray(arr):
return np.asarray([Decimal(x) for x in arr])
def Poisson(L_, n):
if L_ < 0:
raise Exception('L must be non-negative')
L = Decimal(L_)
L_pow = toDecimalArray(np.power(L, n))
# print(L_pow)
n_fact = toDecimalArray(s.factorial(np.array(n, dtype=np.int64)))
# print(n_fact)
exp_pow = Decimal(-L).exp()
# print(exp_pow)
P = (L_pow * exp_pow) / n_fact
return P
def Initial_moment(L, k, n, p):
if type(n) is not np.ndarray or type(k) is not int:
raise Exception('n must be array and k must be int')
nk = np.sum(toDecimalArray(np.power(n, k)) * p)
return Decimal(nk)
def Average(L, n, p):
average = Initial_moment(L, 1, n, p)
return Decimal(average)
def Variance(L, n, p):
element = n - Average(L, n, p)
variance = Initial_moment(L, 2, element, p)
return Decimal(variance)
def Compare(x, L, error):
if abs(x - L) < error:
print('Values match')
else:
print('Values do not match')
#-------------------------------------------------------------------
print('Enter Lamda: ')
L = int(input())
print('Enter N: ')
N = int(input())
n = toDecimalArray(np.arange(0, N + 1, dtype = 'float64'))
p = toDecimalArray(Poisson(L, n))
#print(p)
print('Enter k: ')
k = int(input())
print('Initial_moment is: ', Initial_moment(L, k, n, p))
print('Average is: ', Average(L, n, p))
print('Variance is: ', Variance(L, n, p))
#print(np.allclose(Average(L, n, p), L ))
#print(np.allclose(Variance(L, n, p), L))
Compare(Average(L, n, p), L, 0.001)
Compare(Variance(L, n, p), L, 0.001)
plt.plot(Poisson(1, n))
plt.plot(Poisson(4, n))
plt.plot(Poisson(10, n))
plt.plot(Poisson(50, n))
plt.xlabel('$x$')
plt.ylabel('$f(x)$')
plt.grid(True)
plt.show()
#print(np.allclose(Average(L, n, p), L ))
#print(np.allclose(Variance(L, n, p), L))