{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import math\n", "from scipy.stats import norm, t" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "s1 = [95, 90, 91, 92]\n", "s2 = [97, 94, 89, 90]\n", "s3 = [85, 84, 79, 80]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "m1 92.0\n", "m2 92.5\n", "m3 82.0\n" ] } ], "source": [ "# calculate the means\n", "m1 = np.average(s1)\n", "m2 = np.average(s2)\n", "m3 = np.average(s3)\n", "print(\"m1\", m1)\n", "print(\"m2\", m2)\n", "print(\"m3\", m3)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "v1 4.666666666666667\n", "v2 13.666666666666666\n", "v3 8.666666666666666\n" ] } ], "source": [ "# calculate the variances (w/ bessel's correction)\n", "# ddof = 1 has it use bessel's correction\n", "v1 = np.var(s1, ddof = 1)\n", "v2 = np.var(s2, ddof = 1)\n", "v3 = np.var(s3, ddof = 1)\n", "print(\"v1\", v1)\n", "print(\"v2\", v2)\n", "print(\"v3\", v3)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "sample 1 vs 2 t value 0.2335496832484569\n" ] } ], "source": [ "# calculate the t-value\n", "s1_n = len(s1)\n", "s2_n = len(s2)\n", "# numerator\n", "signal = abs(m1 - m2)\n", "# denominator\n", "noise = math.sqrt((v1/s1_n) + (v2/s2_n))\n", "t_val = signal / noise\n", "print(\"sample 1 vs 2 t value\", t_val)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.41154942928551863\n" ] } ], "source": [ "# find the p-value via the scipy package:\n", "# use the cdf of t values to see how far through\n", "# the distribution this t value is\n", "# pass in the degrees of freedom\n", "# This is the one-tailed value\n", "print(1 - t.cdf(t_val, df = (s1_n + s2_n - 2)))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.8.12" } }, "nbformat": 4, "nbformat_minor": 4 }