{
  "cells": [
    {
      "source": [
        "%matplotlib inline"
      ],
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "cell_type": "code",
      "execution_count": null
    },
    {
      "source": [
        "\n# Plotting bandit ucb behavior\n\n\nShow the evolution of the current min and the selected input\nwith bandit ucb.\n\n"
      ],
      "cell_type": "markdown",
      "metadata": {}
    },
    {
      "source": [
        "import numpy as np\nimport matplotlib.pyplot as plt\nfrom scipy import minimum\n\nfrom fluentopt import Bandit\nfrom fluentopt.bandit import ucb_minimize\n\nnp.random.seed(42)\n\n\ndef sampler(rng):\n    return rng.uniform(-1, 1)\n\n\ndef feval(x):\n    return (x ** 2 - 2)\n\nopt = Bandit(sampler=sampler, score=ucb_minimize)\nn_iter = 100\nfor _ in range(n_iter):\n    x = opt.suggest()\n    y = feval(x)\n    opt.update(x=x, y=y)\n\nidx = np.argmin(opt.output_history_)\nbest_input = opt.input_history_[idx]\nbest_output = opt.output_history_[idx]\nprint('best input : {:.2f}, best output : {:.2f}'.format(best_input, best_output))\niters = np.arange(len(opt.output_history_))\nfig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 5))\n\nax1.plot(iters, minimum.accumulate(opt.output_history_))\nax1.set_xlabel('iteration')\nax1.set_ylabel('current $min_x({x**2-2})$')\n\nax2.plot(iters, opt.input_history_)\nax2.set_xlabel('iteration')\nax2.set_ylabel('selected input')\n\nplt.show()"
      ],
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "cell_type": "code",
      "execution_count": null
    }
  ],
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "language_info": {
      "nbconvert_exporter": "python",
      "mimetype": "text/x-python",
      "pygments_lexer": "ipython3",
      "file_extension": ".py",
      "name": "python",
      "version": "3.5.2",
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      }
    },
    "kernelspec": {
      "name": "python3",
      "language": "python",
      "display_name": "Python 3"
    }
  }
}