0

I am trying to create a Beta for stocks on 756 days, calculating the Covariance of the stocks Weighted on an index and divided by the Variance of the index.

When i run it for a DF with a single stock, without the groupby arguments, it runs and creates the Beta Column, but i need to do it for all the stocks on my DF and the Groupby was a solution that i read here on Stack Overflow, but i am not able to use it yet.

This is the line that causes the error

df2['Beta 756d'] = df2.groupby('CODNEG').apply(df2['Retorno_Ação'].rolling(window=756,center=False).cov(df2['Retorno_Ibov']) / df2['Retorno_Ibov'].rolling(window=756,center=False).var())

--

This error comes up when the code gets to the line above

TypeError: 'Series' objects are mutable, thus they cannot be hashed

--

Here is an example of df2

TIPREG       DATE  CODBDI  CODNEG  TPMERC        NOMRES      ESPECI PRAZOT MODREF  PREABE  PREMAX  PREMIN  PREMED  PREULT  PREOFC  PREOFV  TOTNEG    QUATOT      VOLTOT  PREEXE  INDOPC    DATVEN  FATCOT  PTOEXE        CODISI  DISMES  Retorno_Ação  Retorno_Ibov
0             1 1995-01-02       2   ACE 3      10  ACESITA       ON *INT             R$      6300    6300    6300    6300    6300    6300    6500       1    200000     1260000       0       0  99991231    1000       0  ACESACON         119           NaN           NaN
105           1 1995-01-02       2   PET 3      10  PETROBRAS     ON *                R$      6400    6400    6250    6287    6250    6250    6750       2     40000      251500       0       0  99991231    1000       0  PETRACON         132           NaN           NaN
106           1 1995-01-02       2   PET 4      10  PETROBRAS     PN *                R$     10700   11000   10400   10599   10500   10500   10650     234  31210000   330805170       0       0  99991231    1000       0  PETRACPN         133           NaN           NaN
107           1 1995-01-02       2   BRD 4      10  PETROBRAS BR  PN *                R$      4600    4600    4540    4591    4540    4333    4500      13  18200000    83566000       0       0  99991231    1000       0  BRDTACPN         102           NaN           NaN
108           1 1995-01-02       2   PTN 4      10  PETTENATI     PN *                R$      5189    5189    5189    5189    5189    4700    5280       1   5000000    25945000       0       0  99991231    1000       0  BRPTNTACNPR3      21           NaN           NaN
...         ...        ...     ...     ...     ...           ...         ...    ...    ...     ...     ...     ...     ...     ...     ...     ...     ...       ...         ...     ...     ...       ...     ...     ...           ...     ...           ...           ...
1826575       1 2020-08-19       2   HGTX3      10  CIA HERING    ON      NM          R$      1576    1598    1547    1575    1575    1574    1575   14582   4640200  7309217700       0       0  99991231       1       0  BRHGTXACNOR9     151      0.005105      0.000788
1826576       1 2020-08-19       2  HOME34      10  HOME DEPOT    DRN                 R$     77798   78612   77798   78413   78612   55000       0       2      1720   134870760       0       0  99991231       1       0  BRHOMEBDR002     133      0.005719      0.000788
1826577       1 2020-08-19       2  HONB34      10  HONEYWELL     DRN ED              R$     86347   86347   86347   86347   86347       0       0       1       100     8634700       0       0  99991231       1       0  BRHONBBDR006     127      0.085375      0.000788
1826579       1 2020-08-19       2   HYPE3      10  HYPERA        ON      NM          R$      3248    3265    3167    3199    3175    3175    3177   15332   3082700  9864018200       0       0  99991231       1       0  BRHYPEACNOR0     122     -0.034367      0.000788
1826517       1 2020-08-19       2   EMAE4      10  EMAE          PN                  R$      2869    2954    2831    2888    2952    2832    2953      10      1100     3177600       0       0  99991231       1       0  BREMAEACNPR1     114      0.040903      0.000788

I am using the last two columns (Retorno_Ação, Retorno_Ibov) to calculate the Cov and the Var to generate the Beta.

Can anyone tell me what is causing the error?

This line works fine when the DF has only one stock:

df2['Beta 756d'] = df2['Retorno_Ação'].rolling(window=756,center=False).cov(df2['Retorno_Ibov']) / df2['Retorno_Ibov'].rolling(window=756,center=False).var()

--

The error happens when i use df2['Beta 756d'] = df2.groupby('CODNEG').apply(

guialmachado
  • 506
  • 5
  • 17

0 Answers0