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

Keith Goodman kwgoodman@gmail....
Wed Jun 4 20:04:20 CDT 2008

```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.
```