Matrix Computation

Roh's warehouse·2025년 9월 21일

Optimization in Learning

목록 보기
1/8
post-thumbnail

Matrix Algebra

Matrix -- The mother of all data structures. The nonmathematical uses of the word matrix reflect its Latin origins in mater, or mother.... The word has two meanings -- a representation of a linear mapping and the basis for all our existence.

Linear Systems

Linear algebra는 Ax=bAx=b 형태의 the system of linear equations에 대한 성질을 탐구한다.

Ax=bAx=b는 row picture로는 n개의 plane에 대한 intersection이며, column picture로는 A의 column vectors들의 조합으로 볼 수 있다. 일반적으로는 column picture로써 문제를 주로 바라본다.

Vector Products

두 개의 Vector를 가정하자.

x=[x1x2x3]y=[y1y2y3]x=\begin{bmatrix} x_1 \\ x_2 \\ x_3 \end{bmatrix} \quad y=\begin{bmatrix} y_1 \\ y_2 \\ y_3 \end{bmatrix}

일반적으로 vector는 column vector를 의미한다.

  • Inner product (dot product, 내적) : scalar
xTy=[x1x2x3][y1y2y3]=x1y1+x2y2+x3y3=i=13xiyi=yTx\mathbf{x}^T\mathbf{y} = \begin{bmatrix} x_1 & x_2 & x_3 \end{bmatrix}\begin{bmatrix}y_1 \\ y_2 \\ y_3\end{bmatrix} = x_1y_1+x_2y_2+x_3y_3 = \sum_{i=1}^3 x_iy_i = \mathbf{y}^T\mathbf{x}
  • Outer product (외적) : matrix
xyT=[x1x2x3][y1y2y3]=[x1y1x1y2x1y3x2y1x2y2x2y3x3y1x3y2x3y3]\mathbf{x}\mathbf{y}^T = \begin{bmatrix} x_1 \\ x_2 \\ x_3 \end{bmatrix}\begin{bmatrix}y_1 & y_2 & y_3\end{bmatrix} = \begin{bmatrix}x_1y_1 & x_1y_2 & x_1y_3 \\ x_2y_1 & x_2y_2 & x_2y_3 \\ x_3y_1 & x_3y_2 & x_3y_3\end{bmatrix}
  • Elementwise product (원소곱) : vector
xy=[x1x2x3][y1y2y3]=[x1y1x2y2x3y3]\mathbf{x} \odot \mathbf{y} = \begin{bmatrix} x_1 \\ x_2 \\ x_3 \end{bmatrix} \odot \begin{bmatrix}y_1 \\ y_2 \\ y_3\end{bmatrix} = \begin{bmatrix}x_1y_1 \\ x_2y_2 \\ x_3y_3\end{bmatrix}

Matrix Multiplication

ARm×pA \in \mathbb{R}^{m\times p}, BRp×nB \in \mathbb{R}^{p\times n}라고 하자. 이 때, C=ABC=AB는 다음과 같다.

cij=k=1paikbkj=A(i,:)B(:,j)c_{ij} = \sum_{k=1}^p a_{ik}b_{kj} = A(i,:)B(:,j)

모르는 사람이 없을 공식인데, 기본적으로 A의 row vector를 원소로 가지는 vector와 B의 column vector를 원소로 가지는 vector에 대해서, 원소 간 곱을 inner product라고 했을 때의 outer product로 계산된다.

또한, 다음과 같이 표현할 수도 있다.

C=AB=k=1pA(:,k)B(k,:)C = AB = \sum_{k=1}^p A(:,k)B(k,:)

즉, A의 column vector를 원소로 가지는 vector와 B의 row vector를 원소로 가지는 vector에 대해서, 원소 간 곱을 outer product라고 했을 때의 inner product로 계산된다.

Matrix Multiplication을 효율적이고 빠르게 하는 것이 학습 속도를 결정하기 때문에 이 부분을 열심히 파보는 것도 좋을 것 같다.

Determinant and Positive Definite

Determinant of a Matrix

A의 determinant는 A의 row vector들로 표현된 nn-dimensional space 상의 parallelepiped PP의 부피와 같다.

아마 Matrix를 하나의 값으로 표현한다면 가장 흔하게 사용될 값이 바로 determinant 이다. (Determinant 값이 음수라면 공간의 방향(orientation)이 뒤집힌다는 의미이다.)

Determinant와 관련된 공식은 많지만, 아래 정도만 기억해도 고차원 수학을 다룰 예정이 아니라면 별 문제는 없었던 것 같다.

  • A matrix AA has an inverse matrix A1A^{-1} if and only if det(A)0det(A)\ne 0
  • If AA is triangular, then det(A)=a11a22...anndet(A)=a_{11}a_{22}...a_{nn}. In Particular, det(In)=1det(I_n)=1.
  • det(AB)=det(A)det(B)det(AB) = det(A)det(B)
  • tr(AB)=tr(BA)tr(AB)=tr(BA)

