Example #1
0
def direct_gls(eqns, scale):
    y = []
    x = []
    for key in eqns:
        y.append(eqns[key]["dependent"])
        x.append(eqns[key]["exog"])
    y = scale * np.vstack(y)
    from scipy.sparse import csc_matrix, lil_matrix

    n, k = x[0].shape
    _x = lil_matrix((len(x) * n, len(x) * k))
    for i, val in enumerate(x):
        _x[i * n:(i + 1) * n, i * k:(i + 1) * k] = val
    from scipy.sparse.linalg import inv as spinv

    b = spinv(csc_matrix(_x.T @ _x)) @ (_x.T @ y)
    e = y - _x @ b
    e = e.reshape((-1, n)).T
    sigma = e.T @ e / n
    omega_inv = np.kron(np.linalg.inv(sigma), np.eye(n))
    b_gls = np.linalg.inv(_x.T @ omega_inv @ _x) @ (_x.T @ omega_inv @ y)
    xpx = _x.T @ omega_inv @ _x / n
    xpxi = np.linalg.inv(xpx)
    e = y - _x @ b_gls
    xe = (_x.T @ omega_inv).T * e
    _xe = np.zeros((n, len(x) * k))
    for i in range(len(x)):
        _xe += xe[i * n:(i + 1) * n]
    xeex = _xe.T @ _xe / n
    cov = xpxi @ xeex @ xpxi / n
    return b_gls, cov, xpx, xeex, _xe
def compute_fundamental_matrix(P, fast=True, drop_tol=1e-5, fill_factor=1000):
    """Computes the fundamental matrix for an absorbing random walk.

    Parameters
    ----------
    P : scipy.sparse matrix
        The transition probability matrix of the absorbing random walk. To
        construct this matrix, you start from the original transition matrix
        and delete the rows that correspond to the absorbing nodes.

    fast : bool, optional
    If True (default), use the iterative SuperLU solver from
    scipy.sparse.linalg.

    drop_tol : float, optional
        If `fast` is True, the `drop_tol` parameter of the SuperLU solver is
        set to this value (default is 1e-5).

    fill_factor: int, optional
        If `If `fast` is True, the `fill_factor` parameter of the SuperLU
        solver is set to this value (default is 1000).

    Returns
    -------
    F : scipy.sparse matrix
        The fundamental matrix of the random walk. Element (i,j) holds the
        expected number of times the random walk will be in state j before
        absorption, when it starts from state i. For more information, check
        [1]_.

    References
    ----------
    .. [1] Doyle, Peter G., and J. Laurie Snell.
       Random walks and electric networks.
       Carus mathematical monographs 22 (2000).
       https://math.dartmouth.edu/~doyle/docs/walks/walks.pdf

    """
    n = P.shape[0]
    F_inv = speye(n, format='csc') - P.tocsc()
    if fast:
        solver = spilu(F_inv, drop_tol=drop_tol, fill_factor=fill_factor)
        F = matrix(solver.solve(eye(n)))
    else:
        F = spinv(F_inv).todense()
    return F
Example #3
0
def compute_fundamental_matrix(P, fast=True, drop_tol=1e-5, fill_factor=1000):
    """Computes the fundamental matrix for an absorbing random walk.

    Parameters
    ----------
    P : scipy.sparse matrix
        The transition probability matrix of the absorbing random walk. To
        construct this matrix, you start from the original transition matrix
        and delete the rows that correspond to the absorbing nodes.

    fast : bool, optional
    If True (default), use the iterative SuperLU solver from
    scipy.sparse.linalg.

    drop_tol : float, optional
        If `fast` is True, the `drop_tol` parameter of the SuperLU solver is
        set to this value (default is 1e-5).

    fill_factor: int, optional
        If `If `fast` is True, the `fill_factor` parameter of the SuperLU
        solver is set to this value (default is 1000).

    Returns
    -------
    F : scipy.sparse matrix
        The fundamental matrix of the random walk. Element (i,j) holds the
        expected number of times the random walk will be in state j before
        absorption, when it starts from state i. For more information, check
        [1]_.

    References
    ----------
    .. [1] Doyle, Peter G., and J. Laurie Snell.
       Random walks and electric networks.
       Carus mathematical monographs 22 (2000).
       https://math.dartmouth.edu/~doyle/docs/walks/walks.pdf

    """
    n = P.shape[0]
    F_inv = speye(n, format='csc') - P.tocsc()
    if fast:
        solver = spilu(F_inv, drop_tol=drop_tol, fill_factor=fill_factor)
        F = matrix(solver.solve(eye(n)))
    else:
        F = spinv(F_inv).todense()
    return F
def create_inv_susceptance_matrix(branches, nodes, slack_node):
    t1 = time.clock()
    B = np.zeros((len(nodes), len(nodes)))
    for branch in branches:
        i = branch.node_from.index
        j = branch.node_to.index
        B[i, i] += -1 / branch.impedance
        B[j, j] += -1 / branch.impedance
        B[i, j] += 1 / branch.impedance
        B[j, i] += 1 / branch.impedance
    B = np.delete(B, slack_node.index, axis=0)
    B = np.delete(B, slack_node.index, axis=1)
    logging.info(f"Susceptance matrix B built in {round(time.clock() - t1, 2)} seconds.")

    t1 = time.clock()
    B2 = csr_matrix(B)
    invB2 = spinv(B2)
    inverseB = invB2.toarray()
    logging.info(f"Susceptance matrix B inverted in {round(time.clock() - t1, 2)} seconds.")
    return inverseB
Example #5
0
 def inv_vcov_matrix(self, incidence_matrices, covariance_matrices, variance_components):
     V = vcov_matrix(
         incidence_matrices, covariance_matrices, variance_components)
     return spinv(V) if issparse(V) else inv(V)