# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Principal Component Analysis (PCA)
# is a linear decomposition to an orgothogal basis.

# # NOT DONE!

import numpy as np

# ## Choose a square Matrix
# Form a square matrix from two vectors.   
# Our square matrix is symmetic, so the eigenvectors will be orthogonal.

# Use this example.
# Same example will be used for SVD.  
# https://www.d.umn.edu/~mhampton/m4326svd_example.pdf

# 2 rows, 3 columns
B = np.array([[3,2,2],[2,3,-2]])
print("2x3 matrix B\n",B)
print("B transpose\n",B.T)
A = B.dot(B.T)
print("Square matrix A = BBT\n",A)

# ## Eigen decomposition with numpy

eigvals,eigvecs=np.linalg.eig(A)
print("Eigenvalues of A\n",eigvals)
print("Eigenvectors of A\n",eigvecs)

# Double check.  
# $ det(A - \lambda_i I) = 0$ for $v_i \ne 0$  

mat1=(A-(eigvals[0]*np.identity(2)))*eigvecs[0]
print("Matrix A minus first eigenvalue\n",mat1)
print("Determinant = ",np.linalg.det(mat1))

mat2=(A-(eigvals[1]*np.identity(2)))*eigvecs[1]
print("Matrix A minus second eigenvalue\n",mat2)
print("Determinant = ",np.linalg.det(mat2))

# ## Eigenvalues computed by hand
# We want the determinant of this matrix  
# $ (a-\lambda) (b)$  
# $ (c) (d-\lambda)$  
# For 2x2 matrix, the determinant = ad-bc so  
# $det = (a-\lambda)(d-\lambda) - b*c$  
# $= (17-\lambda)(17-\lambda) - 8*8$  
# $= (17^2-2*17*\lambda+\lambda^2) - 64$  
# $= (289-34\lambda+\lambda^2) - 64$  
# $= \lambda^2 -34\lambda +225$  
#   
#

# The characteristic (polynomial) equation of matrix A is  
# $0 = \lambda^2 -34\lambda +225$  
#

# We want the roots of the equation above.  
# For any quadratic equation   
# $ax^2 + bx + c = 0$, 
# $a(x-r)(x-s) = 0$, and roots 
# $r,s=\frac{-b \pm \sqrt{b^2-4ac}}{2a}$  

# The quadratic equation $\lambda^2 -34\lambda +225 = 0$  
# has roots $r,s=\frac{34 \pm \sqrt{1156-4*225}}{2}
#  =\frac{34 \pm \sqrt{256}}{2}
#  =\frac{34 \pm 16}{2}
#  =25,9$  
# So $0 = (\lambda-25)(\lambda-9)$.  
# The eigenvalues of A are 25 and 9 (and 0?).  
# The singular values of A are the square roots, 5 and 3.

# ## Eigenvectors computed by hand
# Note A is symmetric so the eigenvectors are orthogonal.  
# Use the following:  
# $(A - \lambda_i I) v_i = 0$   

# First eigenvector v1:  
# $(\begin{bmatrix}
# 17 & 8 \\ 8 & 17
# \end{bmatrix} - 
# \begin{bmatrix}
# 25 & 0 \\ 0 & 25
# \end{bmatrix}) *
# \begin{bmatrix}v1_1 \\ v1_2 \end{bmatrix} =0 $

# $\begin{bmatrix}
# -8 & 8 \\ 8 & -8
# \end{bmatrix} *
# \begin{bmatrix}v1_1 \\ v1_2 \end{bmatrix} =0 $

# $
# \begin{bmatrix}
# -8v1_1+8v1_2 \\ 8v1_1-8v1_2
# \end{bmatrix} = 0
# $

# Choose any v11 = - v12: v1 = [5 -5].

# Second eigenvetor v2:  
# $\begin{bmatrix}
# 8 & 8 \\ 8 & 8
# \end{bmatrix} *
# \begin{bmatrix}v1_1 \\ v1_2 \end{bmatrix} =0 $

# Choose any v21 = v22: v2 = [5 5]



StarcoderdataJupyterScriptsDedupFiltered