Skip to content

Commit

Permalink
moved functions from py script to code cell
Browse files Browse the repository at this point in the history
  • Loading branch information
Cheffrey2000 committed Jun 2, 2023
1 parent 87c51ea commit 3b82cdd
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 48 deletions.
46 changes: 0 additions & 46 deletions flatiron_stats.py

This file was deleted.

58 changes: 56 additions & 2 deletions index.ipynb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand All @@ -18,6 +19,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand All @@ -27,6 +29,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -243,6 +246,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -471,7 +475,51 @@
"outputs": [],
"source": [
"# __SOLUTION__ \n",
"import flatiron_stats as fs"
"import numpy as np\n",
"import scipy.stats as stats\n",
"\n",
"def welch_t(a, b):\n",
" \n",
" \"\"\" Calculate Welch's t statistic for two samples. \"\"\"\n",
"\n",
" numerator = a.mean() - b.mean()\n",
" \n",
" # “ddof = Delta Degrees of Freedom”: the divisor used in the calculation is N - ddof, \n",
" # where N represents the number of elements. By default ddof is zero.\n",
" \n",
" denominator = np.sqrt(a.var(ddof=1)/a.size + b.var(ddof=1)/b.size)\n",
" \n",
" return np.abs(numerator/denominator)\n",
"\n",
"def welch_df(a, b):\n",
" \n",
" \"\"\" Calculate the effective degrees of freedom for two samples. This function returns the degrees of freedom \"\"\"\n",
" \n",
" s1 = a.var(ddof=1) \n",
" s2 = b.var(ddof=1)\n",
" n1 = a.size\n",
" n2 = b.size\n",
" \n",
" numerator = (s1/n1 + s2/n2)**2\n",
" denominator = (s1/ n1)**2/(n1 - 1) + (s2/ n2)**2/(n2 - 1)\n",
" \n",
" return numerator/denominator\n",
"\n",
"\n",
"def p_value_welch_ttest(a, b, two_sided=False):\n",
" \"\"\"Calculates the p-value for Welch's t-test given two samples.\n",
" By default, the returned p-value is for a one-sided t-test. \n",
" Set the two-sided parameter to True if you wish to perform a two-sided t-test instead.\n",
" \"\"\"\n",
" t = welch_t(a, b)\n",
" df = welch_df(a, b)\n",
" \n",
" p = 1-stats.t.cdf(np.abs(t), df)\n",
" \n",
" if two_sided:\n",
" return 2*p\n",
" else:\n",
" return p"
]
},
{
Expand All @@ -492,10 +540,11 @@
],
"source": [
"# __SOLUTION__ \n",
"fs.p_value_welch_ttest(control.click, experiment.click)"
"p_value_welch_ttest(control.click, experiment.click)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand All @@ -514,6 +563,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -551,6 +601,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -610,6 +661,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -647,6 +699,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand All @@ -670,6 +723,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down

0 comments on commit 3b82cdd

Please sign in to comment.