Add pretty prints

This commit is contained in:
Marcin Kostrzewski 2022-05-17 21:08:54 +02:00
parent fdb95361cb
commit 39c9a260e1

View File

@ -25,7 +25,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 313, "execution_count": 61,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -47,7 +47,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 314, "execution_count": 62,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -56,7 +56,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 315, "execution_count": 63,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -67,7 +67,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 316, "execution_count": 64,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -88,7 +88,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 317, "execution_count": 65,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -110,7 +110,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 318, "execution_count": 66,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -128,7 +128,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 319, "execution_count": 67,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -146,7 +146,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 320, "execution_count": 68,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -171,6 +171,36 @@
" return t_stat_list" " return t_stat_list"
] ]
}, },
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"outputs": [],
"source": [
"def pretty_print_test(p, t_stat_from_sample, t_stat_list, thesis, alternative, max_print=5):\n",
" print('Wyniki bootstrapowej wersji testu T-studenta')\n",
" print()\n",
" print(f'Hipoteza: {thesis}')\n",
" if alternative is Alternatives.LESS:\n",
" print(f'Hipoteza alternatywna: średnia jest mniejsza')\n",
" else:\n",
" print(f'Hipoteza alternatywna: średnia jest większa')\n",
" print()\n",
" print(f'p: {p}')\n",
" print(f'Wartość statystyki testowej z próby: {t_stat_from_sample}')\n",
" print(f'Wartości statystyk z prób boostrapowych:')\n",
"\n",
" t_stat_list_len = len(t_stat_list)\n",
" for i in range(min(max_print, t_stat_list_len)):\n",
" print(f'{t_stat_list[i]}, ', end='')\n",
" if max_print < t_stat_list_len:\n",
" remaining = t_stat_list_len - max_print\n",
" print(f'... (i {remaining} pozostałych)')\n",
"\n",
" print()\n",
" print()"
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
@ -193,7 +223,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 321, "execution_count": 70,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -221,7 +251,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 322, "execution_count": 71,
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"pycharm": { "pycharm": {
@ -231,11 +261,15 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"def bootstrap_one_sample(sample, population_mean, alternative=Alternatives.LESS):\n", "def bootstrap_one_sample(sample, population_mean, alternative=Alternatives.LESS):\n",
" return t_test_1_samp(\n", " p, t, ts = t_test_1_samp(\n",
" sample_1=sample,\n", " sample_1=sample,\n",
" population_mean=population_mean,\n", " population_mean=population_mean,\n",
" alternative=alternative,\n", " alternative=alternative,\n",
" )" " )\n",
" \n",
" pretty_print_test(p, t, ts, f'średnia jest równa {population_mean}', alternative)\n",
" print()\n",
" return p, t, ts"
] ]
}, },
{ {
@ -247,7 +281,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 323, "execution_count": 72,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -258,7 +292,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 324, "execution_count": 73,
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"pycharm": { "pycharm": {
@ -270,15 +304,22 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"p: 0.73\n" "Wyniki bootstrapowej wersji testu T-studenta\n",
"\n",
"Hipoteza: średnia jest równa 165\n",
"Hipoteza alternatywna: średnia jest mniejsza\n",
"\n",
"p: 0.58\n",
"Wartość statystyki testowej z próby: [-229.1025971]\n",
"Wartości statystyk z prób boostrapowych:\n",
"[-316.92367438], [-406.5], [-201.5], [-243.92267776], [-330.27286699], ... (i 95 pozostałych)\n"
] ]
} }
], ],
"source": [ "source": [
"#TODO: poprawić kod aby można było podawać kolumny\n", "#TODO: poprawić kod aby można było podawać kolumny\n",
"\n", "\n",
"p, _ = bootstrap_one_sample(dummy, 165)\n", "p, t, ts = bootstrap_one_sample(dummy, 165)"
"print(f'p: {p}')"
] ]
}, },
{ {
@ -302,7 +343,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 325, "execution_count": 74,
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"pycharm": { "pycharm": {
@ -312,11 +353,14 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"def bootstrap_independent(sample_1, sample_2, alternative=Alternatives.LESS):\n", "def bootstrap_independent(sample_1, sample_2, alternative=Alternatives.LESS):\n",
" return t_test_ind(\n", " p, t, ts = t_test_ind(\n",
" sample_1=sample_1,\n", " sample_1=sample_1,\n",
" sample_2=sample_2,\n", " sample_2=sample_2,\n",
" alternative=alternative,\n", " alternative=alternative,\n",
" )" " )\n",
" \n",
" pretty_print_test(p, t, ts, 'średnie są takie same', alternative)\n",
" return p, t, ts"
] ]
}, },
{ {
@ -348,7 +392,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 326, "execution_count": 75,
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"pycharm": { "pycharm": {
@ -358,11 +402,14 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"def bootstrap_dependent(sample_1, sample_2, alternative=Alternatives.LESS):\n", "def bootstrap_dependent(sample_1, sample_2, alternative=Alternatives.LESS):\n",
" return t_test_dep(\n", " p, t, ts = t_test_dep(\n",
" sample_1=sample_1,\n", " sample_1=sample_1,\n",
" sample_2=sample_2,\n", " sample_2=sample_2,\n",
" alternative=alternative,\n", " alternative=alternative,\n",
" )" " )\n",
" \n",
" pretty_print_test(p, t, ts, 'średnie są takie same', alternative)\n",
" return p, t, ts"
] ]
}, },
{ {
@ -390,7 +437,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 327, "execution_count": 76,
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"pycharm": { "pycharm": {
@ -419,7 +466,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 328, "execution_count": 77,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -427,11 +474,35 @@
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Statystyki dla jednej próby:\n", "Statystyki dla jednej próby:\n",
"0.44\n", "Wyniki bootstrapowej wersji testu T-studenta\n",
"\n",
"Hipoteza: średnia jest równa 2\n",
"Hipoteza alternatywna: średnia jest mniejsza\n",
"\n",
"p: 0.52\n",
"Wartość statystyki testowej z próby: [1.41421356]\n",
"Wartości statystyk z prób boostrapowych:\n",
"[-0.53452248], [0.], [-1.63299316], [1.5], [2.3590713], ... (i 95 pozostałych)\n",
"Statystyki dla dwóch prób zależnych:\n", "Statystyki dla dwóch prób zależnych:\n",
"0.0\n", "Wyniki bootstrapowej wersji testu T-studenta\n",
"\n",
"Hipoteza: średnie są takie same\n",
"Hipoteza alternatywna: średnia jest mniejsza\n",
"\n",
"p: 0.0\n",
"Wartość statystyki testowej z próby: [10.61445555]\n",
"Wartości statystyk z prób boostrapowych:\n",
"[nan], [nan], [nan], [nan], [nan], ... (i 95 pozostałych)\n",
"Statystyki dla dwóch prób niezależnych:\n", "Statystyki dla dwóch prób niezależnych:\n",
"1.0\n" "Wyniki bootstrapowej wersji testu T-studenta\n",
"\n",
"Hipoteza: średnie są takie same\n",
"Hipoteza alternatywna: średnia jest mniejsza\n",
"\n",
"p: 1.0\n",
"Wartość statystyki testowej z próby: [2.4140394]\n",
"Wartości statystyk z prób boostrapowych:\n",
"[0.], [0.], [0.], [0.], [0.], ... (i 95 pozostałych)\n"
] ]
} }
], ],
@ -440,16 +511,13 @@
"\n", "\n",
"\n", "\n",
"print('Statystyki dla jednej próby:')\n", "print('Statystyki dla jednej próby:')\n",
"p, _ = bootstrap_one_sample(dummy, 2)\n", "p, t, ts = bootstrap_one_sample(dummy, 2)\n",
"print(f'p {p}')\n",
"\n", "\n",
"print('Statystyki dla dwóch prób zależnych:')\n", "print('Statystyki dla dwóch prób zależnych:')\n",
"p, _ = bootstrap_dependent(dummy2, dummy3)\n", "p, t, ts = bootstrap_dependent(dummy2, dummy3)\n",
"print(f'p {p}')\n",
"\n", "\n",
"print('Statystyki dla dwóch prób niezależnych:')\n", "print('Statystyki dla dwóch prób niezależnych:')\n",
"p, _ = bootstrap_independent(dummy2, dummy3)\n", "p, t, ts = bootstrap_independent(dummy2, dummy3)"
"print(f'p {p}')"
] ]
}, },
{ {
@ -483,7 +551,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.8.10-final" "version": "3.9.1"
}, },
"orig_nbformat": 4 "orig_nbformat": 4
}, },