{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from scipy import signal\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import dautil as dl\n",
    "import statsmodels.api as sm\n",
    "from IPython.display import HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "context = dl.nb.Context('lomb_scargle')\n",
    "lr = dl.nb.LatexRenderer(chapter=6, start=7, context=context)\n",
    "lr.render(r'\\tan{2 \\omega \\tau} = \\frac{\\sum_j \\sin 2 \\omega t_j}{\\sum_j \\cos 2 \\omega t_j}')\n",
    "eqn = r''' P_x(\\omega) = \\frac{1}{2} \n",
    "\\left(\n",
    "  \\frac { \\left[ \\sum_j X_j \\cos \\omega ( t_j - \\tau ) \\right] ^ 2}\n",
    "        { \\sum_j \\cos^2 \\omega ( t_j - \\tau ) }\n",
    "+\n",
    " \\frac {\\left[ \\sum_j X_j \\sin \\omega ( t_j - \\tau ) \\right] ^ 2}\n",
    "        { \\sum_j \\sin^2 \\omega ( t_j - \\tau ) }\n",
    "\\right) '''\n",
    "lr.render(eqn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df = sm.datasets.sunspots.load_pandas().data\n",
    "sunspots = df['SUNACTIVITY'].values\n",
    "size = len(sunspots)\n",
    "t = np.linspace(-2 * np.pi, 2 * np.pi, size)\n",
    "sine = dl.ts.sine_like(sunspots)\n",
    "f = np.linspace(0.01, 2, 10 * size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "dl.options.mimic_seaborn()\n",
    "dl.nb.RcWidget(context)\n",
    "dl.nb.LabelWidget(2, 2, context)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "sp = dl.plotting.Subplotter(2, 2, context)\n",
    "sp.ax.plot(t, sine)\n",
    "sp.label()\n",
    "\n",
    "sp.next_ax().plot(df['YEAR'], df['SUNACTIVITY'])\n",
    "sp.label()\n",
    "\n",
    "pgram = signal.lombscargle(t, sine, f)\n",
    "sp.next_ax().plot(f, 2 * np.sqrt(pgram/size))\n",
    "sp.label()\n",
    "\n",
    "pgram = signal.lombscargle(np.arange(size, dtype=float), sunspots, f)\n",
    "sp.next_ax().plot(f, 2 * np.sqrt(pgram/size))\n",
    "sp.label()\n",
    "HTML(sp.exit())"
   ]
  },
  {
   "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
}
