{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ethical-chinese",
   "metadata": {},
   "source": [
    "# Learning a causal model\n",
    "\n",
    "The following example is adapted from \"Elements of Causal Inference\" by Jonas Peters, Dominik Janzig, and Bernhard Schölkopf (2017).\n",
    "See also Jonas Peters great [4-part lecture series on causality](https://www.youtube.com/watch?v=zvrcyqcN9Wo)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "transsexual-moore",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.linear_model import LinearRegression, LassoLars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "canadian-problem",
   "metadata": {},
   "outputs": [],
   "source": [
    "def sample_data(seed=None, n=10000, data_drift=False, concept_drift=False):\n",
    "    # generate a sample from the distribution entailed by the causal graph\n",
    "    np.random.seed(seed)\n",
    "    # C, A, K\n",
    "    C = np.random.randn(n)\n",
    "    A = 0.8*np.random.randn(n)\n",
    "    K = A + 0.1*np.random.randn(n)\n",
    "    # X with and without data drift\n",
    "    if data_drift:\n",
    "        X = C - 2*A + 2.0*np.random.randn(n)\n",
    "    else:\n",
    "        X = C - 2*A + 0.2*np.random.randn(n)\n",
    "    # F \n",
    "    F = 3*X + 0.8*np.random.randn(n)\n",
    "    # D with and without concept drift\n",
    "    if concept_drift:\n",
    "        D =  2*X + 0.5*np.random.randn(n)\n",
    "    else:\n",
    "        D = -2*X + 0.5*np.random.randn(n)\n",
    "    # G, Y \n",
    "    G = D + 0.5*np.random.randn(n)\n",
    "    Y = 2*K - D + 0.2*np.random.randn(n)\n",
    "    # H with and without data drift\n",
    "    if data_drift:\n",
    "        H = 0.5*Y + 1.0*np.random.randn(n)\n",
    "    else:\n",
    "        H = 0.5*Y + 0.1*np.random.randn(n)  \n",
    "    # put all in a nice dataframe\n",
    "    df = pd.DataFrame(np.vstack([C, A, K, X, F, D, G, Y, H]).T, columns=[\"C\", \"A\", \"K\", \"X\", \"F\", \"D\", \"G\", \"Y\", \"H\"])\n",
    "    return df\n",
    "\n",
    "def test_model(input_vars, df_train, df_test):\n",
    "    # fit model with given variables\n",
    "    lm = LinearRegression().fit(df_train[input_vars], df_train[[\"Y\"]])\n",
    "    # check model fit and coefficients\n",
    "    # true coefficients would be all values on the edges multiplied from the path from X to Y\n",
    "    print(f\"R^2 (train): {lm.score(df_train[input_vars], df_train[['Y']]):0.3f}; (test): {lm.score(df_test[input_vars], df_test[['Y']]):0.3f} => Y ~ {lm.intercept_[0]:0.3f} + \" + \" + \".join([f\"{lm.coef_[0, i]:0.3f} * {input_vars[i]}\" for i in range(len(input_vars))]))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "revised-millennium",
   "metadata": {},
   "source": [
    "## Train & evaluate the model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "southwest-printing",
   "metadata": {},
   "outputs": [],
   "source": [
    "# generate train and test data with different random seeds\n",
    "df_train = sample_data(1)\n",
    "df_test = sample_data(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "transparent-sacrifice",
   "metadata": {},
   "outputs": [],
   "source": [
    "# (1) missing relevant input feature K -> wrong coefficient for X\n",
    "test_model([\"X\"], df_train, df_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "colonial-wagner",
   "metadata": {},
   "outputs": [],
   "source": [
    "# (2) all the right input features -> correct coefficients\n",
    "test_model([\"X\", \"K\"], df_train, df_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "wireless-corner",
   "metadata": {},
   "outputs": [],
   "source": [
    "# (3) additional input feature D, which has a more direct influence on Y than X\n",
    "test_model([\"X\", \"K\", \"D\"], df_train, df_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "supposed-yugoslavia",
   "metadata": {},
   "outputs": [],
   "source": [
    "# (4) additional input feature H, which is dependent on (i.e. highly correlated with) Y\n",
    "test_model([\"X\", \"K\", \"H\"], df_train, df_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "welcome-appraisal",
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": [
    "# (5) regularized model with all inputs -> chooses dependent variable H instead of causal influences\n",
    "input_vars = [c for c in df_train.columns if c != \"Y\"]\n",
    "lm = LassoLars(alpha=0.003).fit(df_train[input_vars], df_train[\"Y\"])\n",
    "print(f\"R^2 (train): {lm.score(df_train[input_vars], df_train['Y']):0.3f}; (test): {lm.score(df_test[input_vars], df_test['Y']):0.3f} => Y ~ {lm.intercept_:0.3f} + \" + \" + \".join([f\"{lm.coef_[i]:0.3f} * {input_vars[i]}\" for i in range(len(input_vars))]))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "collectible-hands",
   "metadata": {},
   "source": [
    "## Data & Concept Drifts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "quiet-groove",
   "metadata": {},
   "outputs": [],
   "source": [
    "# generate test data with a data drift in X and H\n",
    "df_test = sample_data(2, data_drift=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "greatest-cliff",
   "metadata": {},
   "outputs": [],
   "source": [
    "# model (2): true relationship between X and Y -> test performance equally good\n",
    "test_model([\"X\", \"K\"], df_train, df_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "indoor-celebrity",
   "metadata": {},
   "outputs": [],
   "source": [
    "# model (4): variable dependent on but not causal of Y -> test performance a lot worse\n",
    "test_model([\"X\", \"K\", \"H\"], df_train, df_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "endless-detective",
   "metadata": {},
   "outputs": [],
   "source": [
    "# generate test data with a concept drift in D, i.e., on the way from X to Y\n",
    "df_test = sample_data(2, concept_drift=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "korean-penguin",
   "metadata": {},
   "outputs": [],
   "source": [
    "# model (2): causal relationship between X and Y changed -> test performance catastrophic\n",
    "test_model([\"X\", \"K\"], df_train, df_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "blessed-passenger",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all",
   "encoding": "# -*- coding: utf-8 -*-",
   "notebook_metadata_filter": "-all"
  },
  "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.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
