Skip to content

Instantly share code, notes, and snippets.

@tok41
Created July 3, 2022 13:51
Show Gist options
  • Save tok41/d08f4e38c47330c6549d57e9b31adc06 to your computer and use it in GitHub Desktop.
Save tok41/d08f4e38c47330c6549d57e9b31adc06 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "b9b044ba-d751-48ec-b727-437d7c0e708d",
"metadata": {},
"source": [
"# About\n",
"\n",
"順位の適合度検定をする"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "8818179d-9fd4-4cca-a8bb-36ffc4e650b0",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"import collections\n",
"import scipy\n",
"\n",
"%matplotlib inline\n",
"import matplotlib.pyplot as plt\n",
"import seaborn as sns\n",
"sns.set(font_scale=1.5)\n",
"c_list = sns.color_palette().as_hex()\n",
"color_num = len(c_list)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "94d695c6-a015-422e-b32c-6044001773ae",
"metadata": {},
"outputs": [],
"source": [
"dct_player = {\n",
" \"さくま\": \"sakuma\", \"ようきひ\":\"yohkihi\", \"ガキ\": \"gaki\"\n",
"}\n",
"\n",
"s = \"\"\"\n",
"さくま:ガキ:ようきひ\n",
"さくま:ようきひ:ガキ\n",
"ガキ:ようきひ:さくま\n",
"ようきひ:ガキ:さくま\n",
"ようきひ:さくま:ガキ\n",
"ようきひ:ガキ:さくま\n",
"ようきひ:ガキ:さくま\n",
"ようきひ:さくま:ガキ\n",
"ようきひ:さくま:ガキ\n",
"さくま:ガキ:ようきひ\n",
"ガキ:さくま:ようきひ\n",
"ようきひ:さくま:ガキ\n",
"ようきひ:さくま:ガキ\n",
"ようきひ:さくま:ガキ\n",
"さくま:ようきひ:ガキ\n",
"さくま:ガキ:ようきひ\n",
"さくま:ようきひ:ガキ\n",
"さくま:ガキ:ようきひ\n",
"ようきひ:ガキ:さくま\n",
"ようきひ:さくま:ガキ\n",
"ようきひ:さくま:ガキ\n",
"ようきひ:さくま:ガキ\n",
"ようきひ:さくま:ガキ\n",
"ようきひ:さくま:ガキ\n",
"ようきひ:さくま:ガキ\n",
"ようきひ:さくま:ガキ\n",
"ようきひ:さくま:ガキ\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "323959d8-0d9b-40f7-955e-5e9f89acbd73",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>1</th>\n",
" <th>2</th>\n",
" <th>3</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>さくま</td>\n",
" <td>ガキ</td>\n",
" <td>ようきひ</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>さくま</td>\n",
" <td>ようきひ</td>\n",
" <td>ガキ</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>ガキ</td>\n",
" <td>ようきひ</td>\n",
" <td>さくま</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>ようきひ</td>\n",
" <td>ガキ</td>\n",
" <td>さくま</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>ようきひ</td>\n",
" <td>さくま</td>\n",
" <td>ガキ</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" 1 2 3\n",
"0 さくま ガキ ようきひ\n",
"1 さくま ようきひ ガキ\n",
"2 ガキ ようきひ さくま\n",
"3 ようきひ ガキ さくま\n",
"4 ようきひ さくま ガキ"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lst_rank = []\n",
"for t in s.strip().split(\"\\n\"):\n",
" rank = t.split(\":\")\n",
" lst_rank.append({1:rank[0], 2:rank[1], 3:rank[2]})\n",
"df_rank = pd.DataFrame(lst_rank)\n",
"df_rank.head()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "1f566143-7d7d-4da6-8673-9848e19022ea",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Counter({'さくま': 7, 'ガキ': 2, 'ようきひ': 18})"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cnt_top = collections.Counter(df_rank[1])\n",
"cnt_top"
]
},
{
"cell_type": "markdown",
"id": "4b6a8c5e-65ee-4b38-9e23-fbac78e04992",
"metadata": {},
"source": [
"# 適合度検定"
]
},
{
"cell_type": "markdown",
"id": "1c61b3ee-51c8-48eb-ab53-2a352adb4718",
"metadata": {},
"source": [
"## 3人の勝率の当確率性"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "50069a12-3406-4075-bf4c-75360bb1cc20",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[7, 2, 18]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"top_cnt = list(cnt_top.values())\n",
"top_cnt"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "6b8d1ae9-41df-4e78-9539-b20eb68db40c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([9., 9., 9.])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"exp = np.ones(3)/3 * sum(top_cnt)\n",
"exp"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "7cb0224a-8608-44d3-8fee-cace1f1fa4b0",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Power_divergenceResult(statistic=14.88888888888889, pvalue=0.0005846808328765149)"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"scipy.stats.chisquare(top_cnt, f_exp=exp)"
]
},
{
"cell_type": "markdown",
"id": "76383dae-624d-45fa-9b4a-94ed7f4af9df",
"metadata": {},
"source": [
"## さくまとようきひの勝率"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "a62d2729-f441-42a9-a205-b4785de02f18",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'さくま': 7, 'ようきひ': 18}"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"[7, 18]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"array([12.5, 12.5])"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"targets = [\"さくま\", \"ようきひ\"]\n",
"dct_top_cnt = {k:v for k, v in cnt_top.items() if (k in targets)}\n",
"top_cnt = list(dct_top_cnt.values())\n",
"exp = (np.ones(len(targets)) / len(targets)) * sum(top_cnt)\n",
"\n",
"display(dct_top_cnt)\n",
"display(top_cnt)\n",
"display(exp)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "3ebafe9e-36f1-4444-8e5c-daca06babae6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Power_divergenceResult(statistic=4.84, pvalue=0.027806895026997226)"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"scipy.stats.chisquare(top_cnt, f_exp=exp)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "051f8640-ff7a-4931-b4ce-52743127d19c",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment