{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Robustness Analysis (Classification)\n\nThis example demonstrates how to analyze model robustness\nfor classification problems using various methods and metrics.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Installation\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# To install the required package, use the following command:\n# !pip install modeva"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Authentication\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# To get authentication, use the following command: (To get full access please replace the token to your own token)\n# from modeva.utils.authenticate import authenticate\n# authenticate(auth_code='eaaa4301-b140-484c-8e93-f9f633c8bacb')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Import required modules\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from modeva import DataSet\nfrom modeva import TestSuite\nfrom modeva.models import MoLGBMClassifier\nfrom modeva.models import MoXGBClassifier\nfrom modeva.testsuite.utils.slicing_utils import get_data_info"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Load and prepare dataset\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "ds = DataSet()\nds.load(name=\"TaiwanCredit\")\nds.set_random_split()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Train models\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "model1 = MoXGBClassifier()\nmodel1.fit(ds.train_x, ds.train_y)\n\nmodel2 = MoLGBMClassifier(max_depth=2, verbose=-1, random_state=0)\nmodel2.fit(ds.train_x, ds.train_y.ravel())"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Basic robustness analysis\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "ts = TestSuite(ds, model1)\nresults = ts.diagnose_robustness(perturb_features=(\"PAY_1\", \"EDUCATION\",),\n                                 noise_levels=(0.1, 0.2, 0.3, 0.4),\n                                 metric=\"AUC\")\nresults.table"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Box plot of robustness performance\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "results.plot(figsize=(6, 5))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Analyze data drift between small and large prediction changes groups\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "data_results = ds.data_drift_test(**results.value[0.2][\"data_info\"],\n                                  distance_metric=\"PSI\",\n                                  psi_method=\"uniform\",\n                                  psi_bins=10)\ndata_results.plot(\"summary\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Analyze data drift for single variable\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "data_results.plot((\"density\", \"PAY_1\"))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Slicing robustness analysis\nSingle feature slicing\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "results = ts.diagnose_slicing_robustness(features=\"PAY_1\",\n                                         perturb_features=(\"PAY_1\", \"EDUCATION\",),\n                                         noise_levels=0.1,\n                                         metric=\"AUC\",\n                                         method=\"auto-xgb1\",\n                                         threshold=0.7)\nresults.plot()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Analyze data drift for a specific feature\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "data_info = get_data_info(res_value=results.value)\ndata_results = ds.data_drift_test(**data_info[\"PAY_1\"],\n                                  distance_metric=\"PSI\",\n                                  psi_method=\"uniform\",\n                                  psi_bins=10)\ndata_results.plot(\"summary\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Single feature density plot\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "data_results.plot((\"density\", \"PAY_1\"))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Bivariate feature slicing\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "results = ts.diagnose_slicing_robustness(features=(\"PAY_1\", \"PAY_2\"),\n                                         perturb_features=(\"PAY_1\", \"EDUCATION\",),\n                                         noise_levels=0.1,\n                                         metric=\"AUC\",\n                                         threshold=0.7)\nresults.table"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Batch mode single feature slicing\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "results = ts.diagnose_slicing_robustness(features=((\"PAY_1\",), (\"PAY_2\",), (\"PAY_3\",)),\n                                         perturb_features=(\"PAY_1\", \"EDUCATION\",),\n                                         noise_levels=0.1, \n                                         perturb_method=\"quantile\",\n                                         metric=\"AUC\",\n                                         threshold=0.7)\nresults.table"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Batch mode 1D Slicing (all features by setting features=None)\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "results = ts.diagnose_slicing_robustness(features=None,\n                                         perturb_features=(\"PAY_1\", \"EDUCATION\",),\n                                         noise_levels=0.1,\n                                         perturb_method=\"quantile\",\n                                         metric=\"AUC\",\n                                         threshold=0.7)\nresults.table"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Robustness comparison\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "tsc = TestSuite(ds, models=[model1, model2])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Compare resilience performance of multiple models\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "results = tsc.compare_robustness(perturb_features=(\"PAY_1\", \"EDUCATION\",),\n                                 noise_levels=(0.1, 0.2, 0.3, 0.4),\n                                 perturb_method=\"quantile\",\n                                 metric=\"AUC\")\nresults.plot(figsize=(6, 5))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Compare robustness performance of multiple models under single slicing feature\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "results = tsc.compare_slicing_robustness(features=\"PAY_1\", noise_levels=0.1,\n                                         method=\"quantile\", metric=\"AUC\")\nresults.plot()"
      ]
    }
  ],
  "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.11.11"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}