{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f89ecbf3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:43:00.339119Z",
     "iopub.status.busy": "2026-10-02T14:43:00.338927Z",
     "iopub.status.idle": "2026-10-02T14:43:00.343063Z",
     "shell.execute_reply": "2026-10-02T14:43:00.342379Z"
    },
    "papermill": {
     "duration": 0.006649,
     "end_time": "2026-10-02T14:43:00.343668+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:00.337019+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "active-ipynb",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "try:\n",
    "    from openmdao.utils.notebook_utils import notebook_mode  # noqa: F401\n",
    "except ImportError:\n",
    "    !python -m pip install openmdao[notebooks]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f72ffd7",
   "metadata": {
    "papermill": {
     "duration": 0.001526,
     "end_time": "2026-10-02T14:43:00.346784+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:00.345258+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "# KrigingSurrogate\n",
    "\n",
    "The KrigingSurrogate implements a simple Kriging interpolation method based on Gaussian Processes for Machine Learning (GPML) by Rasmussen and Williams. In the default configuration, the surrogate computes the mean of the predicted value. KrigingSurrogate also has an option “eval_rmse”, which can be set to True to also compute the RMSE (root mean squared error).\n",
    "\n",
    "Here is a simple example where a Kriging model is used to approximate the output of a sinusoidal component."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "eb97c148",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:43:00.362599Z",
     "iopub.status.busy": "2026-10-02T14:43:00.362464Z",
     "iopub.status.idle": "2026-10-02T14:43:03.039411Z",
     "shell.execute_reply": "2026-10-02T14:43:03.038817Z"
    },
    "papermill": {
     "duration": 2.679861,
     "end_time": "2026-10-02T14:43:03.040133+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:00.360272+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1790952183.023012] [runnervm8df0l:7453 :0]        ib_iface.c:1269 UCX  ERROR mana_0: iface 0x5564149115f0 failed to create UD QP TX wr:256 sge:6 inl:64 resp:0 RX wr:4096 sge:1 resp:0 failed: Operation not supported\n",
      "[1790952183.023251] [runnervm8df0l:7453 :0]      ucp_worker.c:1412 UCX  ERROR uct_iface_open(ud_verbs/mana_0:1) failed: Input/output error\n",
      "[0.43161039]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[runnervm8df0l:07453] pml_ucx.c:313  Error: Failed to create UCP worker\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "import openmdao.api as om\n",
    "\n",
    "prob = om.Problem()\n",
    "\n",
    "sin_mm = om.MetaModelUnStructuredComp()\n",
    "sin_mm.add_input('x', 2.1)\n",
    "sin_mm.add_output('f_x', 0., surrogate=om.KrigingSurrogate())\n",
    "\n",
    "prob.model.add_subsystem('sin_mm', sin_mm)\n",
    "\n",
    "prob.setup()\n",
    "\n",
    "# train the surrogate and check predicted value\n",
    "sin_mm.options['train_x'] = np.linspace(0,10,20)\n",
    "sin_mm.options['train_f_x'] = .5*np.sin(sin_mm.options['train_x'])\n",
    "\n",
    "prob.set_val('sin_mm.x', 2.1)\n",
    "\n",
    "prob.run_model()\n",
    "\n",
    "print(prob.get_val('sin_mm.f_x'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4290f9ef",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:43:03.044000Z",
     "iopub.status.busy": "2026-10-02T14:43:03.043651Z",
     "iopub.status.idle": "2026-10-02T14:43:03.051047Z",
     "shell.execute_reply": "2026-10-02T14:43:03.050205Z"
    },
    "papermill": {
     "duration": 0.009988,
     "end_time": "2026-10-02T14:43:03.051646+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:03.041658+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(1.3231776533541246e-05)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from openmdao.utils.assert_utils import assert_near_equal\n",
    "assert_near_equal(prob.get_val('sin_mm.f_x'), .5*np.sin(prob.get_val('sin_mm.x')), 1e-4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9f191454",
   "metadata": {
    "papermill": {
     "duration": 0.001678,
     "end_time": "2026-10-02T14:43:03.054660+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:03.052982+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "## KrigingSurrogate Options\n",
    "\n",
    "All options can be passed in as arguments or set later by accessing the options dictionary."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2e91e8f1",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:43:03.093807Z",
     "iopub.status.busy": "2026-10-02T14:43:03.093589Z",
     "iopub.status.idle": "2026-10-02T14:43:03.098236Z",
     "shell.execute_reply": "2026-10-02T14:43:03.097529Z"
    },
    "papermill": {
     "duration": 0.04241,
     "end_time": "2026-10-02T14:43:03.098889+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:03.056479+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<!DOCTYPE html>\n",
       "<html lang=\"en\">\n",
       "<head>\n",
       "    <style>\n",
       "        h2 {\n",
       "            text-align: center;\n",
       "        }\n",
       "    </style>\n",
       "</head>\n",
       "<body>\n",
       "    <h2></h2>\n",
       "        <table style=\"border: 1px solid #999; border-collapse: collapse;\">\n",
       "        <tr><th style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; background-color: #E9E9E9; text-align: left;\">Option</th><th style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; background-color: #E9E9E9; text-align: left;\">Default</th><th style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; background-color: #E9E9E9; text-align: left;\">Acceptable Values</th><th style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; background-color: #E9E9E9; text-align: left;\">Acceptable Types</th><th style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; background-color: #E9E9E9; text-align: left;\">Description</th></tr>\n",
       "       <tr style=\"background-color: ghostwhite;\"><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">eval_rmse</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">False</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">[True, False]</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">[&#x27;bool&#x27;]</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">Flag indicating whether the Root Mean Squared Error (RMSE) should be computed. Set to False by default.</td></tr>\n",
       "       <tr style=\"background-color: #F3F3F3;\"><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">lapack_driver</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">gesvd</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">N/A</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">[&#x27;str&#x27;]</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">Which lapack driver should be used for scipy&#x27;s linalg.svd. Options are &#x27;gesdd&#x27; which is faster but not as robust, or &#x27;gesvd&#x27; which is slower but more reliable. Default is &#x27;gesvd&#x27; for this platform.</td></tr>\n",
       "       <tr style=\"background-color: ghostwhite;\"><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">nugget</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">2.220446049250313e-15</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">N/A</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">N/A</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">Nugget smoothing parameter for smoothing noisy data. Represents the variance of the input values. If nugget is an ndarray, it must be of the same length as the number of training points. Default: 10. * Machine Epsilon</td></tr>\n",
       "       <tr style=\"background-color: #F3F3F3;\"><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">training_cache</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">N/A</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">N/A</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">[&#x27;str&#x27;]</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">Cache the trained model to avoid repeating training and write it to the given file. If the specified file exists, it will be used to load the weights</td></tr>\n",
       "    </table>\n",
       "</body>\n",
       "</html>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "om.show_options_table(\"openmdao.surrogate_models.kriging.KrigingSurrogate\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5928defe",
   "metadata": {
    "papermill": {
     "duration": 0.001458,
     "end_time": "2026-10-02T14:43:03.129193+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:03.127735+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "## KrigingSurrogate Constructor\n",
    "\n",
    "The call signature for the `KrigingSurrogate` constructor is:\n",
    "```{eval-rst}\n",
    "    .. automethod:: openmdao.surrogate_models.kriging.KrigingSurrogate.__init__\n",
    "        :noindex:\n",
    "```\n",
    "\n",
    "## KrigingSurrogate Option Examples\n",
    "\n",
    "**eval_rmse**\n",
    "\n",
    "By default, `KrigingSurrogate` only returns the mean of the predicted outputs. You can compute both the mean and the root mean square prediction error by setting the “eval_rmse” option to True. The most recent calculation of error is stored in the component’s metadata, and accessed as follows."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e8415a9b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:43:03.133514Z",
     "iopub.status.busy": "2026-10-02T14:43:03.133331Z",
     "iopub.status.idle": "2026-10-02T14:43:03.145164Z",
     "shell.execute_reply": "2026-10-02T14:43:03.144485Z"
    },
    "papermill": {
     "duration": 0.015258,
     "end_time": "2026-10-02T14:43:03.146086+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:03.130828+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mean\n",
      "[0.43161039]\n",
      "std\n",
      "2.113465343166703e-05\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "import openmdao.api as om\n",
    "\n",
    "prob = om.Problem()\n",
    "\n",
    "sin_mm = om.MetaModelUnStructuredComp()\n",
    "sin_mm.add_input('x', 2.1)\n",
    "sin_mm.add_output('f_x', 0., surrogate=om.KrigingSurrogate(eval_rmse=True))\n",
    "\n",
    "prob.model.add_subsystem('sin_mm', sin_mm)\n",
    "\n",
    "prob.setup()\n",
    "\n",
    "# train the surrogate and check predicted value\n",
    "sin_mm.options['train_x'] = np.linspace(0,10,20)\n",
    "sin_mm.options['train_f_x'] = .5*np.sin(sin_mm.options['train_x'])\n",
    "\n",
    "prob.set_val('sin_mm.x', 2.1)\n",
    "\n",
    "prob.run_model()\n",
    "\n",
    "print(\"mean\")\n",
    "print(prob.get_val('sin_mm.f_x'))\n",
    "print(\"std\")\n",
    "print(sin_mm._metadata('f_x')['rmse'][0, 0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1fd0b053",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:43:03.150372Z",
     "iopub.status.busy": "2026-10-02T14:43:03.150220Z",
     "iopub.status.idle": "2026-10-02T14:43:03.155272Z",
     "shell.execute_reply": "2026-10-02T14:43:03.154790Z"
    },
    "papermill": {
     "duration": 0.008272,
     "end_time": "2026-10-02T14:43:03.155805+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:03.147533+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(2.113465343166703e-05)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "assert_near_equal(prob.get_val('sin_mm.f_x'), .5*np.sin(prob.get_val('sin_mm.x')), 1e-4)\n",
    "assert_near_equal(sin_mm._metadata('f_x')['rmse'][0, 0], 0.0, 1e-4)"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Tags",
  "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.13.14"
  },
  "orphan": true,
  "papermill": {
   "default_parameters": {},
   "duration": 4.012005,
   "end_time": "2026-10-02T14:43:03.673595+00:00",
   "environment_variables": {},
   "exception": null,
   "input_path": "/home/runner/work/OpenMDAO/OpenMDAO/openmdao/docs/openmdao_book/features/building_blocks/surrogates/kriging.ipynb",
   "output_path": "/home/runner/work/OpenMDAO/OpenMDAO/openmdao/docs/_executed_book/features/building_blocks/surrogates/kriging.ipynb",
   "parameters": {},
   "start_time": "2026-10-02T14:42:59.661590+00:00",
   "version": "2.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}