Raw File
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "import pyaf.ForecastEngine as autof\n",
    "import pyaf.Bench.TS_datasets as tsds\n",
    "\n",
    "\n",
    "%matplotlib inline  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "GENERATING_RANDOM_DATASET Signal_12_D_4243_linear_52__0.01_20\n",
      "TREND 76.91383696439541 -1.0638593345711 0.9954661351814997\n",
      "GENERATING_RANDOM_DATASET Signal_12_D_4242_poly_52__0.01_20\n",
      "TREND -35.01010267649731 8.808291590368945 1.8280158855438828\n"
     ]
    }
   ],
   "source": [
    "b1 = tsds.generate_random_TS(12, 'D', 4243, \"linear\" , 52 , \"\" , 0.01)\n",
    "b2 = tsds.generate_random_TS(12 , 'D', 4242, \"poly\" , 52 , \"\" , 0.01)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\n",
    "def compute_R2(signal , estimator):\n",
    "    lMean = np.mean(signal.values)\n",
    "    SSTot = np.dot((signal.values - lMean), (signal.values - lMean)) + 1.0e-10\n",
    "    SSRes = np.dot((estimator.values - signal.values), (estimator.values - signal.values))\n",
    "    R2 = 1.0 - SSRes/SSTot\n",
    "    print(\"compute_R2_sig\" , signal.T)\n",
    "    print(\"compute_R2_est\" , estimator.T)\n",
    "    print(\"compute_R2\" , lMean, SSTot, SSRes, R2)\n",
    "    return R2\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "compute_R2_sig 0     -0.365727\n",
      "1      5.990854\n",
      "2     12.226381\n",
      "3     18.435008\n",
      "4     25.248230\n",
      "5     31.915865\n",
      "6     38.297449\n",
      "7     44.228520\n",
      "8     50.433997\n",
      "9     57.302744\n",
      "10    63.178568\n",
      "Name: Signal, dtype: float64\n",
      "compute_R2_est 0      2.137169\n",
      "1      2.656958\n",
      "2      2.691334\n",
      "3      2.403626\n",
      "4      1.655721\n",
      "5     -0.438003\n",
      "6     -1.975673\n",
      "7     -4.115653\n",
      "8     -7.601332\n",
      "9    -10.571572\n",
      "10   -15.119331\n",
      "Name: Signal, dtype: float64\n",
      "compute_R2 31.5356262017 4483.58114829 20033.3475039 -3.46815767159\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "-3.4681576715938753"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "compute_R2(b1.mPastData.Signal , b2.mPastData.Signal)"
   ]
  },
  {
   "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.5.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
back to top