{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Automatidata project \n",
    "**Course 3 - The Power of Statistics**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "WVeXS7BNzX6e"
   },
   "source": [
    "You are a data professional in a data consulting firm, called Automatidata. The current project for their newest client, the New York City Taxi & Limousine Commission (New York City TLC) is reaching its midpoint, having completed a project proposal, Python coding work, and exploratory data analysis.\n",
    "\n",
    "You receive a new email from Uli King, Automatidata’s project manager. Uli tells your team about a new request from the New York City TLC: to analyze the relationship between fare amount and payment type. A follow-up email from Luana includes your specific assignment: to conduct an A/B test. \n",
    "\n",
    "A notebook was structured and prepared to help you in this project. Please complete the following questions.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "rgSbVJvomcVa"
   },
   "source": [
    "# Course 3 End-of-course project: Statistical analysis\n",
    "\n",
    "In this activity, you will practice using statistics to analyze and interpret data. The activity covers fundamental concepts such as descriptive statistics and hypothesis testing. You will explore the data provided and conduct A/B and hypothesis testing.  \n",
    "<br/>   \n",
    "\n",
    "**The purpose** of this project is to demonstrate knowledge of how to prepare, create, and analyze A/B tests. Your A/B test results should aim to find ways to generate more revenue for taxi cab drivers.\n",
    "\n",
    "**Note:** For the purpose of this exercise, assume that the sample data comes from an experiment in which customers are randomly selected and divided into two groups: 1) customers who are required to pay with credit card, 2) customers who are required to pay with cash. Without this assumption, we cannot draw causal conclusions about how payment method affects fare amount.\n",
    "\n",
    "**The goal** is to apply descriptive statistics and hypothesis testing in Python. The goal for this A/B test is to sample data and analyze whether there is a relationship between payment type and fare amount. For example: discover if customers who use credit cards pay higher fare amounts than customers who use cash.\n",
    "  \n",
    "*This activity has four parts:*\n",
    "\n",
    "**Part 1:** Imports and data loading\n",
    "* What data packages will be necessary for hypothesis testing?\n",
    "\n",
    "**Part 2:** Conduct EDA and hypothesis testing\n",
    "* How did computing descriptive statistics help you analyze your data? \n",
    "\n",
    "* How did you formulate your null hypothesis and alternative hypothesis? \n",
    "\n",
    "**Part 3:** Communicate insights with stakeholders\n",
    "\n",
    "* What key business insight(s) emerged from your A/B test?\n",
    "\n",
    "* What business recommendations do you propose based on your results?\n",
    "\n",
    "<br/> \n",
    "Follow the instructions and answer the questions below to complete the activity. Then, you will complete an Executive Summary using the questions listed on the PACE Strategy Document.\n",
    "\n",
    "Be sure to complete this activity before moving on. The next course item will provide you with a completed exemplar to compare to your own work. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "55zRrnwOelDs"
   },
   "source": [
    "# **Conduct an A/B test**\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "N8qrn_UAzX6j"
   },
   "source": [
    "<img src=\"images/Pace.png\" width=\"100\" height=\"100\" align=left>\n",
    "\n",
    "# **PACE stages**\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Throughout these project notebooks, you'll see references to the problem-solving framework PACE. The following notebook components are labeled with the respective PACE stage: Plan, Analyze, Construct, and Execute."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "Bb1HNu3FzX6k"
   },
   "source": [
    "<img src=\"images/Plan.png\" width=\"100\" height=\"100\" align=left>\n",
    "\n",
    "\n",
    "## PACE: Plan \n",
    "\n",
    "In this stage, consider the following questions where applicable to complete your code response:\n",
    "1. What is your research question for this data project? Later on, you will need to formulate the null and alternative hypotheses as the first step of your hypothesis test. Consider your research question now, at the start of this task.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The research question for this data project: \"Is there a relationship between fare amount and payment type?\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "Kp3uDhhSg_qu"
   },
   "source": [
    "*Complete the following steps to perform statistical analysis of your data:* "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "xgQEdIN7f1Jq"
   },
   "source": [
    "### Task 1. Imports and data loading"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "ehehvUzFj0-e"
   },
   "source": [
    "Import packages and libraries needed to compute descriptive statistics and conduct a hypothesis test."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "e6cryW4WgiIL"
   },
   "source": [
    "<details>\n",
    "  <summary><h4><strong>Hint: </strong></h4></summary>\n",
    "\n",
    "Before you begin, recall the following Python packages and functions that may be useful:\n",
    "\n",
    "*Main functions*: stats.ttest_ind(a, b, equal_var)\n",
    "\n",
    "*Other functions*: mean() \n",
    "\n",
    "*Packages*: pandas, stats.scipy\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "id": "1Q-GL2UrgWWG"
   },
   "outputs": [],
   "source": [
    "#==> ENTER YOUR CODE HERE\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from scipy import stats"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "IHSa7TiTkg78"
   },
   "source": [
    "**Note:** As shown in this cell, the dataset has been automatically loaded in for you. You do not need to download the .csv file, or provide more code, in order to access the dataset and proceed with this lab. Please continue with this activity by completing the following instructions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "id": "SiuhAz57klHJ"
   },
   "outputs": [],
   "source": [
    "# Load dataset into dataframe\n",
    "taxi_data = pd.read_csv(\"2017_Yellow_Taxi_Trip_Data.csv\", index_col = 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "c8paWsdKzX6o"
   },
   "source": [
    "<img src=\"images/Analyze.png\" width=\"100\" height=\"100\" align=left>\n",
    "\n",
    "<img src=\"images/Construct.png\" width=\"100\" height=\"100\" align=left>\n",
    "\n",
    "## PACE: **Analyze and Construct**\n",
    "\n",
    "In this stage, consider the following questions where applicable to complete your code response:\n",
    "1. Data professionals use descriptive statistics for Exploratory Data Analysis. How can computing descriptive statistics help you learn more about your data in this stage of your analysis?\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Descriptive statistics are used to explore and summarize data."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "UKhYTdVhf5B2"
   },
   "source": [
    "### Task 2. Data exploration\n",
    "\n",
    "Use descriptive statistics to conduct Exploratory Data Analysis (EDA). "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "  <summary><h4><strong>Hint: </strong></h4></summary>\n",
    "\n",
    "Refer back to *Self Review Descriptive Statistics* for this step-by-step proccess.\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "JAq7ag0pTNgl"
   },
   "source": [
    "**Note:** In the dataset, `payment_type` is encoded in integers:\n",
    "*   1: Credit card\n",
    "*   2: Cash\n",
    "*   3: No charge\n",
    "*   4: Dispute\n",
    "*   5: Unknown\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "id": "70vpvcv1gArp",
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "           VendorID    tpep_pickup_datetime   tpep_dropoff_datetime  \\\n",
      "24870114          2   03/25/2017 8:55:43 AM   03/25/2017 9:09:47 AM   \n",
      "35634249          1   04/11/2017 2:53:28 PM   04/11/2017 3:19:58 PM   \n",
      "106203690         1   12/15/2017 7:26:56 AM   12/15/2017 7:34:08 AM   \n",
      "38942136          2   05/07/2017 1:17:59 PM   05/07/2017 1:48:14 PM   \n",
      "30841670          2  04/15/2017 11:32:20 PM  04/15/2017 11:49:03 PM   \n",
      "\n",
      "           passenger_count  trip_distance  RatecodeID store_and_fwd_flag  \\\n",
      "24870114                 6           3.34           1                  N   \n",
      "35634249                 1           1.80           1                  N   \n",
      "106203690                1           1.00           1                  N   \n",
      "38942136                 1           3.70           1                  N   \n",
      "30841670                 1           4.37           1                  N   \n",
      "\n",
      "           PULocationID  DOLocationID  payment_type  fare_amount  extra  \\\n",
      "24870114            100           231             1         13.0    0.0   \n",
      "35634249            186            43             1         16.0    0.0   \n",
      "106203690           262           236             1          6.5    0.0   \n",
      "38942136            188            97             1         20.5    0.0   \n",
      "30841670              4           112             2         16.5    0.5   \n",
      "\n",
      "           mta_tax  tip_amount  tolls_amount  improvement_surcharge  \\\n",
      "24870114       0.5        2.76           0.0                    0.3   \n",
      "35634249       0.5        4.00           0.0                    0.3   \n",
      "106203690      0.5        1.45           0.0                    0.3   \n",
      "38942136       0.5        6.39           0.0                    0.3   \n",
      "30841670       0.5        0.00           0.0                    0.3   \n",
      "\n",
      "           total_amount  \n",
      "24870114          16.56  \n",
      "35634249          20.80  \n",
      "106203690          8.75  \n",
      "38942136          27.69  \n",
      "30841670          17.80  \n",
      "            VendorID   tpep_pickup_datetime  tpep_dropoff_datetime  \\\n",
      "count   22699.000000                  22699                  22699   \n",
      "unique           NaN                  22687                  22688   \n",
      "top              NaN  07/03/2017 3:45:19 PM  10/18/2017 8:07:45 PM   \n",
      "freq             NaN                      2                      2   \n",
      "mean        1.556236                    NaN                    NaN   \n",
      "std         0.496838                    NaN                    NaN   \n",
      "min         1.000000                    NaN                    NaN   \n",
      "25%         1.000000                    NaN                    NaN   \n",
      "50%         2.000000                    NaN                    NaN   \n",
      "75%         2.000000                    NaN                    NaN   \n",
      "max         2.000000                    NaN                    NaN   \n",
      "\n",
      "        passenger_count  trip_distance    RatecodeID store_and_fwd_flag  \\\n",
      "count      22699.000000   22699.000000  22699.000000              22699   \n",
      "unique              NaN            NaN           NaN                  2   \n",
      "top                 NaN            NaN           NaN                  N   \n",
      "freq                NaN            NaN           NaN              22600   \n",
      "mean           1.642319       2.913313      1.043394                NaN   \n",
      "std            1.285231       3.653171      0.708391                NaN   \n",
      "min            0.000000       0.000000      1.000000                NaN   \n",
      "25%            1.000000       0.990000      1.000000                NaN   \n",
      "50%            1.000000       1.610000      1.000000                NaN   \n",
      "75%            2.000000       3.060000      1.000000                NaN   \n",
      "max            6.000000      33.960000     99.000000                NaN   \n",
      "\n",
      "        PULocationID  DOLocationID  payment_type   fare_amount         extra  \\\n",
      "count   22699.000000  22699.000000  22699.000000  22699.000000  22699.000000   \n",
      "unique           NaN           NaN           NaN           NaN           NaN   \n",
      "top              NaN           NaN           NaN           NaN           NaN   \n",
      "freq             NaN           NaN           NaN           NaN           NaN   \n",
      "mean      162.412353    161.527997      1.336887     13.026629      0.333275   \n",
      "std        66.633373     70.139691      0.496211     13.243791      0.463097   \n",
      "min         1.000000      1.000000      1.000000   -120.000000     -1.000000   \n",
      "25%       114.000000    112.000000      1.000000      6.500000      0.000000   \n",
      "50%       162.000000    162.000000      1.000000      9.500000      0.000000   \n",
      "75%       233.000000    233.000000      2.000000     14.500000      0.500000   \n",
      "max       265.000000    265.000000      4.000000    999.990000      4.500000   \n",
      "\n",
      "             mta_tax    tip_amount  tolls_amount  improvement_surcharge  \\\n",
      "count   22699.000000  22699.000000  22699.000000           22699.000000   \n",
      "unique           NaN           NaN           NaN                    NaN   \n",
      "top              NaN           NaN           NaN                    NaN   \n",
      "freq             NaN           NaN           NaN                    NaN   \n",
      "mean        0.497445      1.835781      0.312542               0.299551   \n",
      "std         0.039465      2.800626      1.399212               0.015673   \n",
      "min        -0.500000      0.000000      0.000000              -0.300000   \n",
      "25%         0.500000      0.000000      0.000000               0.300000   \n",
      "50%         0.500000      1.350000      0.000000               0.300000   \n",
      "75%         0.500000      2.450000      0.000000               0.300000   \n",
      "max         0.500000    200.000000     19.100000               0.300000   \n",
      "\n",
      "        total_amount  \n",
      "count   22699.000000  \n",
      "unique           NaN  \n",
      "top              NaN  \n",
      "freq             NaN  \n",
      "mean       16.310502  \n",
      "std        16.097295  \n",
      "min      -120.300000  \n",
      "25%         8.750000  \n",
      "50%        11.800000  \n",
      "75%        17.800000  \n",
      "max      1200.290000  \n"
     ]
    }
   ],
   "source": [
    "#==> ENTER YOUR CODE HERE\n",
    "print(taxi_data.head())\n",
    "print(taxi_data.describe(include=\"all\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "-fFiNg4hdGBK"
   },
   "source": [
    "You are interested in the relationship between payment type and the fare amount the customer pays. One approach is to look at the average fare amount for each payment type. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "id": "2LGEWrdVXgI2"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>fare_amount</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>payment_type</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Credit card</th>\n",
       "      <td>13.429748</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cash</th>\n",
       "      <td>12.213546</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>No charge</th>\n",
       "      <td>12.186116</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Dispute</th>\n",
       "      <td>9.913043</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              fare_amount\n",
       "payment_type             \n",
       "Credit card     13.429748\n",
       "Cash            12.213546\n",
       "No charge       12.186116\n",
       "Dispute          9.913043"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#==> ENTER YOUR CODE HERE\n",
    "taxi_data_r = taxi_data.copy()\n",
    "taxi_data_r[\"payment_type\"].replace({1: \"Credit card\", 2: \"Cash\", 3: \"No charge\", 4: \"Dispute\", 5: \"Unknown\", 6: \"Voided trip\"}, inplace=True)\n",
    "taxi_data_r[[\"payment_type\", \"fare_amount\"]].groupby(\"payment_type\", sort=False).mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "Z_u-zWvGdfII"
   },
   "source": [
    "Based on the averages shown, it appears that customers who pay in credit card tend to pay a larger fare amount than customers who pay in cash. However, this difference might arise from random sampling, rather than being a true difference in fare amount. To assess whether the difference is statistically significant, you conduct a hypothesis test."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "QpFRQDaNzX6p"
   },
   "source": [
    "\n",
    "### Task 3. Hypothesis testing\n",
    "\n",
    "Before you conduct your hypothesis test, consider the following questions where applicable to complete your code response:\n",
    "\n",
    "1. Recall the difference between the null hypothesis and the alternative hypotheses. Consider your hypotheses for this project as listed below.\n",
    "\n",
    "$H_0$: There is no difference in the average fare amount between customers who use credit cards and customers who use cash.\n",
    "\n",
    "$H_A$: There is a difference in the average fare amount between customers who use credit cards and customers who use cash."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "7Mn5GJ460HUS"
   },
   "source": [
    "\n",
    "\n",
    "Your goal in this step is to conduct a two-sample t-test. Recall the steps for conducting a hypothesis test: \n",
    "\n",
    "\n",
    "1.   State the null hypothesis and the alternative hypothesis\n",
    "2.   Choose a signficance level\n",
    "3.   Find the p-value\n",
    "4.   Reject or fail to reject the null hypothesis \n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "leLgy_x_hKgA"
   },
   "source": [
    "**Note:** For the purpose of this exercise, your hypothesis test is the main component of your A/B test. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "IVHpnfvNeTUA"
   },
   "source": [
    "You choose 5% as the significance level and proceed with a two-sample t-test."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "id": "rdlsXg7Dhbrd"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Ttest_indResult(statistic=6.866800855655372, pvalue=6.797387473030518e-12)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#==> ENTER YOUR CODE HERE\n",
    "significance_level = 0.05\n",
    "a = taxi_data_r[taxi_data_r[\"payment_type\"] == \"Credit card\"][\"fare_amount\"]\n",
    "b = taxi_data_r[taxi_data_r[\"payment_type\"] == \"Cash\"][\"fare_amount\"]\n",
    "stats.ttest_ind(a=a, b=b, equal_var=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "With a p-value (6.8e-12) being less than 0.05 (as the significance level is 5%), reject the null hypothesis in favor of the alternative hypothesis.\n",
    "\n",
    "Therefore, we can conclude at the 5% significance level that there is a statistically significant difference in the average fare amount between customers who use credit cards and customers who use cash."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "Ab4e-W3mzX6r"
   },
   "source": [
    "<img src=\"images/Execute.png\" width=\"100\" height=\"100\" align=left>\n",
    "\n",
    "## PACE: **Execute**\n",
    "\n",
    "Consider the questions in your PACE Strategy Document to reflect on the Execute stage."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "h1PRfiD3hcwE"
   },
   "source": [
    "### Task 4. Communicate insights with stakeholders"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "ItreFaZKhvVw"
   },
   "source": [
    "*Ask yourself the following questions:*\n",
    "\n",
    "1. What business insight(s) can you draw from the result of your hypothesis test?\n",
    "2. Consider why this A/B test project might not be realistic, and what assumptions had to be made for this educational project."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1) We can conclude at the 5% significance level that customers who pay in credit card tend to pay a larger fare amount than customers who pay in cash. Hence, encouraging customers to pay with credit cards can generate more revenue for taxi cab drivers.  \n",
    "2) For the purpose of this project, the following assumptions were made: the sample data comes from an experiment in which customers are randomly selected and divided into two groups (customers who are required to pay with credit card, customers who are required to pay with cash). Without this assumption, we cannot draw causal conclusions about how payment method affects fare amount. Also, riders might not carry lots of cash, so it's easier to pay for more expensive trips with a credit card. Thus, it's far more likely that fare amount determines payment type, rather than vice versa."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Congratulations!** You've completed this lab. However, you may not notice a green check mark next to this item on Coursera's platform. Please continue your progress regardless of the check mark. Just click on the \"save\" icon at the top of this notebook to ensure your work has been logged."
   ]
  }
 ],
 "metadata": {
  "colab": {
   "collapsed_sections": [],
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
