{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "293692f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy.stats import norm, burr, chi, gamma\n",
    "import pickle\n",
    "\n",
    "import sys\n",
    "sys.path += ['utils/']  \n",
    "\n",
    "from EDRep import *\n",
    "\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b66c4124",
   "metadata": {},
   "outputs": [],
   "source": [
    "def Normal(μ, σ2, t):\n",
    "    \n",
    "    return 1/np.sqrt(2*np.pi*σ2)*np.exp(-(t - μ)**2/(2*σ2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fff6f927",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I am reading f: youtube\n",
      "64503 127\n",
      "I am computing the Z values\n",
      "-------------------------\n",
      "\n",
      "I am reading f: 0\n",
      "163473 300\n",
      "I am computing the Z values\n",
      "-------------------------\n",
      "\n",
      "I am reading f: 7\n",
      "273930 300\n",
      "I am computing the Z values\n",
      "-------------------------\n",
      "\n",
      "I am reading f: 9\n",
      "273930 300\n",
      "I am computing the Z values\n",
      "-------------------------\n",
      "\n",
      "I am reading f: 224\n",
      "99884 200\n",
      "I am computing the Z values\n",
      "-------------------------\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# all_f = ['youtube', 0, 7, 9, 224]\n",
    "# kv = [1, 2, 3, 4, 5, 6, 7, 8]\n",
    "\n",
    "# Xv, Zv = [], []\n",
    "# res = [[] for i in range(len(all_f))]\n",
    "\n",
    "# for i, f in enumerate(all_f):\n",
    "\n",
    "#     print('I am reading f: ' + str(f))\n",
    "#     X = pd.read_csv('embeddings/' + str(f) + '.txt', skiprows = 1, on_bad_lines='skip', \n",
    "#                     sep = ' ', header = None, encoding='latin-1')\n",
    "#     X.set_index(0, inplace = True)\n",
    "#     X = X.dropna(axis = 1)\n",
    "#     X = X.values\n",
    "\n",
    "#     n, dim = np.shape(X)\n",
    "#     print(n, dim)\n",
    "\n",
    "#     X = normalize(X, norm = 'l2', axis = 1)\n",
    "#     Xv.append(X)\n",
    "\n",
    "#     indeces = np.random.choice(np.arange(n), 1000, replace = False)\n",
    "    \n",
    "#     print('I am computing the Z values')\n",
    "    \n",
    "    \n",
    "#     Zv.append(computeZ(X, indeces))\n",
    "    \n",
    "#     res[i] = [computeZest(X, indeces, k = k) for k in kv]   \n",
    "#     print('-------------------------\\n')\n",
    "    \n",
    "# # save\n",
    "# with open('saved_files/test_approx.pkl', 'wb') as file:       \n",
    "#     pickle.dump([Zv, Xv, res], file) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3cb800f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# load\n",
    "with open('saved_files/test_approx.pkl', 'rb') as file: \n",
    "    Zv, Xv, resv = pickle.load(file) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "2d1cd46e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 218404 characters]\n",
      "text/plain": [
       "<Figure size 1800x720 with 10 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "fs = 22\n",
    "ffs = 18\n",
    "Fs = 28\n",
    "\n",
    "kv = [1, 2, 3, 4, 5, 6, 7, 8]\n",
    "maxt = [0.02, 0.015, 0.003, 0.004, 0.007]\n",
    "titles = ['Youtube graph', 'Skip-Gram', 'GloVe', 'FastText', 'CBOW']\n",
    "A = 2\n",
    "B = len(titles)\n",
    "cmap = plt.cm.get_cmap('PuOr', len(kv))\n",
    "\n",
    "fig, ax = plt.subplots(A, B, figsize = (5*B, 5*A))\n",
    "fig.subplots_adjust(hspace = .3)\n",
    "\n",
    "for b, (X, Z, res) in enumerate(zip(Xv, Zv, resv)):\n",
    "\n",
    "    tv = np.linspace(0, maxt[b], 1000)\n",
    "    for j in range(len(kv)):\n",
    "        ϵ = np.abs(Z - res[j].Zest)/Z\n",
    "        ax[0,b].plot(tv, [np.mean(ϵ < t) for t in tv], color = cmap(j), \n",
    "                       label = r'$\\kappa =$' + str(kv[j]), linewidth = 3.5)\n",
    "\n",
    "        ax[0,b].set_xticks(np.linspace(0, max(tv), 3))\n",
    "        ax[0,b].tick_params(axis = 'x', labelsize = fs)\n",
    "        if b == 0:\n",
    "            ax[0,b].tick_params(axis = 'y', labelsize = fs)\n",
    "        else:\n",
    "            ax[0,b].tick_params(axis = 'y', labelsize = 0)\n",
    "\n",
    "        ax[0,b].set_xlabel(r'$\\epsilon$', fontsize = Fs)\n",
    "        ax[0,b].set_title(titles[b], fontsize = Fs)\n",
    "        ax[0,b].ticklabel_format(style='sci', axis='x', scilimits=(0,0))\n",
    "        ax[0,b].xaxis.get_offset_text().set_fontsize(18)\n",
    "\n",
    "    n, dim = np.shape(X)   \n",
    "    α = np.random.randint(n)\n",
    "\n",
    "    ax[1, b].hist(X[α]@X.T, bins = 30, edgecolor = 'white', color = 'chartreuse', \n",
    "                  density = True, label = 'Empirical')\n",
    "\n",
    "    m = np.mean(X[α]@X.T) \n",
    "    s = np.sqrt(np.var(X[α]@X.T))\n",
    "    t = np.linspace(m - 5*s, m + 5*s)\n",
    "    k = 4\n",
    "    π, μ, σ2 = res[k].π, res[k].μ, res[k].Ω\n",
    "\n",
    "    y = np.sum(np.diag(π/n)@np.array([Normal(X[α]@μ[b].T, X[α]@σ2[b]@X[α], t) for b in range(len(μ))]), axis = 0)\n",
    "    ax[1, b].plot(t, y, color = 'k', label = 'Predicted')\n",
    "\n",
    "    ax[1,b].tick_params(axis = 'both', labelsize = fs)\n",
    "    ax[1,b].set_xlabel(r'$x_i^Tx_j$', fontsize = Fs)\n",
    "    if b == 0:\n",
    "        ax[1,b].tick_params(axis = 'y', labelsize = fs)\n",
    "    else:\n",
    "        ax[1,b].tick_params(axis = 'y', labelsize = 0)\n",
    "\n",
    "ax[0,-1].legend(fontsize = ffs)\n",
    "ax[0,0].set_ylabel('CDF', fontsize = Fs)      \n",
    "\n",
    "# ax[1,-1].legend(bbox_to_anchor=(1., .7), fontsize = ffs)\n",
    "ax[1,-1].legend(fontsize = ffs)\n",
    "\n",
    "   \n",
    "plt.tight_layout() \n",
    "plt.savefig('../../paper/v2/paper/Figures/test_full.pdf', dpi = 400)\n",
    "plt.show();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4abbc295",
   "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.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
