Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import statistics
- from collections import Counter
- import matplotlib.pyplot as plt
- import numpy as np
- import math
- dots = [
- 1.07, 1.59,
- -1.49, -0.1,
- 0.11, 1.18,
- 0.35, -0.73,
- 1.07, 0.31,
- -0.26, -1.2,
- -0.35, 0.73,
- 1.01, -0.1,
- 0.28, -1.3,
- -1.1, -0.2
- ]
- def draw_plot(x: list, y: list, xs: str, ys: str, title: str, plot: bool):
- if plot:
- plt.plot(x, y)
- else:
- plt.bar(x, y)
- plt.xlabel(xs)
- plt.ylabel(ys)
- plt.title(title)
- plt.show()
- def main():
- print("Вариационный ряд ")
- dots.sort()
- print(dots)
- print("Наименьшее значение:", dots[0])
- print("Наибольшее значение:", dots[-1])
- print("Размах: ", dots[-1] - dots[0])
- # подсчёт мат.ожидания и среднеквадратичного отклонения
- expected_value = 0
- counter = Counter(dots)
- for item, frequency in counter.items():
- expected_value += item * frequency / len(dots)
- print("Математическое ожидание:", expected_value)
- print(statistics.mean(dots))
- dispersion = 0
- for item, frequency in counter.items():
- dispersion += (item - expected_value) ** 2 * frequency / len(dots)
- print("Среднеквадратичное отклонение: ", dispersion ** 0.5)
- # Эмпирическая функция распределения, полигон приведённых частот
- nx = 0
- ed_func = {}
- nx_list = {}
- for item, frequency in counter.items():
- nx += frequency
- nx_list[item] = frequency
- ed_func[item] = nx / len(dots)
- print("Эмпирическая функция распределения")
- print(ed_func)
- x_start = dots[0]
- x_end = dots[-1]
- step = (dots[-1] - dots[0]) / (1 + math.log2(len(dots)))
- amount = 0
- pol_map = {}
- for interval in np.arange(x_start, x_end, step):
- amount += len([item for item in dots if interval < item < interval + step])
- pol_map[interval] = amount
- hist, edges = np.histogram(dots, bins=len(dots))
- Y = hist.cumsum()
- for i in range(len(Y)):
- plt.plot([edges[i], edges[i + 1]], [Y[i] / len(Y), Y[i] / len(Y)], c="blue")
- plt.title("Эмпирическая функция распределения")
- plt.show()
- # Гистограмма
- h = (dots[-1] - dots[0]) / (1 + math.log2(len(dots)))
- x_beg = dots[0] - h/2
- x_fin = dots[-1] + h/2
- hist_map = {}
- print("Интервалы для гистограммы")
- for interval in np.arange(x_beg, x_fin, h):
- elements_amount = len([item for item in dots if interval < item < interval + h])
- print("[", interval, interval + h, "]", "Вхождения ", elements_amount, "Вероятность: ", elements_amount/ len(dots))
- hist_map[interval] = elements_amount / len(dots)
- draw_plot(list(hist_map.keys()), list(hist_map.values()), "x", "y", "Гистограмма частот", False)
- draw_plot(list(hist_map.keys()), list(hist_map.values()), "x", "y", "Полигон частот", True)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement