{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input",
     "active-ipynb",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "try:\n",
    "    from openmdao.utils.notebook_utils import notebook_mode\n",
    "except ImportError:\n",
    "    !python -m pip install openmdao[notebooks]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "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": null,
   "metadata": {},
   "outputs": [],
   "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": null,
   "metadata": {
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "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",
   "metadata": {},
   "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": null,
   "metadata": {
    "tags": [
     "remove-input"
    ]
   },
   "outputs": [],
   "source": [
    "om.show_options_table(\"openmdao.surrogate_models.kriging.KrigingSurrogate\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "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": null,
   "metadata": {},
   "outputs": [],
   "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": null,
   "metadata": {
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "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.8.1"
  },
  "orphan": true
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
