{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# FuseKernel Regression\n\nFuseKernel fuses complementary kernel channels and decodes them with kernel\nridge regression. This example fuses the XGBoost tree co-membership kernel with\na learned multi-scale spectral kernel (MS-SKM) on the CaliforniaHousing dataset.\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": [
        "import warnings\nwarnings.filterwarnings(\"ignore\")\n\nimport numpy as np\nimport pandas as pd\nfrom modeva import DataSet, ModelZoo, TestSuite"
      ]
    },
    {
      "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=\"CaliforniaHousing\")\nds.set_random_split()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Train FuseKernel\nFuseKernel fuses complementary kernel channels -- here the XGBoost tree\nco-membership kernel and the learned multi-scale spectral kernel (MS-SKM) --\nand decodes them with kernel ridge regression. Every channel is fit on a\nsupport fold and the mixture weights and ridge are selected on a held-out\nquery fold (``fit_method=\"grid\"``), so the selection is leakage-free.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from modeva.models import MoFuseKernelRegressor\n\nmodel = MoFuseKernelRegressor(\n    name=\"FuseKernel\",\n    use_xgb=True, use_spectral=True,            # channels to fuse\n    fit_method=\"grid\",                          # leakage-free, query-scored\n    spectral_params={\"H\": 4, \"K\": 8, \"kernel\": \"laplace\", \"epochs\": 50},\n    interpret_ref_size=400, interpret_n_grid=20,\n    random_state=0,\n)\n\nmz = ModelZoo(dataset=ds)\nmz.add_model(model)\nmz.train_all()\nmz.leaderboard()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Accuracy\nFuseKernel is a standard MoDeVa model, so the whole TestSuite applies.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "ts = TestSuite(ds, model)\nts.diagnose_accuracy_table().table"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Inherent Interpretation (FANOVA)\nWith a spectral channel present, FuseKernel exposes a functional-ANOVA\ndecomposition of the fused predictor through the standard ``interpret_*``\nmethods.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "results = ts.interpret_fi()\nresults.table"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "results = ts.interpret_ei()\nresults.table"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "results = ts.interpret_effects(features=\"MedInc\")\nresults.plot(results.get_figure_names()[0])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "results = ts.interpret_effects(features=\"Latitude\")\nresults.plot(results.get_figure_names()[0])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "results = ts.interpret_local_fi(sample_index=1)\nresults.table"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Channel Decomposition\nThe fused prediction is an exact additive sum over channels.\n``channel_contributions`` returns each channel's contribution in target units.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "result = model.channel_contributions(ds.test_x)\nresult.table"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Inherent GP Predictive Uncertainty\nFor regression the prediction intervals are the closed-form Gaussian-process\nposterior of the fused kernel, so ``diagnose_reliability`` reflects the\nmodel's own uncertainty.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "ts.diagnose_reliability().table"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "mean, var = model.predict_dist(ds.test_x)\nintervals = model.predict_interval(ds.test_x)      # (n, 2) at the calibrated level\npd.DataFrame({\"mean\": mean[:5], \"std\": np.sqrt(var)[:5],\n              \"low\": intervals[:5, 0], \"high\": intervals[:5, 1]})"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Weakness Localization\nSlicing surfaces low-accuracy regions of input space.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "ts.diagnose_slicing_accuracy(features=\"MedInc\").table"
      ]
    }
  ],
  "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
}