Skip to content

Instantly share code, notes, and snippets.

@laughing
Created October 17, 2017 15:27
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 laughing/7f85fc108c1dd8fe7410defa7bdcdb4b to your computer and use it in GitHub Desktop.
Save laughing/7f85fc108c1dd8fe7410defa7bdcdb4b to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np\n",
"from scipy import stats"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2つの分布から適当にサンプルを生成"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(0.25936262785715875, 0.95276283153718899)"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"n = 30\n",
"m = 30\n",
"x1 = np.random.normal(0, 1, n)\n",
"x2 = np.random.normal(1, 1, m)\n",
"np.mean(x1), np.mean(x2)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"0.69340020368003019"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mean = np.mean(x2) - np.mean(x1)\n",
"mean"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"平均が同じかどうかを検定する"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### t検定する"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"0.0044220720669139683"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t, p = stats.ttest_ind(x1, x2)\n",
"p"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### bootstrap 法で検定する"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"x = x1 + x2"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"b = 10000\n",
"a = []\n",
"for i in range(b):\n",
" xx1 = np.random.choice(x, n, replace=True)\n",
" xx2 = np.random.choice(x, m, replace=True)\n",
" mm = -np.mean(xx1) + np.mean(xx2)\n",
" a.append(mm)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"0.0142"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = np.array(a)\n",
"len(a[a > mean]) * 1. / len(a)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment