{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import mpmath\n",
    "import dautil as dl\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from IPython.display import HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def mpmean(arr):\n",
    "    mpfs = [mpmath.mpf(a) for a in arr]\n",
    "\n",
    "    return sum(mpfs)/len(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "vals = dl.data.Weather.load()['WIND_SPEED'].dropna()\n",
    "vals = dl.ts.groupby_yday(vals).apply(mpmean)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "days = np.arange(1, 367, dtype=int)\n",
    "A = [[], [], []]\n",
    "A[0] = np.ones_like(days, dtype=int).tolist()\n",
    "A[1] = days.tolist()\n",
    "A[2] = (days ** 2).tolist()\n",
    "A = mpmath.matrix(A).transpose()\n",
    "\n",
    "params = mpmath.lu_solve(A, vals)\n",
    "\n",
    "result = dl.report.HTMLBuilder()\n",
    "result.h1('Arbitrary Precision Linear Algebra')\n",
    "result.h2('Polynomial fit')\n",
    "dfb = dl.report.DFBuilder(['Coefficient 0', 'Coefficient 1', 'Coefficient 2'])\n",
    "dfb.row(params)\n",
    "result.add_df(dfb.build())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def poly(x):\n",
    "    return mpmath.polyval(params[::-1], x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "cs = mpmath.fourier(poly, days.tolist(), 1)\n",
    "result.h2('Cosine and sine terms')\n",
    "dfb = dl.report.DFBuilder(['Coefficient 1', 'Coefficient 2'])\n",
    "dfb.row(cs[0])\n",
    "dfb.row(cs[1])\n",
    "result.add_df(dfb.build(index=['Cosine', 'Sine']), index=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "dl.options.mimic_seaborn()\n",
    "context = dl.nb.Context('mpmath_linalg')\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",
    "\n",
    "cp = dl.plotting.CyclePlotter(sp.ax)\n",
    "cp.plot(days, vals, label='Data')\n",
    "cp.plot(days, poly(days), label='Fit')\n",
    "yvar = dl.data.Weather.get_header('WIND_SPEED')\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, [mpmath.fourierval(cs, days, d) for d in days], label='Approximation')\n",
    "sp.label(ylabel_params=yvar)\n",
    "plt.tight_layout()\n",
    "HTML(result.html)"
   ]
  },
  {
   "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
}
