{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Prediction Market Arbitrage Scanner\n",
        "\n",
        "Build a scanner that looks for mispriced event probabilities across prediction markets or within related contracts.\n",
        "\n",
        "**How to use this notebook:** We give you the structure, the key ideas, starter code, and places where you need to make the important research decisions yourself."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## What you should have by the end\n",
        "\n",
        "- A working prototype\n",
        "- A few graphs/tables that explain what your model is doing\n",
        "- A short write-up of your assumptions\n",
        "- At least one extension that makes the project feel like yours"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Concept\n",
        "\n",
        "Prediction markets quote probabilities as prices. A simple arbitrage idea is: if a set of mutually exclusive outcomes has probabilities that add to less than 1, you may be able to buy all outcomes and lock in a payout.\n",
        "\n",
        "In practice, fees, liquidity, settlement risk, and market rules make this much harder."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Starter Code\n",
        "\n",
        "1. Represent prediction contracts as probabilities or prices.\n",
        "2. Check whether mutually exclusive outcomes sum below or above 1.\n",
        "3. Add fees and spreads.\n",
        "4. Flag only opportunities that survive costs.\n",
        "5. Explain why many signals are fake."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import pandas as pd\n",
        "import numpy as np\n",
        "\n",
        "# Toy market data.\n",
        "# TODO: later replace this with API data or scraped data you are allowed to use.\n",
        "markets = pd.DataFrame({\n",
        "    \"event_id\": [\"election_1\"] * 3 + [\"rates_1\"] * 3,\n",
        "    \"outcome\": [\"A wins\", \"B wins\", \"Other\", \"Cut\", \"Hold\", \"Hike\"],\n",
        "    \"ask_price\": [0.47, 0.48, 0.03, 0.28, 0.62, 0.08],\n",
        "    \"bid_price\": [0.45, 0.46, 0.02, 0.26, 0.60, 0.06],\n",
        "    \"fee_rate\": [0.02] * 6\n",
        "})\n",
        "\n",
        "markets"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def scan_complete_set_arbitrage(df):\n",
        "    # Find events where buying every outcome costs less than a unit payout.\n",
        "    # TODO:\n",
        "    # - Include position sizing.\n",
        "    # - Include market-specific fees.\n",
        "    # - Handle incomplete outcome sets.\n",
        "    # - Add liquidity constraints.\n",
        "    rows = []\n",
        "    for event_id, group in df.groupby(\"event_id\"):\n",
        "        gross_cost = group[\"ask_price\"].sum()\n",
        "        estimated_fees = (group[\"ask_price\"] * group[\"fee_rate\"]).sum()\n",
        "        total_cost = gross_cost + estimated_fees\n",
        "        edge = 1.0 - total_cost\n",
        "\n",
        "        rows.append({\n",
        "            \"event_id\": event_id,\n",
        "            \"gross_cost\": gross_cost,\n",
        "            \"estimated_fees\": estimated_fees,\n",
        "            \"total_cost\": total_cost,\n",
        "            \"edge_before_slippage\": edge,\n",
        "            \"possible_arbitrage\": edge > 0\n",
        "        })\n",
        "    return pd.DataFrame(rows)\n",
        "\n",
        "scan_complete_set_arbitrage(markets)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Open-ended extensions\n",
        "\n",
        "- Add cross-market comparison for the same event\n",
        "- Add fee structures for different venues\n",
        "- Add liquidity and maximum executable size\n",
        "- Detect logically related contracts, for example candidate wins state vs candidate wins election\n",
        "- Build a small dashboard with latest opportunities and reasons they may be invalid"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Final write-up prompts\n",
        "\n",
        "Answer these before putting the project on your resume:\n",
        "\n",
        "1. What problem were you trying to solve?\n",
        "2. What assumptions did your model make?\n",
        "3. Where did the model work well?\n",
        "4. Where did it fail?\n",
        "5. What would you improve with more time?\n",
        "6. What is one interesting thing you learned that was not obvious before starting?"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3.x"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
