Advertisement
alsiva

ForSultan

May 4th, 2022
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. import statistics
  2. from collections import Counter
  3. import matplotlib.pyplot as plt
  4. import numpy as np
  5. import math
  6.  
  7.  
  8. dots = [
  9. 1.07, 1.59,
  10. -1.49, -0.1,
  11. 0.11, 1.18,
  12. 0.35, -0.73,
  13. 1.07, 0.31,
  14. -0.26, -1.2,
  15. -0.35, 0.73,
  16. 1.01, -0.1,
  17. 0.28, -1.3,
  18. -1.1, -0.2
  19. ]
  20.  
  21.  
  22. def draw_plot(x: list, y: list, xs: str, ys: str, title: str, plot: bool):
  23. if plot:
  24. plt.plot(x, y)
  25. else:
  26. plt.bar(x, y)
  27. plt.xlabel(xs)
  28. plt.ylabel(ys)
  29. plt.title(title)
  30. plt.show()
  31.  
  32. def main():
  33. print("Вариационный ряд ")
  34. dots.sort()
  35. print(dots)
  36. print("Наименьшее значение:", dots[0])
  37. print("Наибольшее значение:", dots[-1])
  38. print("Размах: ", dots[-1] - dots[0])
  39.  
  40. # подсчёт мат.ожидания и среднеквадратичного отклонения
  41. expected_value = 0
  42. counter = Counter(dots)
  43. for item, frequency in counter.items():
  44. expected_value += item * frequency / len(dots)
  45. print("Математическое ожидание:", expected_value)
  46. print(statistics.mean(dots))
  47.  
  48. dispersion = 0
  49. for item, frequency in counter.items():
  50. dispersion += (item - expected_value) ** 2 * frequency / len(dots)
  51. print("Среднеквадратичное отклонение: ", dispersion ** 0.5)
  52.  
  53. # Эмпирическая функция распределения, полигон приведённых частот
  54. nx = 0
  55. ed_func = {}
  56. nx_list = {}
  57. for item, frequency in counter.items():
  58. nx += frequency
  59. nx_list[item] = frequency
  60. ed_func[item] = nx / len(dots)
  61.  
  62. print("Эмпирическая функция распределения")
  63. print(ed_func)
  64.  
  65. x_start = dots[0]
  66. x_end = dots[-1]
  67. step = (dots[-1] - dots[0]) / (1 + math.log2(len(dots)))
  68. amount = 0
  69. pol_map = {}
  70. for interval in np.arange(x_start, x_end, step):
  71. amount += len([item for item in dots if interval < item < interval + step])
  72. pol_map[interval] = amount
  73.  
  74.  
  75.  
  76. hist, edges = np.histogram(dots, bins=len(dots))
  77. Y = hist.cumsum()
  78. for i in range(len(Y)):
  79. plt.plot([edges[i], edges[i + 1]], [Y[i] / len(Y), Y[i] / len(Y)], c="blue")
  80. plt.title("Эмпирическая функция распределения")
  81. plt.show()
  82.  
  83.  
  84. # Гистограмма
  85. h = (dots[-1] - dots[0]) / (1 + math.log2(len(dots)))
  86. x_beg = dots[0] - h/2
  87. x_fin = dots[-1] + h/2
  88. hist_map = {}
  89. print("Интервалы для гистограммы")
  90. for interval in np.arange(x_beg, x_fin, h):
  91. elements_amount = len([item for item in dots if interval < item < interval + h])
  92. print("[", interval, interval + h, "]", "Вхождения ", elements_amount, "Вероятность: ", elements_amount/ len(dots))
  93. hist_map[interval] = elements_amount / len(dots)
  94.  
  95. draw_plot(list(hist_map.keys()), list(hist_map.values()), "x", "y", "Гистограмма частот", False)
  96. draw_plot(list(hist_map.keys()), list(hist_map.values()), "x", "y", "Полигон частот", True)
  97.  
  98.  
  99. main()
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement