{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "68886bb2",
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy import stats\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0287a609",
   "metadata": {},
   "source": [
    "# Problem 1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "33118992",
   "metadata": {},
   "source": [
    "## Wilcoxon signed rank test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e62c9a05",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "WilcoxonResult(statistic=2.0, pvalue=0.375)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stats.wilcoxon([1,3,4,-2])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28294400",
   "metadata": {},
   "source": [
    "stats.wilcoxon returns $min(T^+, T^-)$, if we use the default, two-sided test."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3bc50ff8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "WilcoxonResult(statistic=8.0, pvalue=0.1875)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stats.wilcoxon([1,3,4,-2], alternative='greater')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "e45de97a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "WilcoxonResult(statistic=8.0, pvalue=0.875)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stats.wilcoxon([1,3,4,-2], alternative='less')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7121a33c",
   "metadata": {},
   "source": [
    "stats.wilcoxon returns $T^+$ for one-sided test.\n",
    "We see that for this few data we cannot reject the null hypothesis."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a4e36f4c",
   "metadata": {},
   "source": [
    "## Sign test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "88ca6222",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "BinomTestResult(k=1, n=4, alternative='two-sided', proportion_estimate=0.25, pvalue=0.625)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stats.binomtest(1,4,p=.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "a53390e9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "BinomTestResult(k=1, n=4, alternative='less', proportion_estimate=0.25, pvalue=0.3125)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stats.binomtest(1,4,p=.5, alternative='less')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "869547d6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "BinomTestResult(k=1, n=4, alternative='greater', proportion_estimate=0.25, pvalue=0.9375)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stats.binomtest(1,4,p=.5, alternative='greater')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a1805ec4",
   "metadata": {},
   "source": [
    "Here we do the sign test \"by hand\" (variant, where alternative is 'less')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "b15c83c1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.3125"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(1+4)/2**4"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24ef680f",
   "metadata": {},
   "source": [
    "# Problem 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "29966064",
   "metadata": {},
   "outputs": [],
   "source": [
    "with_problems = np.array([591, 621, 683, 579, 451, 680, 691, 769, 563, 575])\n",
    "without_problems = np.array([509, 540, 688, 502, 424, 683, 568, 748, 530, 524])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "0855407a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "620.3"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(with_problems)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "d2b5c2cb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "571.6"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(without_problems)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb8a48aa",
   "metadata": {},
   "source": [
    "## Wilcoxon"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2655412b",
   "metadata": {},
   "source": [
    "Is there a significant increase at all?\n",
    "($H_0$: same distribution)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "973dc60c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "WilcoxonResult(statistic=46.0, pvalue=0.0322265625)"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stats.wilcoxon(x=with_problems,y=without_problems,alternative='greater')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "f5956cda",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "WilcoxonResult(statistic=52.0, pvalue=0.0048828125)"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stats.wilcoxon(x=with_problems,y=without_problems,alternative='greater')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "b6fdf854",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "WilcoxonResult(statistic=46.0, pvalue=0.0322265625)"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stats.wilcoxon(x=with_problems,y=without_problems,alternative='greater')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a7b32748",
   "metadata": {},
   "source": [
    "Here we called the function with two data sets, as a paired test. \n",
    "\n",
    "Alternatively, we can make a difference and call the library function with just one parameter, getting the same result. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "b0a2a0cb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "WilcoxonResult(statistic=52.0, pvalue=0.0048828125)"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stats.wilcoxon(x=with_problems-without_problems,alternative='greater')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0583b181",
   "metadata": {},
   "source": [
    "For mere 10 measurements the library function used 'exact' method how to transform the obtained statistic to the pvalue. We can ask, however, for an approximation: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "0360870d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "WilcoxonResult(statistic=52.0, pvalue=0.0062576593450369864)"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stats.wilcoxon(x=with_problems-without_problems,alternative='greater',method='approx')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e8ad51f",
   "metadata": {},
   "source": [
    "Now we test, whether the increase was at least 50 points:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "26de6bfa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "WilcoxonResult(statistic=46.0, pvalue=0.02966805994045431)"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stats.wilcoxon(x=with_problems-without_problems-20,alternative='greater',method='approx')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "992f82f4",
   "metadata": {},
   "source": [
    "## Sign test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "be471b8e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(with_problems > without_problems+20)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6845d362",
   "metadata": {},
   "source": [
    "# Wilcoxon signed rank test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "99d09aab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(with_problems < without_problems+20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "468b97ca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(with_problems==without_problems+20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "c9b9db1a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "BinomTestResult(k=8, n=10, alternative='greater', proportion_estimate=0.8, pvalue=0.0546875)"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stats.binomtest(8,10,p=.5,alternative='greater')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3cd0eb2f",
   "metadata": {},
   "source": [
    "# Problem 3\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "9dbbb9fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "waiting_times = np.array([17, 15, 20, 20, 32, 28, 12, 26, 25, 25, 35, 24])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0778ccb5",
   "metadata": {},
   "source": [
    "# Problem 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "id": "a14bc76f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MannwhitneyuResult(statistic=1.0, pvalue=0.6666666666666666)"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stats.mannwhitneyu([1,3],[2,4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "id": "aa0ad944",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MannwhitneyuResult(statistic=1.0, pvalue=0.3333333333333333)"
      ]
     },
     "execution_count": 140,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stats.mannwhitneyu([1,3],[2,4], alternative='less')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "id": "a6a73d4e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def mystatistics(*data):\n",
    "    return (np.mean(data[0]) - np.mean(data[1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "id": "a20dd4f3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PermutationTestResult(statistic=-1.0, pvalue=0.6666666666666666, null_distribution=array([-1., -2.,  0.,  0.,  2.,  1.]))"
      ]
     },
     "execution_count": 138,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stats.permutation_test([[1,3],[2,4]], statistic=mystatistics)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "id": "1e9104c9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PermutationTestResult(statistic=-1.0, pvalue=0.3333333333333333, null_distribution=array([-1., -2.,  0.,  0.,  2.,  1.]))"
      ]
     },
     "execution_count": 139,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stats.permutation_test([[1,3],[2,4]], statistic=mystatistics,alternative='less')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "08c2db06",
   "metadata": {},
   "source": [
    "# Problem 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "id": "fc293074",
   "metadata": {},
   "outputs": [],
   "source": [
    "phoneA = np.array([2.1, 4.0, 6.3, 5.4, 4.8, 3.7, 6.1, 3.3])\n",
    "phoneB = np.array([4.1, 0.6, 3.1, 2.5, 4.0, 6.2, 1.6, 2.2, 1.9, 5.424])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "id": "0c08edee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4.4624999999999995, 3.1623999999999994)"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(phoneA), np.mean(phoneB)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "id": "35df595e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MannwhitneyuResult(statistic=56.5, pvalue=0.07745922570254606)"
      ]
     },
     "execution_count": 135,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stats.mannwhitneyu(phoneA,phoneB, alternative='greater')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "id": "6fe9eaa7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PermutationTestResult(statistic=1.3001, pvalue=0.0553, null_distribution=array([ 0.2876,  0.338 ,  1.2326, ..., -0.9049, -0.5674,  0.2876]))"
      ]
     },
     "execution_count": 137,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stats.permutation_test([phoneA,phoneB], statistic=mystatistics, alternative='greater')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1518f477",
   "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.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
