Example #1
0
def partition_functions(psis, z, F, neighbors=None, iat_method=DEFAULT_IAT):
    """Estimates the asymptotic variance of the partition function (normalization constant) for each window.  To get an estimate of the autocovariance of the free energy for each window, multiply the autocovariance of window :math:`i` by :math:` (k_B T / z_i)^2`.

    Parameters
    ----------
    psis : 3D data structure
        The values of the bias functions evaluated each window and timepoint.  See `datastructures <../datastructures.html#data-from-sampling>`__ for more information.
    z : 1D array
        Array containing the normalization constants
    F : 2D array
        Overlap matrix for the first EMUS iteration.
    neighbors : 2D array, optional
        List showing which windows neighbor which.  See neighbors_harmonic in usutils for explanation.
    iat_method : string, optional
        Method used to estimate autocorrelation time.  See the documentation above.

    Returns
    -------
    autocovars : ndarray
        Array of length L (no. windows) where the i'th value corresponds to the autocovariance estimate for :math:`z_i` 
    z_var_contribs : ndarray 
        Two dimensional array, where element i,j corresponds to window j's contribution to the autocovariance of window i.
    z_var_iats : ndarray 
        Two dimensional array, where element i,j corresponds to the autocorrelation time associated with window j's contribution to the autocovariance of window i.
    """

    iat_routine = ac._get_iat_method(iat_method)
    L = len(z)
    z_var_contribs = np.zeros((L, L))
    z_var_iats = np.zeros((L, L))
    if neighbors is None:  # If no neighborlist, assume all windows neighbor
        neighbors = np.outer(np.ones(L), range(L)).astype(int)

    groupInv = lm.groupInverse(np.eye(L) - F)
    # Calculate the partial derivatives of z .
    # (i,j,k)'th element is partial of z_k w.r.t. F_ij
    dzdFij = np.outer(z, groupInv).reshape((L, L, L))

    # Iterate over windows, getting err contribution from sampling in each
    for i, psi_i in enumerate(psis):
        # Data cleaning
        psi_i_arr = np.array(psi_i)
        Lneighb = len(neighbors[i])  # Number of neighbors

        # Normalize psi_j(x_i^t) for all j
        psi_sum = np.sum(psi_i_arr, axis=1)
        normedpsis = np.zeros(psi_i_arr.shape)  # psi_j / sum_k psi_k
        for j in xrange(Lneighb):
            normedpsis[:, j] = psi_i_arr[:, j] / psi_sum

        # Calculate contribution to as. err. for each z_k
        for k in xrange(L):
            dzkdFij = dzdFij[:, :, k]
            err_t_series = np.dot(normedpsis, dzkdFij[i][neighbors[i]])
            iat, mn, sigma = iat_routine(err_t_series)
            z_var_contribs[k, i] = sigma * sigma
            z_var_iats[k, i] = iat
    autocovars = np.sum(z_var_contribs, axis=1)
    return autocovars, z_var_contribs, z_var_iats
Example #2
0
def emus_iter(psis, Avals=None, neighbors=None, return_iats = False,iat_method=DEFAULT_IAT):
    """Performs one step of the the EMUS iteration.
    
    Parameters
    ----------
    psis : 3D data structure
        The values of the bias functions evaluated each window and timepoint.  See `datastructures <../datastructures.html#data-from-sampling>`__ for more information.
    Avals : 2D array-like, optional
        Weights in front of :math:`\psi` in the overlap matrix.
    neighbors : 2D array-like, optional
        List showing which windows neighbor which.  See neighbors_harmonic in usutils. 
    return_iats : bool, optional
        Whether or not to calculate integrated autocorrelation times of :math:`\psi_ii^*` for each window.
    iat_method : string, optional
        Routine to use for calculating said iats.  Accepts 'ipce', 'acor', and 'icce'.
    
    Returns
    -------
    z : 1D array
        Normalization constants for each window
    F : 2D array
        The overlap matrix constructed for the eigenproblem.
    iats : 1D array
        If return_iats chosen, returns the iats that have been estimated.

    """
    
    # Initialize variables
    L = len(psis) # Number of windows
    F = np.zeros((L,L)) # Initialize F Matrix
    # Take care of defaults..
    if return_iats:
        iats = np.ones(L)
        iatroutine = ac._get_iat_method(iat_method)
    if Avals is None:
        Avals = np.ones((L,L))
    if neighbors is None:
        neighbors = np.outer(np.ones(L),range(L)).astype(int)
        
    for i in xrange(L):
        nbrs_i = neighbors[i]
        A_nbs = Avals[i][nbrs_i]
        nbr_index = list(nbrs_i).index(i)
        Fi_out = calculate_Fi(psis[i],nbr_index,A_nbs,return_iats)
        if return_iats:
            Fi, trajs = Fi_out
            iats[i] = iatroutine(trajs[nbr_index])[0]
        else:
            Fi = Fi_out
        # Unpack the Neighbor list
        F[i] = unpackNbrs(Fi,nbrs_i,L)

    z = lm.stationary_distrib(F)
    if return_iats:
        return z, F, iats
    else:
        return z, F
Example #3
0
def _calculate_acovar(psis, dBdF, gdata=None, dBdg=None, neighbors=None, iat_method=DEFAULT_IAT):
    """
    Estimates the autocovariance and autocorrelation times for each window's contribution to the autocovariance of some observable B.

    Parameters
    ----------
    psis : 3D data structure
        The values of the bias functions evaluated each window and timepoint.  See `datastructures <../datastructures.html#data-from-sampling>`__ for more information.
    dBdF : array-like
        Two dimensional array, where element :math:`i,j` is the derivative of the estimate of B with respect to :math:`F_{ij}`
    gdata : array-like, optional
        Three dimensional data structure containing data from various observables.  The first index n
    dBdg : array-like, optional
        Two dimensional array, where element :math:`n,j` is the derivative of the estimate of B with respect to :math:`gn_j^*`.


    Returns
    -------

    """
    L = len(psis)
    if gdata is not None:
        if len(gdata) != len(dBdg):
            raise ValueError(
                "Function data provided is mismatched with derivatives: respective sizes are ",
                np.shape(gdata),
                " and ",
                np.shape(dBdg),
            )
    if neighbors is None:
        neighbors = np.outer(np.ones(L), range(L)).astype(int)
    dBdF = np.array(dBdF)

    iat_routine = ac._get_iat_method(iat_method)

    sigmas = np.zeros(L)
    taus = np.zeros(L)
    for i, psi_i in enumerate(psis):
        nbrs_i = neighbors[i]
        denom_i = 1.0 / np.sum(psi_i, axis=1)
        errtraj = psi_i * np.transpose([denom_i])
        Fi = np.average(errtraj, axis=0)
        errtraj = np.dot((psi_i * np.transpose([denom_i]) - Fi), dBdF[i, nbrs_i])
        if gdata is not None:
            for n, g_n in enumerate(gdata):
                g_ni = g_n[i]
                dBdg_n = dBdg[n]
                g_ni_wtd = g_ni * denom_i
                errtraj += dBdg_n[i] * (g_ni_wtd - np.average(g_ni_wtd))
        tau, mean, sigma = iat_routine(errtraj)
        taus[i] = tau
        sigmas[i] = sigma
    return taus, sigmas ** 2