示例#1
0
def test_find_first():
    X = np.array([[0, 0, 1, 1, 1], [0, 0, 1, 0, 0], [0, 1, 0, 0, 0],
                  [0, 0, 0, 0, 0], [1, 1, 1, 1, 1]],
                 dtype=np.uint8).T

    reference = [2, 2, 1, -1, 0]
    for j in range(X.shape[1]):
        np.testing.assert_array_equal(find_first(X[:, j]), reference[j])
示例#2
0
def test_find_first():
    X = np.array([
         [0, 0, 1, 1, 1],
         [0, 0, 1, 0, 0],
         [0, 1, 0, 0, 0],
         [0, 0, 0, 0, 0],
         [1, 1, 1, 1, 1]], dtype=np.uint8).T

    reference = [2, 2, 1, -1, 0]
    for j in range(X.shape[1]):
        np.testing.assert_array_equal(
            find_first(X[:,j]), reference[j])
示例#3
0
def integrated_autocorr6(x, c=6):
    r"""Estimate the integrated autocorrelation time, :math:`\tau_{int}` of a
    time series.

    This method performancs a summation of empirical autocorrelation function,
    using Sokal's "automatic windowing" procedure. The window length, ``M`` is
    chosen self-consistently to be the smallest value such that ``M`` is at
    least ``c`` times the estimated autocorrelation time, where ``c`` should
    be a constant in the range of 4, 6, or 10. See Appendix C of Sokal 1988.

    Parameters
    ----------
    x : ndarray, shape=(n_samples, n_dims)
        The time series, with time along axis 0.
    max_length : int
        The data ``x`` is aggregated if necessary by taking batch means so that
        the length of the series is less than ``max.length``.

    References
    ----------
    .. [1] Madras, Neal, and Alan D. Sokal. "The pivot algorithm: a highly
       efficient Monte Carlo method for the self-avoiding walk." J.
       Stat. Phys. 50.1-2 (1988): 109-186.

    Returns
    -------
    tau_int : ndarray, shape=(n_dims,)
        The estimated integrated autocorrelation time of each dimension in
        ``x``, considered independently.
    """
    if x.ndim == 1:
        x = x.reshape(-1, 1)

    tau = np.zeros(x.shape[1])
    for j in range(x.shape[1]):
        f = acf(x[:, j], nlags=len(x), unbiased=False, fft=True)
        # vector of the taus, all with different choices of the window
        # length
        taus = 1 + 2 * np.cumsum(f)[1:]
        ms = np.arange(len(f) - 1)
        ind = find_first((ms > c * taus).astype(np.uint8))
        tau[j] = taus[ind]
    return tau
示例#4
0
def integrated_autocorr6(x, c=6):
    r"""Estimate the integrated autocorrelation time, :math:`\tau_{int}` of a
    time series.

    This method performancs a summation of empirical autocorrelation function,
    using Sokal's "automatic windowing" procedure. The window length, ``M`` is
    chosen self-consistently to be the smallest value such that ``M`` is at
    least ``c`` times the estimated autocorrelation time, where ``c`` should
    be a constant in the range of 4, 6, or 10. See Appendix C of Sokal 1988.

    Parameters
    ----------
    x : ndarray, shape=(n_samples, n_dims)
        The time series, with time along axis 0.
    max_length : int
        The data ``x`` is aggregated if necessary by taking batch means so that
        the length of the series is less than ``max.length``.

    References
    ----------
    .. [1] Madras, Neal, and Alan D. Sokal. "The pivot algorithm: a highly
       efficient Monte Carlo method for the self-avoiding walk." J.
       Stat. Phys. 50.1-2 (1988): 109-186.

    Returns
    -------
    tau_int : ndarray, shape=(n_dims,)
        The estimated integrated autocorrelation time of each dimension in
        ``x``, considered independently.
    """
    if x.ndim == 1:
        x = x.reshape(-1, 1)

    tau = np.zeros(x.shape[1])
    for j in range(x.shape[1]):
        f = acf(x[:,j], nlags=len(x), unbiased=False, fft=True)
        # vector of the taus, all with different choices of the window
        # length
        taus = 1 + 2*np.cumsum(f)[1:]
        ms = np.arange(len(f)-1)
        ind = find_first((ms > c*taus).astype(np.uint8))
        tau[j] = taus[ind]
    return tau
示例#5
0
def integrated_autocorr3(x):
    r"""Estimate the integrated autocorrelation time, :math:`\tau_{int}` of a
    time series.

    This method performancs a summation of empirical autocorrelation function,
    setting the window based on the initial sequence estimator (Geyer 1992),
    which stops when the sum of two consecutive elements in the empirica
    autocorrelation function become negative.

    Parameters
    ----------
    x : ndarray, shape=(n_samples, n_dims)
        The time series, with time along axis 0.

    References
    ----------
    .. [1] Geyer, Charles J. "Practical markov chain monte carlo." Statistical
       Science (1992): 473-483.

    Returns
    -------
    tau_int : ndarray, shape=(n_dims,)
        The estimated integrated autocorrelation time of each dimension in
        ``x``, considered independently.
    """
    if x.ndim == 1:
        x = x.reshape(-1, 1)

    tau = np.zeros(x.shape[1])
    for j in range(x.shape[1]):
        f = acf(x[:,j], nlags=2*(len(x)//2)-1, unbiased=False, fft=True)
        # reshape and thens sum over the second axis to get the sum of the pairs
        # [1,2,3,4,5,6,7,8] -> [[1,2], [3,4], [5,6], [7,8]] -> [3, 7, 11, 15]
        gamma = f.reshape(-1, 2).sum(axis=1)
        ind = find_first((gamma<0).astype(np.uint8))
        tau[j] = -1 + 2*np.sum(gamma[:ind])
    return tau
示例#6
0
def integrated_autocorr3(x):
    r"""Estimate the integrated autocorrelation time, :math:`\tau_{int}` of a
    time series.

    This method performancs a summation of empirical autocorrelation function,
    setting the window based on the initial sequence estimator (Geyer 1992),
    which stops when the sum of two consecutive elements in the empirica
    autocorrelation function become negative.

    Parameters
    ----------
    x : ndarray, shape=(n_samples, n_dims)
        The time series, with time along axis 0.

    References
    ----------
    .. [1] Geyer, Charles J. "Practical markov chain monte carlo." Statistical
       Science (1992): 473-483.

    Returns
    -------
    tau_int : ndarray, shape=(n_dims,)
        The estimated integrated autocorrelation time of each dimension in
        ``x``, considered independently.
    """
    if x.ndim == 1:
        x = x.reshape(-1, 1)

    tau = np.zeros(x.shape[1])
    for j in range(x.shape[1]):
        f = acf(x[:, j], nlags=2 * (len(x) // 2) - 1, unbiased=False, fft=True)
        # reshape and thens sum over the second axis to get the sum of the pairs
        # [1,2,3,4,5,6,7,8] -> [[1,2], [3,4], [5,6], [7,8]] -> [3, 7, 11, 15]
        gamma = f.reshape(-1, 2).sum(axis=1)
        ind = find_first((gamma < 0).astype(np.uint8))
        tau[j] = -1 + 2 * np.sum(gamma[:ind])
    return tau