def acf(x, unbiased=False, nlags=40, qstat=False, fft=False, alpha=None, missing='none'):
    "\n    Autocorrelation function for 1d arrays.\n\n    Parameters\n    ----------\n    x : array\n       Time series data\n    unbiased : bool\n       If True, then denominators for autocovariance are n-k, otherwise n\n    nlags: int, optional\n        Number of lags to return autocorrelation for.\n    qstat : bool, optional\n        If True, returns the Ljung-Box q statistic for each autocorrelation\n        coefficient.  See q_stat for more information.\n    fft : bool, optional\n        If True, computes the ACF via FFT.\n    alpha : scalar, optional\n        If a number is given, the confidence intervals for the given level are\n        returned. For instance if alpha=.05, 95 % confidence intervals are\n        returned where the standard deviation is computed according to\n        Bartlett's formula.\n    missing : str, optional\n        A string in ['none', 'raise', 'conservative', 'drop'] specifying how the NaNs\n        are to be treated.\n\n    Returns\n    -------\n    acf : array\n        autocorrelation function\n    confint : array, optional\n        Confidence intervals for the ACF. Returned if confint is not None.\n    qstat : array, optional\n        The Ljung-Box Q-Statistic.  Returned if q_stat is True.\n    pvalues : array, optional\n        The p-values associated with the Q-statistics.  Returned if q_stat is\n        True.\n\n    Notes\n    -----\n    The acf at lag 0 (ie., 1) is returned.\n\n    This is based np.correlate which does full convolution. For very long time\n    series it is recommended to use fft convolution instead.\n\n    If unbiased is true, the denominator for the autocovariance is adjusted\n    but the autocorrelation is not an unbiased estimtor.\n\n    References\n    ----------\n    .. [*] Parzen, E., 1963. On spectral analysis with missing observations\n       and amplitude modulation. Sankhya: The Indian Journal of\n       Statistics, Series A, pp.383-392.\n\n    "
    nobs = len(x)
    avf = acovf(x, unbiased=unbiased, demean=True, fft=fft, missing=missing)
    acf = (avf[:(nlags + 1)] / avf[0])
    if (not (qstat or alpha)):
        return acf
    if (alpha is not None):
        varacf = (np.ones((nlags + 1)) / nobs)
        varacf[0] = 0
        varacf[1] = (1.0 / nobs)
        varacf[2:] *= (1 + (2 * np.cumsum((acf[1:(- 1)] ** 2))))
        interval = (stats.norm.ppf((1 - (alpha / 2.0))) * np.sqrt(varacf))
        confint = np.array(lzip((acf - interval), (acf + interval)))
        if (not qstat):
            return (acf, confint)
    if qstat:
        (qstat, pvalue) = q_stat(acf[1:], nobs=nobs)
        if (alpha is not None):
            return (acf, confint, qstat, pvalue)
        else:
            return (acf, qstat, pvalue)
Autocorrelation function for 1d arrays.

Parameters
----------
x : array
   Time series data
unbiased : bool
   If True, then denominators for autocovariance are n-k, otherwise n
nlags: int, optional
    Number of lags to return autocorrelation for.
qstat : bool, optional
    If True, returns the Ljung-Box q statistic for each autocorrelation
    coefficient.  See q_stat for more information.
fft : bool, optional
    If True, computes the ACF via FFT.
alpha : scalar, optional
    If a number is given, the confidence intervals for the given level are
    returned. For instance if alpha=.05, 95 % confidence intervals are
    returned where the standard deviation is computed according to
    Bartlett's formula.
missing : str, optional
    A string in ['none', 'raise', 'conservative', 'drop'] specifying how the NaNs
    are to be treated.

Returns
-------
acf : array
    autocorrelation function
confint : array, optional
    Confidence intervals for the ACF. Returned if confint is not None.
qstat : array, optional
    The Ljung-Box Q-Statistic.  Returned if q_stat is True.
pvalues : array, optional
    The p-values associated with the Q-statistics.  Returned if q_stat is
    True.

Notes
-----
The acf at lag 0 (ie., 1) is returned.

This is based np.correlate which does full convolution. For very long time
series it is recommended to use fft convolution instead.

If unbiased is true, the denominator for the autocovariance is adjusted
but the autocorrelation is not an unbiased estimtor.

References
----------
.. [*] Parzen, E., 1963. On spectral analysis with missing observations
   and amplitude modulation. Sankhya: The Indian Journal of
   Statistics, Series A, pp.383-392.
def acf(x, unbiased=False, nlags=40, qstat=False, fft=False, alpha=None, missing='none'):
    "\n    Autocorrelation function for 1d arrays.\n\n    Parameters\n    ----------\n    x : array\n       Time series data\n    unbiased : bool\n       If True, then denominators for autocovariance are n-k, otherwise n\n    nlags: int, optional\n        Number of lags to return autocorrelation for.\n    qstat : bool, optional\n        If True, returns the Ljung-Box q statistic for each autocorrelation\n        coefficient.  See q_stat for more information.\n    fft : bool, optional\n        If True, computes the ACF via FFT.\n    alpha : scalar, optional\n        If a number is given, the confidence intervals for the given level are\n        returned. For instance if alpha=.05, 95 % confidence intervals are\n        returned where the standard deviation is computed according to\n        Bartlett's formula.\n    missing : str, optional\n        A string in ['none', 'raise', 'conservative', 'drop'] specifying how the NaNs\n        are to be treated.\n\n    Returns\n    -------\n    acf : array\n        autocorrelation function\n    confint : array, optional\n        Confidence intervals for the ACF. Returned if confint is not None.\n    qstat : array, optional\n        The Ljung-Box Q-Statistic.  Returned if q_stat is True.\n    pvalues : array, optional\n        The p-values associated with the Q-statistics.  Returned if q_stat is\n        True.\n\n    Notes\n    -----\n    The acf at lag 0 (ie., 1) is returned.\n\n    This is based np.correlate which does full convolution. For very long time\n    series it is recommended to use fft convolution instead.\n\n    If unbiased is true, the denominator for the autocovariance is adjusted\n    but the autocorrelation is not an unbiased estimtor.\n\n    References\n    ----------\n    .. [*] Parzen, E., 1963. On spectral analysis with missing observations\n       and amplitude modulation. Sankhya: The Indian Journal of\n       Statistics, Series A, pp.383-392.\n\n    "
    nobs = len(x)
    avf = acovf(x, unbiased=unbiased, demean=True, fft=fft, missing=missing)
    acf = (avf[:(nlags + 1)] / avf[0])
    if (not (qstat or alpha)):
        return acf
    if (alpha is not None):
        varacf = (np.ones((nlags + 1)) / nobs)
        varacf[0] = 0
        varacf[1] = (1.0 / nobs)
        varacf[2:] *= (1 + (2 * np.cumsum((acf[1:(- 1)] ** 2))))
        interval = (stats.norm.ppf((1 - (alpha / 2.0))) * np.sqrt(varacf))
        confint = np.array(lzip((acf - interval), (acf + interval)))
        if (not qstat):
            return (acf, confint)
    if qstat:
        (qstat, pvalue) = q_stat(acf[1:], nobs=nobs)
        if (alpha is not None):
            return (acf, confint, qstat, pvalue)
        else:
            return (acf, qstat, pvalue)