간혹 invese matrix를 프로그램이나 알고리즘 내에서 직접 explicit하게 계산하도록 코드를 구현하는 사람들이 있는데, 높은 확률로 뻗어버릴테니 꼭 피하길 바란다.

Symmetric Positive Definite (SPD) Matrix

Symmetric Positive Definite(SPD)는 이후 다룰 Optimization 내용에서 중요하게 사용되는 성질이다.

SPD의 정의는 다음과 같다.

  • Symmetric: A=ATA=A^T
  • Positive Definite (or positive semi-definite): if xTAx>0x^TAx>0 (or xTAx0x^TAx\ge 0) for all nonzero xRnx \in \mathbb{R}^n, denoted by A0A \succ 0 (or A0A \succeq 0).

만약 CRn×nC\in\mathbb{R}^{n\times n}가 full rank를 가지고 A=CTCA=C^TC이면, AA는 SPD이다.

xTAx=xTCTCx=Cx2>0x^TAx = x^TC^TCx = \|Cx\|^2>0

참고로 Covariance Matrix는 SPD이다.

C=1N1XTX=1N1j=1NxjxjTC=\frac{1}{N-1}\mathbf{X}^T\mathbf{X}=\frac{1}{N-1}\sum_{j=1}^N\mathbf{x}_j\mathbf{x}_j^T

where xi=[xi1...xip]T\mathbf{x}_i = \begin{bmatrix} x_{i1} & ... & x_{ip} \end{bmatrix}^T.

The Cholesky Factorization

The Cholesky Factorization는 SPD matrix가 갖는 중요한 성질로, 모든 SPD는 positive diagonal entry를 갖는 upper-triangular matrix로 unique하게 분해된다.

Theorem: Cholesky factorization
Every SPD matrix A=(aij)Rn×nA=(a_{ij})\in\mathbb{R}^{n\times n} has a uniqe Cholesky factorization

A=RTR,rii>0A=R^TR, \qquad r_{ii} > 0

where R=(rij)R=(r_{ij}) is an n×nn\times n upper-triangular matrix with positive diagonal entries.

RRA12A^{\frac{1}{2}}로 표현하기도 한다.

Tests for Positive Definiteness

어떤 Matrix가 Positive Definite인지 판별하는 방법은 다음과 같 은 것들이 있다.

  • All the eigenvalues of AA satisfy λi>0\lambda_i>0.

  • All the upper left submatrices AkA_k have positive determinants.

  • 2×22\times 2-matrix [abbc]\left[ \begin{array}{cc} a & b \\ b & c\\ \end{array} \right] is positive definite when a>0a>0 and acb2>0ac-b^2>0.

Linear Algebra

Linear Dependency and Basis

  • The vectors v1,v2,...,vkv_1, v_2, ..., v_k에 대해 c1v1+...+ckvk=0c_1v_1 + ... + c_kv_k=0을 만족하는 조건이 오직 c1=...=ck=0c_1=...=c_k=0이면, 이는 linearly independent 이다. (반대는 linearly dependent)

    • 만약 viv_i들이 linearly dependent하면, viv_i들 중 하나(vkv_k)를 나머지 vector들 (v1,,vk1,vk+1,,vn)(v_1,\dots,v_{k-1},v_{k+1},\dots,v_n)의 linear combination으로 표현할 수 있다.
  • 어떤 vector space VV에 대해, VV 내 모든 vector vvviv_i들의 linear combination들로 표현할 수 있는 경우, viv_i들이 VV를 생성(span)한다고 말한다.

  • 만약 다음 조건들이 만족되는 경우 {vi}\lbrace v_i\rbraceVVbasis 라고 한다.

    1. viv_i's are linearly independent.
    2. {vi}\lbrace v_i\rbrace spans the space VV.
  • Vector space VV의 basis를 구성하는 vector의 수를 VVdimension 이라고 한다.

Norms

  • Let SS be a vector space with elements xx.
    이 때, 다음 조건들을 만족하는 real-valued function x\Vert x \Vertnorm 이라고 한다:
    1. x0\Vert x \Vert \ge 0 for any xSx\in S
    2. x=0\Vert x \Vert=0 if and only if x=0x=0
    3. αx=αx\Vert\alpha x \Vert = \vert \alpha \vert \Vert x \Vert, where α\alpha is an arbitrary scalar
    4. x+yx+y\Vert x+y \Vert \le \Vert x\Vert+\Vert y\Vert (triangular inequality)

새로운 Norm을 만들 때, triangular inequality를 만족하는지 꼭 체크해야한다.

Vector Norms

  • Vector pp-norm: xp=(i=1nxip)1/p\Vert x \Vert_p = \left( \sum_{i=1}^n \vert x_i \vert^p \right)^{1/p}

  • Manhattan: x1=1inxi\Vert x\Vert_1=\sum_{1\le i \le n} \vert x_i\vert$

  • Euclidian: x2=xTx\Vert x \Vert_2=\sqrt{x^Tx}

  • Chebyshev: x=max1inxi\Vert x\Vert_\infty=\max_{1\le i \le n} \vert x_i\vert

