From 352c2f87c501e57c95d3b91a342ea9cb67736395 Mon Sep 17 00:00:00 2001 From: DIvan2000 Date: Mon, 17 Mar 2025 02:39:34 +0400 Subject: [PATCH] initial commit --- .gitignore | 2 + .idea/.gitignore | 3 ++ .../inspectionProfiles/profiles_settings.xml | 6 +++ .idea/modules.xml | 8 ++++ main.py | 9 ++++ phi_star.py | 46 +++++++++++++++++++ 6 files changed, 74 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/modules.xml create mode 100644 main.py create mode 100644 phi_star.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f2cc716 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.gitignore +responses.csv \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..b6c6c65 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..b383033 --- /dev/null +++ b/main.py @@ -0,0 +1,9 @@ +import phi_star +import csv + +if __name__ == '__main__': + with open("responses.csv", encoding="utf-8") as file: + reader = csv.reader(file) + for row in reader: + print(row) + print(phi_star.calculate_phi_star([[1, 1], [1, 1]])) \ No newline at end of file diff --git a/phi_star.py b/phi_star.py new file mode 100644 index 0000000..53f6c6f --- /dev/null +++ b/phi_star.py @@ -0,0 +1,46 @@ +import math + +def calculate_phi_star(table): + """ + Вычисляет эмпирическое значение φ* по 2x2 таблице сопряжённости. + + Параметры: + table: Список списков 2x2, где + table[0][0] — количество «есть эффект» в группе 1, + table[0][1] — количество «нет эффекта» в группе 1, + table[1][0] — количество «есть эффект» в группе 2, + table[1][1] — количество «нет эффекта» в группе 2. + + Возвращает: + phi_star — рассчитанное значение φ*, + """ + # Извлекаем данные из таблицы + group1_ef = table[0][0] + group1_ne = table[0][1] + group2_ef = table[1][0] + group2_ne = table[1][1] + + # Суммарные значения по группам + total_group1 = group1_ef + group1_ne + total_group2 = group2_ef + group2_ne + total = total_group1 + total_group2 + + # Суммы по столбцам (категориям) + total_ef = group1_ef + group2_ef + total_ne = group1_ne + group2_ne + + # Вычисляем ожидаемые частоты для каждой категории + expected_group1_ef = total_group1 * total_ef / total + expected_group1_ne = total_group1 * total_ne / total + expected_group2_ef = total_group2 * total_ef / total + expected_group2_ne = total_group2 * total_ne / total + + # Вычисляем вклад по каждому элементу (χ²) + chi_squared_1 = ((group1_ef - expected_group1_ef) ** 2 / expected_group1_ef) + \ + ((group1_ne - expected_group1_ne) ** 2 / expected_group1_ne) + + chi_squared_2 = ((group2_ef - expected_group2_ef) ** 2 / expected_group2_ef) + \ + ((group2_ne - expected_group2_ne) ** 2 / expected_group2_ne) + + # Вычисляем φ* + return math.sqrt(chi_squared_1 + chi_squared_2)