{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Ellipse Scaling\n",
    "\n",
    "The code to calculate the percentages included in ellipses with various values of \"k\" in `plot.py`.\n",
    "\n",
    "Thanks to @senselessDev, January 26, for providing the code in [PR #1067](https://github.com/borglab/gtsam/pull/1067)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import scipy\n",
    "import scipy.stats\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def pct_to_sigma(pct, dof):\n",
    "    return np.sqrt(scipy.stats.chi2.ppf(pct / 100., df=dof))\n",
    "\n",
    "def sigma_to_pct(sigma, dof):\n",
    "    return scipy.stats.chi2.cdf(sigma**2, df=dof) * 100."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0D\t    1    \t    2    \t    3    \t    4    \t    5    \n",
      "1D\t68.26895%\t95.44997%\t99.73002%\t99.99367%\t99.99994%\n",
      "2D\t39.34693%\t86.46647%\t98.88910%\t99.96645%\t99.99963%\n",
      "3D\t19.87480%\t73.85359%\t97.07091%\t99.88660%\t99.99846%\n"
     ]
    }
   ],
   "source": [
    "for dof in range(0, 4):\n",
    "    print(\"{}D\".format(dof), end=\"\")\n",
    "    for sigma in range(1, 6):\n",
    "        if dof == 0: print(\"\\t    {}    \".format(sigma), end=\"\")\n",
    "        else: print(\"\\t{:.5f}%\".format(sigma_to_pct(sigma, dof)), end=\"\")\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1D\n",
      "\n",
      "pct=50.0 -> sigma=0.674489750196\n",
      "pct=95.0 -> sigma=1.959963984540\n",
      "pct=99.0 -> sigma=2.575829303549\n",
      "\n",
      "2D\n",
      "\n",
      "pct=50.0 -> sigma=1.177410022515\n",
      "pct=95.0 -> sigma=2.447746830681\n",
      "pct=99.0 -> sigma=3.034854258770\n",
      "\n",
      "3D\n",
      "\n",
      "pct=50.0 -> sigma=1.538172254455\n",
      "pct=95.0 -> sigma=2.795483482915\n",
      "pct=99.0 -> sigma=3.368214175219\n",
      "\n"
     ]
    }
   ],
   "source": [
    "for dof in range(1, 4):\n",
    "    print(\"{}D\\n\".format(dof))\n",
    "    for pct in [50, 95, 99]:\n",
    "        print(\"pct={:.1f} -> sigma={:.12f}\".format(pct, pct_to_sigma(pct, dof)))\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "4d608302ba82e7596903db5446e6fa05f049271852e8cc6e1cafaafe5fbd9fed"
  },
  "kernelspec": {
   "display_name": "Python 3.8.13 ('gtsfm-v1')",
   "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.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
