nodes: 0, module; 1, function_definition; 2, function_name:eig_corr; 3, parameters; 4, block; 5, identifier:C0; 6, identifier:Ct; 7, default_parameter; 8, default_parameter; 9, default_parameter; 10, expression_statement; 11, expression_statement; 12, expression_statement; 13, comment:# solve the symmetric eigenvalue problem in the new basis; 14, if_statement; 15, comment:# sort eigenpairs; 16, expression_statement; 17, comment:# transform the eigenvectors back to the old basis; 18, expression_statement; 19, comment:# Change signs of eigenvectors:; 20, if_statement; 21, comment:# return result; 22, return_statement; 23, identifier:epsilon; 24, float:1e-10; 25, identifier:method; 26, string; 27, identifier:sign_maxelement; 28, False; 29, comment:r""" Solve generalized eigenvalue problem with correlation matrices C0 and Ct
  
      Numerically robust solution of a generalized Hermitian (symmetric) eigenvalue
      problem of the form
  
      .. math::
          \mathbf{C}_t \mathbf{r}_i = \mathbf{C}_0 \mathbf{r}_i l_i
  
      Computes :math:`m` dominant eigenvalues :math:`l_i` and eigenvectors
      :math:`\mathbf{r}_i`, where :math:`m` is the numerical rank of the problem.
      This is done by first conducting a Schur decomposition of the symmetric
      positive matrix :math:`\mathbf{C}_0`, then truncating its spectrum to
      retain only eigenvalues that are numerically greater than zero, then using
      this decomposition to define an ordinary eigenvalue Problem for
      :math:`\mathbf{C}_t` of size :math:`m`, and then solving this eigenvalue
      problem.
  
      Parameters
      ----------
      C0 : ndarray (n,n)
          time-instantaneous correlation matrix. Must be symmetric positive definite
      Ct : ndarray (n,n)
          time-lagged correlation matrix. Must be symmetric
      epsilon : float
          eigenvalue norm cutoff. Eigenvalues of C0 with norms <= epsilon will be
          cut off. The remaining number of Eigenvalues define the size of
          the output.
      method : str
          Method to perform the decomposition of :math:`W` before inverting. Options are:
  
          * 'QR': QR-based robust eigenvalue decomposition of W
          * 'schur': Schur decomposition of W
      sign_maxelement : bool
          If True, re-scale each eigenvector such that its entry with maximal absolute value
          is positive.
  
  
      Returns
      -------
      l : ndarray (m)
          The first m generalized eigenvalues, sorted by descending norm
      R : ndarray (n,m)
          The first m generalized eigenvectors, as a column matrix.
  
      """; 30, assignment; 31, assignment; 32, call; 33, block; 34, else_clause; 35, assignment; 36, assignment; 37, identifier:sign_maxelement; 38, block; 39, expression_list; 40, string_content:QR; 41, identifier:L; 42, call; 43, identifier:Ct_trans; 44, call; 45, attribute; 46, argument_list; 47, import_from_statement; 48, expression_statement; 49, block; 50, pattern_list; 51, call; 52, identifier:R; 53, call; 54, for_statement; 55, identifier:l; 56, identifier:R; 57, identifier:spd_inv_split; 58, argument_list; 59, attribute; 60, argument_list; 61, identifier:_np; 62, identifier:allclose; 63, attribute; 64, identifier:Ct; 65, dotted_name; 66, dotted_name; 67, assignment; 68, import_from_statement; 69, expression_statement; 70, identifier:l; 71, identifier:R_trans; 72, identifier:sort_by_norm; 73, argument_list; 74, attribute; 75, argument_list; 76, identifier:j; 77, call; 78, block; 79, identifier:C0; 80, keyword_argument; 81, keyword_argument; 82, keyword_argument; 83, identifier:_np; 84, identifier:dot; 85, call; 86, identifier:L; 87, identifier:Ct; 88, identifier:T; 89, identifier:scipy; 90, identifier:linalg; 91, identifier:eigh; 92, pattern_list; 93, call; 94, dotted_name; 95, dotted_name; 96, assignment; 97, identifier:l; 98, identifier:R_trans; 99, identifier:_np; 100, identifier:dot; 101, identifier:L; 102, identifier:R_trans; 103, identifier:range; 104, argument_list; 105, expression_statement; 106, expression_statement; 107, identifier:epsilon; 108, identifier:epsilon; 109, identifier:method; 110, identifier:method; 111, identifier:canonical_signs; 112, True; 113, attribute; 114, argument_list; 115, identifier:l; 116, identifier:R_trans; 117, identifier:eigh; 118, argument_list; 119, identifier:scipy; 120, identifier:linalg; 121, identifier:eig; 122, pattern_list; 123, call; 124, subscript; 125, assignment; 126, augmented_assignment; 127, identifier:_np; 128, identifier:dot; 129, attribute; 130, identifier:Ct; 131, identifier:Ct_trans; 132, identifier:l; 133, identifier:R_trans; 134, identifier:eig; 135, argument_list; 136, attribute; 137, integer:1; 138, identifier:imax; 139, call; 140, subscript; 141, call; 142, identifier:L; 143, identifier:T; 144, identifier:Ct_trans; 145, identifier:R; 146, identifier:shape; 147, attribute; 148, argument_list; 149, identifier:R; 150, slice; 151, identifier:j; 152, attribute; 153, argument_list; 154, identifier:_np; 155, identifier:argmax; 156, call; 157, identifier:_np; 158, identifier:sign; 159, subscript; 160, attribute; 161, argument_list; 162, identifier:R; 163, identifier:imax; 164, identifier:j; 165, identifier:_np; 166, identifier:abs; 167, subscript; 168, identifier:R; 169, slice; 170, identifier:j
code: def eig_corr(C0, Ct, epsilon=1e-10, method='QR', sign_maxelement=False):
      r""" Solve generalized eigenvalue problem with correlation matrices C0 and Ct
  
      Numerically robust solution of a generalized Hermitian (symmetric) eigenvalue
      problem of the form
  
      .. math::
          \mathbf{C}_t \mathbf{r}_i = \mathbf{C}_0 \mathbf{r}_i l_i
  
      Computes :math:`m` dominant eigenvalues :math:`l_i` and eigenvectors
      :math:`\mathbf{r}_i`, where :math:`m` is the numerical rank of the problem.
      This is done by first conducting a Schur decomposition of the symmetric
      positive matrix :math:`\mathbf{C}_0`, then truncating its spectrum to
      retain only eigenvalues that are numerically greater than zero, then using
      this decomposition to define an ordinary eigenvalue Problem for
      :math:`\mathbf{C}_t` of size :math:`m`, and then solving this eigenvalue
      problem.
  
      Parameters
      ----------
      C0 : ndarray (n,n)
          time-instantaneous correlation matrix. Must be symmetric positive definite
      Ct : ndarray (n,n)
          time-lagged correlation matrix. Must be symmetric
      epsilon : float
          eigenvalue norm cutoff. Eigenvalues of C0 with norms <= epsilon will be
          cut off. The remaining number of Eigenvalues define the size of
          the output.
      method : str
          Method to perform the decomposition of :math:`W` before inverting. Options are:
  
          * 'QR': QR-based robust eigenvalue decomposition of W
          * 'schur': Schur decomposition of W
      sign_maxelement : bool
          If True, re-scale each eigenvector such that its entry with maximal absolute value
          is positive.
  
  
      Returns
      -------
      l : ndarray (m)
          The first m generalized eigenvalues, sorted by descending norm
      R : ndarray (n,m)
          The first m generalized eigenvectors, as a column matrix.
  
      """
      L = spd_inv_split(C0, epsilon=epsilon, method=method, canonical_signs=True)
      Ct_trans = _np.dot(_np.dot(L.T, Ct), L)
  
      # solve the symmetric eigenvalue problem in the new basis
      if _np.allclose(Ct.T, Ct):
          from scipy.linalg import eigh
          l, R_trans = eigh(Ct_trans)
      else:
          from scipy.linalg import eig
          l, R_trans = eig(Ct_trans)
  
      # sort eigenpairs
      l, R_trans = sort_by_norm(l, R_trans)
  
      # transform the eigenvectors back to the old basis
      R = _np.dot(L, R_trans)
  
      # Change signs of eigenvectors:
      if sign_maxelement:
          for j in range(R.shape[1]):
              imax = _np.argmax(_np.abs(R[:, j]))
              R[:, j] *= _np.sign(R[imax, j])
  
      # return result
      return l, R