{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Conditional Probability Solution"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First we'll modify the code to have some fixed purchase probability regardless of age, say 40%:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from numpy import random\n",
    "random.seed(0)\n",
    "\n",
    "totals = {20:0, 30:0, 40:0, 50:0, 60:0, 70:0}\n",
    "purchases = {20:0, 30:0, 40:0, 50:0, 60:0, 70:0}\n",
    "totalPurchases = 0\n",
    "for _ in range(100000):\n",
    "    ageDecade = random.choice([20, 30, 40, 50, 60, 70])\n",
    "    purchaseProbability = 0.4\n",
    "    totals[ageDecade] += 1\n",
    "    if (random.random() < purchaseProbability):\n",
    "        totalPurchases += 1\n",
    "        purchases[ageDecade] += 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next we will compute P(E|F) for some age group, let's pick 30 year olds again:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "P(purchase | 30s): 0.3987604549010169\n"
     ]
    }
   ],
   "source": [
    "PEF = float(purchases[30]) / float(totals[30])\n",
    "print(\"P(purchase | 30s): \" + str(PEF))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we'll compute P(E)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "P(Purchase):0.4003\n"
     ]
    }
   ],
   "source": [
    "PE = float(totalPurchases) / 100000.0\n",
    "print(\"P(Purchase):\" + str(PE))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "P(E|F) is pretty darn close to P(E), so we can say that E and F are likely indepedent variables."
   ]
  },
  {
   "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.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}