Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regression on stock data using pandas and matplotlib

I'd like to plot what Excel calls an "Exponential Trend/Regression" on a stock chart. When I run the code below in the IPython notebook it simply says "The kernel has died, would you like to restart it?". Any ideas on how to fix it? Also, this is just attempting to do a linear regression and I'm not quite sure how to do a regression on exponential data.

import datetime
import matplotlib.pyplot as plt
import statsmodels.api as sm
from pandas.io.data import DataReader

sp500 = DataReader("AGG", "yahoo", start=datetime.datetime(2000, 1, 1)) # returns a DataFrame
sp500["regression"] = sm.OLS(sp500["Adj Close"], sp500.index).fit().fittedvalues()
top = plt.subplot2grid((3,1), (0, 0), rowspan=2)
top.plot(sp500.index, sp500["Adj Close"], 'b-', sp500.index, sp500["regression"], 'r-')
bottom = plt.subplot2grid((3,1), (2,0))
bottom.bar(sp500.index, sp500.Volume)
plt.gcf().set_size_inches(18,8)
like image 666
Ben McCann Avatar asked Dec 20 '25 05:12

Ben McCann


1 Answers

I took another look at this and realized that my previous answer fit poorly since it didn't include an intercept. I've updated my answer.

The segfault comes from trying to us the Datetime index as the exogenous variable. Instead try:

import datetime
import matplotlib.pyplot as plt
import statsmodels.api as sm
import pandas
from pandas.io.data import DataReader

sp500 = DataReader("AGG", "yahoo", start=datetime.datetime(2000, 1, 1)) # returns a DataFrame
sp500["regression"] = sm.OLS(sp500["Adj Close"],
    sm.add_constant(range(len(sp500.index)),
    prepend=True)).fit().fittedvalues

Notice that you don't need to call statsmodels' fittedvalues as a function. If your data points are all equally space this model will give the same results as using the actual index.

For your second question, pandas as a built in exponentially weighted moving average that you may want to look in to: pandas.ewma here.

like image 162
TomAugspurger Avatar answered Dec 22 '25 18:12

TomAugspurger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!