

def _compute_multivariate_acovf_from_coefficients(coefficients, error_variance, maxlag=None, forward_autocovariances=False):
    "\n    Compute multivariate autocovariances from vector autoregression coefficient\n    matrices\n\n    Parameters\n    ----------\n    coefficients : array or list\n        The coefficients matrices. If a list, should be a list of length\n        `order`, where each element is an array sized `k_endog` x `k_endog`. If\n        an array, should be the coefficient matrices horizontally concatenated\n        and sized `k_endog` x `k_endog * order`.\n    error_variance : array\n        The variance / covariance matrix of the error term. Should be sized\n        `k_endog` x `k_endog`.\n    maxlag : integer, optional\n        The maximum autocovariance to compute. Default is `order`-1. Can be\n        zero, in which case it returns the variance.\n    forward_autocovariances : boolean, optional\n        Whether or not to compute forward autocovariances\n        :math:`E(y_t y_{t+j}')`. Default is False, so that backward\n        autocovariances :math:`E(y_t y_{t-j}')` are returned.\n\n    Returns\n    -------\n    autocovariances : list\n        A list of the first `maxlag` autocovariance matrices. Each matrix is\n        shaped `k_endog` x `k_endog`.\n\n    Notes\n    -----\n    Computes\n\n    ..math::\n\n        \\Gamma(j) = E(y_t y_{t-j}')\n\n    for j = 1, ..., `maxlag`, unless `forward_autocovariances` is specified,\n    in which case it computes:\n\n    ..math::\n\n        E(y_t y_{t+j}') = \\Gamma(j)'\n\n    Coefficients are assumed to be provided from the VAR model:\n\n    .. math::\n        y_t = A_1 y_{t-1} + \\dots + A_p y_{t-p} + \x0barepsilon_t\n\n    Autocovariances are calculated by solving the associated discrete Lyapunov\n    equation of the state space representation of the VAR process.\n\n    "
    from scipy import linalg
    if (type(coefficients) == list):
        order = len(coefficients)
        k_endog = coefficients[0].shape[0]
    else:
        (k_endog, order) = coefficients.shape
        order //= k_endog
        coefficients = [coefficients[:k_endog, (i * k_endog):((i + 1) * k_endog)] for i in range(order)]
    if (maxlag is None):
        maxlag = (order - 1)
    companion = companion_matrix(([1] + [(- coefficients[i]) for i in range(order)])).T
    selected_variance = np.zeros(companion.shape)
    selected_variance[:k_endog, :k_endog] = error_variance
    stacked_cov = linalg.solve_discrete_lyapunov(companion, selected_variance)
    autocovariances = [stacked_cov[:k_endog, (i * k_endog):((i + 1) * k_endog)] for i in range(min(order, (maxlag + 1)))]
    for i in range((maxlag - (order - 1))):
        stacked_cov = np.dot(companion, stacked_cov)
        autocovariances += [stacked_cov[:k_endog, (- k_endog):]]
    if forward_autocovariances:
        for i in range(len(autocovariances)):
            autocovariances[i] = autocovariances[i].T
    return autocovariances

dataset/ETHPy150Open statsmodels/statsmodels/statsmodels/tsa/statespace/tools.py _compute_multivariate_acovf_from_coefficients/original