{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "912f7d9d",
   "metadata": {},
   "source": [
    "# Percentage of All Non-Overlapping Data (PAND)\n",
    "\n",
    "The `pand` function calculates the Percentage of All Non-Overlapping Data (PAND) for single-case experimental designs. PAND is a non-parametric effect size measure that quantifies the proportion of non-overlapping data points between two phases."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f35af76c",
   "metadata": {},
   "source": [
    "## Function Overview\n",
    "\n",
    "The `pand` function computes PAND values along with several related statistics including chi-square tests and effect sizes.\n",
    "\n",
    "### Arguments\n",
    "- `data`: DataFrame or list of DataFrames containing single-case data\n",
    "- `dvar`: Name of dependent variable column (default: \"values\")\n",
    "- `pvar`: Name of phase variable column (default: \"phase\")\n",
    "- `decreasing`: If True, lower values in Phase B indicate improvement (default: False)\n",
    "- `phases`: Tuple of phases to compare (default: (\"A\", \"B\"))\n",
    "- `method`: Calculation method (\"sort\" or \"minimum\", default: \"sort\")\n",
    "\n",
    "The function prints all results directly and does not return a value."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "cde262e8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "📖 scia 1.101.0.dev6 - For Documentation, visit: https://ahsankhodami.github.io/scia/intro.html\n"
     ]
    }
   ],
   "source": [
    "import scia as sc"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3823ec50",
   "metadata": {},
   "source": [
    "## Example 1: Single-Case PAND Calculation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "387090f2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Percentage of all non-overlapping data\n",
      "\n",
      "Method: sort\n",
      "\n",
      "PAND = 100.00%\n",
      "Φ = 1.000  ;  Φ² = 1.000\n",
      "\n",
      "11 measurements (5 Phase A, 6 Phase B) in 1 cases\n",
      "Overlapping data: n = 0 ; percentage = 0.00 \n",
      "\n",
      "2 x 2 Matrix of percentages\n",
      "          A         B\n",
      "A  0.454545  0.000000\n",
      "B  0.000000  0.545455 \n",
      "\n",
      "2 x 2 Matrix of counts\n",
      "   A  B\n",
      "A  5  0\n",
      "B  0  6 \n",
      "\n",
      "Chi-Squared test:\n",
      "X² = 11.000, df = 1, p = 0.001 \n",
      "\n",
      "Fisher exact test:\n",
      "Odds ratio = inf, p = 0.002 \n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Create a single-case AB design\n",
    "df = sc.create_scd(\n",
    "    values=[5, 7, 8, 5, 7, 12, 16, 18, 15, 14, 19],\n",
    "    phase_starts={\"A\": 1, \"B\": 6}\n",
    ")\n",
    "\n",
    "# Compute PAND\n",
    "sc.pand(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cccab618",
   "metadata": {},
   "source": [
    "## Example 2: Multiple-Case PAND Calculation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f7fea81d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Percentage of all non-overlapping data\n",
      "\n",
      "Method: sort\n",
      "\n",
      "PAND = 57.58%\n",
      "Φ = 0.144  ;  Φ² = 0.021\n",
      "\n",
      "33 measurements (15 Phase A, 18 Phase B) in 1 cases\n",
      "Overlapping data: n = 14 ; percentage = 42.42 \n",
      "\n",
      "2 x 2 Matrix of percentages\n",
      "          A         B\n",
      "A  0.242424  0.212121\n",
      "B  0.212121  0.333333 \n",
      "\n",
      "2 x 2 Matrix of counts\n",
      "   A   B\n",
      "A  8   7\n",
      "B  7  11 \n",
      "\n",
      "Chi-Squared test:\n",
      "X² = 0.689, df = 1, p = 0.407 \n",
      "\n",
      "Fisher exact test:\n",
      "Odds ratio = 1.796, p = 0.494 \n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Create multiple cases\n",
    "case1 = sc.create_scd(values={\"A\": [5, 7, 8, 5, 7], \"B\": [12, 16, 18, 15, 14, 19]})\n",
    "case2 = sc.create_scd(values={\"A\": [3, 4, 5, 3], \"B\": [6, 7, 8, 9, 10, 11, 12]})\n",
    "case3 = sc.create_scd(values={\"A\": [6, 8, 7, 8, 7, 8], \"B\": [10, 12, 13, 14, 15]})\n",
    "\n",
    "# Compute PAND for all cases\n",
    "sc.pand([case1, case2, case3])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f8a7deb9",
   "metadata": {},
   "source": [
    "## Notes\n",
    "- PAND values range from 0% (complete overlap) to 100% (complete separation)\n",
    "- The function also provides chi-square test results and effect sizes (Phi)\n",
    "- For decreasing outcomes (where lower values indicate improvement), set `decreasing=True`\n",
    "- Two calculation methods are available: \"sort\" (default) and \"minimum\""
   ]
  }
 ],
 "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.12.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}