title: Python: Super Dictionary of Simple OLS
body: <p>I am trying to build a super dictionary which holds within a number of lower level libraries</p>
  
  <p><strong>Concept</strong></p>
  
  <p>I have interest rates for my retail bank for the last 12 years and I am trying to model the interest rates by using a portfolio of different bonds.</p>
  
  <p><strong>Regression formula</strong></p>
  
  <pre><code>Y_i - Y_i-1 = A + B(X_i - X_i-1) + E
  </code></pre>
  
  <p>In words, Y_Lag = alpha + beta(X_Lag) + Error term</p>
  
  <p><strong>Data</strong></p>
  
  <pre><code>Note: Y = Historic Rate
  
  df = pd.DataFrame(np.random.randint(low=0, high=10, size=(100,17)), 
                columns=['Historic Rate', 'Overnight', '1M', '3M', '6M','1Y','2Y','3Y','4Y','5Y','6Y','7Y','8Y','9Y','10Y','12Y','15Y'])
  </code></pre>
  
  <p><strong>Code thus far</strong></p>
  
  <pre><code>#Import packages required for the analysis
  
  import pandas as pd
  import numpy as np
  import statsmodels.api as sm
  
  def Simulation(TotalSim,j):
      #super dictionary to hold all iterations of the loop
      Super_fit_d = {}
      for i in range(1,TotalSim):
          #Create a introductory loop to run the first set of regressions
          #Each loop produces a univariate regression
          #Each loop has a fixed lag of i
  
          fit_d = {}  # This will hold all of the fit results and summaries
          for col in [x for x in df.columns if x != 'Historic Rate']:
              Y = df['Historic Rate'] - df['Historic Rate'].shift(1)
              # Need to remove the NaN for fit
              Y = Y[Y.notnull()]
  
              X = df[col] - df[col].shift(i)
              X = X[X.notnull()]
              #Y now has more observations than X due to lag, drop rows to match
              Y = Y.drop(Y.index[0:i-1])
  
              if j = 1:
                  X = sm.add_constant(X)  # Add a constant to the fit
  
              fit_d[col] = sm.OLS(Y,X).fit()
          #append the dictionary for each lag onto the super dictionary
          Super_fit_d[lag_i] = fit_d
  
  #Check the output for one column
  fit_d['Overnight'].summary()
  
  #Check the output for one column in one segment of the super dictionary
  Super_fit_d['lag_5'].fit_d['Overnight'].summary()
  
  Simulation(11,1)
  </code></pre>
  
  <p><strong>Question</strong></p>
  
  <p>I seem to be overwriting my dictionary with every loop and I'm not evaluating the i properly to index the iteration as lag_1, lag_2, lag_3 etc. How do I fix this?</p>
  
  <p>Thanks in advance</p>