def ar_nitime(x, order=1, center=False): """Derive a model of the noise present in the functional timeseries for the calculation of the standardized DVARS. - Borrowed from nipy.algorithms.AR_est_YW. aka "from nitime import algorithms as alg". :type x: Nibabel data :param x: The vector of one voxel's timeseries. :type order: int :param order: (default: 1) Which lag of the autocorrelation of the timeseries to use in the calculation. :type center: bool :param center: (default: False) Whether to center the timeseries (to demean it). :rtype: float :return: The modeled noise value for the current voxel's timeseries. """ from nitime.lazy import scipy_linalg as linalg import nitime.utils as utils if center: x = x.copy() x = x - x.mean() r_m = utils.autocorr(x)[:order + 1] Tm = linalg.toeplitz(r_m[:order]) y = r_m[1:] ak = linalg.solve(Tm, y) return ak[0]
def AR_est_YW(x, order, rxx=None): r"""Determine the autoregressive (AR) model of a random process x using the Yule Walker equations. The AR model takes this convention: .. math:: x(n) = a(1)x(n-1) + a(2)x(n-2) + \dots + a(p)x(n-p) + e(n) where e(n) is a zero-mean white noise process with variance sig_sq, and p is the order of the AR model. This method returns the a_i and sigma The orthogonality property of minimum mean square error estimates states that .. math:: E\{e(n)x^{*}(n-k)\} = 0 \quad 1\leq k\leq p Inserting the definition of the error signal into the equations above yields the Yule Walker system of equations: .. math:: R_{xx}(k) = \sum_{i=1}^{p}a(i)R_{xx}(k-i) \quad1\leq k\leq p Similarly, the variance of the error process is .. math:: E\{e(n)e^{*}(n)\} = E\{e(n)x^{*}(n)\} = R_{xx}(0)-\sum_{i=1}^{p}a(i)R^{*}(i) Parameters ---------- x : ndarray The sampled autoregressive random process order : int The order p of the AR system rxx : ndarray (optional) An optional, possibly unbiased estimate of the autocorrelation of x Returns ------- ak, sig_sq : The estimated AR coefficients and innovations variance """ if rxx is not None and type(rxx) == np.ndarray: r_m = rxx[:order + 1] else: r_m = utils.autocorr(x)[:order + 1] Tm = linalg.toeplitz(r_m[:order]) y = r_m[1:] ak = linalg.solve(Tm, y) sigma_v = r_m[0].real - np.dot(r_m[1:].conj(), ak).real return ak, sigma_v
def ar_nitime(x, order=1, center=False): """ Borrowed from nipy.algorithms.AR_est_YW. aka from nitime import algorithms as alg. We could speed this up by having the autocorr only compute lag1. """ from nitime.lazy import scipy_linalg as linalg import nitime.utils as utils if center: x = x.copy() x = x - x.mean() r_m = utils.autocorr(x)[:order + 1] Tm = linalg.toeplitz(r_m[:order]) y = r_m[1:] ak = linalg.solve(Tm, y) return ak[0]