{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Transformed Simplex Rounding Error Plots"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-07-23T03:45:06.001725Z",
     "start_time": "2018-07-23T03:45:05.999724Z"
    }
   },
   "source": [
    "## Parameter Selection\n",
    "This script calculates errors from the simulations run by `rounding_simplex_sims.ipynb`.  Here are the parameters:\n",
    "\n",
    "- `d`: dimension, one of `{10, 20, 40}`\n",
    "- `k`: power in transformation, one of `{1, 2, 3, 4}`.  The standard simplex (corresponding to `k = 1`) has condition number $(d + 1)$.  A linear transformation $L$ was chosen such that after transforming the standard simplex, the resultant covariance matrix has condition number $(d+1)^k$.\n",
    "- `n_steps`: An upper bound total of number of steps to consider. In the simulation script, `10**6` steps of the random walk was run, so this value must be less than or equal to `10**6`. \n",
    "- `n_min`: Minimum number of samples to consider when generating the mean and covariance error.  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-16T07:05:06.316435Z",
     "start_time": "2018-08-16T07:03:30.147410Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------------------------------------\n",
      "\t d = 10 \t k = 1\n",
      "------------------------------------------\n",
      "[1.5  1.75 2.   2.25 2.5  2.75 3.   3.25 3.5  3.75 4.   4.25 4.5  4.75\n",
      " 5.  ]\n",
      "[    31     56    100    177    316    562   1000   1778   3162   5623\n",
      "  10000  17782  31622  56234 100000]\n",
      "[3.22580645e+04 1.78571429e+04 1.00000000e+04 5.64971751e+03\n",
      " 3.16455696e+03 1.77935943e+03 1.00000000e+03 5.62429696e+02\n",
      " 3.16255534e+02 1.77841010e+02 1.00000000e+02 5.62366438e+01\n",
      " 3.16235532e+01 1.77828360e+01 1.00000000e+01]\n",
      "mean errors:\n",
      " [[ 0.58711852  0.93197266  1.27350798]\n",
      " [ 0.56909864  0.9140366   1.25499646]\n",
      " [ 0.53763903  0.88565052  1.22427965]\n",
      " [ 0.4884468   0.84206692  1.17302945]\n",
      " [ 0.40901771  0.77194255  1.10100089]\n",
      " [ 0.30531507  0.67335634  0.99818302]\n",
      " [ 0.16064586  0.54323937  0.87041481]\n",
      " [-0.02134225  0.37446733  0.68984658]\n",
      " [-0.25684084  0.16811441  0.5273512 ]\n",
      " [-0.49131213 -0.04883976  0.22887318]\n",
      " [-0.6712158  -0.25751353  0.03741558]\n",
      " [-0.86078334 -0.54319486 -0.23685613]\n",
      " [-1.18869871 -0.76338929 -0.50152028]\n",
      " [-1.55264308 -0.97988079 -0.67902265]\n",
      " [-1.42104534 -1.19716231 -0.87217869]]\n",
      "cov errors:\n",
      " [[3.60419869 4.75198725 7.01589073]\n",
      " [3.21160332 4.3301789  6.59839322]\n",
      " [2.8594552  3.97722969 6.25947063]\n",
      " [2.53568145 3.60796379 5.90296733]\n",
      " [2.15218665 3.19589659 5.47084886]\n",
      " [1.75976105 2.68041144 4.93368627]\n",
      " [1.28767056 2.03561749 4.24125106]\n",
      " [0.9768844  1.38814762 3.30427659]\n",
      " [0.68814669 0.91900845 2.0383457 ]\n",
      " [0.47929667 0.63172792 0.89189511]\n",
      " [0.33054224 0.46361124 0.58891118]\n",
      " [0.27488914 0.33025255 0.41570546]\n",
      " [0.18039614 0.23775794 0.31283563]\n",
      " [0.12560741 0.17830751 0.21481897]\n",
      " [0.10440926 0.12773017 0.16181924]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------------------------------------\n",
      "\t d = 10 \t k = 2\n",
      "------------------------------------------\n",
      "[1.5  1.75 2.   2.25 2.5  2.75 3.   3.25 3.5  3.75 4.   4.25 4.5  4.75\n",
      " 5.  ]\n",
      "[    31     56    100    177    316    562   1000   1778   3162   5623\n",
      "  10000  17782  31622  56234 100000]\n",
      "[3.22580645e+04 1.78571429e+04 1.00000000e+04 5.64971751e+03\n",
      " 3.16455696e+03 1.77935943e+03 1.00000000e+03 5.62429696e+02\n",
      " 3.16255534e+02 1.77841010e+02 1.00000000e+02 5.62366438e+01\n",
      " 3.16235532e+01 1.77828360e+01 1.00000000e+01]\n",
      "mean errors:\n",
      " [[ 0.58175368  0.92505367  1.26603932]\n",
      " [ 0.5679828   0.90785849  1.2444538 ]\n",
      " [ 0.54128772  0.88003286  1.21359866]\n",
      " [ 0.48941281  0.83394936  1.16270364]\n",
      " [ 0.41602735  0.76308538  1.08511866]\n",
      " [ 0.30397359  0.66555611  0.97635072]\n",
      " [ 0.15775132  0.53844477  0.84610731]\n",
      " [-0.03026052  0.36397307  0.68950971]\n",
      " [-0.25246357  0.156943    0.48867132]\n",
      " [-0.50938863 -0.07765496  0.30824897]\n",
      " [-0.65355625 -0.29934437  0.04082005]\n",
      " [-0.88321556 -0.47129584 -0.24919811]\n",
      " [-0.99755144 -0.74239404 -0.54513646]\n",
      " [-1.33725689 -1.00187501 -0.74718272]\n",
      " [-1.44673714 -1.19774744 -1.00131143]]\n",
      "cov errors:\n",
      " [[3.61662897 4.73383741 6.97622844]\n",
      " [3.20471421 4.31691734 6.55964899]\n",
      " [2.87179302 3.96754256 6.19131827]\n",
      " [2.51302653 3.59663053 5.83867113]\n",
      " [2.12417452 3.14942964 5.4287655 ]\n",
      " [1.72519976 2.66176462 4.99394375]\n",
      " [1.28919249 2.00752634 4.25386333]\n",
      " [0.93863826 1.38096964 3.05885832]\n",
      " [0.68744441 0.93209387 1.96614436]\n",
      " [0.47840057 0.63341815 0.91614294]\n",
      " [0.35180717 0.42059367 0.58335543]\n",
      " [0.26947468 0.32072713 0.46257762]\n",
      " [0.19392714 0.25531408 0.31126147]\n",
      " [0.1381515  0.16834214 0.20666566]\n",
      " [0.08679155 0.12768481 0.15475175]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------------------------------------\n",
      "\t d = 10 \t k = 3\n",
      "------------------------------------------\n",
      "[1.5  1.75 2.   2.25 2.5  2.75 3.   3.25 3.5  3.75 4.   4.25 4.5  4.75\n",
      " 5.  ]\n",
      "[    31     56    100    177    316    562   1000   1778   3162   5623\n",
      "  10000  17782  31622  56234 100000]\n",
      "[3.22580645e+04 1.78571429e+04 1.00000000e+04 5.64971751e+03\n",
      " 3.16455696e+03 1.77935943e+03 1.00000000e+03 5.62429696e+02\n",
      " 3.16255534e+02 1.77841010e+02 1.00000000e+02 5.62366438e+01\n",
      " 3.16235532e+01 1.77828360e+01 1.00000000e+01]\n",
      "mean errors:\n",
      " [[ 5.77586453e-01  9.24389876e-01  1.26859823e+00]\n",
      " [ 5.63145702e-01  9.06146812e-01  1.24946203e+00]\n",
      " [ 5.29542915e-01  8.76926170e-01  1.21890499e+00]\n",
      " [ 4.72591712e-01  8.29666886e-01  1.17056738e+00]\n",
      " [ 4.10238746e-01  7.61228724e-01  1.09265715e+00]\n",
      " [ 3.12578113e-01  6.67066746e-01  9.92937802e-01]\n",
      " [ 1.69609665e-01  5.31462314e-01  8.53046653e-01]\n",
      " [-1.92150838e-02  3.71126596e-01  6.83779227e-01]\n",
      " [-2.06687400e-01  1.65386877e-01  4.66334440e-01]\n",
      " [-3.72112316e-01 -2.71697027e-02  2.57316891e-01]\n",
      " [-7.03465235e-01 -2.66262674e-01 -5.81591579e-04]\n",
      " [-1.03363153e+00 -5.58184680e-01 -1.87565374e-01]\n",
      " [-1.11229595e+00 -7.71111970e-01 -3.94607976e-01]\n",
      " [-1.53243239e+00 -1.03485125e+00 -5.51778782e-01]\n",
      " [-2.06752412e+00 -1.43449507e+00 -7.28517170e-01]]\n",
      "cov errors:\n",
      " [[3.60752249 4.71794871 6.84585448]\n",
      " [3.19641232 4.30180147 6.43283589]\n",
      " [2.861998   3.94381201 6.07462532]\n",
      " [2.50577606 3.58269061 5.67583837]\n",
      " [2.12807006 3.16458292 5.2507579 ]\n",
      " [1.71000089 2.63951903 4.7161073 ]\n",
      " [1.32190355 1.98906894 4.11024786]\n",
      " [0.96984444 1.38112694 3.23880372]\n",
      " [0.67430588 0.92426057 1.85055225]\n",
      " [0.48583191 0.65306502 0.99401408]\n",
      " [0.34238769 0.45094769 0.62713876]\n",
      " [0.25287199 0.33537352 0.4358172 ]\n",
      " [0.18924873 0.22475257 0.30684019]\n",
      " [0.14957801 0.17655495 0.21821101]\n",
      " [0.10488388 0.12379323 0.1959764 ]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------------------------------------\n",
      "\t d = 10 \t k = 4\n",
      "------------------------------------------\n",
      "[1.5  1.75 2.   2.25 2.5  2.75 3.   3.25 3.5  3.75 4.   4.25 4.5  4.75\n",
      " 5.  ]\n",
      "[    31     56    100    177    316    562   1000   1778   3162   5623\n",
      "  10000  17782  31622  56234 100000]\n",
      "[3.22580645e+04 1.78571429e+04 1.00000000e+04 5.64971751e+03\n",
      " 3.16455696e+03 1.77935943e+03 1.00000000e+03 5.62429696e+02\n",
      " 3.16255534e+02 1.77841010e+02 1.00000000e+02 5.62366438e+01\n",
      " 3.16235532e+01 1.77828360e+01 1.00000000e+01]\n",
      "mean errors:\n",
      " [[ 0.57851329  0.9285396   1.26974827]\n",
      " [ 0.56040041  0.91185913  1.25170836]\n",
      " [ 0.52984234  0.88100658  1.21921789]\n",
      " [ 0.48442708  0.83469148  1.17216543]\n",
      " [ 0.39671383  0.77022386  1.10538481]\n",
      " [ 0.30374996  0.66771372  1.00436604]\n",
      " [ 0.17380441  0.5369719   0.84298521]\n",
      " [ 0.01106339  0.37644928  0.69870458]\n",
      " [-0.21397475  0.18322184  0.4770149 ]\n",
      " [-0.40890842 -0.01763142  0.24274498]\n",
      " [-0.60701961 -0.29196782 -0.03631775]\n",
      " [-0.92017472 -0.51273593 -0.26341151]\n",
      " [-1.17390663 -0.78701528 -0.57570062]\n",
      " [-1.46155807 -1.02806289 -0.70454311]\n",
      " [-1.61206443 -1.3382997  -0.98304544]]\n",
      "cov errors:\n",
      " [[3.61544633 4.76527879 6.71492391]\n",
      " [3.20097501 4.34051376 6.29777036]\n",
      " [2.86562832 3.98893542 5.9391663 ]\n",
      " [2.52543448 3.6360819  5.59035695]\n",
      " [2.13087635 3.21264985 5.12978732]\n",
      " [1.75130231 2.69490222 4.6154808 ]\n",
      " [1.3089729  2.04151292 3.84105248]\n",
      " [0.94333327 1.36442793 3.04252631]\n",
      " [0.65956884 0.91103244 1.88959756]\n",
      " [0.48673662 0.64868437 1.11020072]\n",
      " [0.34874459 0.45984149 0.59484019]\n",
      " [0.2644898  0.31950034 0.41159749]\n",
      " [0.17119979 0.23187415 0.32619688]\n",
      " [0.12427948 0.17112367 0.21272082]\n",
      " [0.10483438 0.12283812 0.15158614]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------------------------------------\n",
      "\t d = 20 \t k = 1\n",
      "------------------------------------------\n",
      "[1.5  1.75 2.   2.25 2.5  2.75 3.   3.25 3.5  3.75]\n",
      "[   89   189   400   845  1788  3782  8000 16917 35777 75659]\n",
      "[11235.95505618  5291.00529101  2500.          1183.43195266\n",
      "   559.28411633   264.41036489   125.            59.11213572\n",
      "    27.95091819    13.21719822]\n",
      "mean errors:\n",
      " [[ 0.97282931  1.23568563  1.50175147]\n",
      " [ 0.94156468  1.19902626  1.45378294]\n",
      " [ 0.87202432  1.13409107  1.39431871]\n",
      " [ 0.77891951  1.02075625  1.2740124 ]\n",
      " [ 0.61010123  0.86683167  1.10643374]\n",
      " [ 0.37386715  0.63456905  0.89757295]\n",
      " [ 0.0865023   0.40357128  0.59585932]\n",
      " [-0.16489247  0.06450749  0.33617145]\n",
      " [-0.4730199  -0.26206689  0.00503549]\n",
      " [-0.78777166 -0.55594528 -0.34108847]]\n",
      "cov errors:\n",
      " [[4.21636839 5.33748784 7.39511183]\n",
      " [3.74234242 4.86589332 6.91766918]\n",
      " [3.26428093 4.37436128 6.40467556]\n",
      " [2.71051801 3.79145289 5.89154193]\n",
      " [2.00890033 2.92911832 5.08372839]\n",
      " [1.34202328 1.89487279 3.55670686]\n",
      " [0.8989105  1.10210502 2.14027454]\n",
      " [0.60805595 0.70249723 0.90023819]\n",
      " [0.39178415 0.42959879 0.52325105]\n",
      " [0.27426918 0.29130013 0.36570373]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------------------------------------\n",
      "\t d = 20 \t k = 2\n",
      "------------------------------------------\n",
      "[1.5  1.75 2.   2.25 2.5  2.75 3.   3.25 3.5  3.75]\n",
      "[   89   189   400   845  1788  3782  8000 16917 35777 75659]\n",
      "[11235.95505618  5291.00529101  2500.          1183.43195266\n",
      "   559.28411633   264.41036489   125.            59.11213572\n",
      "    27.95091819    13.21719822]\n",
      "mean errors:\n",
      " [[ 0.97342673  1.2327624   1.49340484]\n",
      " [ 0.94256811  1.19578115  1.44958129]\n",
      " [ 0.883064    1.12767867  1.38717054]\n",
      " [ 0.77512663  1.02366693  1.26084632]\n",
      " [ 0.62450384  0.8668277   1.09508941]\n",
      " [ 0.41535841  0.64381212  0.89020476]\n",
      " [ 0.14913137  0.38379101  0.60144593]\n",
      " [-0.13648282  0.09616698  0.30021041]\n",
      " [-0.48112103 -0.21604354  0.04942508]\n",
      " [-0.87680972 -0.50599666 -0.32529491]]\n",
      "cov errors:\n",
      " [[4.20261107 5.36094387 7.40389278]\n",
      " [3.74153492 4.89175368 6.92115143]\n",
      " [3.27215262 4.41411444 6.48151809]\n",
      " [2.68643501 3.78091638 5.85792178]\n",
      " [2.01184029 2.96813421 4.98285125]\n",
      " [1.36425912 1.91216662 3.79421016]\n",
      " [0.91415359 1.13921371 1.83641704]\n",
      " [0.58910033 0.68381909 0.8853939 ]\n",
      " [0.38596889 0.44912118 0.63344431]\n",
      " [0.2686344  0.31418676 0.37294876]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------------------------------------\n",
      "\t d = 20 \t k = 3\n",
      "------------------------------------------\n",
      "[1.5  1.75 2.   2.25 2.5  2.75 3.   3.25 3.5  3.75]\n",
      "[   89   189   400   845  1788  3782  8000 16917 35777 75659]\n",
      "[11235.95505618  5291.00529101  2500.          1183.43195266\n",
      "   559.28411633   264.41036489   125.            59.11213572\n",
      "    27.95091819    13.21719822]\n",
      "mean errors:\n",
      " [[ 0.97295146  1.22328705  1.49606072]\n",
      " [ 0.94135222  1.18543205  1.45335356]\n",
      " [ 0.87671804  1.12330988  1.38170376]\n",
      " [ 0.76097795  1.01648279  1.25684015]\n",
      " [ 0.61001901  0.85346831  1.08601594]\n",
      " [ 0.38363085  0.63019128  0.85654472]\n",
      " [ 0.13207396  0.35734654  0.56749695]\n",
      " [-0.18131737  0.04173814  0.28435993]\n",
      " [-0.44291982 -0.23663848 -0.10096328]\n",
      " [-0.80363823 -0.58890564 -0.35111187]]\n",
      "cov errors:\n",
      " [[4.15263177 5.28808311 7.42397503]\n",
      " [3.68543798 4.82712372 6.9519719 ]\n",
      " [3.18768249 4.34063431 6.51832198]\n",
      " [2.63984548 3.70229867 5.91231843]\n",
      " [1.9465972  2.89401622 5.04403638]\n",
      " [1.31488963 1.87085462 3.93948972]\n",
      " [0.89261218 1.11068108 1.66532249]\n",
      " [0.59013931 0.69088096 0.86604642]\n",
      " [0.38557702 0.48275891 0.52486519]\n",
      " [0.27530619 0.30614805 0.41673173]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------------------------------------\n",
      "\t d = 20 \t k = 4\n",
      "------------------------------------------\n",
      "[1.5  1.75 2.   2.25 2.5  2.75 3.   3.25 3.5  3.75]\n",
      "[   89   189   400   845  1788  3782  8000 16917 35777 75659]\n",
      "[11235.95505618  5291.00529101  2500.          1183.43195266\n",
      "   559.28411633   264.41036489   125.            59.11213572\n",
      "    27.95091819    13.21719822]\n",
      "mean errors:\n",
      " [[ 0.97459553  1.2328299   1.49982892]\n",
      " [ 0.93720076  1.19796148  1.45448795]\n",
      " [ 0.8780571   1.13019727  1.3897512 ]\n",
      " [ 0.76136055  1.02761547  1.27358744]\n",
      " [ 0.60279386  0.8753283   1.10142502]\n",
      " [ 0.3968323   0.67276919  0.88448121]\n",
      " [ 0.18348761  0.39418229  0.61374317]\n",
      " [-0.21102225  0.07308043  0.29063308]\n",
      " [-0.42688042 -0.2409622  -0.06645151]\n",
      " [-0.86071358 -0.55131804 -0.43003192]]\n",
      "cov errors:\n",
      " [[4.18392105 5.3718791  7.38619428]\n",
      " [3.71592878 4.89906482 6.92697682]\n",
      " [3.23388734 4.41513377 6.47961274]\n",
      " [2.70739711 3.82700842 5.88241558]\n",
      " [2.00446134 2.96807589 5.16468807]\n",
      " [1.33048678 1.92260643 3.41334631]\n",
      " [0.90765724 1.1328501  1.93323838]\n",
      " [0.58238141 0.69636222 0.85189104]\n",
      " [0.39091478 0.44471395 0.51179907]\n",
      " [0.25262814 0.30328214 0.35815305]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------------------------------------\n",
      "\t d = 40 \t k = 1\n",
      "------------------------------------------\n",
      "[1.5  1.75 2.   2.25 2.5  2.75 3.  ]\n",
      "[  252   636  1600  4023 10119 25448 64000]\n",
      "[3968.25396825 1572.32704403  625.          248.57071837   98.82399447\n",
      "   39.29581892   15.625     ]\n",
      "mean errors:\n",
      " [[ 1.33687023  1.52634268  1.73482374]\n",
      " [ 1.27507354  1.46342778  1.65513247]\n",
      " [ 1.14449331  1.33634857  1.51010993]\n",
      " [ 0.94858571  1.1292226   1.30992465]\n",
      " [ 0.70653984  0.84084471  1.00954208]\n",
      " [ 0.32912352  0.49333295  0.60930885]\n",
      " [-0.05166376  0.16358722  0.3075007 ]]\n",
      "cov errors:\n",
      " [[4.84980065 5.94905196 8.18614681]\n",
      " [4.27734018 5.36791313 7.62055974]\n",
      " [3.51741702 4.63541704 6.81485838]\n",
      " [2.60216382 3.50197164 5.56022731]\n",
      " [1.5627951  2.08998433 4.01581051]\n",
      " [0.97008495 1.1410803  1.49429302]\n",
      " [0.62620675 0.67229904 0.79544231]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------------------------------------\n",
      "\t d = 40 \t k = 2\n",
      "------------------------------------------\n",
      "[1.5  1.75 2.   2.25 2.5  2.75 3.  ]\n",
      "[  252   636  1600  4023 10119 25448 64000]\n",
      "[3968.25396825 1572.32704403  625.          248.57071837   98.82399447\n",
      "   39.29581892   15.625     ]\n",
      "mean errors:\n",
      " [[ 1.34165584  1.53151223  1.72730459]\n",
      " [ 1.27963497  1.46504762  1.65025742]\n",
      " [ 1.16048094  1.33958114  1.51824381]\n",
      " [ 0.97435741  1.14236682  1.30232236]\n",
      " [ 0.66724758  0.85358308  1.02248723]\n",
      " [ 0.27522484  0.51519176  0.5831569 ]\n",
      " [-0.11884519  0.07457987  0.30704906]]\n",
      "cov errors:\n",
      " [[4.87061047 6.13735245 8.28813523]\n",
      " [4.28776348 5.58591865 7.70131622]\n",
      " [3.56066688 4.81912368 6.95271869]\n",
      " [2.66450716 3.71387909 6.19405146]\n",
      " [1.58811597 2.27713936 4.36087219]\n",
      " [0.97830014 1.0600491  1.58558471]\n",
      " [0.6104515  0.64762301 1.2033682 ]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------------------------------------\n",
      "\t d = 40 \t k = 3\n",
      "------------------------------------------\n",
      "[1.5  1.75 2.   2.25 2.5  2.75 3.  ]\n",
      "[  252   636  1600  4023 10119 25448 64000]\n",
      "[3968.25396825 1572.32704403  625.          248.57071837   98.82399447\n",
      "   39.29581892   15.625     ]\n",
      "mean errors:\n",
      " [[ 1.3277071   1.52252846  1.71960345]\n",
      " [ 1.26340558  1.45490527  1.64510235]\n",
      " [ 1.15055637  1.3290093   1.50947365]\n",
      " [ 0.95361056  1.12261114  1.30877008]\n",
      " [ 0.64323516  0.82710856  1.03565044]\n",
      " [ 0.26224419  0.47792752  0.59644687]\n",
      " [-0.05338366  0.06074467  0.26102341]]\n",
      "cov errors:\n",
      " [[4.84321494 6.02424067 7.95575092]\n",
      " [4.27353964 5.46760963 7.36840009]\n",
      " [3.54757738 4.73602871 6.59676276]\n",
      " [2.49767083 3.64933908 5.63373271]\n",
      " [1.5064635  2.04866999 3.60090243]\n",
      " [0.9799644  1.08632506 1.68535271]\n",
      " [0.58847181 0.65781738 0.76157381]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------------------------------------\n",
      "\t d = 40 \t k = 4\n",
      "------------------------------------------\n",
      "[1.5  1.75 2.   2.25 2.5  2.75 3.  ]\n",
      "[  252   636  1600  4023 10119 25448 64000]\n",
      "[3968.25396825 1572.32704403  625.          248.57071837   98.82399447\n",
      "   39.29581892   15.625     ]\n",
      "mean errors:\n",
      " [[ 1.34202115  1.52363255  1.71782175]\n",
      " [ 1.27722274  1.45613406  1.64137123]\n",
      " [ 1.15768812  1.33385708  1.50562788]\n",
      " [ 0.94729218  1.12643031  1.31140119]\n",
      " [ 0.68397542  0.82142001  1.00805551]\n",
      " [ 0.30650331  0.49553501  0.67030004]\n",
      " [-0.01770109  0.14536727  0.22185113]]\n",
      "cov errors:\n",
      " [[4.78966507 5.90529117 7.29565186]\n",
      " [4.20849221 5.34078071 6.65350092]\n",
      " [3.52667992 4.57513632 5.9260332 ]\n",
      " [2.54971895 3.60785503 4.71833635]\n",
      " [1.56776643 2.09953015 3.22406441]\n",
      " [0.96836319 1.09887984 1.81133661]\n",
      " [0.59943999 0.66939482 0.78180227]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "from scipy.linalg import fractional_matrix_power\n",
    "from scipy.stats.mstats import mquantiles\n",
    "import pickle\n",
    "import os\n",
    "import time\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "sns.set()\n",
    "\n",
    "###########################################################\n",
    "# dimension, k value, conifidence levels, data load\n",
    "###########################################################\n",
    "d_vals = [10, 20, 40]\n",
    "k_vals = [1, 2, 3, 4]\n",
    "conf_level = 0.90\n",
    "alpha = 1 - conf_level\n",
    "quantiles = np.array([alpha/2, .5, 1 - alpha/2])\n",
    "\n",
    "\n",
    "# loop through each d, k pair and produce plots\n",
    "for d in d_vals:\n",
    "    for k in k_vals:\n",
    "        print('------------------------------------------')\n",
    "        print('\\t d = {}'.format(d), '\\t k = {}'.format(k))\n",
    "        print('------------------------------------------')\n",
    "        # load result (dictionary with keys d, k, X, and rejections)\n",
    "        filename = os.path.join(os.path.normpath('E:/Data/Rounding'), \n",
    "                                'simplex_d{}_k{}.pkl'.format(d, k))\n",
    "        with open(filename, 'rb') as file:\n",
    "            result = pickle.load(file)\n",
    "\n",
    "        X = result['X']\n",
    "\n",
    "        ############################################################\n",
    "        #  form population parameters\n",
    "        ############################################################\n",
    "        # population mean\n",
    "        a = np.ones(d + 1)\n",
    "        a0 = a.sum()\n",
    "        mu_full = a/a0\n",
    "        mu = mu_full[:d]\n",
    "        # population covariance\n",
    "        const = (1/(a0**2 * (a0 + 1)))\n",
    "        sigma_full = np.outer(-a * const, a)\n",
    "        sigma_full[np.diag_indices(d + 1)] = a*(a0 - a)*const\n",
    "        sigma = sigma_full[0:d, 0:d]\n",
    "        # transformation matrix\n",
    "        if k == 1:\n",
    "            sigma_new = sigma\n",
    "            mu_new = mu\n",
    "        else:\n",
    "            kappa = (d + 1)**k\n",
    "            vals, vecs = np.linalg.eigh(sigma)\n",
    "            U = vecs.copy()\n",
    "            D = np.diag([np.sqrt(a0/kappa)] + [1]*(d -1))\n",
    "            L = np.matmul(U, np.matmul(D, U.T))\n",
    "            sigma_new = np.matmul(L, np.matmul(sigma, L.T))\n",
    "            mu_new = np.matmul(L, mu)\n",
    "        # matrix for scaling (used in covariance error later)\n",
    "        scale_mtx = fractional_matrix_power(sigma_new, -.5)\n",
    "\n",
    "        n_steps = 10**6\n",
    "        powers = np.arange(1.5, 5.5, step=.25)\n",
    "        n_vals = np.floor(d**powers).astype('int')\n",
    "        n_sims = n_steps/n_vals\n",
    "        powers = powers[n_sims >= 10]\n",
    "        n_vals = n_vals[n_sims >= 10]\n",
    "        n_sims = n_sims[0:len(n_vals)]\n",
    "        print(powers)\n",
    "        print(n_vals)\n",
    "        print(n_sims)\n",
    "\n",
    "        ############################################################\n",
    "        #  form error matrix\n",
    "        ############################################################\n",
    "        n_steps = 10**6\n",
    "        err_mtx = np.zeros((n_steps, 3)) \n",
    "        i = 0\n",
    "\n",
    "        for n in n_vals:\n",
    "            for start_idx in range(0, n_steps, n):\n",
    "                if (start_idx + n <= n_steps):\n",
    "                    idx = range(start_idx, start_idx + n)\n",
    "                    X_sub = X[idx]\n",
    "                    mu_hat = X_sub.mean(axis=0)\n",
    "                    sigma_hat = np.cov(X_sub, rowvar=False, bias=True)\n",
    "                    mu_err = np.linalg.norm(scale_mtx @ (mu_new - mu_hat))**2\n",
    "                    sigma_err_mtx = scale_mtx @ sigma_hat @ scale_mtx\n",
    "                    smallest = np.linalg.norm(sigma_err_mtx, -2)\n",
    "                    largest = np.linalg.norm(sigma_err_mtx, 2)\n",
    "                    sigma_err = max(1/smallest, largest)\n",
    "                    err_mtx[i, :] = (n, mu_err, sigma_err)\n",
    "                    i = i + 1\n",
    "        err_mtx = err_mtx[err_mtx[:, 0] != 0, :]\n",
    "\n",
    "        # mean and cov error matrices with columns: lower, median, upper \n",
    "        mean_errs = np.zeros((len(n_vals), 3)) \n",
    "        cov_errs = np.zeros((len(n_vals), 3))\n",
    "        for (i, n) in enumerate(n_vals):\n",
    "            err_mtx_sub = err_mtx[err_mtx[:, 0] == n]\n",
    "            mean_errs[i, :] = np.log10(mquantiles(a=err_mtx_sub[:, 1], prob=quantiles))\n",
    "            cov_errs[i, :] = np.log10(mquantiles(a=err_mtx_sub[:, 2], prob=quantiles))\n",
    "        print('mean errors:\\n', mean_errs)\n",
    "        print('cov errors:\\n', cov_errs)\n",
    "\n",
    "        save_dir = os.path.normpath('C:/Users/Adam/OneDrive/Thesis')\n",
    "        covfig_filename = 'simplex_cov_n{}_k{}.pdf'\n",
    "        covfig_filename = covfig_filename.format(d, k)\n",
    "        covfig_filename = os.path.join(save_dir, covfig_filename)\n",
    "\n",
    "        fig, ax = plt.subplots()\n",
    "        fig.set_size_inches(8, 8)\n",
    "        txt = 'Covariance Error (n = {}, k = {})'\n",
    "        ax.set_title(txt.format(d, k), pad=20, fontsize=16)\n",
    "        ax.plot(powers, cov_errs[:, 1], linewidth=2, marker='o', color='darkred')\n",
    "        ax.fill_between(powers, cov_errs[:, 0], cov_errs[:, 2], alpha=.3, color='red')\n",
    "        plt.yticks(np.arange(0, 11))\n",
    "        plt.xticks(powers)\n",
    "        ax.set_xlabel(r'$\\gamma$', fontsize=14)\n",
    "        ax.set_ylabel(r'$\\log_{10}\\, C$', fontsize=14)\n",
    "        plt.savefig(covfig_filename)\n",
    "        plt.show()\n",
    "\n",
    "\n",
    "        meanfig_filename = 'simplex_mean_n{}_k{}.pdf'\n",
    "        meanfig_filename = meanfig_filename.format(d, k)\n",
    "        meanfig_filename = os.path.join(save_dir, meanfig_filename)\n",
    "\n",
    "        fig, ax = plt.subplots()\n",
    "        fig.set_size_inches(8, 8)\n",
    "        txt = 'Mean Error (n = {}, k = {})'\n",
    "        ax.set_title(txt.format(d, k), pad=20, fontsize=16)\n",
    "        ax.plot(powers, mean_errs[:, 1], linewidth=2, marker='o', color='darkred')\n",
    "        ax.fill_between(powers, mean_errs[:, 0], mean_errs[:, 2], alpha=.3, color='red')\n",
    "        #plt.yticks(np.arange(0, 11))\n",
    "        plt.xticks(powers)\n",
    "        ax.set_xlabel(r'$\\gamma$', fontsize=14)\n",
    "        ax.set_ylabel(r'$\\log_{10}\\, \\epsilon$', fontsize=14)\n",
    "        plt.savefig(meanfig_filename)\n",
    "        plt.show()\n"
   ]
  }
 ],
 "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.6.5"
  },
  "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": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
