{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5dc1dba6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from scipy.sparse import csr_matrix, diags\n",
    "import matplotlib.pyplot as plt\n",
    "from os import listdir\n",
    "\n",
    "import sys\n",
    "sys.path += ['utils/']  \n",
    "\n",
    "from word_embedding import * \n",
    "\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "397c5e6c",
   "metadata": {},
   "outputs": [],
   "source": [
    "directory = 'Bkp_vars/'\n",
    "\n",
    "df = pd.read_csv(directory + 'events.csv')\n",
    "\n",
    "all_visitor_id = df.visitorid.unique()\n",
    "n = len(all_visitor_id)\n",
    "visitor_mapper = dict(zip(all_visitor_id, np.arange(n)))\n",
    "\n",
    "all_item_id = df.itemid.unique()\n",
    "m = len(all_item_id)\n",
    "item_mapper = dict(zip(all_item_id, np.arange(m)))\n",
    "\n",
    "df.visitorid = df.visitorid.map(lambda x: visitor_mapper[x])\n",
    "df.itemid = df.itemid.map(lambda x: item_mapper[x])\n",
    "\n",
    "event_mapper = dict(zip(df.event.unique(), np.array([1,2,3])))\n",
    "df.event = df.event.map(lambda x: event_mapper[x])\n",
    "\n",
    "nrows = len(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9fdbb412",
   "metadata": {},
   "outputs": [],
   "source": [
    "def RunSimulation(dim):\n",
    "\n",
    "    pv = [0.1, 0.3, 0.5, 0.7, 0.9]\n",
    "\n",
    "    Fscore = []\n",
    "\n",
    "\n",
    "    for p in pv[::-1]:\n",
    "        print(p, end = '\\r')\n",
    "        \n",
    "        # create train and test sets\n",
    "        train_bool = np.random.binomial(1, p, nrows) == 1\n",
    "        df_train = df[train_bool]\n",
    "        df_test = df[~train_bool]\n",
    "\n",
    "        # obtain the probability matrix from the train set\n",
    "        A = csr_matrix((df_train.event, (df_train.visitorid, df_train.itemid)), shape = (n,m))\n",
    "        D_1 = diags((A@np.ones(m))**(-1))\n",
    "        P = D_1@A\n",
    "        \n",
    "        # compute the embeddings\n",
    "        res = CreateEmbedding([P], dim = dim, n_epochs = 30, η = 0.8, sym = False)\n",
    "\n",
    "        # true positives\n",
    "        tp = np.mean((res.X[df.visitorid] * res.Y[df.itemid]).sum(axis = 1) > 0)\n",
    "        \n",
    "        # false negatives\n",
    "        fn = 1 - tp\n",
    "        \n",
    "        # true negatives\n",
    "        idxa, idxb = np.random.choice(np.arange(n), len(df)), np.random.choice(np.arange(m), len(df))\n",
    "        tn = np.mean((res.X[idxa] * res.Y[idxb]).sum(axis = 1) < 0)\n",
    "        \n",
    "        # false positives\n",
    "        fp = 1 - tn\n",
    "\n",
    "        \n",
    "        precision = tp/(tp + fp)\n",
    "        recall = tp/(tp + fn)\n",
    "\n",
    "        Fscore.append(2/(1/precision + 1/recall))\n",
    "        \n",
    "    # save the result\n",
    "    Fscore = pd.DataFrame(Fscore)\n",
    "    try:\n",
    "        nn = (np.max([int(x.split('_')[1]) for x in listdir('saved_files/matrix_completion')]))\n",
    "        print(nn)\n",
    "        Fscore.to_csv('saved_files/matrix_completion/v_' + str(nn+1) + '_.csv', index = False)\n",
    "\n",
    "    except:\n",
    "        Fscore.to_csv('saved_files/matrix_completion/v_' + str(1) + '_.csv', index = False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c480abce",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running the optimization for k = 1\n",
      "Running the optimization for k = 1\n",
      "Running the optimization for k = 1\n",
      "Running the optimization for k = 1\n",
      "Running the optimization for k = 1\n",
      "6========================>] 100%\n",
      "Running the optimization for k = 1\n",
      "Running the optimization for k = 1\n",
      "Running the optimization for k = 1\n",
      "Running the optimization for k = 1\n",
      "Running the optimization for k = 1\n",
      "7========================>] 100%\n",
      "Running the optimization for k = 1\n",
      "Running the optimization for k = 1\n",
      "Running the optimization for k = 1\n",
      "Running the optimization for k = 1\n",
      "Running the optimization for k = 1\n",
      "8========================>] 100%\n",
      "Running the optimization for k = 1\n",
      "Running the optimization for k = 1\n",
      "Running the optimization for k = 1\n",
      "Running the optimization for k = 1\n",
      "Running the optimization for k = 1\n",
      "9========================>] 100%\n"
     ]
    }
   ],
   "source": [
    "dim = 32\n",
    "n_sim = 4\n",
    "for i in range(n_sim):\n",
    "    RunSimulation(dim)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "936ffe40",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 36564 characters]\n",
      "text/plain": [
       "<Figure size 720x360 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "directory = 'saved_files/matrix_completion/'\n",
    "files = listdir(directory)\n",
    "\n",
    "data = []\n",
    "\n",
    "for f in files:\n",
    "    data.append(pd.read_csv(directory + f))\n",
    "    \n",
    "data = pd.concat(data, axis = 1)\n",
    "\n",
    "pv = [0.1, 0.3, 0.5, 0.7, 0.9]\n",
    "\n",
    "μ = data.mean(axis = 1)\n",
    "σ = data.std(axis = 1) \n",
    "\n",
    "plt.figure(figsize = (10,5))\n",
    "\n",
    "plt.plot(pv[::-1], μ, color = 'navy', marker = 'd', markersize = 18, fillstyle = 'none')\n",
    "plt.fill_between(pv[::-1], μ - σ, μ + σ, color = 'dodgerblue', alpha = 0.4)\n",
    "plt.xlabel(r'$p$', fontsize = 25)\n",
    "plt.ylabel('F-score', fontsize = 25)\n",
    "plt.xticks(fontsize = 16)\n",
    "plt.yticks(fontsize = 16)\n",
    "plt.savefig('../../paper/v2/arXiv/Figures/mat_comp.pdf', dpi = 400, bbox_inches = 'tight')\n",
    "plt.show();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "30bdd4d0",
   "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
}
