Skip to content

Commit

Permalink
Added notebook on Equivalence tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanistheone committed Dec 28, 2023
1 parent 5cb766e commit 9031ac7
Show file tree
Hide file tree
Showing 5 changed files with 2,008 additions and 56 deletions.
35 changes: 23 additions & 12 deletions examples/one_sample_t-test.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"id": "229b9ce5-9ba8-406a-bb1a-5979d1e2a64f",
"metadata": {},
"source": [
"### One-sample $z$-test for the mean\n",
"### One-sample t-test for the mean\n",
"\n",
"The goal of the one-sample $t$-test to check if the mean $\\mu$ of an unknown population $X \\sim \\mathcal{N}(\\mu, \\sigma_0)$,\n",
"equals the mean $\\mu_0$ of a theoretical distribution $X_0 \\sim \\mathcal{N}(\\mu_0, \\sigma_0)$.\n",
Expand Down Expand Up @@ -99,18 +99,30 @@
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "7674af35-fd9d-4608-970a-b74c27ca3475",
"metadata": {},
"source": [
"#### Estimates\n",
"\n",
"Calculate the sample mean $\\overline{\\mathbf{x}} = \\Mean(\\mathbf{x})$\n",
"and the sample standard deviation $s_{\\mathbf{x}} = \\Std(\\mathbf{x})$.\n",
"We can then calculate the estimated standard error of the mean\n",
"$\\stderrhat{\\overline{\\mathbf{x}}} = \\frac{ s_{\\mathbf{x}} }{ \\sqrt{n} }$."
]
},
{
"cell_type": "markdown",
"id": "ab880b56-b783-4c32-b2cd-8cb66b59f766",
"metadata": {},
"source": [
"#### Test statistic\n",
"\n",
"Compute $t = \\frac{\\overline{\\mathbf{x}} - \\mu_0}{s/\\sqrt{n}}$,\n",
"where $\\overline{\\mathbf{x}}$ is the sample mean,\n",
"$\\mu_0$ is the theoretical population mean,\n",
"$n$ is the sample size.\n",
"and $s$ is the sample standard deviation, TODO UPDATE ME WITH NEW NOTATION\n"
"Compute $t = \\frac{\\overline{\\mathbf{x}} - \\mu_0}{ \\stderrhat{\\overline{\\mathbf{x}}} }$ from\n",
"the sample mean $\\overline{\\mathbf{x}}$,\n",
"the theoretical population mean $\\mu_0$,\n",
"the estimated standard error $\\stderrhat{\\overline{\\mathbf{x}}}$."
]
},
{
Expand All @@ -128,7 +140,7 @@
"id": "15cf13c7-78bf-495a-baf8-2312c6210c2a",
"metadata": {},
"source": [
"#### Calculating $p$-values"
"#### P-value calculation"
]
},
{
Expand Down Expand Up @@ -261,7 +273,7 @@
"id": "d563fb81-7b86-41e1-9a6a-c781d7cde415",
"metadata": {},
"source": [
"Let's generate a sample `xBs` of size $n=20$ from the random variable $X = \\texttt{rvXA}$."
"Let's generate a sample `xAs` of size $n=20$ from the random variable $X = \\texttt{rvXA}$."
]
},
{
Expand Down Expand Up @@ -341,7 +353,7 @@
{
"data": {
"text/plain": [
"(5.413149619618617, 3.1862282356088544e-05)"
"(5.413149619618617, 3.186228235607658e-05)"
]
},
"execution_count": 8,
Expand All @@ -357,8 +369,7 @@
"sehat = std / np.sqrt(n)\n",
"obst = (obsmean - muX0) / sehat\n",
"rvT0 = tdist(df=n-1)\n",
"from stats_helpers import tailprobs\n",
"pvalue = tailprobs(rvT0, obst, alt=\"two-sided\")\n",
"pvalue = rvT0.cdf(-obst) + 1-rvT0.cdf(obst)\n",
"obst, pvalue"
]
},
Expand Down Expand Up @@ -436,7 +447,7 @@
"id": "869366ba-2513-4501-ae31-f98ee4dcb134",
"metadata": {},
"source": [
"Let's generate a sample `xs` of size $n=20$ from the random variable $X = \\texttt{rvX}$,\n",
"Let's generate a sample `xBs` of size $n=20$ from the random variable $X = \\texttt{rvX}$,\n",
"which has the same distribution as the theoretical distribution we expect under the null hypothesis."
]
},
Expand Down
29 changes: 14 additions & 15 deletions examples/one_sample_z-test.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"id": "229b9ce5-9ba8-406a-bb1a-5979d1e2a64f",
"metadata": {},
"source": [
"### One-sample $z$-test for the mean\n",
"### One-sample z-test for the mean\n",
"\n",
"The goal of the one-sample $z$-test to check if the mean $\\mu$ of an unknown population $X \\sim \\mathcal{N}(\\mu, \\sigma_0)$,\n",
"equals the mean $\\mu_0$ of a theoretical distribution $X_0 \\sim \\mathcal{N}(\\mu_0, \\sigma_0)$.\n",
Expand Down Expand Up @@ -129,7 +129,7 @@
"id": "15cf13c7-78bf-495a-baf8-2312c6210c2a",
"metadata": {},
"source": [
"#### Calculating $p$-values"
"#### P-value calculation"
]
},
{
Expand Down Expand Up @@ -260,7 +260,7 @@
"id": "d563fb81-7b86-41e1-9a6a-c781d7cde415",
"metadata": {},
"source": [
"Let's generate a sample `xBs` of size $n=20$ from the random variable $X = \\texttt{rvXA}$."
"Let's generate a sample `xAs` of size $n=20$ from the random variable $X = \\texttt{rvXA}$."
]
},
{
Expand Down Expand Up @@ -333,29 +333,28 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 20,
"id": "b80ab201-a6c9-4325-85c4-15dc290bea12",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(3.1180664906014357, 0.0018204172963375855)"
"(3.1180664906014357, 0.0018204172963375287)"
]
},
"execution_count": 8,
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mean = np.mean(xAs)\n",
"obsmean = np.mean(xAs)\n",
"se = sigmaX0 / np.sqrt(n)\n",
"obsz = (mean - muX0) / se\n",
"obsz = (obsmean - muX0) / se\n",
"rvZ = norm(0,1)\n",
"from stats_helpers import tailprobs\n",
"pval = tailprobs(rvZ, obsz, alt=\"two-sided\")\n",
"obsz, pval"
"pvalue = rvZ.cdf(-obsz) + 1-rvZ.cdf(obsz)\n",
"obsz, pvalue"
]
},
{
Expand All @@ -369,7 +368,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 21,
"id": "d76a7c12-eab2-4f44-8040-3d170b6f16d2",
"metadata": {},
"outputs": [
Expand All @@ -379,7 +378,7 @@
"0.0018204172963375855"
]
},
"execution_count": 9,
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -432,7 +431,7 @@
"id": "869366ba-2513-4501-ae31-f98ee4dcb134",
"metadata": {},
"source": [
"Let's generate a sample `xs` of size $n=20$ from the random variable $X = \\texttt{rvX}$,\n",
"Let's generate a sample `xBs` of size $n=20$ from the random variable $X = \\texttt{rvX}$,\n",
"which has the same distribution as the theoretical distribution we expect under the null hypothesis."
]
},
Expand Down Expand Up @@ -566,7 +565,7 @@
"source": [
"#### Related\n",
"\n",
"- Use the one-sample $t$-test if you want to use the sample standard deviation\n",
"- Use the [one-sample $t$-test](./one_sample_t-test.ipynb) if you want to use the sample standard deviation\n",
"- Identical calculations to the one-saple $z$-test for proportions"
]
},
Expand Down
Loading

0 comments on commit 9031ac7

Please sign in to comment.