In [12]:
import os
os.system('jupyter nbconvert --to html yourNotebook.ipynb')
import numpy as np
from tabulate import tabulate
import matplotlib.pyplot as plt
import os
    
def print_log(idx, v_list, lambda_list, diff_list):
    info_list = [[i, v_list[i], lambda_list[i], diff_list[i]] for i in range(idx)]
    print(tabulate(info_list, headers=["iteration","eigenvector", "eigenvalue","lambda_diff"]))

Power Iteration Method

Power Iteration is utilizing the convergence of the sequence

$$ \frac{x}{||x||}, \frac{Ax}{||Ax||}, \frac{A^2x}{||A^2x|}, \frac{A^3x}{||A^3x||}, \cdots$$

For any matrix $A$ with a unique dominant eigenvalue, the sequence would converge to $A$'s eigenvector corresponding to its dominant eigenvalue.

To calculate an estimated natural eigenvalue for a give vector, we use Rayleigh Quotient to measure the value making this vector like an eigenvector the most.

$$ r(x) = \frac{x^T A x}{x^T x} $$
In [13]:
def PowerMethod(A, convergence_condition=0.00001):
    idx = 0
    r, c = A.shape

    if r != c:
        raise Exception("not a square matrix")
    # initialize eigenvectors
    v = np.zeros(r)
    v[-1] = 1
    # initialize eigenvalues
    lam = v.dot(A.dot(v))
    while True:
        idx = idx + 1
        # new vector
        v_new = A.dot(v)
        v_new = v_new / np.linalg.norm(v_new)

        lam_new = v_new.dot(A.dot(v_new))
        if np.abs(lam_new - lam) < convergence_condition:
            break
        lam = lam_new
        v = v_new
    return v_new, lam_new, idx


if __name__ == '__main__':
    A = np.array([[1.5,.5],[.5,1.5]])
    PowerMethod(A)
  iteration  eigenvector                eigenvalue    lambda_diff
-----------  -----------------------  ------------  -------------
          0  [0. 1.]                       1.5      inf
          1  [0.31622777 0.9486833 ]       1.8        0.3
          2  [0.51449576 0.85749293]       1.94118    0.141176
          3  [0.61394061 0.78935222]       1.98462    0.0434389
          4  [0.66162164 0.74983786]       1.99611    0.0114936
          5  [0.68467546 0.72884807]       1.99902    0.00291544
          6  [0.69597329 0.71806768]       1.99976    0.000731529
          7  [0.7015611  0.71260931]       1.99994    0.00018305
In [13]: