Добавил:
yurochka
Периодически делаю учебные работы по предметам ЛЭТИ и выгружаю их сюда для пополнения базы, с которой можно будет свериться.
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лабораторная 1 / Линейная смешанная формула / Lab1
.pyimport matplotlib.pyplot as plt
import numpy as np
import random
import time
class MyRandom:
def __init__(self, r, K, A, c, m):
self.r = r
self.K = K
self.A = A
self.c = c
self.m = m
if (len(K) != r or len(A) != r):
raise Exception
def next(self):
x = sum([self.K[i] * self.A[i] for i in range(self.r)]) % self.m
self.A.pop(0)
self.A.append(x)
return x / self.m
random.seed(time.time())
r = 8
K = [random.randint(0, 2000) for _ in range(r)]
A = [random.randint(0, 2**16) for _ in range(r)]
rand = MyRandom(r, K, A, 946785, 2**32)
rand_list = []
for i in range(5000):
rand_list.append(rand.next())
N = len(rand_list)
M = sum(rand_list) / N
D = (sum(z * z for z in rand_list) - (M * M) * N) / N
print(f"Моменты: M = {M}; D = {D}")
base = np.linspace(0, 1, N)
plt.hist([rand_list, base],
bins=10,
weights=[np.ones_like(rand_list) / len(rand_list),
np.ones_like(base) / base.size],
label=['Эмпирическое', 'Теоретическое'])
plt.legend(loc='lower right')
plt.savefig("Danila/ms1.1.svg")
plt.show()
S_list = (2, 5, 10)
for S in S_list:
targetNs = [x*200 for x in range(1, 60)]
Rs = []
for targetN in targetNs:
rand = MyRandom(r, K, A, 946785, 2**32)
rand_list.clear()
for k in range(targetN):
rand_list.append(rand.next())
N = len(rand_list)
randX = rand_list[:N-S]
randY = rand_list[S:]
R = np.corrcoef(randX, randY)[0, 1]
Rs.append(R)
if S == S_list[0]:
plt.plot(targetNs, Rs, color='r', label='s = 2')
if S == S_list[1]:
plt.plot(targetNs, Rs, color='b', label='s = 5')
if S == S_list[2]:
plt.plot(targetNs, Rs, color='g', label='s = 10')
plt.title("Коэффициент корреляции при s = 2, 5, 10")
plt.legend()
plt.xlabel("N")
plt.ylabel("R")
plt.grid(True)
plt.axhline(y=0, color='k')
plt.axvline(x=0, color='k')
plt.savefig("Danila/ms1.s.svg")
plt.show()
Соседние файлы в папке Линейная смешанная формула
