def q_stat(x, nobs, type='ljungbox'):
 x = np.asarray(x)
 if (type == 'ljungbox'):
  ret = ((nobs * (nobs + 2)) * np.cumsum(((1.0 / (nobs - np.arange(1, (len(x) + 1)))) * (x ** 2))))
 chi2 = stats.chi2.sf(ret, np.arange(1, (len(x) + 1)))
 return (ret, chi2)

def q_stat ( x , nobs , type = 'ljungbox' ) : x = np . asarray ( x ) if ( type == 'ljungbox' ) : ret = ( ( nobs * ( nobs + 2 ) ) * np . cumsum ( ( ( 1.0 / ( nobs - np . arange ( 1 , ( len ( x ) + 1 ) ) ) ) * ( x ** 2 ) ) ) ) chi2 = stats . chi2 . sf ( ret , np . arange ( 1 , ( len ( x ) + 1 ) ) ) return ( ret , chi2 )
returns ljung-box q statistic x : array-like array of autocorrelation coefficients .