{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# QuadratiK Usage Examples"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Normality Test"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This section contains example for the Parametric and Non-parametric Normality Test based on kernel-based quadratic distances"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Parametric"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-01-31T14:07:46.134295Z",
     "iopub.status.busy": "2024-01-31T14:07:46.133992Z",
     "iopub.status.idle": "2024-01-31T14:07:48.727644Z",
     "shell.execute_reply": "2024-01-31T14:07:48.727126Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Test : Kernel-based quadratic distance Normality test\n",
      "Execution time: 1.578\n",
      "H0 is Rejected : False\n",
      "Test Statistic : -0.004422397826208057\n",
      "Critical Value (CV) : 0.00495159345113745\n",
      "CV Method : Empirical\n",
      "Selected tuning parameter : 0.4\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from QuadratiK.kernel_test import KernelTest\n",
    "np.random.seed(42)\n",
    "data = np.random.randn(100,2)\n",
    "\n",
    "normality_test = KernelTest(h=0.4, centering_type=\"param\",random_state=42).test(data)\n",
    "print(\"Test : {}\".format(normality_test.test_type_))\n",
    "print(\"Execution time: {:.3f}\".format(normality_test.execution_time))\n",
    "print(\"H0 is Rejected : {}\".format(normality_test.h0_rejected_))\n",
    "print(\"Test Statistic : {}\".format(normality_test.test_statistic_))\n",
    "print(\"Critical Value (CV) : {}\".format(normality_test.cv_))\n",
    "print(\"CV Method : {}\".format(normality_test.cv_method_))\n",
    "print(\"Selected tuning parameter : {}\".format(normality_test.h))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-01-31T14:07:48.750902Z",
     "iopub.status.busy": "2024-01-31T14:07:48.750577Z",
     "iopub.status.idle": "2024-01-31T14:07:48.758448Z",
     "shell.execute_reply": "2024-01-31T14:07:48.758193Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Time taken for execution: 1.578 seconds\n",
      "Test Results \n",
      "--------------  ----------------------------------------------\n",
      "Test Type       Kernel-based quadratic distance Normality test\n",
      "Test Statistic  -0.004422397826208057\n",
      "Critical Value  0.00495159345113745\n",
      "Reject H0       False\n",
      "--------------  ----------------------------------------------\n",
      "Summary Statistics \n",
      "           Feature 0    Feature 1\n",
      "-------  -----------  -----------\n",
      "Mean         -0.1156       0.034\n",
      "Std Dev       0.8563       0.9989\n",
      "Median       -0.0353       0.1323\n",
      "IQR           1.0704       1.3333\n",
      "Min          -2.6197      -1.9876\n",
      "Max           1.8862       2.7202\n"
     ]
    }
   ],
   "source": [
    "print(normality_test.summary())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Non-parametric"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-01-31T14:07:48.759776Z",
     "iopub.status.busy": "2024-01-31T14:07:48.759682Z",
     "iopub.status.idle": "2024-01-31T14:07:48.894526Z",
     "shell.execute_reply": "2024-01-31T14:07:48.893940Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Test : Kernel-based quadratic distance Normality test\n",
      "Execution time: 0.131\n",
      "H0 is Rejected : False\n",
      "Test Statistic : 0.0015387891795935942\n",
      "Critical Value (CV) : 0.0020181255711485594\n",
      "CV Method : Empirical\n",
      "Selected tuning parameter : 0.4\n"
     ]
    }
   ],
   "source": [
    "normality_test = KernelTest(h=0.4, centering_type=\"nonparam\").test(data)\n",
    "print(\"Test : {}\".format(normality_test.test_type_))\n",
    "print(\"Execution time: {:.3f}\".format(normality_test.execution_time))\n",
    "print(\"H0 is Rejected : {}\".format(normality_test.h0_rejected_))\n",
    "print(\"Test Statistic : {}\".format(normality_test.test_statistic_))\n",
    "print(\"Critical Value (CV) : {}\".format(normality_test.cv_))\n",
    "print(\"CV Method : {}\".format(normality_test.cv_method_))\n",
    "print(\"Selected tuning parameter : {}\".format(normality_test.h))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-01-31T14:07:48.897727Z",
     "iopub.status.busy": "2024-01-31T14:07:48.897443Z",
     "iopub.status.idle": "2024-01-31T14:07:48.904544Z",
     "shell.execute_reply": "2024-01-31T14:07:48.903941Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Time taken for execution: 0.131 seconds\n",
      "Test Results \n",
      "--------------  ----------------------------------------------\n",
      "Test Type       Kernel-based quadratic distance Normality test\n",
      "Test Statistic  0.0015387891795935942\n",
      "Critical Value  0.0020181255711485594\n",
      "Reject H0       False\n",
      "--------------  ----------------------------------------------\n",
      "Summary Statistics \n",
      "           Feature 0    Feature 1\n",
      "-------  -----------  -----------\n",
      "Mean         -0.1156       0.034\n",
      "Std Dev       0.8563       0.9989\n",
      "Median       -0.0353       0.1323\n",
      "IQR           1.0704       1.3333\n",
      "Min          -2.6197      -1.9876\n",
      "Max           1.8862       2.7202\n"
     ]
    }
   ],
   "source": [
    "print(normality_test.summary())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### QQ Plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-01-31T14:07:48.907774Z",
     "iopub.status.busy": "2024-01-31T14:07:48.907501Z",
     "iopub.status.idle": "2024-01-31T14:07:49.067052Z",
     "shell.execute_reply": "2024-01-31T14:07:49.066782Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 57276 characters]",
      "text/plain": [
       "<Figure size 600x600 with 2 Axes>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from QuadratiK.tools import qq_plot\n",
    "qq_plot(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Two Sample Test"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This sections shows example for the two-sample test using normal kernel-based quadratic distance "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-01-31T14:07:49.068728Z",
     "iopub.status.busy": "2024-01-31T14:07:49.068616Z",
     "iopub.status.idle": "2024-01-31T14:07:49.112691Z",
     "shell.execute_reply": "2024-01-31T14:07:49.112411Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Test : Kernel-based quadratic distance two-sample test\n",
      "Execution time: 0.041\n",
      "H0 is Rejected : False\n",
      "Test Statistic : -0.018355578706893333\n",
      "Critical Value (CV) : 0.011282236253872464\n",
      "CV Method : subsampling\n",
      "Selected tuning parameter : 0.4\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from QuadratiK.kernel_test import KernelTest\n",
    "np.random.seed(42)\n",
    "X = np.random.randn(100,2)\n",
    "np.random.seed(42)\n",
    "Y = np.random.randn(100,2)\n",
    "\n",
    "two_sample_test = KernelTest(h=0.4, random_state=42).test(X,Y)\n",
    "print(\"Test : {}\".format(two_sample_test.test_type_))\n",
    "print(\"Execution time: {:.3f}\".format(two_sample_test.execution_time))\n",
    "print(\"H0 is Rejected : {}\".format(two_sample_test.h0_rejected_))\n",
    "print(\"Test Statistic : {}\".format(two_sample_test.test_statistic_))\n",
    "print(\"Critical Value (CV) : {}\".format(two_sample_test.cv_))\n",
    "print(\"CV Method : {}\".format(two_sample_test.cv_method_))\n",
    "print(\"Selected tuning parameter : {}\".format(two_sample_test.h))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-01-31T14:07:49.114184Z",
     "iopub.status.busy": "2024-01-31T14:07:49.114088Z",
     "iopub.status.idle": "2024-01-31T14:07:49.129380Z",
     "shell.execute_reply": "2024-01-31T14:07:49.128795Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Time taken for execution: 0.041 seconds\n",
      "Test Results \n",
      "--------------  -----------------------------------------------\n",
      "Test Type       Kernel-based quadratic distance two-sample test\n",
      "Test Statistic  -0.018355578706893333\n",
      "Critical Value  0.011282236253872464\n",
      "Reject H0       False\n",
      "--------------  -----------------------------------------------\n",
      "Summary Statistics \n",
      "                            Group 1    Group 2    Overall\n",
      "------------------------  ---------  ---------  ---------\n",
      "('Feature 0', 'Mean')       -0.1156    -0.1156    -0.1156\n",
      "('Feature 0', 'Std Dev')     0.8563     0.8563     0.8542\n",
      "('Feature 0', 'Median')     -0.0353    -0.0353    -0.0353\n",
      "('Feature 0', 'IQR')         1.0704     1.0704     1.0704\n",
      "('Feature 0', 'Min')        -2.6197    -2.6197    -2.6197\n",
      "('Feature 0', 'Max')         1.8862     1.8862     1.8862\n",
      "('Feature 1', 'Mean')        0.034      0.034      0.034\n",
      "('Feature 1', 'Std Dev')     0.9989     0.9989     0.9963\n",
      "('Feature 1', 'Median')      0.1323     0.1323     0.1323\n",
      "('Feature 1', 'IQR')         1.3333     1.3333     1.3333\n",
      "('Feature 1', 'Min')        -1.9876    -1.9876    -1.9876\n",
      "('Feature 1', 'Max')         2.7202     2.7202     2.7202\n"
     ]
    }
   ],
   "source": [
    "print(two_sample_test.summary())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## K-Sample Test"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Shows examples for the kernel-based quadratic distance k-sample tests with the Normal kernel and bandwidth\n",
    "parameter h."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-01-31T14:07:49.132861Z",
     "iopub.status.busy": "2024-01-31T14:07:49.132540Z",
     "iopub.status.idle": "2024-01-31T14:07:49.386802Z",
     "shell.execute_reply": "2024-01-31T14:07:49.385945Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Test : Kernel-based quadratic distance K-sample test\n",
      "Execution time: 0.248 seconds\n",
      "H0 is Rejected : False\n",
      "Test Statistic : [0.00140789 0.00035197]\n",
      "Critical Value (CV) : [0.00431479 0.0010787 ]\n",
      "CV Method : permutation\n",
      "Selected tuning parameter : 1.5\n"
     ]
    }
   ],
   "source": [
    "from QuadratiK.kernel_test import KernelTest\n",
    "np.random.seed(42)\n",
    "X = np.random.randn(500,2)\n",
    "np.random.seed(42)\n",
    "y = np.random.randint(0,5,500)\n",
    "k_sample_test = KernelTest(h = 1.5, method = \"permutation\").test(X,y)\n",
    "\n",
    "print(\"Test : {}\".format(k_sample_test.test_type_))\n",
    "print(\"Execution time: {:.3f} seconds\".format(k_sample_test.execution_time))\n",
    "print(\"H0 is Rejected : {}\".format(k_sample_test.h0_rejected_))\n",
    "print(\"Test Statistic : {}\".format(k_sample_test.test_statistic_))\n",
    "print(\"Critical Value (CV) : {}\".format(k_sample_test.cv_))\n",
    "print(\"CV Method : {}\".format(k_sample_test.cv_method_))\n",
    "print(\"Selected tuning parameter : {}\".format(k_sample_test.h))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-01-31T14:07:49.389931Z",
     "iopub.status.busy": "2024-01-31T14:07:49.389651Z",
     "iopub.status.idle": "2024-01-31T14:07:49.405221Z",
     "shell.execute_reply": "2024-01-31T14:07:49.404921Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Time taken for execution: 0.248 seconds\n",
      "Test Results \n",
      "--------------  ---------------------------------------------\n",
      "Test Type       Kernel-based quadratic distance K-sample test\n",
      "Test Statistic  [0.00140789 0.00035197]\n",
      "Critical Value  [0.00431479 0.0010787 ]\n",
      "Reject H0       False\n",
      "--------------  ---------------------------------------------\n",
      "Summary Statistics \n",
      "                            Group 0    Group 1    Group 2    Group 3    Group 4    Overall\n",
      "------------------------  ---------  ---------  ---------  ---------  ---------  ---------\n",
      "('Feature 0', 'Mean')        0.033     -0.1227     0.0547    -0.0554     0.1192     0.0036\n",
      "('Feature 0', 'Std Dev')     1.0563     0.874      0.8279     0.9351     1.1038     0.967\n",
      "('Feature 0', 'Median')      0.0485    -0.0347     0.0675    -0.0349     0.1958     0.0184\n",
      "('Feature 0', 'IQR')         1.4214     1.0371     0.9924     1.1388     1.3338     1.239\n",
      "('Feature 0', 'Min')        -2.6969    -2.0819    -1.7787    -2.651     -3.2413    -3.2413\n",
      "('Feature 0', 'Max')         2.5269     2.2989     2.1898     2.4458     3.0789     3.0789\n",
      "('Feature 1', 'Mean')        0.0501     0.072     -0.0934    -0.0257     0.1786     0.0351\n",
      "('Feature 1', 'Std Dev')     1.0116     1.0488     0.9651     0.9411     0.9945     0.992\n",
      "('Feature 1', 'Median')      0.0481     0.1714    -0.1857    -0.1872     0.2239     0.0283\n",
      "('Feature 1', 'IQR')         1.2537     1.3063     1.2909     1.3971     1.4369     1.3616\n",
      "('Feature 1', 'Min')        -2.0417    -2.3019    -2.0392    -2.4239    -2.2111    -2.4239\n",
      "('Feature 1', 'Max')         3.8527     2.6324     2.7202     2.4632     2.1905     3.8527\n"
     ]
    }
   ],
   "source": [
    "print(k_sample_test.summary())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Poisson Kernel Test"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Shows example for perforing the the kernel-based quadratic distance Goodness-of-fit tests for Uniformity for\n",
    "spherical data using the Poisson kernel with concentration parameter rho."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-01-31T14:07:49.406697Z",
     "iopub.status.busy": "2024-01-31T14:07:49.406597Z",
     "iopub.status.idle": "2024-01-31T14:07:49.451277Z",
     "shell.execute_reply": "2024-01-31T14:07:49.450982Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Execution time: 0.041 seconds\n",
      "U Statistic Results\n",
      "H0 is rejected : False\n",
      "Un Statistic : 1.6156682048968174\n",
      "Critical Value : 0.06155875299050079\n",
      "V Statistic Results\n",
      "H0 is rejected : False\n",
      "Vn Statistic : 22.83255917641962\n",
      "Critical Value : 23.229486935225513\n"
     ]
    }
   ],
   "source": [
    "from QuadratiK.tools import sample_hypersphere\n",
    "from QuadratiK.poisson_kernel_test import PoissonKernelTest\n",
    "np.random.seed(42)\n",
    "X = sample_hypersphere(100,3, random_state=42)\n",
    "unif_test = PoissonKernelTest(rho = 0.7, random_state=42).test(X)\n",
    "\n",
    "print(\"Execution time: {:.3f} seconds\".format(unif_test.execution_time))\n",
    "\n",
    "print(\"U Statistic Results\")\n",
    "print(\"H0 is rejected : {}\".format(unif_test.u_statistic_h0_))\n",
    "print(\"Un Statistic : {}\".format(unif_test.u_statistic_un_))\n",
    "print(\"Critical Value : {}\".format(unif_test.u_statistic_cv_))\n",
    "\n",
    "print(\"V Statistic Results\")\n",
    "print(\"H0 is rejected : {}\".format(unif_test.v_statistic_h0_))\n",
    "print(\"Vn Statistic : {}\".format(unif_test.v_statistic_vn_))\n",
    "print(\"Critical Value : {}\".format(unif_test.v_statistic_cv_))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-01-31T14:07:49.452821Z",
     "iopub.status.busy": "2024-01-31T14:07:49.452705Z",
     "iopub.status.idle": "2024-01-31T14:07:49.456182Z",
     "shell.execute_reply": "2024-01-31T14:07:49.455906Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Time taken for execution: 0.041 seconds\n",
      "Test Results \n",
      "--------------------------  -------------------\n",
      "Test Type                   Poisson Kernel-based quadratic \n",
      " distance test of Uniformity on the Sphere\n",
      "U Statistic Un              1.6156682048968174\n",
      "U Statistic Critical Value  0.06155875299050079\n",
      "U Statistic Reject H0       False\n",
      "V Statistic Vn              22.83255917641962\n",
      "V Statistic Critical Value  23.229486935225513\n",
      "V Statistic Reject H0       False\n",
      "--------------------------  -------------------\n",
      "Summary Statistics \n",
      "           Feature 0    Feature 1    Feature 2\n",
      "-------  -----------  -----------  -----------\n",
      "Mean          0.0451      -0.1206       0.0309\n",
      "Std Dev       0.509        0.5988       0.6122\n",
      "Median        0.132       -0.1596       0.0879\n",
      "IQR           0.8051       1.0063       1.1473\n",
      "Min          -0.9548      -0.9929      -0.9904\n",
      "Max           0.9772       0.9738       0.9996\n"
     ]
    }
   ],
   "source": [
    "print(unif_test.summary())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### QQ Plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-01-31T14:07:49.457572Z",
     "iopub.status.busy": "2024-01-31T14:07:49.457484Z",
     "iopub.status.idle": "2024-01-31T14:07:49.608102Z",
     "shell.execute_reply": "2024-01-31T14:07:49.607794Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 94752 characters]",
      "text/plain": [
       "<Figure size 600x900 with 3 Axes>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from QuadratiK.tools import qq_plot\n",
    "\n",
    "qq_plot(X,dist = \"uniform\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Poisson Kernel based Clustering"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Shows example for performing the Poisson kernel-based clustering algorithm on the Sphere based on\n",
    "the Poisson kernel-based densities."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-01-31T14:07:49.609632Z",
     "iopub.status.busy": "2024-01-31T14:07:49.609542Z",
     "iopub.status.idle": "2024-01-31T14:07:51.197055Z",
     "shell.execute_reply": "2024-01-31T14:07:51.195021Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Estimated mixing proportions : [0.23590339 0.24977919 0.25777522 0.25654219]\n",
      "Estimated concentration parameters:  [0.97773265 0.98348976 0.98226901 0.98572597]\n",
      "Adjusted Rand Index: 0.9403086353805835\n",
      "Macro Precision: 0.9771870612442508\n",
      "Macro Recall: 0.9769999999999999\n",
      "Average Silhouette Score: 0.3803089203572107\n"
     ]
    }
   ],
   "source": [
    "from QuadratiK.datasets import load_wireless_data\n",
    "from QuadratiK.spherical_clustering import PKBC\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "\n",
    "X, y = load_wireless_data(return_X_y=True)\n",
    "\n",
    "le = LabelEncoder()\n",
    "le.fit(y)\n",
    "y = le.transform(y)\n",
    "\n",
    "cluster_fit = PKBC(num_clust=4, random_state=42).fit(X)\n",
    "ari, macro_precision, macro_recall, avg_silhouette_Score = cluster_fit.validation(y)\n",
    "\n",
    "print(\"Estimated mixing proportions :\", cluster_fit.alpha_)\n",
    "print(\"Estimated concentration parameters: \", cluster_fit.rho_)\n",
    "\n",
    "print(\"Adjusted Rand Index:\", ari)\n",
    "print(\"Macro Precision:\", macro_precision)\n",
    "print(\"Macro Recall:\", macro_recall)\n",
    "print(\"Average Silhouette Score:\", avg_silhouette_Score)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Elbow Plot using Euclidean Distance and Cosine Similarity based WCSS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-01-31T14:07:51.201567Z",
     "iopub.status.busy": "2024-01-31T14:07:51.201254Z",
     "iopub.status.idle": "2024-01-31T14:08:09.182285Z",
     "shell.execute_reply": "2024-01-31T14:08:09.181615Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 38120 characters]",
      "text/plain": [
       "<Figure size 600x400 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 46892 characters]",
      "text/plain": [
       "<Figure size 600x400 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "wcss_euc = []\n",
    "wcss_cos = []\n",
    "\n",
    "for i in range(2, 10):\n",
    "    clus_fit = PKBC(num_clust=i).fit(X)\n",
    "    wcss_euc.append(clus_fit.euclidean_wcss_)\n",
    "    wcss_cos.append(clus_fit.cosine_wcss_)\n",
    "\n",
    "fig = plt.figure(figsize=(6, 4))\n",
    "plt.plot(list(range(2, 10)), wcss_euc, \"--o\")\n",
    "plt.xlabel(\"Number of Cluster\")\n",
    "plt.ylabel(\"Within Cluster Sum of Squares (WCSS)\")\n",
    "plt.title(\"Elbow Plot for Wireless Indoor Localization dataset\")\n",
    "plt.show()\n",
    "\n",
    "fig = plt.figure(figsize=(6, 4))\n",
    "plt.plot(list(range(2,10)),wcss_cos, \"--o\")\n",
    "plt.xlabel(\"Number of Cluster\")\n",
    "plt.ylabel(\"Within Cluster Sum of Squares (WCSS)\")\n",
    "plt.title(\"Elbow Plot for Wireless Indoor Localization dataset\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Density Estimation and Sample Generation from PKBD"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-01-31T14:08:09.186222Z",
     "iopub.status.busy": "2024-01-31T14:08:09.185946Z",
     "iopub.status.idle": "2024-01-31T14:08:09.202059Z",
     "shell.execute_reply": "2024-01-31T14:08:09.200680Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.46827108 0.05479605 0.21163936 0.06195099 0.39567698 0.40473724\n",
      " 0.26561508 0.36791766 0.09324676 0.46847274]\n"
     ]
    }
   ],
   "source": [
    "from QuadratiK.spherical_clustering import PKBD\n",
    "pkbd_data = PKBD().rpkb(10,[0.5,0],0.5, \"rejvmf\", random_state= 42)\n",
    "dens_val  = PKBD().dpkb(pkbd_data, [0.5,0.5],0.5)\n",
    "print(dens_val)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tuning Parameter $h$ selection"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Computes the kernel bandwidth of the Gaussian kernel for the two-sample and ksample\n",
    "kernel-based quadratic distance (KBQD) tests."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-01-31T14:08:09.204210Z",
     "iopub.status.busy": "2024-01-31T14:08:09.204088Z",
     "iopub.status.idle": "2024-01-31T14:08:19.527021Z",
     "shell.execute_reply": "2024-01-31T14:08:19.526672Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Selected h is:  2.8\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from QuadratiK.kernel_test import select_h\n",
    "np.random.seed(42)\n",
    "X = np.random.randn(200, 2)\n",
    "np.random.seed(42)\n",
    "y = np.random.randint(0, 2, 200)\n",
    "h_selected, all_values, power_plot = select_h(\n",
    "    X, y, alternative='location', power_plot=True,  random_state=None)\n",
    "print(\"Selected h is: \", h_selected)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-01-31T14:08:19.528565Z",
     "iopub.status.busy": "2024-01-31T14:08:19.528449Z",
     "iopub.status.idle": "2024-01-31T14:08:19.534176Z",
     "shell.execute_reply": "2024-01-31T14:08:19.533943Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>h</th>\n",
       "      <th>delta</th>\n",
       "      <th>power</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.4</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.8</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.2</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.42</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.6</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2.0</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2.4</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.36</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2.8</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.56</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>3.2</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.38</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     h  delta  power\n",
       "0  0.4    0.2   0.20\n",
       "1  0.8    0.2   0.26\n",
       "2  1.2    0.2   0.42\n",
       "3  1.6    0.2   0.34\n",
       "4  2.0    0.2   0.38\n",
       "5  2.4    0.2   0.36\n",
       "6  2.8    0.2   0.56\n",
       "7  3.2    0.2   0.38"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#shows the detailed power vs h table\n",
    "all_values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-01-31T14:08:19.535461Z",
     "iopub.status.busy": "2024-01-31T14:08:19.535362Z",
     "iopub.status.idle": "2024-01-31T14:08:19.710661Z",
     "shell.execute_reply": "2024-01-31T14:08:19.710021Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 37940 characters]",
      "text/plain": [
       "<Figure size 800x400 with 1 Axes>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#shows the power plot\n",
    "power_plot"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "test2",
   "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.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
