{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a013cc19",
   "metadata": {},
   "source": [
    "# Nonoverlap of All Pairs (NAP)\n",
    "\n",
    "The `nap` function calculates the Nonoverlap of All Pairs (NAP) for single-case data. NAP is a non-parametric effect size measure that quantifies the proportion of data pairs showing improvement between two phases."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "05e596f0",
   "metadata": {},
   "source": [
    "## Function Overview\n",
    "\n",
    "The `nap` function computes NAP values along with several related statistics including Mann-Whitney U test results 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",
    "\n",
    "### Returns\n",
    "A DataFrame with:\n",
    "- NAP and rescaled NAP values\n",
    "- Number of pairs and non-overlapping pairs\n",
    "- Mann-Whitney U test results (W statistic and p-value)\n",
    "- Effect sizes (d and R²)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "573d3062",
   "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": "f0367da6",
   "metadata": {},
   "source": [
    "## Example 1: Single-Case NAP Calculation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f343f4a6",
   "metadata": {},
   "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>Case</th>\n",
       "      <th>NAP</th>\n",
       "      <th>NAP Rescaled</th>\n",
       "      <th>Pairs</th>\n",
       "      <th>Non-overlaps</th>\n",
       "      <th>Positives</th>\n",
       "      <th>Ties</th>\n",
       "      <th>W</th>\n",
       "      <th>p</th>\n",
       "      <th>d</th>\n",
       "      <th>R²</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>NAs</td>\n",
       "      <td>100.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.003914</td>\n",
       "      <td>3.464</td>\n",
       "      <td>0.749989</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  Case    NAP NAP Rescaled Pairs Non-overlaps Positives Ties    W         p  \\\n",
       "0  NAs  100.0        100.0  30.0         30.0      30.0  0.0  0.0  0.003914   \n",
       "\n",
       "       d        R²  \n",
       "0  3.464  0.749989  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "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 NAP\n",
    "sc.nap(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd6fafda",
   "metadata": {},
   "source": [
    "## Example 2: Multiple-Case NAP Calculation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b32e1649",
   "metadata": {},
   "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>Case</th>\n",
       "      <th>NAP</th>\n",
       "      <th>NAP Rescaled</th>\n",
       "      <th>Pairs</th>\n",
       "      <th>Non-overlaps</th>\n",
       "      <th>Positives</th>\n",
       "      <th>Ties</th>\n",
       "      <th>W</th>\n",
       "      <th>p</th>\n",
       "      <th>d</th>\n",
       "      <th>R²</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>100.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.003914</td>\n",
       "      <td>3.464</td>\n",
       "      <td>0.749989</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>100.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>28.0</td>\n",
       "      <td>28.0</td>\n",
       "      <td>28.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.005278</td>\n",
       "      <td>3.464</td>\n",
       "      <td>0.749989</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>100.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.003704</td>\n",
       "      <td>3.464</td>\n",
       "      <td>0.749989</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Case    NAP NAP Rescaled Pairs Non-overlaps Positives Ties    W         p  \\\n",
       "0     1  100.0        100.0  30.0         30.0      30.0  0.0  0.0  0.003914   \n",
       "1     2  100.0        100.0  28.0         28.0      28.0  0.0  0.0  0.005278   \n",
       "2     3  100.0        100.0  30.0         30.0      30.0  0.0  0.0  0.003704   \n",
       "\n",
       "       d        R²  \n",
       "0  3.464  0.749989  \n",
       "1  3.464  0.749989  \n",
       "2  3.464  0.749989  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "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 NAP for all cases\n",
    "sc.nap([case1, case2, case3])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ceda989b",
   "metadata": {},
   "source": [
    "## Notes\n",
    "- NAP values range from 50% (no effect) to 100% (complete separation)\n",
    "- The rescaled version ranges from 0% to 100% for easier interpretation\n",
    "- For decreasing outcomes (where lower values indicate improvement), set `decreasing=True`\n",
    "- The function also provides Mann-Whitney U test results and effect sizes"
   ]
  }
 ],
 "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
}
