Skip to content

Instantly share code, notes, and snippets.

@MIKEk8
Last active October 27, 2023 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MIKEk8/279ecc53a07a7fb17f9eeebe4e86fc1e to your computer and use it in GitHub Desktop.
Save MIKEk8/279ecc53a07a7fb17f9eeebe4e86fc1e to your computer and use it in GitHub Desktop.
from scipy.stats import chi2
def chi2_test_for_dice(observations):
# Рассчет количества граней и общего числа бросков
num_faces = len(observations)
total_throws = sum(observations)
# Ожидаемые частоты (равномерное распределение)
expected_frequencies = [total_throws/num_faces] * num_faces
# Рассчет статистики хи-квадрат
chi2_stat = sum([(obs - exp)**2 / exp for obs, exp in zip(observations, expected_frequencies)])
# Рассчитываем p-значение (num_faces - 1 степеней свободы)
p_value = 1 - chi2.cdf(chi2_stat, num_faces - 1)
return chi2_stat, p_value
# Наблюдаемые частоты выпадений каждой грани
observed_frequencies = [3, 1, 2, 1, 2, 1]
chi2_value, p_val = chi2_test_for_dice(observed_frequencies)
print(f"Значение хи-квадрат: {chi2_value}")
print(f"p-значение: {p_val}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment