{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ac5353c3",
   "metadata": {},
   "source": [
    "# Improvement Rate Difference (IRD)\n",
    "\n",
    "The `ird` function computes the Improvement Rate Difference (IRD) for single-case experimental design (SCED) data. IRD is a non-parametric effect size metric that quantifies the difference in improvement rates between two phases (typically A and B)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "593c6a19",
   "metadata": {},
   "source": [
    "## Function Overview\n",
    "\n",
    "The `ird` function can be used with a single-case DataFrame or a list of DataFrames (for multiple cases). It prints the IRD value(s) for the specified phases.\n",
    "\n",
    "### Arguments\n",
    "- `data`: DataFrame or list of DataFrames containing SCED data.\n",
    "- `dvar`: Name of the dependent variable column (default: \"values\").\n",
    "- `pvar`: Name of the phase variable column (default: \"phase\").\n",
    "- `decreasing`: If `True`, lower values in Phase B are considered improvement (default: `False`).\n",
    "- `phases`: Tuple of two phases to compare (default: (\"A\", \"B\")).\n",
    "\n",
    "The function prints IRD values and does not return a value."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7df1840c",
   "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": "653ad34d",
   "metadata": {},
   "source": [
    "## Example 1: Single-Case IRD Calculation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2f809506",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "IRD = 1.0\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 IRD\n",
    "sc.ird(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e8e306fd",
   "metadata": {},
   "source": [
    "## Example 2: Multiple-Case IRD Calculation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "117fe08c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Case 1: IRD = 0.6333333333333335\n",
      "Case 2: IRD = 1.0\n",
      "Case 3: IRD = 1.0\n",
      "All cases Average: 0.8777777777777779\n"
     ]
    }
   ],
   "source": [
    "# Create multiple cases\n",
    "case1 = sc.create_scd(values={\"A\": [5, 7, 10, 5, 12], \"B\": [7, 10, 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 IRD for all cases\n",
    "sc.ird([case1, case2, case3])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ceda989b",
   "metadata": {},
   "source": [
    "## Notes\n",
    "- IRD values range from 0 (no improvement) to 1 (maximum improvement).\n",
    "- For decreasing outcomes (where lower values indicate improvement), set `decreasing=True`."
   ]
  }
 ],
 "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
}
