{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Plotting routines\n",
    "\n",
    "Here, we show how to use internal function to plot the results of profile likelihood calculation.\n",
    "There are 3 plotting routines available: `plot_likelihoods`, `plot_paths`, and `plot_likelihoods_and_paths`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T16:05:30.870686Z",
     "start_time": "2022-02-28T16:05:30.849138Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from profile_likelihood import profile_likelihood\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Define the model\n",
    "\n",
    "The model is defined as the sum of trigonometric functions, in the form of\n",
    "$$ f\\left(t; \\vec{\\theta}\\right) = \\sin(\\theta_0 t) + \\cos(\\theta_1 t), $$\n",
    "where we sample at 3 time points $t = [1.0, 1.2, 2.0]^T$.\n",
    "The default parameters are chosen to be $\\vec{\\theta} = [1.0, 2.5]^T$.\n",
    "The data is set to be the predictions of the model at the default parameters and the standard deviation is chosen to be 30% of the data.\n",
    "The implementation of this model is given in `model_trigonometric.py`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T16:05:31.783618Z",
     "start_time": "2022-02-28T16:05:31.775016Z"
    }
   },
   "outputs": [],
   "source": [
    "import model_trigonometric as model\n",
    "import manifold"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load computation results\n",
    "\n",
    "To save some time, we will just load the saved results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T16:05:32.561734Z",
     "start_time": "2022-02-28T16:05:32.548241Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Load data from results_trig_model.json ....\n"
     ]
    }
   ],
   "source": [
    "# Define the likelihood object\n",
    "likelihood = profile_likelihood(\n",
    "    model.residuals, model.nparams, model.npred, model.param_names\n",
    ")\n",
    "\n",
    "# Load results\n",
    "results = likelihood.load_results(\"results_trig_model.json\", model.best_fit)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Plot the negative log-likelihood\n",
    "\n",
    "This is done by calling `plot_likelihoods`.\n",
    "This will plot the cost along the profile likelihood path for each parameter."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T16:05:35.452199Z",
     "start_time": "2022-02-28T16:05:34.000217Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Generating 2 plots of cost profile\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 36244 characters]\n",
      "text/plain": [
       "<Figure size 300x600 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "axes = likelihood.plot_profiles(cpscale=\"log\", cplim=(1e-2, 1e3))\n",
    "plt.tight_layout()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The dashed vertical lines above show the location of the best-fit parameters."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Plot the profile likelihood paths\n",
    "\n",
    "This is done using `plot_paths`.\n",
    "The profile likelihood paths are projected to 2-dimensional plane.\n",
    "In the case where there are more than 2 parameters, the plots will be in like a lower triangular matrix.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T16:05:38.133472Z",
     "start_time": "2022-02-28T16:05:37.951757Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Generating 1 plots of profile likelihood paths.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<AxesSubplot:xlabel='$\\\\theta_0$', ylabel='$\\\\theta_1$'>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 19732 characters]\n",
      "text/plain": [
       "<Figure size 300x300 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "likelihood.plot_paths(bounds=[-5, 5], aspect=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The dashed curve is the paths scanning the parameter on the horizontal direction.\n",
    "The small black dot above show the location of the best-fit parameters."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Plot the paths and the cost\n",
    "\n",
    "Using `plot_likelihoods_and_paths`, we can combine the two figures above.\n",
    "In addition to the plot of the paths, the cost will be plotted on the diagonal."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T16:05:41.542339Z",
     "start_time": "2022-02-28T16:05:40.282410Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Generating 3 plots of profile likelihood and the paths.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[<AxesSubplot:ylabel='cost'>, <AxesSubplot:>],\n",
       "       [<AxesSubplot:xlabel='$\\\\theta_0$', ylabel='$\\\\theta_1$'>,\n",
       "        <AxesSubplot:xlabel='$\\\\theta_1$', ylabel='cost'>]], dtype=object)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 57784 characters]\n",
      "text/plain": [
       "<Figure size 600x600 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "likelihood.plot_paths_and_profiles(bounds=[-5, 5], aspect=1, cpscale=\"log\", cplim=(1e-2, 1e3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.7.4"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
