<commit_before>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Stationay SCM and DAG (STAT.) from figure 1 in paper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append(\"../src/\")\n",
    "sys.path.append(\"..\")\n",
    "\n",
    "from src.examples.example_setups import setup_stat_scm\n",
    "from src.utils.sequential_causal_functions import sequentially_sample_model\n",
    "\n",
    "# Models\n",
    "from src.methods.bo import BO\n",
    "from src.methods.cbo import CBO\n",
    "from src.methods.abo import ABO\n",
    "from src.methods.dcbo import DCBO\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Sample SEM to get observational samples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "T = 3 # In this example, as in the paper, we consider three time-slices\n",
    "init_sem, sem, dag_view, dag  = setup_stat_scm(T=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Number of independent samples per time-index\n",
    "observational_samples = sequentially_sample_model(initial_structural_equation_model,\n",
    "                                                  structural_equation_model,\n",
    "                                                  total_timesteps=3,\n",
    "                                                  sample_count=5,\n",
    "                                                  epsilon=None)"
   ]
  }
 ],
 "metadata": {
  "interpreter": {<commit_msg>wrinting up STAT graph<commit_after>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Stationay SCM and DAG (STAT.) from figure 1 in paper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append(\"../src/\")\n",
    "sys.path.append(\"..\")\n",
    "\n",
    "from src.examples.example_setups import setup_stat_scm\n",
    "from src.utils.sequential_causal_functions import sequentially_sample_model\n",
    "from src.utils.sem_utils.toy_sems import StationaryDependentSEM\n",
    "\n",
    "# Models\n",
    "from src.methods.bo import BO\n",
    "from src.methods.cbo import CBO\n",
    "from src.methods.abo import ABO\n",
    "from src.methods.dcbo import DCBO\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Sample SEM to get observational samples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Generated by graphviz version 2.42.3 (20191010.1750)\n -->\n<!-- Title: %3 Pages: 1 -->\n<svg width=\"242pt\" height=\"152pt\"\n viewBox=\"0.00 0.00 242.00 152.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 148)\">\n<title>%3</title>\n<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-148 238,-148 238,4 -4,4\"/>\n<!-- X_0 -->\n<g id=\"node1\" class=\"node\">\n<title>X_0</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-126\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"27\" y=\"-122.3\" font-family=\"Times,serif\" font-size=\"14.00\">X_0</text>\n</g>\n<!-- Z_0 -->\n<g id=\"node2\" class=\"node\">\n<title>Z_0</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-72\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"27\" y=\"-68.3\" font-family=\"Times,serif\" font-size=\"14.00\">Z_0</text>\n</g>\n<!-- X_0&#45;&gt;Z_0 -->\n<g id=\"edge1\" class=\"edge\">\n<title>X_0&#45;&gt;Z_0</title>\n<path fill=\"none\" stroke=\"black\" d=\"M27,-107.79C27,-105.31 27,-102.83 27,-100.34\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"30.5,-100.14 27,-90.14 23.5,-100.14 30.5,-100.14\"/>\n</g>\n<!-- X_1 -->\n<g id=\"node4\" class=\"node\">\n<title>X_1</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"117\" cy=\"-126\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"117\" y=\"-122.3\" font-family=\"Times,serif\" font-size=\"14.00\">X_1</text>\n</g>\n<!-- X_0&#45;&gt;X_1 -->\n<g id=\"edge7\" class=\"edge\">\n<title>X_0&#45;&gt;X_1</title>\n<path fill=\"none\" stroke=\"black\" d=\"M54.4,-126C62.39,-126 71.31,-126 79.82,-126\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"79.92,-129.5 89.92,-126 79.92,-122.5 79.92,-129.5\"/>\n</g>\n<!-- Y_0 -->\n<g id=\"node3\" class=\"node\">\n<title>Y_0</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"27\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">Y_0</text>\n</g>\n<!-- Z_0&#45;&gt;Y_0 -->\n<g id=\"edge2\" class=\"edge\">\n<title>Z_0&#45;&gt;Y_0</title>\n<path fill=\"none\" stroke=\"black\" d=\"M27,-53.79C27,-51.31 27,-48.83 27,-46.34\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"30.5,-46.14 27,-36.14 23.5,-46.14 30.5,-46.14\"/>\n</g>\n<!-- Z_1 -->\n<g id=\"node5\" class=\"node\">\n<title>Z_1</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"117\" cy=\"-72\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"117\" y=\"-68.3\" font-family=\"Times,serif\" font-size=\"14.00\">Z_1</text>\n</g>\n<!-- Z_0&#45;&gt;Z_1 -->\n<g id=\"edge8\" class=\"edge\">\n<title>Z_0&#45;&gt;Z_1</title>\n<path fill=\"none\" stroke=\"black\" d=\"M54.4,-72C62.39,-72 71.31,-72 79.82,-72\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"79.92,-75.5 89.92,-72 79.92,-68.5 79.92,-75.5\"/>\n</g>\n<!-- Y_1 -->\n<g id=\"node6\" class=\"node\">\n<title>Y_1</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"117\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"117\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">Y_1</text>\n</g>\n<!-- Y_0&#45;&gt;Y_1 -->\n<g id=\"edge9\" class=\"edge\">\n<title>Y_0&#45;&gt;Y_1</title>\n<path fill=\"none\" stroke=\"black\" d=\"M54.4,-18C62.39,-18 71.31,-18 79.82,-18\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"79.92,-21.5 89.92,-18 79.92,-14.5 79.92,-21.5\"/>\n</g>\n<!-- X_1&#45;&gt;Z_1 -->\n<g id=\"edge3\" class=\"edge\">\n<title>X_1&#45;&gt;Z_1</title>\n<path fill=\"none\" stroke=\"black\" d=\"M117,-107.79C117,-105.31 117,-102.83 117,-100.34\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"120.5,-100.14 117,-90.14 113.5,-100.14 120.5,-100.14\"/>\n</g>\n<!-- X_2 -->\n<g id=\"node7\" class=\"node\">\n<title>X_2</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"207\" cy=\"-126\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"207\" y=\"-122.3\" font-family=\"Times,serif\" font-size=\"14.00\">X_2</text>\n</g>\n<!-- X_1&#45;&gt;X_2 -->\n<g id=\"edge10\" class=\"edge\">\n<title>X_1&#45;&gt;X_2</title>\n<path fill=\"none\" stroke=\"black\" d=\"M144.4,-126C152.39,-126 161.31,-126 169.82,-126\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"169.92,-129.5 179.92,-126 169.92,-122.5 169.92,-129.5\"/>\n</g>\n<!-- Z_1&#45;&gt;Y_1 -->\n<g id=\"edge4\" class=\"edge\">\n<title>Z_1&#45;&gt;Y_1</title>\n<path fill=\"none\" stroke=\"black\" d=\"M117,-53.79C117,-51.31 117,-48.83 117,-46.34\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"120.5,-46.14 117,-36.14 113.5,-46.14 120.5,-46.14\"/>\n</g>\n<!-- Z_2 -->\n<g id=\"node8\" class=\"node\">\n<title>Z_2</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"207\" cy=\"-72\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"207\" y=\"-68.3\" font-family=\"Times,serif\" font-size=\"14.00\">Z_2</text>\n</g>\n<!-- Z_1&#45;&gt;Z_2 -->\n<g id=\"edge11\" class=\"edge\">\n<title>Z_1&#45;&gt;Z_2</title>\n<path fill=\"none\" stroke=\"black\" d=\"M144.4,-72C152.39,-72 161.31,-72 169.82,-72\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"169.92,-75.5 179.92,-72 169.92,-68.5 169.92,-75.5\"/>\n</g>\n<!-- Y_2 -->\n<g id=\"node9\" class=\"node\">\n<title>Y_2</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"207\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"207\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">Y_2</text>\n</g>\n<!-- Y_1&#45;&gt;Y_2 -->\n<g id=\"edge12\" class=\"edge\">\n<title>Y_1&#45;&gt;Y_2</title>\n<path fill=\"none\" stroke=\"black\" d=\"M144.4,-18C152.39,-18 161.31,-18 169.82,-18\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"169.92,-21.5 179.92,-18 169.92,-14.5 169.92,-21.5\"/>\n</g>\n<!-- X_2&#45;&gt;Z_2 -->\n<g id=\"edge5\" class=\"edge\">\n<title>X_2&#45;&gt;Z_2</title>\n<path fill=\"none\" stroke=\"black\" d=\"M207,-107.79C207,-105.31 207,-102.83 207,-100.34\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"210.5,-100.14 207,-90.14 203.5,-100.14 210.5,-100.14\"/>\n</g>\n<!-- Z_2&#45;&gt;Y_2 -->\n<g id=\"edge6\" class=\"edge\">\n<title>Z_2&#45;&gt;Y_2</title>\n<path fill=\"none\" stroke=\"black\" d=\"M207,-53.79C207,-51.31 207,-48.83 207,-46.34\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"210.5,-46.14 207,-36.14 203.5,-46.14 210.5,-46.14\"/>\n</g>\n</g>\n</svg>\n",
      "text/plain": [
       "<graphviz.files.Source at 0x7fe3187e7950>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "T = 3 # In this example, as in the paper, we consider three time-slices\n",
    "init_sem, sem, dag_view, dag  = setup_stat_scm(T=3)\n",
    "dag_view"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The above DAG is the graphical structure we will be working with and is faithful to the one used in figure one in the paper. `dag` is a networkx object i.e."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "networkx.classes.multidigraph.MultiDiGraph"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(dag)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "and is the one we will use for exploring the optimization methods further down. `dag_view` is simply a convenient way to visualise the structure at hand and is just a string that we visulise using `pygraphviz`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "digraph { rankdir=LR; X_0 -> Z_0; Z_0 -> Y_0; X_1 -> Z_1; Z_1 -> Y_1; X_2 -> Z_2; Z_2 -> Y_2;  X_0 -> X_1; Z_0 -> Z_1; Y_0 -> Y_1; X_1 -> X_2; Z_1 -> Z_2; Y_1 -> Y_2;  { rank=same; X_0 Z_0 Y_0 } { rank=same; X_1 Z_1 Y_1 } { rank=same; X_2 Z_2 Y_2 }  }\n"
     ]
    }
   ],
   "source": [
    "print(dag_view)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Notice that we make use of two types of structural equations models: `init_sem` and `sem`. The latter concerns interactions with the first time-slice in the DBN, which has no incoming edges from the previous time-slices, and is only active at $t=0$. For all other time-slices i.e. when $t>0$ the `sem` model is used."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'X': array([[-0.56782134, -0.70287856, -0.22035261],\n",
       "        [ 0.8089576 , -0.01391157, -0.96019663],\n",
       "        [-1.06494067, -1.41363813, -2.56603851],\n",
       "        [-0.46340272,  0.02742972,  2.43171383],\n",
       "        [-0.19257217, -0.20708109, -0.35612655]]),\n",
       " 'Z': array([[ 1.47034355,  3.50650397,  3.94172276],\n",
       "        [ 2.0147878 ,  4.13318748,  8.46298562],\n",
       "        [ 2.94324436,  5.94096229, 20.26070363],\n",
       "        [ 4.72775791,  4.21166437,  2.76579469],\n",
       "        [ 0.64248659,  2.23308413,  4.27395593]]),\n",
       " 'Y': array([[-2.10910858, -3.30576148, -3.49202066],\n",
       "        [-0.86977444, -1.45798619, -2.11556171],\n",
       "        [-2.57496445, -1.05672848, -0.60518715],\n",
       "        [-0.49055256, -2.76543881, -5.0358863 ],\n",
       "        [ 0.10853582, -1.88867869, -3.81768144]])}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Number of independent samples per time-index\n",
    "D_Obs = sequentially_sample_model(init_sem,\n",
    "                                sem,\n",
    "                                total_timesteps=T,\n",
    "                                sample_count=5, # How many samples we take per node in each time-slice\n",
    "                                epsilon=None) # If we employ a noise model or not\n",
    "\n",
    "D_Obs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The above observation samples contained in `D_obs` are sequential in the sense that the sample function generates each column per variable, conditional on the column samples at $t-1$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Additional problem setup"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Explore optimization methods"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "BO_input_params = {\n",
    "    \"graph\": dag,\n",
    "    \"sem\": StationaryDependentSEM,\n",
    "    \"observational_samples\":D_Obs,\n",
    "    \"intervention_domain\": intervention_domain,\n",
    "    \"interventional_samples\":None,   \n",
    "    \"number_of_trials\": 10,\n",
    "    \"optimal_assigned_blankets\": None,\n",
    "    \"sample_anchor_points\": True,\n",
    "    \"seed_anchor_points\": 1,\n",
    "    \"change_points\": None\n",
    "}"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
{'fraction_non_alphanumeric': 0.26610133856612234, 'fraction_numerical': 0.11115004555329736, 'mean_word_length': 2.6264294790343077, 'pattern_counts': {'":': 110, '<': 171, '<?xml version=': 1, '>': 184, 'https://': 0, 'lorem ipsum': 0, 'www.': 3, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5490882', 'n_tokens_mistral': 6523, 'n_tokens_neox': 5630, 'n_words': 949}