forked from vongostev/202-Advanced-Python-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumpyDecimal
More file actions
77 lines (57 loc) · 2.22 KB
/
numpyDecimal
File metadata and controls
77 lines (57 loc) · 2.22 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
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np
from decimal import Decimal, getcontext
getcontext().prec = 6
# Функция работает очень долго, потому что
# каждый новый факториал считается с 1, а не через предыдущий
# @np.vectorize
# def factorial(x):
# if x == 0:
# return Decimal(1)
# return factorial(x-1)*Decimal(x)
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(la, N):
if la < 0:
raise ValueError("Lambda must be positive")
# numpy_arr = np.arange(N, dtype='float64')
numpy_arrD = np.asarray([Decimal(i) for i in range(N)])
return la ** 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__':
la = 50 # int(input())
N = 100 # int(input())
k = 2 # int(input())
error = 0.1 # float(input())
arr = Puasson(la, N)
Plotter(arr)
print("Начальный момент случайной великичны для k =",
k, "равен", Moment(arr, k))
print("Среднее значение равно", Mean(arr), "оно",
Test(Mean(arr), la), "с реальным значением", la)
print("Дисперсия случайной величины равна", Diviation(arr),
"она", Test(Diviation(arr), la), "с реальным значением", la)