'How can make my for loop multiply specific values in a Series?

I have a dataset and I'm regressing one series on the other one inside a for loop. What i'm getting are the coefficients of the regression for the index [0:6] then [0:7] and so on...

Then I want to compute SPREAD = first_series - coeff * sec_series, and what I want to get is the value of the first series at index 7 (for example) minus the coefficient calculated from 0 to 7, multiplied by the value of second series at index 7.

So the spread would be: The first six values calculated using coeff[0:6], the seventh value using coeff [0:7], the eighth [0:8]...

What I did is:

import pandas as pd
import statsmodels.api as sm

A = [1,4,6,8,12,10, 9 , 7, 4, 5, 7, 9 , 11, 14 , 20];
B = [2,3,4,2,7,9, 4, 2, 6, 9, 13, 15, 14, 14, 17];
C = [2,3,5,6,7,6,5,6,7,10,12,15,22,24,21];
A = pd.Series(A); A = A.rename("A")
B = pd.Series(B); B = B.rename("B")
C = pd.Series(C); C = C.rename("C")
List = [A, B]
beta = []
pair = []
SPREADS=[]

for letter in List:
  for s_letter in List:
    if letter.name!=s_letter.name:
      for t in range(7,16):
        result = sm.OLS(letter.iloc[0:t], sm.add_constant(s_letter.iloc[0:t])).fit()
        beta.append(result.params[1]); b = pd.Series(beta); 
        pair.append(f"{letter.name}-{s_letter.name}")
        Cointegration_test = (pd.Series(beta, index = pair))
        df_test = pd.DataFrame(Cointegration_test).T
        SPREAD = letter.iloc[6:t] - result.params[1]*s_letter.iloc[6:t]; SPREADS.append(SPREAD)
        spreads_df_test  = pd.DataFrame(SPREADS).T; spreads_df_test.columns = pair
        spreads_df_test = spreads_df_test.add_suffix(" SPREAD")

spreads_df_test

The output is:

spreads_df_test

Where actually the values within the red line are what I'm trying to achieve.

Is there a way I can use the "for t in range()" to get what I want?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source