diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 59b955a..59eb2a1 100755 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -12,12 +12,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "metadata": {}, "outputs": [], "source": [ - "# import numpy and pandas\n", - "\n" + "import numpy as np\n", + "import pandas as pd\n", + "import scipy.stats as st" ] }, { @@ -31,11 +32,11 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "salaries=pd.read_csv(\"Current_Employee_Names__Salaries__and_Position_Titles.csv\")" ] }, { @@ -47,12 +48,130 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
NameJob TitlesDepartmentFull or Part-TimeSalary or HourlyTypical HoursAnnual SalaryHourly Rate
0AARON, JEFFERY MSERGEANTPOLICEFSalaryNaN101442.0NaN
1AARON, KARINAPOLICE OFFICER (ASSIGNED AS DETECTIVE)POLICEFSalaryNaN94122.0NaN
2AARON, KIMBERLEI RCHIEF CONTRACT EXPEDITERGENERAL SERVICESFSalaryNaN101592.0NaN
3ABAD JR, VICENTE MCIVIL ENGINEER IVWATER MGMNTFSalaryNaN110064.0NaN
4ABASCAL, REECE ETRAFFIC CONTROL AIDE-HOURLYOEMCPHourly20.0NaN19.86
\n", + "
" + ], + "text/plain": [ + " Name Job Titles \\\n", + "0 AARON, JEFFERY M SERGEANT \n", + "1 AARON, KARINA POLICE OFFICER (ASSIGNED AS DETECTIVE) \n", + "2 AARON, KIMBERLEI R CHIEF CONTRACT EXPEDITER \n", + "3 ABAD JR, VICENTE M CIVIL ENGINEER IV \n", + "4 ABASCAL, REECE E TRAFFIC CONTROL AIDE-HOURLY \n", + "\n", + " Department Full or Part-Time Salary or Hourly Typical Hours \\\n", + "0 POLICE F Salary NaN \n", + "1 POLICE F Salary NaN \n", + "2 GENERAL SERVICES F Salary NaN \n", + "3 WATER MGMNT F Salary NaN \n", + "4 OEMC P Hourly 20.0 \n", + "\n", + " Annual Salary Hourly Rate \n", + "0 101442.0 NaN \n", + "1 94122.0 NaN \n", + "2 101592.0 NaN \n", + "3 110064.0 NaN \n", + "4 NaN 19.86 " + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n", - "\n" + "salaries.head()" ] }, { @@ -64,12 +183,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "Name 0\n", + "Job Titles 0\n", + "Department 0\n", + "Full or Part-Time 0\n", + "Salary or Hourly 0\n", + "Typical Hours 25161\n", + "Annual Salary 8022\n", + "Hourly Rate 25161\n", + "dtype: int64" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n", - "\n" + "salaries.isnull().sum()" ] }, { @@ -81,12 +218,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "Salary 25161\n", + "Hourly 8022\n", + "Name: Salary or Hourly, dtype: int64" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n", - "\n" + "salaries[\"Salary or Hourly\"].value_counts()" ] }, { @@ -105,12 +254,229 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
Name
Department
ADMIN HEARNG39
ANIMAL CONTRL81
AVIATION1629
BOARD OF ELECTION107
BOARD OF ETHICS8
BUDGET & MGMT46
BUILDINGS269
BUSINESS AFFAIRS171
CITY CLERK84
CITY COUNCIL411
COMMUNITY DEVELOPMENT207
COPA116
CULTURAL AFFAIRS65
DISABILITIES28
DoIT99
FAMILY & SUPPORT615
FINANCE560
FIRE4641
GENERAL SERVICES980
HEALTH488
HUMAN RELATIONS16
HUMAN RESOURCES79
INSPECTOR GEN87
LAW407
LICENSE APPL COMM1
MAYOR'S OFFICE85
OEMC2102
POLICE13414
POLICE BOARD2
PROCUREMENT92
PUBLIC LIBRARY1015
STREETS & SAN2198
TRANSPORTN1140
TREASURER22
WATER MGMNT1879
\n", + "
" + ], + "text/plain": [ + " Name\n", + "Department \n", + "ADMIN HEARNG 39\n", + "ANIMAL CONTRL 81\n", + "AVIATION 1629\n", + "BOARD OF ELECTION 107\n", + "BOARD OF ETHICS 8\n", + "BUDGET & MGMT 46\n", + "BUILDINGS 269\n", + "BUSINESS AFFAIRS 171\n", + "CITY CLERK 84\n", + "CITY COUNCIL 411\n", + "COMMUNITY DEVELOPMENT 207\n", + "COPA 116\n", + "CULTURAL AFFAIRS 65\n", + "DISABILITIES 28\n", + "DoIT 99\n", + "FAMILY & SUPPORT 615\n", + "FINANCE 560\n", + "FIRE 4641\n", + "GENERAL SERVICES 980\n", + "HEALTH 488\n", + "HUMAN RELATIONS 16\n", + "HUMAN RESOURCES 79\n", + "INSPECTOR GEN 87\n", + "LAW 407\n", + "LICENSE APPL COMM 1\n", + "MAYOR'S OFFICE 85\n", + "OEMC 2102\n", + "POLICE 13414\n", + "POLICE BOARD 2\n", + "PROCUREMENT 92\n", + "PUBLIC LIBRARY 1015\n", + "STREETS & SAN 2198\n", + "TRANSPORTN 1140\n", + "TREASURER 22\n", + "WATER MGMNT 1879" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n", - "\n" + "salaries.groupby(\"Department\").agg({\"Name\":\"count\"})\n" ] }, { @@ -124,12 +490,56 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 86, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Manual stat: 20.619805785494183\n", + "Manual p-value: 4.3230240486245884e-92\n", + "Python calc stat: 20.6198057854942\n", + "Python calc p-value: 4.3230240486229894e-92\n", + "I CAN reject the null hypothesis, so mu of hourly wage != 30\n" + ] + } + ], "source": [ - "# Your code here:\n", - "\n" + "#1. setting hypothesis\n", + "H0=\"mu of hourly wage = 30\"\n", + "H1=\"mu of hourly wage != 30\"\n", + "\n", + "#2. choosing significance level\n", + "alpha=0.05\n", + "\n", + "#3. sampling - not clear whether we needed to sample a certain amount out of all hourly workers OR just take all hourly workers since it's already a slice\n", + "#I went with taking all hourly workers as a sample from salaries population\n", + "h_sample=salaries[salaries[\"Salary or Hourly\"]==\"Hourly\"][\"Hourly Rate\"]\n", + "\n", + "#4. compute statistic\n", + "#I'll calculate manually to practice; then confirm with python...\n", + "mu=30\n", + "mean=h_sample.mean()\n", + "#again, not quite clear if we can just use std of the population since we have access to it? I chose to calculate sample std with ddof=1 instead and follow t distribution\n", + "s=h_sample.std(ddof=1)\n", + "n=h_sample.count()\n", + "stat=(mean-mu)/(s/np.sqrt(n))\n", + "print(\"Manual stat: \",stat)\n", + "\n", + "#5. compute p-value - two-tailed\n", + "p_value=st.t.sf(abs(stat),n-1)*2\n", + "print(\"Manual p-value: \",p_value)\n", + "\n", + "#computing both stat and p value through function:\n", + "print(\"Python calc stat: \",st.ttest_1samp(h_sample,mu)[0])\n", + "print(\"Python calc p-value: \",st.ttest_1samp(h_sample,mu)[1])\n", + "\n", + "#6. decision\n", + "if p_value>alpha:\n", + " print(\"I cannot reject the null hypothesis, which is \",H0)\n", + "else:\n", + " print(\"I CAN reject the null hypothesis, so \",H1)" ] }, { @@ -143,12 +553,44 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 87, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "statistic: 3.081997005712994\n", + "p-value: 0.0010301701775482569\n", + "I CAN reject the null hypothesis, so mu salary > 86000\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\valer.DESKTOP-8CM37D7\\AppData\\Local\\Temp\\ipykernel_23372\\2277271498.py:6: SettingWithCopyWarning: \n", + "A value is trying to be set on a copy of a slice from a DataFrame\n", + "\n", + "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n", + " p_sample.dropna(inplace=True)\n" + ] + } + ], "source": [ - "# Your code here:\n", - "\n" + "H0=\"mu salary <= 86000\"\n", + "H1=\"mu salary > 86000\"\n", + "mu=86000\n", + "alpha=0.05\n", + "p_sample=salaries[salaries[\"Department\"]==\"POLICE\"][\"Annual Salary\"]\n", + "p_sample.dropna(inplace=True)\n", + "print(\"statistic: \",st.ttest_1samp(p_sample,mu,alternative=\"greater\")[0])\n", + "#statistic with the same sign as the alternative hypothesis\n", + "print(\"p-value: \",st.ttest_1samp(p_sample,mu,alternative=\"greater\")[1])\n", + "if p_value>alpha:\n", + " print(\"I cannot reject the null hypothesis, which is \",H0)\n", + "else:\n", + " print(\"I CAN reject the null hypothesis, so \",H1)" ] }, { @@ -160,12 +602,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 73, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'STREETS & SAN'" + ] + }, + "execution_count": 73, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n", - "\n" + "pd.crosstab(salaries[\"Department\"],salaries[\"Salary or Hourly\"])[\"Hourly\"].idxmax()" ] }, { @@ -177,12 +629,44 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 89, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "statistic: -9.567447887848152\n", + "p-value: 1.6689265282353859e-21\n", + "I CAN reject the null hypothesis, so mu hourly < 35\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\valer.DESKTOP-8CM37D7\\AppData\\Local\\Temp\\ipykernel_23372\\158618580.py:6: SettingWithCopyWarning: \n", + "A value is trying to be set on a copy of a slice from a DataFrame\n", + "\n", + "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n", + " h_san_sample.dropna(inplace=True)\n" + ] + } + ], "source": [ - "# Your code here:\n", - "\n" + "H0=\"mu hourly >= 35\"\n", + "H1=\"mu hourly < 35\"\n", + "mu=35\n", + "alpha=0.05\n", + "h_san_sample=salaries[salaries[\"Department\"]==\"STREETS & SAN\"][\"Hourly Rate\"]\n", + "h_san_sample.dropna(inplace=True)\n", + "print(\"statistic: \",st.ttest_1samp(h_san_sample,mu,alternative=\"less\")[0])\n", + "#statistic with the same sign as the alternative hypothesis\n", + "print(\"p-value: \",st.ttest_1samp(h_san_sample,mu,alternative=\"less\")[1])\n", + "if p_value>alpha:\n", + " print(\"I cannot reject the null hypothesis, which is \",H0)\n", + "else:\n", + " print(\"I CAN reject the null hypothesis, so \",H1)\n" ] }, { @@ -206,12 +690,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 96, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "(32.52345834488425, 33.05365708767623)" + ] + }, + "execution_count": 96, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n", - "\n" + "h_sample=salaries[salaries[\"Salary or Hourly\"]==\"Hourly\"][\"Hourly Rate\"]\n", + "mean=h_sample.mean()\n", + "c=0.95\n", + "n=h_sample.count()\n", + "s = h_sample.std(ddof=1)\n", + "st.t.interval(c,n-1,loc=mean,scale=s/np.sqrt(n))" ] }, { @@ -223,12 +722,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 97, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "(86177.05631531785, 86795.77269094893)" + ] + }, + "execution_count": 97, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n", - "\n" + "mean=p_sample.mean()\n", + "c=0.95\n", + "n=p_sample.count()\n", + "s = p_sample.std(ddof=1)\n", + "st.t.interval(c,n-1,loc=mean,scale=s/np.sqrt(n))" ] }, { @@ -246,12 +759,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 113, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(-3.5099964213703005, 0.0004481127249057967)\n", + "I CAN reject the null hypothesis, so p != 0.25\n" + ] + } + ], "source": [ - "# Your code here:\n", - "\n" + "H0=\"p = 0.25\"\n", + "H1=\"p != 0.25\"\n", + "\n", + "alpha=0.05\n", + "\n", + "from statsmodels.stats.proportion import proportions_ztest\n", + "p=0.25\n", + "success=salaries[salaries[\"Salary or Hourly\"]==\"Hourly\"][\"Name\"].count()\n", + "n=salaries[\"Name\"].count()\n", + "\n", + "p_value=proportions_ztest(success, n, p)\n", + "print(p_value)\n", + "if p_value[1]>alpha:\n", + " print(\"I cannot reject the null hypothesis, which is \",H0)\n", + "else:\n", + " print(\"I CAN reject the null hypothesis, so \",H1)\n" ] } ], @@ -271,7 +807,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.11.4" } }, "nbformat": 4,