aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian T <julian@jtle.dk>2021-03-16 12:18:57 +0100
committerJulian T <julian@jtle.dk>2021-03-16 12:18:57 +0100
commit83ced2d4cee2e46fe8d47e3e192b34efaa37bf0f (patch)
tree0fc87b439ec6a3764eb7de2bc2eb90a6992f8cec
parent8430a7f363589260c809f180bfa2c0a7097d5e6b (diff)
Added some assignments for prob
-rw-r--r--sem6/prob/stat4/Opgaver.ipynb230
1 files changed, 230 insertions, 0 deletions
diff --git a/sem6/prob/stat4/Opgaver.ipynb b/sem6/prob/stat4/Opgaver.ipynb
new file mode 100644
index 0000000..441d171
--- /dev/null
+++ b/sem6/prob/stat4/Opgaver.ipynb
@@ -0,0 +1,230 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Opgaver til stat4"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 34,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "import numpy as np\n",
+ "from scipy.stats import norm\n",
+ "from IPython.display import display, Markdown"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Problem 1\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 9,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "mu = 8.20\n",
+ "sigma = 0.02\n",
+ "samples = np.array([8.18, 8.17, 8.16, 8.15, 8.17, 8.21, 8.22, 8.16, 8.19, 8.18])\n",
+ "n = len(samples)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### Part A\n",
+ "\n",
+ "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",
+ "\n",
+ "We can calculate the stats \n",
+ "$$\n",
+ "v = \\frac {\\sqrt{n}} \\sigma | \\bar{X} - \\mu|\n",
+ "$$\n",
+ "then the *p-value* is the probability that the $H_0$ is falsely rejected when observing $v$.\n",
+ "If the *p-value* is less that $\\alpha$ then $H_0$ is rejected.\n",
+ "\n",
+ "The *p-value* can be calculated by finding the area outside $v$.\n",
+ "$$\n",
+ "p = 2 \\cdot P(Z > v) = 2 \\cdot (1 - \\Phi(V))\n",
+ "$$"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 37,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "3.320391543176363\n",
+ "0.0008989127881156023 0.1 False\n"
+ ]
+ },
+ {
+ "data": {
+ "text/markdown": [
+ "Thus $H_0$ is rejected"
+ ],
+ "text/plain": [
+ "<IPython.core.display.Markdown object>"
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ }
+ ],
+ "source": [
+ "x_hat = np.mean(samples)\n",
+ "\n",
+ "def assign(alpha):\n",
+ " v = np.sqrt(n) / sigma * np.abs(x_hat - mu)\n",
+ " print(v)\n",
+ " p_val = 2 * (1 - norm.cdf(v))\n",
+ " print(p_val, alpha, p_val >= alpha)\n",
+ " if p_val >= alpha:\n",
+ " display(Markdown(\"Thus H_0 is accepted\"))\n",
+ " else:\n",
+ " display(Markdown(\"Thus $H_0$ is rejected\"))\n",
+ " \n",
+ "assign(0.1)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 39,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "3.320391543176363\n",
+ "0.0008989127881156023 0.05 False\n"
+ ]
+ },
+ {
+ "data": {
+ "text/markdown": [
+ "Thus $H_0$ is rejected"
+ ],
+ "text/plain": [
+ "<IPython.core.display.Markdown object>"
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ }
+ ],
+ "source": [
+ "# Part b\n",
+ "\n",
+ "assign(0.05)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Problem 2"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### Part A\n",
+ "\n",
+ "We want a test where we handle the case of *type II* error where $H_0$ should be rejected with a probability of $0.95$ or larger if $|\\mu - 8.20| \\geq 0.03$.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 42,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "sigma_reject = 0.03"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### Part B\n",
+ "\n",
+ "The $n$ required can be found with the following formula\n",
+ "$$\n",
+ "n \\approx \\frac {(z_{\\alpha / 2} + z_\\beta)^2 \\sigma^2} {(\\mu_s - \\mu)^2} \\,,\n",
+ "$$\n",
+ "where $n$ is the number of required samples to have $H_0$ rejected with probability $\\beta$ when the real mean is $\\mu_s$."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 49,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/markdown": [
+ "Thus $n$ must be $6.0$"
+ ],
+ "text/plain": [
+ "<IPython.core.display.Markdown object>"
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ }
+ ],
+ "source": [
+ "beta = 0.05\n",
+ "alpha = 0.05\n",
+ "\n",
+ "def the_z_thing(p):\n",
+ " return norm.ppf(1 - p)\n",
+ "\n",
+ "z_alpha2 = the_z_thing(alpha / 2)\n",
+ "z_beta = the_z_thing(beta)\n",
+ "\n",
+ "n = ((z_alpha2 + z_beta)**2 * sigma**2) / ((mu + sigma_reject - mu)**2)\n",
+ "display(Markdown(f\"Thus $n$ must be ${np.ceil(n)}$\"))"
+ ]
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 3",
+ "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.9.2"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 4
+}