{
 "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": null,
   "metadata": {},
   "outputs": [],
   "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": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(normality_test.summary())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Non-parametric"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "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": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(normality_test.summary())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### QQ Plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "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": null,
   "metadata": {},
   "outputs": [],
   "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": null,
   "metadata": {},
   "outputs": [],
   "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": null,
   "metadata": {},
   "outputs": [],
   "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": null,
   "metadata": {},
   "outputs": [],
   "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": null,
   "metadata": {},
   "outputs": [],
   "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": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(unif_test.summary())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### QQ Plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "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": null,
   "metadata": {},
   "outputs": [],
   "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": null,
   "metadata": {},
   "outputs": [],
   "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": null,
   "metadata": {},
   "outputs": [],
   "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": null,
   "metadata": {},
   "outputs": [],
   "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": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#shows the detailed power vs h table\n",
    "all_values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "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
}
