{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import mpmath\n",
    "import dautil as dl\n",
    "from IPython.display import HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def model(t):\n",
    "    mu, C, w, phi = (9.6848106, -7.59870042, -0.01766333, -5.83349705)\n",
    "\n",
    "    return mu + C * mpmath.cos(w * t + phi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def diff_model(t):\n",
    "    return mpmath.diff(model, t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "vals = dl.data.Weather.load()['TEMP'].dropna()\n",
    "vals = dl.ts.groupby_yday(vals).mean()\n",
    "diff_root = mpmath.findroot(diff_model, (1, 366), solver='anderson')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "days = range(1, 367)\n",
    "poly = mpmath.chebyfit(model, (1, 366), 3)\n",
    "poly = np.array([float(c) for c in poly])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "dl.options.mimic_seaborn()\n",
    "context = dl.nb.Context('mpmath_fit')\n",
    "dl.nb.RcWidget(context)\n",
    "dl.nb.LabelWidget(2, 1, context)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "sp = dl.plotting.Subplotter(2, 1, context)\n",
    "cp = dl.plotting.CyclePlotter(sp.ax)\n",
    "cp.plot(days, [model(i) for i in days], label='Model')\n",
    "cp.plot(days, vals, label='Data')\n",
    "sp.ax.annotate(s='Root of derivative', xy=(diff_root, vals.max() - 1),\n",
    "            xytext=(diff_root, vals.max() - 8),\n",
    "            arrowprops=dict(arrowstyle='->'))\n",
    "yvar = dl.data.Weather.get_header('TEMP')\n",
    "sp.label(ylabel_params=yvar)\n",
    "\n",
    "cp = dl.plotting.CyclePlotter(sp.next_ax())\n",
    "cp.plot(days, vals, label='Data')\n",
    "cp.plot(days, np.polyval(poly, days), label='Approximation')\n",
    "sp.label(ylabel_params=yvar)\n",
    "plt.tight_layout()\n",
    "HTML(dl.report.HTMLBuilder().watermark())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "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.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
