Bhushan
Bhushan

Reputation: 87

Nested for loop with multiple operations using List Comprehension

Just understanding List Comprehensions, as in a recent interview the tech guy asked me this question and being a self learner I answered lambda which is NOT list comprehension.

let say we have a time series data "Shiller", http://us.spindices.com/indices/real-estate/sp-corelogic-case-shiller-us-national-home-price-nsa-index

I calculated aic/bic using following loop:

shiller = [please use some random data or use the link above]

import matplotlib.pyplot as plt
import statsmodels.api as sm
import pandas as pd

def aicbic(shiller):
  arimaijk = []
  aicijk = []
  bicijk = []
  index = []
  for i in range(1,3):
      for j in range(1,2):
          for k in range(0,5):
              arimaijk.append(sm.tsa.ARIMA(shiller,(i,j,k)).fit())
              index.append([i,j,k])
              aicijk.append(arimaijk[k].aic)            
              bicijk.append(arimaijk[k].bic)
  return aicijk, bicijk

aicbic(shiller)
Out[9]: 
([-235.77314152121426,-233.9375761653174,-233.3841011331017,-241.65994870973782,-240.2975620564456,-235.77314152121426,-233.9375761653174,-233.3841011331017,-241.65994870973782,-240.2975620564456],
 [-227.98778197081049,-223.55709676477906,-220.40850188242874,-226.08922960893028,-222.13172310550345,-227.98778197081049,-223.55709676477906,-220.40850188242874,-226.08922960893028,-222.13172310550345])

Now, I want this result using List Comprehension, so I wrote following lines, which are returning error:

def aicbic(data):    
  arimaijk = []
  aicijk = []
  bicijk = []
  index = []
  [(sm.tsa.ARIMA(data,(i,j,k)).fit(),index.append([i,j,k]),\
  aicijk.append(arimaijk[k].aic),bicijk.append(arimaijk[k].bic)) \
  for i in range(1,3) for j in range(1,2) for k in range(0,5)]

IndexError: list index out of range

Upvotes: 2

Views: 827

Answers (3)

Bhushan
Bhushan

Reputation: 87

Thanks to Hossein, I guess now I know how to use List Comprehensions!

def aicbic(data):
  arimaijk = []
  aicijk = []
  bicijk = []
  index = []
  [(arimaijk.append(sm.tsa.ARIMA(data,(i,j,k)).fit()),index.append([i,j,k]),\
  aicijk.append(arimaijk[k].aic),bicijk.append(arimaijk[k].bic)) \
  for i in range(1,3) for j in range(1,2) for k in range(0,5)]
  return aicijk, bicijk


result = aicbic(shiller)

Upvotes: 0

Stephen Rauch
Stephen Rauch

Reputation: 49794

Your function recast using itertools and list comprehensions:

Code:

import itertools as it

def aicbic(shiller):
    loop = list(it.product(range(1, 3), range(1, 2), range(0, 5)))
    arimaijk = [sm.tsa.ARIMA(shiller, (i, j, k)).fit() for i, j, k in loop]
    aicijk = [arimaijk[k].aic for i, j, k in loop]
    bicijk = [arimaijk[k].bic for i, j, k in loop]
    return aicijk, bicijk

Test Code:

result = aicbic(shiller)

import numpy as np
assert np.all(np.isclose(result, (
    [-235.77314152121426, -233.9375761653174, -233.3841011331017,
     -241.65994870973782, -240.2975620564456, -235.77314152121426,
     -233.9375761653174, -233.3841011331017, -241.65994870973782,
     -240.2975620564456],
    [-227.98778197081049, -223.55709676477906, -220.40850188242874,
     -226.08922960893028, -222.13172310550345, -227.98778197081049,
     -223.55709676477906, -220.40850188242874, -226.08922960893028,
     -222.13172310550345]
)))

Note:

You likely have a bug of some sort, as aicijk and bicijk depend only on k.

Upvotes: 1

Hossein
Hossein

Reputation: 2111

The error does not relate to list comprehenstion itself:

[(sm.tsa.ARIMA(data,(i,j,k)).fit(),index.append([i,j,k]),\
  aicijk.append(arimaijk[k].aic),bicijk.append(arimaijk[k].bic)) \
  for i in range(1,3) for j in range(1,2) for k in range(0,5)]

The error IndexError: list index out of range is raised because you want to access arimaijk[k] while arimaijk is an empty list (the first line of aicbic(data) function is arimaijk=[]).

Upvotes: 2

Related Questions