[Numpy-discussion] Is there a function to calculate ecnomic beta coefficient in numpy given two time series data.

Robert Kern robert.kern@gmail....
Thu Jun 19 18:32:14 CDT 2008


On Thu, Jun 19, 2008 at 18:18, Vineet Jain (gmail) <vinjvinj@gmail.com> wrote:
> Thanks.
>
> With your changes, I'm getting a beta of 0.23 which I think is still wrong.
> I would have expected a value closer to 2.

To get the beta of AAPL versus the index QQQQ, you need to swap
aapl_ret and qqqq_ret when you construct the arguments to lstsq().
That gives a beta of 1.0-1.2 depending on whether or not you omit the
outlier. Google Finance reports a beta of 2.4 for AAPL recently, but
this is probably against the S&P 500 rather than a tech index like
QQQQ. Try computing the betas of AAPL and QQQQ against the S&P 500.


import numpy as np
lstsq = np.linalg.lstsq
from numpy import float64, extract

aapl_array = np.array([row[0] for row in stock_and_market_values])
aapl_ret = np.log(aapl_array[1:] / aapl_array[:-1])
qqqq_array = np.array([row[1] for row in stock_and_market_values])
qqqq_ret = np.log(qqqq_array[1:] / qqqq_array[:-1])

# There's a big outlier in the AAPL returns. AAPL did really well one day.
#m = aapl_ret < 0.1
#aapl_ret = aapl_ret[m]
#qqqq_ret = qqqq_ret[m]

A = np.ones((len(qqqq_ret), 2), dtype=float64)
A[:,0] = qqqq_ret
result = lstsq(A, aapl_ret)
print result

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
 -- Umberto Eco


More information about the Numpy-discussion mailing list