{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Populating the interactive namespace from numpy and matplotlib\n"
     ]
    }
   ],
   "source": [
    "%pylab inline\n",
    "from pycheops.models import RVModel\n",
    "from lmfit import report_fit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "T_0 = 0.5\n",
    "P = 2.0\n",
    "K = 1.5\n",
    "f_c = 0.3\n",
    "f_s = -0.2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Parameters([('T_0', <Parameter 'T_0', 0.5, bounds=[-inf:inf]>),\n",
       "            ('P', <Parameter 'P', value=2.0 (fixed), bounds=[1e-15:inf]>),\n",
       "            ('K', <Parameter 'K', 1.5, bounds=[1e-15:inf]>),\n",
       "            ('f_c', <Parameter 'f_c', value=0.3 (fixed), bounds=[-1:1]>),\n",
       "            ('f_s', <Parameter 'f_s', value=-0.2 (fixed), bounds=[-1:1]>),\n",
       "            ('sini', <Parameter 'sini', value=1 (fixed), bounds=[0:1]>)])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m = RVModel()\n",
    "pars = m.make_params(T_0=T_0, K=K, f_c=f_c, f_s=f_s)\n",
    "pars['P'].set(value=P, vary=False)\n",
    "pars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0.5, 2.450000000000002)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 16960 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "t = np.arange(T_0,T_0 + P, 0.05)\n",
    "yerr = 0.05\n",
    "rv_true = m.eval(params=pars, t=t)\n",
    "rv = rv_true + normal(0.0, yerr, size=len(t))\n",
    "fig,ax=subplots()\n",
    "ax.errorbar(t, rv, yerr=yerr,fmt='bo',ms=2)\n",
    "ax.plot(t, rv_true,'c')\n",
    "ax.set_xlabel('Time')\n",
    "ax.set_ylabel('RV [km/s]')\n",
    "ax.set_xlim(min(t),max(t))\n",
    "#ax.set_ylim(0,1.5*pars['_F'].value)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[Fit Statistics]]\n",
      "    # fitting method   = leastsq\n",
      "    # function evals   = 10\n",
      "    # data points      = 40\n",
      "    # variables        = 2\n",
      "    chi-square         = 39.5152021\n",
      "    reduced chi-square = 1.03987374\n",
      "    Akaike info crit   = 3.51224026\n",
      "    Bayesian info crit = 6.88999917\n",
      "[[Variables]]\n",
      "    T_0:   0.49584288 +/- 0.00237177 (0.48%) (init = 0.5)\n",
      "    P:     2 (fixed)\n",
      "    K:     1.50311980 +/- 0.01148923 (0.76%) (init = 1.5)\n",
      "    f_c:   0.3 (fixed)\n",
      "    f_s:  -0.2 (fixed)\n",
      "    sini:  1 (fixed)\n"
     ]
    }
   ],
   "source": [
    "result = m.fit(rv, pars, t=t, weights=1/yerr)\n",
    "report_fit(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(<Figure size 432x432 with 2 Axes>, GridSpec(2, 1, height_ratios=[1, 4]))"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 23984 characters]\n",
      "text/plain": [
       "<Figure size 432x432 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "result.plot(xlabel='Time',ylabel=r'V$_r$ [km/s]',data_kws={'ms':1},ax_fit_kws={'xlim':[min(t),max(t)]})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pyvenv",
   "language": "python",
   "name": "pyvenv"
  },
  "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.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
