aboutsummaryrefslogtreecommitdiff
path: root/sem6/prob/stat4/Opgaver.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'sem6/prob/stat4/Opgaver.ipynb')
-rw-r--r--sem6/prob/stat4/Opgaver.ipynb93
1 files changed, 84 insertions, 9 deletions
diff --git a/sem6/prob/stat4/Opgaver.ipynb b/sem6/prob/stat4/Opgaver.ipynb
index 563260b..e4b0984 100644
--- a/sem6/prob/stat4/Opgaver.ipynb
+++ b/sem6/prob/stat4/Opgaver.ipynb
@@ -9,7 +9,7 @@
},
{
"cell_type": "code",
- "execution_count": 34,
+ "execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
@@ -27,7 +27,7 @@
},
{
"cell_type": "code",
- "execution_count": 9,
+ "execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
@@ -46,7 +46,7 @@
"What conclusion can be made if $\\alpha = 0.10$ level of significance.\n",
"\n",
"We let our $H_0: \\mu = 8.20$.\n",
- "We can conclude the probability of a *type 1* error, thus a false positive.\n",
+ "We can conclude the probability of a *type 1* error.\n",
"\n",
"We can calculate the stats \n",
"$$\n",
@@ -63,15 +63,16 @@
},
{
"cell_type": "code",
- "execution_count": 37,
+ "execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
- "3.320391543176363\n",
- "0.0008989127881156023 0.1 False\n"
+ "8.179000000000002\n",
+ "5.143928459843998\n",
+ "2.6905202554772245e-07 0.1 False\n"
]
},
{
@@ -89,6 +90,7 @@
],
"source": [
"x_hat = np.mean(samples)\n",
+ "print(x_hat)\n",
"\n",
"def assign(alpha):\n",
" v = np.sqrt(n) / sigma * np.abs(x_hat - mu)\n",
@@ -105,7 +107,7 @@
},
{
"cell_type": "code",
- "execution_count": 39,
+ "execution_count": 6,
"metadata": {},
"outputs": [
{
@@ -153,7 +155,7 @@
},
{
"cell_type": "code",
- "execution_count": 42,
+ "execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
@@ -175,7 +177,7 @@
},
{
"cell_type": "code",
- "execution_count": 49,
+ "execution_count": 8,
"metadata": {},
"outputs": [
{
@@ -204,6 +206,79 @@
"n = ((z_alpha2 + z_beta)**2 * sigma**2) / ((mu + sigma_reject - mu)**2)\n",
"display(Markdown(f\"Thus $n$ must be ${np.ceil(n)}$\"))"
]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 33,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "p_value: 0.0 ts: 13.472193585307627\n",
+ "H_0 is rejected\n"
+ ]
+ }
+ ],
+ "source": [
+ "## Part C\n",
+ "n = 6\n",
+ "\n",
+ "# We start by calculating a new test statistic\n",
+ "ts = np.sqrt(n) / sigma * (8.31 - mu)\n",
+ "p_value = 2 * (1 - norm.cdf(np.abs(ts)))\n",
+ "print(\"p_value:\", p_value, \"ts:\", ts)\n",
+ "if p_value < alpha:\n",
+ " print(\"H_0 is rejected\")\n",
+ "else:\n",
+ " print(\"H_0 is accepted\")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Part D\n",
+ "\n",
+ "The probability of accepting $H_0$ when the true mean is $\\mu$ is:\n",
+ "\n",
+ "$$\n",
+ "\\beta(\\mu) = \\Phi\\left(\\frac {\\mu_0 - \\mu} {\\sigma / \\sqrt{n}} + z_{\\alpha / 2}\\right) - \\Phi\\left(\\frac {\\mu_0 - \\mu} {\\sigma / \\sqrt{n}} - z_{\\alpha / 2}\\right)\n",
+ "$$\n",
+ "\n",
+ "To find the probability of rejecting $H_0$ we subtract this from 1.\n",
+ "\n",
+ "$$\n",
+ "P(reject \\: H_0) = 1 - \\beta(\\mu)\n",
+ "$$"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 35,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "beta: 1.8420324769867475e-37\n",
+ "There is a 1.0 change that H_0 is rejected\n"
+ ]
+ }
+ ],
+ "source": [
+ "real_mu = 8.32\n",
+ "blob = (mu - real_mu) / (sigma / np.sqrt(n))\n",
+ "za2 = - norm.ppf(alpha / 2)\n",
+ "\n",
+ "beta = norm.cdf(blob + za2) - norm.cdf(blob - za2)\n",
+ "print(\"beta:\", beta)\n",
+ "\n",
+ "prob = 1 - beta\n",
+ "print(f\"There is a {prob} change that H_0 is rejected\")"
+ ]
}
],
"metadata": {