Advertisement
AIwinter

Untitled

Sep 29th, 2024
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.79 KB | None | 0 0
  1. import random
  2. import math
  3. import numpy as np
  4. import pandas as pd
  5.  
  6. alpha = 0.05
  7. from google.colab import drive
  8. drive.mount('/content/drive')
  9. file_path = '/content/drive/MyDrive/table.txt'
  10.  
  11. def PoissonCalculate(lambdaP, x):
  12. return pow(lambdaP, x) * math.exp(-lambdaP) / math.factorial(x)
  13. def PoissonStandartGenerator(lambdaP):
  14. r = random.random()
  15. x = 0
  16. p = PoissonCalculate(lambdaP, x)
  17. while(r > p):
  18. x += 1
  19. p += PoissonCalculate(lambdaP, x)
  20. return x
  21.  
  22. def PoissonModifiedGenerator(lambdaP):
  23. r = random.random()
  24. x = 0
  25. p = math.exp(-lambdaP)
  26. sumP = p
  27. while(r > sumP):
  28. x += 1
  29. p = p * lambdaP / x
  30. sumP += p
  31. return x
  32.  
  33. xiTable = pd.read_csv(file_path, sep="\t", header=None)
  34. print (xiTable)
  35. xiTable = np.asarray(xiTable, dtype=float)
  36.  
  37. "ввод: 0 1 2 3 4 5 6 7
  38. 0 0 0.95000 0.75000 0.25000 0.10000 0.05000 0.01000 0.00500
  39. 1 1 0.00393 0.10153 1.32330 2.70554 3.84146 6.63490 7.87944
  40. 2 2 0.10259 0.57536 2.77259 4.60517 5.99146 9.21034 10.59663
  41. 3 3 0.35185 1.21253 4.10834 6.25139 7.81473 11.34487 12.83816
  42. 4 4 0.71072 1.92256 5.38527 7.77944 9.48773 13.27670 14.86026
  43. 5 5 1.14548 2.67460 6.62568 9.23636 11.07050 15.08627 16.74960
  44. 6 6 1.63538 3.45460 7.84080 10.64464 12.59159 16.81189 18.54758
  45. 7 7 2.16735 4.25485 9.03715 12.01704 14.06714 18.47531 20.27774
  46. 8 8 2.73264 5.07064 10.21885 13.36157 15.50731 20.09024 21.95495
  47. 9 9 3.32511 5.89883 11.38875 14.68366 16.91898 21.66599 23.58935
  48. 10 10 3.94030 6.73720 12.54886 15.98718 18.30704 23.20925 25.18818
  49. 11 15 7.26094 11.03654 18.24509 22.30713 24.99579 30.57791 32.80132
  50. 12 20 10.85081 15.45177 23.82769 28.41198 31.41043 37.56623 39.99685
  51. 13 25 14.61141 19.93934 29.33885 34.38159 37.65248 44.31410 46.92789
  52. 14 30 18.49266 24.47761 34.79974 40.25602 43.77297 46.97924 50.89218"
  53.  
  54. def findTableXi(k, alpha):
  55. column = 0
  56. for i in range (1, xiTable.shape[1]):
  57. if xiTable[0][i] == alpha:
  58. column = i
  59. if column == 0:
  60. return 'Нет нужного значения альфа в таблице'
  61. i = 1
  62. while i < len(xiTable) - 1 and xiTable[i][0] < k:
  63. i += 1
  64. if k == xiTable[i][0]:
  65. xi = xiTable[i][column]
  66. elif k > xiTable[len(xiTable) - 1][0]:
  67. xi = xiTable[len(xiTable) - 1][column] + (xiTable[len(xiTable) - 1][column] - xiTable[len(xiTable) - 2][column]) / (xiTable[len(xiTable) - 1][0] - xiTable[len(xiTable) - 2][0]) * (k - xiTable[len(xiTable) - 1][0])
  68. else:
  69. xi = xiTable[i - 1][column] + (xiTable[i][column] - xiTable[i - 1][column]) / (xiTable[i][0] - xiTable[i - 1][0]) * (k - xiTable[i - 1][0])
  70. return xi
  71.  
  72. lambdaP = 0
  73. while lambdaP <= 0:
  74. lambdaP = float(input('Введите лямбда для распределения Пуассона: '))
  75. if lambdaP <= 0:
  76. print('Лямбда не может быть не положительным!')
  77. N = int(input('Введите объём выборки: '))
  78.  
  79. "ввод: Введите лямбда для распределения Пуассона: 3.75
  80. Введите объём выборки: 1000"
  81.  
  82. R = np.zeros(N)
  83. for i in range(0, N):
  84. R[i] = PoissonStandartGenerator(lambdaP)
  85. unique, counts = np.unique(R, return_counts=True)
  86. distribution = np.vstack((unique, counts))
  87. distribution = np.asarray(distribution, dtype=int)
  88. distribution
  89.  
  90. "ввод: array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
  91. [ 25, 77, 154, 204, 212, 133, 99, 50, 30, 12, 3, 1]])"
  92.  
  93. R = np.zeros(N)
  94. for i in range(0, N):
  95. R[i] = PoissonModifiedGenerator(lambdaP)
  96. unique, counts = np.unique(R, return_counts=True)
  97. distribution = np.vstack((unique, counts))
  98. distribution = np.asarray(distribution, dtype=int)
  99. distribution
  100.  
  101. "ввод: array([[ 60, 66, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78,
  102. 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
  103. 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104,
  104. 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117,
  105. 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130,
  106. 131, 132, 133, 134, 137, 138],
  107. [ 1, 1, 3, 3, 4, 2, 3, 9, 14, 20, 19, 32, 30,
  108. 37, 46, 57, 84, 95, 101, 120, 146, 154, 216, 237, 232, 285,
  109. 286, 334, 360, 352, 359, 359, 399, 369, 412, 398, 401, 365, 381,
  110. 355, 332, 297, 271, 265, 250, 216, 201, 175, 145, 139, 102, 83,
  111. 85, 69, 62, 41, 33, 29, 34, 25, 21, 5, 12, 8, 6,
  112. 3, 2, 3, 3, 1, 1]])"
  113.  
  114. minNumber = 5
  115. expectedDistribution = np.zeros((2, len(unique)))
  116. validArray = True
  117. for i in range (0, len(unique)):
  118. expectedDistribution[0][i] = distribution[1][i]
  119. expectedDistribution[1][i] = N * PoissonCalculate(lambdaP, distribution[0][i])
  120. if expectedDistribution[1][i] < minNumber:
  121. validArray = False
  122. while not validArray:
  123. validArray = True
  124. for i in range (0, expectedDistribution.shape[1] - 1):
  125. if expectedDistribution[1][i] < minNumber:
  126. expectedDistribution[0][i + 1] += expectedDistribution[0][i]
  127. expectedDistribution[1][i + 1] += expectedDistribution[1][i]
  128. expectedDistribution[0][i] = 0
  129. expectedDistribution[1][i] = 0
  130. if expectedDistribution[1][expectedDistribution.shape[1] - 1] < minNumber:
  131. expectedDistribution[0][expectedDistribution.shape[1] - 2] += expectedDistribution[0][expectedDistribution.shape[1] - 1]
  132. expectedDistribution[1][expectedDistribution.shape[1] - 2] += expectedDistribution[1][expectedDistribution.shape[1] - 1]
  133. expectedDistribution[0][expectedDistribution.shape[1] - 1] = 0
  134. expectedDistribution[1][expectedDistribution.shape[1] - 1] = 0
  135. expectedDistribution = expectedDistribution[expectedDistribution != 0]
  136. expectedDistribution = expectedDistribution.reshape(2, int(len(expectedDistribution) / 2))
  137. for i in range (0, expectedDistribution.shape[1]):
  138. if expectedDistribution[1][i] < minNumber:
  139. validArray = False
  140. print(expectedDistribution)
  141.  
  142.  
  143. "ввод: [[ 25. 77. 154. 204. 212.
  144. 133. 99. 50. 30. 16. ]
  145. [ 23.51774586 88.19154696 165.35915055 206.69893819 193.78025455
  146. 145.33519091 90.83449432 48.66133624 22.81000136 14.28325133]]"
  147.  
  148. MT = lambdaP
  149. DT = lambdaP
  150. ME = 0
  151. DE = 0
  152. for i in range (0, len(unique)):
  153. ME += distribution[0][i] * distribution[1][i]
  154. ME /= N
  155. for i in range (0, N):
  156. DE += (R[i] - ME) * (R[i] - ME)
  157. DE /= (N - 1)
  158. print('Теоретическое значение мат. ожидания: ' + str(MT))
  159. print('Расчётное значение мат. ожидания: ' + str(ME))
  160. print('Теоретическое значение дисперсии: ' + str(DT))
  161. print('Расчётное значение мат. дисперсии: ' + str(DE))
  162.  
  163. "ввод: Теоретическое значение мат. ожидания: 3.75
  164. Расчётное значение мат. ожидания: 3.843
  165. Теоретическое значение дисперсии: 3.75
  166. Расчётное значение мат. дисперсии: 3.8081591591591484"
  167.  
  168. intervalsNumber = len(unique)
  169. k = intervalsNumber - 1
  170. XSquaredE = 0
  171. for i in range (0, intervalsNumber):
  172. XSquaredE += (distribution[1][i] - N * PoissonCalculate(lambdaP, distribution[0][i])) * (distribution[1][i] - N * PoissonCalculate(lambdaP, distribution[0][i])) / (N * PoissonCalculate(lambdaP, distribution[0][i]))
  173. XSquaredT = findTableXi(k, alpha)
  174. print('Теоретическое значение хи-квадрат: ' + str(XSquaredT))
  175. print('Расчётное значение хи-квадрат: ' + str(XSquaredE))
  176.  
  177. "ввод: Теоретическое значение хи-квадрат: 19.64479
  178. Расчётное значение хи-квадрат: 8.90916222592578"
  179.  
  180. intervalsNumber = expectedDistribution.shape[1]
  181. k = intervalsNumber - 1
  182. XSquaredE = 0
  183. for i in range (0, intervalsNumber):
  184. XSquaredE += (expectedDistribution[0][i] - expectedDistribution[1][i]) * (expectedDistribution[0][i] - expectedDistribution[1][i]) / (expectedDistribution[1][i])
  185. XSquaredT = findTableXi(k, alpha)
  186. print('Теоретическое значение хи-квадрат: ' + str(XSquaredT))
  187. print('Расчётное значение хи-квадрат: ' + str(XSquaredE))
  188.  
  189. "ввод: Теоретическое значение хи-квадрат: 16.91898
  190. Расчётное значение хи-квадрат: 8.332764871150305"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement