'Ljung-box test for no serial correlation of standardized residuals
Null hypothesis is no serial correlation.
Parameters
method : string {\'ljungbox\',\'boxpierece\'} or None
The statistical test for serial correlation. If None, an attempt is
made to select an appropriate test.
lags : None, int or array_like
If lags is an integer then this is taken to be the largest lag
that is included, the test result is reported for all smaller lag
length.
If lags is a list or array, then all lags are included up to the
largest lag in the list, however only the tests for the lags in the
list are reported.
If lags is None, then the default maxlag is 12*(nobs/100)^{1/4}
Returns
output : array
An array with `(test_statistic, pvalue)` for each endogenous
variable and each lag. The array is then sized
`(k_endog, 2, lags)`. If the method is called as
`ljungbox = res.test_serial_correlation()`, then `ljungbox[i]`
holds the results of the Ljung-Box test (as would be returned by
`statsmodels.stats.diagnostic.acorr_ljungbox`) for the `i` th
endogenous variable.
Notes
If the first `d` loglikelihood values were burned (i.e. in the
specified model, `loglikelihood_burn=d`), then this test is calculated
ignoring the first `d` residuals.
Output is nan for any endogenous variable which has missing values.
See Also
statsmodels.stats.diagnostic.acorr_ljungbox'

def test_serial_correlation(self, method, lags=None):

    if (method is None):
        method = 'ljungbox'
    if ((method == 'ljungbox') or (method == 'boxpierce')):
        from statsmodels.stats.diagnostic import acorr_ljungbox
        d = self.loglikelihood_burn
        output = []
        if (lags is None):
            lags = min(40, (self.nobs_effective - 1))
        for i in range(self.model.k_endog):
            results = acorr_ljungbox(self.filter_results.standardized_forecasts_error[i][d:], lags=lags, boxpierce=(method == 'boxpierce'))
            if (method == 'ljungbox'):
                output.append(results[0:2])
            else:
                output.append(results[2:])
        output = np.c_[output]
    else:
        raise NotImplementedError('Invalid    serial    correlation    test    method.')
    return output
