[Linear Algebra] Lecture 16

Author

SEOYEON CHOI

Published

April 10, 2024

Import

import numpy as np import matplotlib.pyplot as plt

Lecture

학습 목표

  • Projections
  • Least Squares and best straight line

\(P = A(A^T A)^{-1} A^T\)

  • If \(b\) in column space \(Pb = b\)
    • 민약 \(b\)가 컬럼 스페이스에 존재한다면 프로젝션 매트릭스에 곱해도 그대로 \(b\)가 나옴
    • \(P = A(A^T A)^{-1} A^T b = A(A^T A)^{-1} A^T Ax = b\)
    • \(b\)는 컬럼 스페이스 위에 존재하는 벡터이므로 위와 같이 계산된다.
  • If \(b\) \(\perp\) column space \(Pb = 0\)
    • 만약 \(b\)가 컬럼 스페이스랑 수직을 이루면 프로젝션 매트릭스에 곱하면 \(0\)이 나옴
    • \(P = Pb = A(A^T A)^{-1} A^T b = 0\)
    • \(b\)가 컬럼 스페이스가 아닌 다른 공간인 left null space에 존재하면 \(0\)이 된다.
  • \(p\) = projection, \(e\) = error, \(b\) = a vector
    • \(p\)는 columns space 에 있고
    • \(e\)는 left null space 에 있다.
  • \(P\) = symmetric \(\to\) \((I-P)\) = symmetric
  • \(P\) = projection \(\to\) \((I-P)\) = projection

- Least Square Method

# 주어진 데이터 포인트
data = [(1, 1), (2, 2), (3, 2)]
x_data = [d[0] for d in data]
y_data = [d[1] for d in data]

# 다항 회귀를 위해 데이터를 변형합니다.
x = np.array(x_data)
y = np.array(y_data)

# 다항 회귀를 위한 최소자승법을 사용하여 회귀선을 구합니다.
coefficients = np.polyfit(x, y, 1)  # 1차 다항식을 사용합니다.

# 다항식 생성
p = np.poly1d(coefficients)

# 회귀선 그리기
plt.scatter(x, y, label='Data Points')
plt.plot(x, p(x), color='red', label='Regression Line')
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Linear Regression')
plt.legend()
plt.grid(True)
plt.show()

\(Ax = b\) \(\to\) \(\begin{bmatrix} 1 & 1 \\ 1 & 2 \\ 1&3 \end{bmatrix} \begin{bmatrix} C \\ D \end{bmatrix} \begin{bmatrix} 1 \\ 2 \\ 2 \end{bmatrix}\)

  • \(y = C + Dt\) 식을 찾아본다고 할 때,
    • \(C + D = 1\)
    • \(C + 2D = 2\)
    • \(C + 3D = 2\)
      • Don’t have a solution.
  • best solution 찾기
    • \(A^T A{\hat x} = A^T b\)
  • \(||Ax - b||^2 = ||e||^2\)
    • \(e_1^2 + e_2^2 + e_3^2\)
    • \(x\) 점에서 projection 한 값들
  • I want to minimized the sum of error.

Find \({\hat x} = \begin{bmatrix} {\hat c} \\ {\hat p} \end{bmatrix}, P\)

  • \(b\) normal wquations(=eqnas)

    • \(3C + 6D = 5\)
    • \(6C + 14D = 15\)
    • \(2D = 1\) \(\therefore D = \frac{1}{2}, C = \frac{2}{3}\)
  • best line \(\frac{1}{2}t + \frac{2}{3}\)

    • \(e_1 = - \frac{1}{6}, ex_2 = \frac{2}{6}, e_3 = - \frac{1}{6}\)
  • \((C+D-1)^2\) + \((C+2D-2)^2 + (-3D - 2^2)\)

  • $A^T A = A^T b $

  • \(P = A {\hat x}\)

  • If \(A\) has independant columns then \(A^TA\) is invertible.

    • Suppose \(A^T A x = 0\)
    • \(X\) must be \(0\)
  • Idea

    • $X^T A^T A x = 0 = (A_x)^T Ax \(\to\) \(Ax = 0, X+0\)
  • columns definitely independent if they are perp unit vectors or thomominal vectores