{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "50b3f652",
   "metadata": {},
   "source": [
    "# Simulating spatial data with `spycause`\n",
    "\n",
    "This tutorial covers the basic functionality and operation of the `simulator` module.\n",
    "At present, this module exposes two classes: \n",
    "- The `Simulator` class, a template that all future simulators must adhere to. This class sketches out how simulators should operate, but does not do any simulation on its own.\n",
    "- The `CARSimulator` class, a child of `Simulator` designed for simulating data with nonspatial confounding, spatial confounding, and spatial interference. This class is the subject of this tutorial.\n",
    "\n",
    "For now, simulation in `spycause` is focused on lattice data for simplicity.\n",
    "Simulation on general spatial supports is a task for future development.\n",
    "If you design other `Simulator`s for your own purposes, please open a pull request! We'd love to include more options in the library."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6cbd0e8b",
   "metadata": {},
   "source": [
    "First, we import the usual libraries."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "51e0c4de",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.chdir(\"../..\")\n",
    "import numpy as np\n",
    "import spycause as spy\n",
    "from libpysal.weights import lat2W"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "410b727f",
   "metadata": {},
   "source": [
    "`CARSimulator` simulates data on a lattice whose size is defined by its first argument. \n",
    "The second argument defines how many covariates to create."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "460a7c3e",
   "metadata": {},
   "outputs": [],
   "source": [
    "Nlat = 40\n",
    "D = 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "3a2c15d4",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "module 'spycause' has no attribute 'CARSimulator'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-12-d0bd9cd9ebfb>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m## Nonspatial linear simulation (scenario 1)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0msim\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mspy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mCARSimulator\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mNlat\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mD\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      3\u001b[0m \u001b[0mX\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mY\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mZ\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msim\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msimulate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[0m_\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0maxes\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mplt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msubplots\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mncols\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mAttributeError\u001b[0m: module 'spycause' has no attribute 'CARSimulator'"
     ]
    }
   ],
   "source": [
    "## Nonspatial linear simulation (scenario 1)\n",
    "sim = spy.CARSimulator(Nlat, D)\n",
    "X, Y, Z = sim.simulate()\n",
    "\n",
    "_, axes = plt.subplots(ncols=3)\n",
    "axes[0].imshow(X[:, 0].reshape(Nlat, Nlat))\n",
    "axes[1].imshow(Y.reshape(Nlat, Nlat))\n",
    "axes[2].imshow(Z.reshape(Nlat, Nlat))\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0257d0da",
   "metadata": {},
   "outputs": [],
   "source": [
    "## Set up parameters\n",
    "Nlat = 30\n",
    "N = Nlat**2\n",
    "D = 2\n",
    "x_sd = 0.75\n",
    "y_sd = 0.1\n",
    "beta = np.array([[0.5, -1]]).T\n",
    "tau = 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aaa2477b",
   "metadata": {},
   "outputs": [],
   "source": [
    "## Generate data\n",
    "sim = spy.CARSimulator(Nlat, D)\n",
    "X, Y, Z = sim.simulate(treat=tau, y_conf=beta, x_sd=x_sd, y_sd=y_sd)"
   ]
  }
 ],
 "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.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
