{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Robustness Analysis (Regression)\n\nThis example demonstrates how to analyze model robustness\nfor regression 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 MoLGBMRegressor\nfrom modeva.models import MoXGBRegressor\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=\"BikeSharing\")\nds.set_random_split()\n\nds.scale_numerical(features=(\"cnt\",), method=\"log1p\")\nds.preprocess()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Train models\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "model1 = MoXGBRegressor()\nmodel1.fit(ds.train_x, ds.train_y)\n\nmodel2 = MoLGBMRegressor(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(\n    perturb_features=None,\n    noise_levels=(0.1, 0.2, 0.3, 0.4),\n    metric=\"MSE\"\n)\nresults.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\", \"hr\"))"
      ]
    },
    {
      "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(\n    features=\"hr\",\n    perturb_features=(\"hum\", \"atemp\"),\n    noise_levels=0.1,\n    metric=\"MAE\",\n    method=\"auto-xgb1\",\n    threshold=0.2\n)\nresults.table"
      ]
    },
    {
      "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(\n    features=(\"hr\", \"atemp\"),\n    perturb_features=(\"hum\", \"temp\"),\n    noise_levels=0.1,\n    metric=\"MSE\"\n)\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(\n    features=((\"hr\",), (\"atemp\",), (\"season\",)),\n    perturb_features=(\"temp\", \"hum\"),\n    noise_levels=0.1,\n    perturb_method=\"quantile\",\n    metric=\"MSE\",\n    threshold=0.15\n)\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=(\"temp\", \"hum\"),\n                                         noise_levels=0.1,\n                                         perturb_method=\"quantile\",\n                                         metric=\"MSE\",\n                                         threshold=0.15\n                                         )\nresults.table"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Analyze data drift\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(\n    **data_info[\"hr\"],\n    distance_metric=\"PSI\",\n    psi_method=\"uniform\",\n    psi_bins=10\n)\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\", \"hr\"))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Model 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(\n    perturb_features=(\"hr\", \"atemp\"),\n    noise_levels=(0.1, 0.2, 0.3, 0.4),\n    perturb_method=\"quantile\",\n    metric=\"MSE\"\n)\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(\n    features=\"hr\",\n    noise_levels=0.1,\n    method=\"quantile\",\n    metric=\"MSE\"\n)\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
}