{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Quasi-Bayesian Vines \n",
    "\n",
    "The code below is a Python implementation of the Quasi-Bayesian Vines algorithm for estimating the a multivariate predictive distribution. It is given as simple sequential code here. A parallel version can be found in QBVine_parallel.py, with digits as the dataset used in that file. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from functions import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#exammple usage\n",
    "data = stats.multivariate_normal.rvs(mean=[0,0], cov=[[1,0.5],[0.5,1]], size=30)\n",
    "fit_and_eval_qb_vine(data,'Cauchy')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### To load datasets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle\n",
    "data_location = './datasets/iono.data'\n",
    "data = pd.read_csv(data_location, header=None)\n",
    "\n",
    "# for diabetes\n",
    "from sklearn.datasets import load_diabetes\n",
    "data = load_diabetes().data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Running the parallel version\n",
    "It can be run from the terminal using 'py .\\example_futures_turbo_fast_digits.py 8'. The parallel version only estimated marginals and the quantities necessary to fit the copula. The copula fitting is done separately with the code below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "nlls_marg_cop_joint = []\n",
    "for subset in [30,50,100,200,300,400,500]:\n",
    "    for seed in range(5):\n",
    "        with open('./outputs/output_digits_full_subset'+str(subset)+'_'+str(seed)+'.txt','rb') as f: \n",
    "            out = pickle.load(f)\n",
    "        # opt,runtime,y_test,y,y_permutations,mean_y,std_y,pseudos,test_nll,test_cdf,marginals, ctxt, seed\n",
    "        print(out[-1])\n",
    "        opt_mnist = out[0]\n",
    "        mnist_test = out[2]\n",
    "        mnist_train = out[3]\n",
    "        mnist_perm = out[4]\n",
    "        mnist_mean = out[5]\n",
    "        mnist_std = out[6]\n",
    "        mnist_pseudos = out[7]\n",
    "        mnist_test_nll = out[8]\n",
    "        mnist_test_cdf = out[9]\n",
    "        mnist_marginals = out[10]\n",
    "        mnist_ctxt = out[11]\n",
    "\n",
    "        optband = 3\n",
    "        start = time.time()\n",
    "        controls = pv.FitControlsVinecop(family_set=[pv.BicopFamily.tll],\n",
    "                                        selection_criterion='mbic',\n",
    "                                        nonparametric_method='constant', #KDE-copula\n",
    "                                        nonparametric_mult=optband, # bandwidth\n",
    "                                        num_threads = 2048)\n",
    "        cop = pv.Vinecop(mnist_pseudos, controls=controls)\n",
    "\n",
    "        start =time.time()\n",
    "        cop_dens = - torch.tensor(cop.loglik(mnist_test_cdf), dtype=torch.float32)\n",
    "\n",
    "        print('cop_dens', (cop_dens / (mnist_test_cdf.shape[0])))\n",
    "        print('marginal_dens: ',mnist_marginals)\n",
    "        print('nll: ',mnist_marginals+(cop_dens / (mnist_test_cdf.shape[0])))\n",
    "        print(out[-1])\n",
    "        nlls_marg_cop_joint.append([mnist_marginals,(cop_dens / (mnist_test_cdf.shape[0])),mnist_marginals+(cop_dens / (mnist_test_cdf.shape[0]))])\n",
    "\n",
    "nlls_marg_cop_joint = np.array(nlls_marg_cop_joint)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Supervied Tasks\n",
    "We provide code for supervised tasks in two files in this folder. They run sequentially and will run simply by calling their name. Their outputs are the complete joint nll."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### PRticle Filter implementation\n",
    "The PRticle filter code is included in the appropriately named file."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### RQ-NSF\n",
    "AN RQ-NSF implementation is also included in the appropriately named file."
   ]
  }
 ],
 "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.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
