A linear combination of a vector is simply a weighted sum of its coordinates
Linear combinations are often represented by dot products
The dot product of two \(p-\)dimensional vectors is a single number
E.g., consider the dot product of \(u = (3,1)\) with weighting vector \(w = (-1, 4)\):
\[<u,w> = u \cdot w = (3, 1) \times \begin{pmatrix} -1 \\ 4 \end{pmatrix}\]
\[ = 3\cdot -1 + 1\cdot 4 = 1\]
\[u = ?\] \[w = ?\] \[C = <u,w> = ?\]
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 55957.2 22896.1 2.443 0.0187 *
size 989.4 195.6 5.056 2.04e-06 ***
bedrooms 10291.9 3688.2 2.791 0.0076 ** \[x = ?\] \[\hat\beta = ?\]
The dot product of two \(p\)-dimensional vectors is a single number
The sign of this number is related to the angle between the vectors:
\[<u,v> = 3\cdot 1 + 1\cdot 4 = 7 > 0\]
\[<u,w> = 3\cdot -1 + 1 \cdot 3 = 0\]
\[<u,x> = 3\cdot -2 + 1 \cdot -3 = -9 < 0\]
It turns out:
\[ <u,v> = ||u||_2 ||v||_2 cos(\theta) \]
where \(\theta\) is the angle formed by \(u\) and \(v\):
Note also: orthogonal (i.e., perpendicular) vectors have dot product = 0!
Consider the \(2 \times 2\) matrix \(A = \begin{pmatrix} 3 & 2 \\ 1 & 2 \end{pmatrix}\) and 2-dimensional vector \(u = (1,2)\).
Then:
\[Au = \begin{pmatrix} 3 & 2 \\ 1 & 2 \end{pmatrix}\begin{pmatrix} 1 \\ 2 \end{pmatrix}\]
\[= \begin{pmatrix} 3\cdot 1 + 2\cdot 2 \\ 1\cdot 1 + 2\cdot 2 \end{pmatrix}\]
\[= \begin{pmatrix} 7 \\ 5 \end{pmatrix}\]
Note that we have transformed one 2-dimensional vector into another 2-dimensional vector!
\[A = \begin{pmatrix} 3 & 1 & 4 \\ 2 & 3 & 1 \end{pmatrix}; A^T = \begin{pmatrix} 3 & 2\\ 1 & 3\\ 4 & 1 \end{pmatrix}\]
When multiplying matrices \(A\) (\(n\times p\)) and \(B\) (\(p \times m\)):
Consider the matrices below \(A\):
\[A = \begin{pmatrix} 2 & 1 & 0\\ 1 & 3 & 2\\ 2 & 1 & 4 \end{pmatrix}, B = \begin{pmatrix} 3 & 1 & 4 \\ 2 & 3 & 1 \end{pmatrix}, C = \begin{pmatrix} 2 & 4 & 0 \\ 1 & 3 & 2 \\ 0 & 2 &1\\ 3 & 1 &4\\ \end{pmatrix}\]
Which of the following matrix products are defined? If defined, what is the dimension of the product? Pick one of the defined products and find it.
\[u^T A = \begin{pmatrix}1 & 2 \end{pmatrix} \begin{pmatrix} 3 & 2 \\ 1 & 2 \end{pmatrix}\]
\[ = \begin{pmatrix}1\cdot 3 + 2\cdot 1 & 2 \cdot 2 + 2 \cdot 2 \end{pmatrix}\]
\[= \begin{pmatrix} 5 & 8 \end{pmatrix}\]
\[\color{CornflowerBlue}{X = {\begin{pmatrix} 0 & 0 \\ 1 & 0 \\ 1 & 1 \\ 0 & 1 \\ \end{pmatrix}}}\]
Consider this a “data frame” of \(n = 4\) rows and \(p=2\) columns.
We will apply the transformation matrix \(A = \begin{pmatrix} 3 & 1 \\ 2 & 2 \end{pmatrix}\).
\[\color{CornflowerBlue}{\begin{pmatrix} 0 & 0 \end{pmatrix}} \begin{pmatrix} 3 & 1 \\ 2 & 2 \end{pmatrix} = \color{Orange}{\begin{pmatrix} 0 & 0 \end{pmatrix}}\]
\[\color{CornflowerBlue}{\begin{pmatrix} 1 & 0 \end{pmatrix}} \begin{pmatrix} 3 & 1 \\ 2 & 2 \end{pmatrix} = \color{Orange}{\begin{pmatrix} 3 & 1 \end{pmatrix}}\]
\[\color{CornflowerBlue}{\begin{pmatrix} 1 & 1 \end{pmatrix}} \begin{pmatrix} 3 & 1 \\ 2 & 2 \end{pmatrix} = \color{Orange}{\begin{pmatrix} 5 & 3 \end{pmatrix}}\]
\[\color{CornflowerBlue}{\begin{pmatrix} 0 & 1 \end{pmatrix}} \begin{pmatrix} 3 & 1 \\ 2 & 2 \end{pmatrix} = \color{Orange}{\begin{pmatrix} 2 & 2 \end{pmatrix}}\]
\[\color{CornflowerBlue}{{\begin{pmatrix} 0 & 0 \\ 1 & 0 \\ 1 & 1 \\ 0 & 1 \\ \end{pmatrix}}}\begin{pmatrix} 3 & 1 \\ 2 & 2 \end{pmatrix} = \color{Orange}{\begin{pmatrix} 0 & 0 \\ 3 & 1 \\ 5 & 3 \\ 2 & 2 \end{pmatrix}}\]
RTo form and multiply matrices:
RTo form and multiply matrices:
RTo form and multiply matrices:
\[ I = \begin{pmatrix} 1 & 0 & \dots & 0 \\ 0 & 1 & \dots& 0 \\ \vdots & \vdots & \ddots & \vdots \\ 0 & 0 & \dots & 1 \end{pmatrix} \]
\[\color{CornflowerBlue}{{\begin{pmatrix} 0 & 0 \\ 1 & 0 \\ 1 & 1 \\ 0 & 1 \\ \end{pmatrix}}}\begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix} = \color{CornflowerBlue}{\begin{pmatrix}0 & 0 \\ 1 & 0 \\ 1 & 1 \\ 0 & 1 \\ \end{pmatrix}}\]
\(\color{CornflowerBlue}{\begin{pmatrix} 0 & 0 \\ 1 & 0 \\ 1 & 1 \\ 0 & 1 \\ \end{pmatrix}} \begin{pmatrix} 3 & 1 \\ 2 & 2 \end{pmatrix} = \color{Orange}{\begin{pmatrix} 0 & 0 \\ 3 & 1 \\ 5 & 3 \\ 2 & 2 \end{pmatrix}}\)
\(\color{Orange}{\begin{pmatrix} 0 & 0 \\ 3 & 1 \\ 5 & 3 \\ 2 & 2 \end{pmatrix}} \begin{pmatrix} 3 & 1 \\ 2 & 2 \end{pmatrix}^{-1}= \color{CornflowerBlue}{\begin{pmatrix} 0 & 0 \\ 1 & 0 \\ 1 & 1 \\ 0 & 1 \\ \end{pmatrix}}\)
The inverse of a square \(p\times p\) matrix \(A\) is notated \(A^{-1}\) and is the matrix such that:
\[ A A^{-1} =I \]
where \(I\) is the identity matrix.
RRRWe can find \(B\) (must be \(2 \times 2\)) using some matrix algebra, given we know coordinates \(\color{CornflowerBlue}{X}\) (\(4 \times 2\)) and \(\color{forestgreen}{W}\) (\(4 \times 2\)):
\[ \color{CornflowerBlue}{X}B =\color{forestgreen}{W}\]
\[ \color{CornflowerBlue}{X^T}\color{CornflowerBlue}{X}B =\color{CornflowerBlue}{X^T}\color{forestgreen}{W}\]
\[ \left(\color{CornflowerBlue}{X^T}\color{CornflowerBlue}{X}\right)^{-1}\color{CornflowerBlue}{X^T}\color{CornflowerBlue}{X}B = \left(\color{CornflowerBlue}{X^T}\color{CornflowerBlue}{X}\right)^{-1}\color{CornflowerBlue}{X^T}\color{forestgreen}{W}\]
\[ B = \left(\color{CornflowerBlue}{X^T}\color{CornflowerBlue}{X}\right)^{-1}\color{CornflowerBlue}{X^T}\color{forestgreen}{W}\]
ggplot codelibrary(ggplot2)
library(patchwork) #for adding plots side-by-side
bluedf <- data.frame(bluecoords)
greendf <- data.frame(greencoords)
# Create base
base <- ggplot() +
scale_x_continuous(breaks=seq(-5,5,by=1),limits=c(-5,5)) +
scale_y_continuous(breaks=seq(-5,5,by=1),limits=c(-5,5)) +
geom_vline(aes(xintercept = 0)) + geom_hline(aes(yintercept = 0)) +
theme_minimal(base_size = 14) +
theme(panel.grid.minor = element_blank()) +
labs(x='', y='')
# Add points
bluepoints <- base +
geom_point(data = bluedf, aes(x = X1, y = X2), size = 3,col='cornflowerblue')
greenpoints <- base +
geom_point(data = df2, aes(x = X1, y = X2), size = 3,col='forestgreen')
p1 + p2If \(A\) transforms blue into orange and \(B\) transforms orange into green, then \(AB\) transforms blue into green.
See activity!
The easiest way to transform down is to use a lower-dimension transformation matrix.
Consider 3-dimensional row vector \(u = (-1, 3, 2)\) and \(3 \times 2\) transformation matrix \(A = \begin{pmatrix}3 & 2 \\ 1 & 1 \\ -1 & 4\end{pmatrix}\)
\[ u A = (-1, 3, 2)\begin{pmatrix}3 & 2 \\ 1 & 1 \\ -1 & 4\end{pmatrix}\]
\[= (-3+3-2 , -2+3+8)\]
\[ = (-2, 9)\]
Thus we have transformed the 3-dimensional \(u\) into 2-dimensional space.
\[A = \begin{pmatrix} 1 & 4 & 2 \\ 2 & 5 & 1 \\ 3 & 6 & 0 \end{pmatrix}\]
\[u A = (-1, 3, 2)\begin{pmatrix} 1 & 4 & 2 \\ 2 & 5 & 1 \\ 3 & 6 & 0 \end{pmatrix}\]
\[ = (11, 23, 1)\]
…so it looks like we’ve stayed in 3 dimensions!
But let’s see what happens if we do this to several \(u\)’s:
[,1] [,2] [,3]
[1,] 0.6550844 0.93924628 0.6369416
[2,] 0.5774114 0.07323043 0.8723149
[3,] 0.3938457 0.61220768 0.8475611
[4,] 0.4048126 0.52145253 0.7292520
[5,] 0.5263594 0.41795632 0.5186670
[6,] 0.1073119 0.74717823 0.7474164
[,1] [,2] [,3]
[1,] 4.444402 11.138218 2.249415
[2,] 3.340817 7.909687 1.228053
[3,] 4.160944 9.721788 1.399899
[4,] 3.635474 8.602025 1.331078
[5,] 2.918273 7.307222 1.470675
[6,] 3.843918 8.649637 0.961802
\(U\) is truly 3D:
\(UA\) is collapsed to a 2D plane!
\[A =\begin{pmatrix} 1 & 4 & 2 \\ 2 & 5 & 1 \\ 3 & 6 & 0 \end{pmatrix}\]
\[= \begin{pmatrix} 1 & 4 & 4-2\cdot 1 \\ 2 & 5 & 5-2\cdot2 \\ 3 & 6 & 6-2\cdot 3 \end{pmatrix}\]
So the 3rd column is a linear combination of the first two!
\[det \begin{pmatrix} a & b \\ c & d \end{pmatrix} =ad-bc\] - …increasingly more complex for higher \(p\)! (use R in general)
Consider the \(n=50\), \(p=2\)-dimensional vectors in the cars data set:
Let \(T\) be a transformation matrix represented by:
\[T = \begin{pmatrix} 1 & 0 \\ 2 & -3 \end{pmatrix}\]
Does this matrix produce a \(2 \rightarrow 2\) or a \(2 \rightarrow 1\) transformation?
Find the determinant to answer this question!
Let \(W\) be a transformation matrix represented by:
\[W = \begin{pmatrix} 1 & 3 \\ 2 & 6 \end{pmatrix}\]
Does this matrix produce a \(2 \rightarrow 2\) or a \(2 \rightarrow 1\) transformation? Answer in 2 ways: