{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a2c73f2a",
   "metadata": {},
   "source": [
    "# What will be the largest company in the world by December 2026?\n",
    "\n",
    "Analysis notebook for `MKT-013`. Runs top to bottom with no network access. It loads the committed CSVs under `data/`, recomputes the devigged market mid, the forecast deviations (Table 1), and the Brier scoring scenarios (Table 3), and regenerates the Vega-Lite specs under `figures/`."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74105094",
   "metadata": {},
   "source": [
    "## Data loading (cell: load)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "546ff6a1",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-06-22T21:56:53.247032Z",
     "iopub.status.busy": "2026-06-22T21:56:53.246823Z",
     "iopub.status.idle": "2026-06-22T21:56:54.129894Z",
     "shell.execute_reply": "2026-06-22T21:56:54.129508Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['standings', 'forecast-vs-market', 'throne-timeline', 'scoring-scenarios']"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import json\n",
    "import pathlib\n",
    "import pandas as pd\n",
    "import yaml\n",
    "\n",
    "PAPER_DIR = pathlib.Path('.').resolve()\n",
    "DATA_DIR = PAPER_DIR / 'data'\n",
    "FIG_DIR = PAPER_DIR / 'figures'\n",
    "\n",
    "with open(DATA_DIR / 'manifest.yaml') as f:\n",
    "    manifest = yaml.safe_load(f)\n",
    "datasets = {d['id']: d for d in manifest['datasets']}\n",
    "\n",
    "standings = pd.read_csv(DATA_DIR / 'standings.csv')\n",
    "forecast = pd.read_csv(DATA_DIR / 'forecast_vs_market.csv')\n",
    "throne = pd.read_csv(DATA_DIR / 'throne_timeline.csv')\n",
    "scoring = pd.read_csv(DATA_DIR / 'scoring_scenarios.csv')\n",
    "list(datasets)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0cdf563b",
   "metadata": {},
   "source": [
    "## The forecast and its tilt (cell: tilt)\n",
    "\n",
    "Devig the raw Polymarket yes prices by Equation (1) and compute the ATOL deviation from the devigged mid per candidate (Table 1)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5685f29d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-06-22T21:56:54.131480Z",
     "iopub.status.busy": "2026-06-22T21:56:54.131378Z",
     "iopub.status.idle": "2026-06-22T21:56:54.150268Z",
     "shell.execute_reply": "2026-06-22T21:56:54.149854Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "overround = 1.029\n"
     ]
    },
    {
     "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>source</th>\n",
       "      <th>ATOL</th>\n",
       "      <th>market_devig</th>\n",
       "      <th>deviation</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>candidate</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Nvidia</th>\n",
       "      <td>0.85</td>\n",
       "      <td>0.719</td>\n",
       "      <td>0.131</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Alphabet</th>\n",
       "      <td>0.08</td>\n",
       "      <td>0.107</td>\n",
       "      <td>-0.027</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Apple</th>\n",
       "      <td>0.04</td>\n",
       "      <td>0.090</td>\n",
       "      <td>-0.050</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SpaceX</th>\n",
       "      <td>0.01</td>\n",
       "      <td>0.045</td>\n",
       "      <td>-0.035</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Field</th>\n",
       "      <td>0.02</td>\n",
       "      <td>0.039</td>\n",
       "      <td>-0.019</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "source     ATOL  market_devig  deviation\n",
       "candidate                               \n",
       "Nvidia     0.85         0.719      0.131\n",
       "Alphabet   0.08         0.107     -0.027\n",
       "Apple      0.04         0.090     -0.050\n",
       "SpaceX     0.01         0.045     -0.035\n",
       "Field      0.02         0.039     -0.019"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "raw = {'Nvidia': 0.740, 'Alphabet': 0.110, 'Apple': 0.093, 'SpaceX': 0.046, 'Field': 0.040}\n",
    "overround = sum(raw.values())\n",
    "devig = {k: v / overround for k, v in raw.items()}\n",
    "\n",
    "wide = forecast.pivot(index='candidate', columns='source', values='p')\n",
    "wide['market_devig'] = wide.index.map(devig)\n",
    "wide['deviation'] = wide['ATOL'] - wide['market_devig']\n",
    "print('overround =', round(overround, 3))\n",
    "wide.loc[['Nvidia', 'Alphabet', 'Apple', 'SpaceX', 'Field'], ['ATOL', 'market_devig', 'deviation']].round(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a06d21b7",
   "metadata": {},
   "source": [
    "## Scoring scenarios (cell: scoring)\n",
    "\n",
    "Brier score (Eq. 2) and paired advantage (Eq. 3) for the forecaster (p_true = 0.85) and the market null (mid = 0.72) under each resolution, plus the expected paired advantage under each party's beliefs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "dd7781f5",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-06-22T21:56:54.151538Z",
     "iopub.status.busy": "2026-06-22T21:56:54.151453Z",
     "iopub.status.idle": "2026-06-22T21:56:54.158521Z",
     "shell.execute_reply": "2026-06-22T21:56:54.157627Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "E[delta] under our belief  = 0.0169\n",
      "E[delta] under market belief = -0.0169\n"
     ]
    },
    {
     "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>y</th>\n",
       "      <th>forecaster</th>\n",
       "      <th>market</th>\n",
       "      <th>delta_to_forecaster</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0.0225</td>\n",
       "      <td>0.0784</td>\n",
       "      <td>0.0559</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>0.7225</td>\n",
       "      <td>0.5184</td>\n",
       "      <td>-0.2041</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   y  forecaster  market  delta_to_forecaster\n",
       "0  1      0.0225  0.0784               0.0559\n",
       "1  0      0.7225  0.5184              -0.2041"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "P_TRUE = 0.85\n",
    "NULL = 0.72\n",
    "\n",
    "def brier(p, y):\n",
    "    return (p - y) ** 2\n",
    "\n",
    "rows = []\n",
    "for y in (1, 0):\n",
    "    bf, bm = brier(P_TRUE, y), brier(NULL, y)\n",
    "    rows.append({'y': y, 'forecaster': round(bf, 4), 'market': round(bm, 4), 'delta_to_forecaster': round(bm - bf, 4)})\n",
    "sc = pd.DataFrame(rows)\n",
    "\n",
    "adv_y1 = sc.loc[sc.y == 1, 'delta_to_forecaster'].iloc[0]\n",
    "adv_y0 = sc.loc[sc.y == 0, 'delta_to_forecaster'].iloc[0]\n",
    "exp_ours = P_TRUE * adv_y1 + (1 - P_TRUE) * adv_y0\n",
    "exp_market = NULL * adv_y1 + (1 - NULL) * adv_y0\n",
    "print('E[delta] under our belief  =', round(exp_ours, 4))\n",
    "print('E[delta] under market belief =', round(exp_market, 4))\n",
    "# cross-check against the committed scoring_scenarios.csv\n",
    "assert abs(scoring['brier'].sum() - (sc['forecaster'].sum() + sc['market'].sum())) < 1e-6\n",
    "sc"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64155852",
   "metadata": {},
   "source": [
    "## Figures\n",
    "\n",
    "Each cell regenerates one Vega-Lite spec under `figures/`. The specs carry no width/height, no colors, and no title; the ATOL chart bootstrap supplies the pillar theme."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "951f6bdb",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-06-22T21:56:54.159697Z",
     "iopub.status.busy": "2026-06-22T21:56:54.159611Z",
     "iopub.status.idle": "2026-06-22T21:56:54.162256Z",
     "shell.execute_reply": "2026-06-22T21:56:54.161841Z"
    }
   },
   "outputs": [],
   "source": [
    "# cell: fig-standings\n",
    "spec = {\n",
    "    '$schema': 'https://vega.github.io/schema/vega-lite/v5.json',\n",
    "    'data': {'url': 'data/standings.csv'},\n",
    "    'mark': 'bar',\n",
    "    'encoding': {\n",
    "        'y': {'field': 'company', 'type': 'nominal', 'title': 'Company', 'sort': '-x'},\n",
    "        'x': {'field': 'market_cap_usd_tn', 'type': 'quantitative', 'title': 'Market capitalization (USD trillions)'},\n",
    "    },\n",
    "}\n",
    "with open(FIG_DIR / '01-standings.vega.json', 'w') as f:\n",
    "    json.dump(spec, f, indent=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "96cd442b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-06-22T21:56:54.163283Z",
     "iopub.status.busy": "2026-06-22T21:56:54.163198Z",
     "iopub.status.idle": "2026-06-22T21:56:54.165616Z",
     "shell.execute_reply": "2026-06-22T21:56:54.165250Z"
    }
   },
   "outputs": [],
   "source": [
    "# cell: fig-forecast\n",
    "spec = {\n",
    "    '$schema': 'https://vega.github.io/schema/vega-lite/v5.json',\n",
    "    'data': {'url': 'data/forecast_vs_market.csv'},\n",
    "    'mark': 'bar',\n",
    "    'encoding': {\n",
    "        'x': {'field': 'candidate', 'type': 'nominal', 'title': 'Candidate', 'sort': ['Nvidia', 'Alphabet', 'Apple', 'SpaceX', 'Field']},\n",
    "        'xOffset': {'field': 'source', 'sort': ['ATOL', 'Market']},\n",
    "        'y': {'field': 'p', 'type': 'quantitative', 'title': 'Probability of being #1 on 31 Dec 2026'},\n",
    "        'color': {'field': 'source', 'type': 'nominal', 'title': 'Source', 'sort': ['ATOL', 'Market']},\n",
    "    },\n",
    "}\n",
    "with open(FIG_DIR / '02-forecast-vs-market.vega.json', 'w') as f:\n",
    "    json.dump(spec, f, indent=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "36f88e00",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-06-22T21:56:54.166584Z",
     "iopub.status.busy": "2026-06-22T21:56:54.166513Z",
     "iopub.status.idle": "2026-06-22T21:56:54.168738Z",
     "shell.execute_reply": "2026-06-22T21:56:54.168388Z"
    }
   },
   "outputs": [],
   "source": "# cell: fig-throne\nspec = {\n    '$schema': 'https://vega.github.io/schema/vega-lite/v5.json',\n    'data': {'url': 'data/throne_timeline.csv'},\n    'mark': 'bar',\n    'encoding': {\n        'x': {'field': 'year', 'type': 'ordinal', 'title': 'Year (end of year; 2026 is the 22 June freeze)'},\n        'y': {'field': 'market_cap_usd_tn', 'type': 'quantitative', 'title': 'Leader market capitalization (USD trillions)'},\n        'color': {'field': 'leader', 'type': 'nominal', 'title': 'Largest company', 'sort': ['Nvidia', 'Apple', 'Microsoft', 'Saudi Aramco', 'ExxonMobil']},\n    },\n}\nwith open(FIG_DIR / '03-throne-timeline.vega.json', 'w') as f:\n    json.dump(spec, f, indent=2)"
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "50efff24",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-06-22T21:56:54.169792Z",
     "iopub.status.busy": "2026-06-22T21:56:54.169726Z",
     "iopub.status.idle": "2026-06-22T21:56:54.172298Z",
     "shell.execute_reply": "2026-06-22T21:56:54.171929Z"
    }
   },
   "outputs": [],
   "source": [
    "# cell: fig-scoring\n",
    "spec = {\n",
    "    '$schema': 'https://vega.github.io/schema/vega-lite/v5.json',\n",
    "    'data': {'url': 'data/scoring_scenarios.csv'},\n",
    "    'mark': 'bar',\n",
    "    'encoding': {\n",
    "        'x': {'field': 'outcome', 'type': 'nominal', 'title': 'Resolution', 'sort': ['Nvidia retains (y=1)', 'Nvidia loses (y=0)']},\n",
    "        'xOffset': {'field': 'party', 'sort': ['Forecaster (p=0.85)', 'Market (mid=0.72)']},\n",
    "        'y': {'field': 'brier', 'type': 'quantitative', 'title': 'Brier score (lower is better)'},\n",
    "        'color': {'field': 'party', 'type': 'nominal', 'title': 'Party', 'sort': ['Forecaster (p=0.85)', 'Market (mid=0.72)']},\n",
    "    },\n",
    "}\n",
    "with open(FIG_DIR / '05-scoring-scenarios.vega.json', 'w') as f:\n",
    "    json.dump(spec, f, indent=2)"
   ]
  }
 ],
 "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.12.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}