def q_stat(x, nobs, type='ljungbox'):
    "\n    Return's Ljung-Box Q Statistic\n\n    x : array-like\n        Array of autocorrelation coefficients.  Can be obtained from acf.\n    nobs : int\n        Number of observations in the entire sample (ie., not just the length\n        of the autocorrelation function results.\n\n    Returns\n    -------\n    q-stat : array\n        Ljung-Box Q-statistic for autocorrelation parameters\n    p-value : array\n        P-value of the Q statistic\n\n    Notes\n    ------\n    Written to be used with acf.\n    "
    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)
Return's Ljung-Box Q Statistic

x : array-like
    Array of autocorrelation coefficients.  Can be obtained from acf.
nobs : int
    Number of observations in the entire sample (ie., not just the length
    of the autocorrelation function results.

Returns
-------
q-stat : array
    Ljung-Box Q-statistic for autocorrelation parameters
p-value : array
    P-value of the Q statistic

Notes
------
Written to be used with acf.
def q_stat(x, nobs, type='ljungbox'):
    "\n    Return's Ljung-Box Q Statistic\n\n    x : array-like\n        Array of autocorrelation coefficients.  Can be obtained from acf.\n    nobs : int\n        Number of observations in the entire sample (ie., not just the length\n        of the autocorrelation function results.\n\n    Returns\n    -------\n    q-stat : array\n        Ljung-Box Q-statistic for autocorrelation parameters\n    p-value : array\n        P-value of the Q statistic\n\n    Notes\n    ------\n    Written to be used with acf.\n    "
    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)