Matrix Norms

  • Matrix pp-norm

    Ap=supx0Axpxp\Vert A\Vert_p = \sup_{x\ne 0}\frac{\Vert Ax\Vert_p}{\Vert x\Vert_p}
  • Frobenius norm

    AF=(i=1mj=1naij2)1/2=tr(ATA)\Vert A\Vert_F=\left( \sum_{i=1}^m\sum_{j=1}^n\vert a_{ij}\vert^2 \right)^{1/2} = \sqrt{tr(A^TA)}

Matrix Operation on Vectors

Linear Transformations

만약 특정 공간의 basis들에 대한 linear transformation (AxiAx_i) 를 안다면, 우리는 그 공간 전체에 대한 linear transformation을 알 수 있다.

  • Linearity: If x=c1x1+...+cnxnx=c_1x_1+...+c_nx_n, then Ax=c1(Ax1)+...+cn(Axn)Ax = c_1(Ax_1)+...+c_n(Ax_n).

자주 사용되는 linear transformation으로는 Scaling, Rotation, Identity, Projection, Reflection 등이 있다.

Projection Using Inner Products

WANT: project xx to aa.

  • p=(xTa)a=(aTx)a=a(aTx)=(aaT)x=Paxp=(x^Ta)a=(a^Tx)a=a(a^Tx)=(aaT)x=P_ax

  • Pa=aaTP_a=aa^T if a=aTa=1\|a\| = a^Ta = 1

  • Pa=aaTaTaP_a=\frac{aa^T}{a^Ta} in general

    • 이 때, PaP_aprojection matrix 라고 한다.

Least Squares

Least Squares Solution

Theorem: Least Squares Solution
The least squares solution to :

minAxbARm×n,  m>n\min \|Ax-b\| \qquad A\in \mathbb{R}^{m\times n}, \; m>n

satisfies the following normal equation : ATAxˉ=ATbA^TA\bar{x} = A^Tb

Least square 문제는 아래 figure에서 볼 수 있듯이 Ax 위로의 b의 projection 문제와 동일하다.

이는 앞으로 나올 수많은 dimension reduction 기법의 가장 기초가 된다.

  • If ATAA^TA is invertible, then xˉ=(ATA)1ATb\bar{x} = (A^TA)^{-1}A^Tb

  • If pp is the projection of b onto the column space of AA, then p=Axˉ=Pb=A(ATA)1ATbp=A\bar{x} = Pb = A(A^TA)^{-1}A^Tb,
    where PP is an orthogonal projection matrix given by A(ATA)1ATA(A^TA)^{-1}A^T

  • PRn×nP\in \mathbb{R}^{n\times n} is said to be a projection if P2=PP^2=P.

  • PRn×nP\in \mathbb{R}^{n\times n} is an orthogonal projection if P2=PP^2=P and P=PTP=P^T.

Orthogonal Matrix

  • Matrix QQ의 column과 row vector들이 orthogonal unit vectors (orthonormal vectors)이면, i.e. QTQ=QQT=IQ^TQ=QQ^T=I, 이 때의 QQorthogonal matrix라고 한다.

Orthogonal matrix는 다음과 같은 좋은 성질을 가진다.

  • QT=Q1Q^T=Q^{-1}

  • Qx=x\Vert Qx\Vert = \Vert x\Vert

  • (Qx)T(Qy)=xTy(Qx)^T(Qy)=x^Ty

Orthogonal matrix를 이용한 transformation은 lengths와 inner products를 보존한다.

Theorem: Orthogonal Matrix
If the columns of Qr=[q1,...,qr]Rn×rQ_r=[q_1,...,q_r]\in \mathbb{R}^{n\times r} are an orthonormal basis for a subspace SS, then the least squares problem minQrxb\min\|Q_rx-b\| becomes easy

QrTQrxˉ=QrTbxˉ=QrTb.Q_r^TQ_r\bar{x} = Q_r^Tb \Rightarrow \bar{x}=Q_r^Tb.

The projection of bb and the unique orthogonal projection matrix onto the column space S=span{q1,...,qr}S=span\lbrace q_1,...,q_r\rbrace is

p=Psb=Qrxˉ=QrQrTb,Ps=QrQrT=i=1rqiqiTp=P_sb=Q_r\bar{x}=Q_rQ_r^Tb, \quad P_s=Q_rQ_r^T=\sum_{i=1}^r q_iq_i^T

만약 Q=[q1,...,qn]Rn×nQ=[q_1,...,q_n]\in R^{n\times n}의 column들이 orthonormal basis이면, bb는 다음과 같이 쓸 수 있다.

b=x1q1+...+xnqn=Qx,x=QTbb=x_1q_1+...+x_nq_n=Qx, \quad x=Q^Tb
b=QQTb=(q1Tb)q1+...(qnTb)qn\Rightarrow b=QQ^Tb=(q_1^Tb)q_1+...(q_n^Tb)q_n

어떤 vector를 다른 basis로 표현하는 기법으로, 이 역시 dimension reduction을 포함한 feature transformation 기법의 기초가 된다.

profile
공부랑 연구랑 생각

0개의 댓글