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

Vineet Jain (gmail) vinjvinj@gmail....
Thu Jun 19 17:48:28 CDT 2008

```I took the following code and applied it to aapl and qqqq time series (see
attached file):

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])
qqqq_array = np.array([row[1] for row in stock_and_market_values])

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

The result is:

(array([  0.13851625,  29.57888955]), array([ 144.23291488]), 2, array([
639.591
08529,    0.94451427]))

And the beta comes out to be 0.138 which is a low. It should be closer to 2.
Any idea on what I'm doing wrong.

Vineet

-----Original Message-----
From: Vineet Jain (gmail) [mailto:vinjvinj@gmail.com]
Sent: Wednesday, June 04, 2008 9:24 PM
To: 'Discussion of Numerical Python'
Subject: RE: [Numpy-discussion] Is there a function to calculate ecnomic
beta coefficient in numpy given two time series data.

Thanks Keith!

-----Original Message-----
From: numpy-discussion-bounces@scipy.org
[mailto:numpy-discussion-bounces@scipy.org] On Behalf Of Keith Goodman
Sent: Wednesday, June 04, 2008 9:04 PM
To: Discussion of Numerical Python
Subject: Re: [Numpy-discussion] Is there a function to calculate ecnomic
beta coefficient in numpy given two time series data.

On Wed, Jun 4, 2008 at 5:39 PM, Vineet Jain (gmail) <vinjvinj@gmail.com>
wrote:
> Timeseries1 = daily or weekly close of stock a
>
> Timeseries2 = daily or weekly close of market index (spx, qqqq, etc)
>
>
>
> Beta of stock a is what I would like to compute as explained in this
article
> on Wikipedia:
>
>
>
> http://en.wikipedia.org/wiki/Beta_coefficient
>
>
>
> I'm trying to compute the beta of entire stock market (about 15,000
> instruments) one stock at a time and would like to use the spiders and
qqqq
> to represent the overall market.

Unless you run out of memory (or if you want to handle missing returns
which may occur on different dates in each series) there is no need to
do it one stock at a time:

>> import numpy.matlib as mp
>> mrkt = mp.randn(250,1)  # <----- 250 days of returns
>> stocks = mp.randn(250, 4)  # <---- 4 stocks
>> beta, resids, rank, s = mp.linalg.lstsq(mrkt, stocks)
>> beta
matrix([[-0.01701467,  0.11242168,  0.00207398,  0.03920687]])

And you can use mp.log to convert the price ratios to log returns.

It might also be useful to shuffle (mp.random.shuffle) the market
returns and repeat the beta calculation many times to estimate the
noise level of your beta estimates.
_______________________________________________
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion
-------------- next part --------------
A non-text attachment was scrubbed...
Name: beta.py
Type: application/octet-stream
Size: 9365 bytes
Desc: not available
Url : http://projects.scipy.org/pipermail/numpy-discussion/attachments/20080619/7a8ffed1/attachment.obj
```