forked from vongostev/202-Advanced-Python-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumpy_dec
More file actions
68 lines (49 loc) · 1.91 KB
/
numpy_dec
File metadata and controls
68 lines (49 loc) · 1.91 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
-*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np
from decimal import Decimal, getcontext
getcontext().prec = 5
def Factorial(arr):
n = Decimal('1')
factorial = np.array([Decimal(1)])
for x in arr[1:]:
n *= x
factorial = np.append(factorial, n)
return factorial
def Puasson(lambda, N):
if lambda < 0:
raise ValueError("Lambda must be positive")
numpy_arrD = np.asarray([Decimal(i) for i in range(N)])
return lambda ** numpy_arrD * Decimal(-la).exp() / Factorial(numpy_arrD)
def Moment(arr, k=1):
if k < 0:
raise ValueError('"k" must be positive')
elif k % 1:
raise ValueError('"k" must be integer')
elif not isinstance(arr, np.ndarray):
raise ValueError('Wrong array format')
return np.sum(arr * np.arange(len(arr)) ** k)
def Mean(arr):
return Moment(arr, 1)
def Diviation(arr):
return Moment(arr, 2) - Moment(arr, 1) ** 2
def Plotter(arr):
plt.plot(arr)
plt.show()
def Test(x, answer, error=0.1):
if abs(x - answer) <= error:
return "cовпадает в пределах погрешности " + str(error)
return "не cовпадает в пределах погрешности " + str(error)
if __name__ == '__main__':
lambda = int(input())
N = int(input())
k = int(input())
error = float(input())
arr = Puasson(la, N)
Plotter(arr)
print("Начальный момент случайной великичны для k =",
k, "равен", Moment(arr, k))
print("Среднее значение равно", Mean(arr),
Test(Mean(arr), lambda), "с реальным значением", lambda)
print("Дисперсия случайной величины равна", Diviation(arr),
Test(Diviation(arr), lambda), "с реальным значением", lambda)