EDS 212: Day 3, Lecture 2

Linear algebra fundamentals


August 7th, 2024

Linear algebra introduction



Linear algebra “branch of mathematics concerning linear equations” (Wikipedia), sometimes also described as the math of vectors & matrices.


It is a fundamental part of data science (and how computers understand & process data), and useful for describing environmental processes.


As Dr. Jason Brownlee writes, “Linear algebra is the mathematics of data.”

The building blocks of linear algebra



  • scalar: a value without direction, representing magnitude

  • vector: an ordered list of values, representing magnitude and direction (physics) or values for an observation or variable (data science)

  • matrix: a table of values

Applications of linear algebra in environmental sciences



  • Dimensional reduction
  • Population matrix models
  • Optimization


Applications of linear algebra in data science:

  • Array programming / vectorized code
  • Machine learning
  • For loops

Let’s start with vectors



Where are vectors in EDS?


Everywhere.


Vectors are lists of values used to describe different features or variables of interest. For example, if you are trying to model fish size based on length (cm) and mass (g), then for a fish with length 32 cm weighing 281 g, you might describe that by:

\[\vec{F} = [32, 281]\]

Notation



Vectors are indicated with an arrow over the vector name:


\(\vec{A} = [1,5]\)


\(\vec{AB} = [10.2, 3.1]\)

Vector addition & subtraction


Just add or subtract the corresponding pieces.


If: \(\vec{A} = [1, 2]\) and \(\vec{B} = [3, 1]\), then:


\[\vec{A} + \vec{B} = [1+3, 2+1] = [4, 3]\]


What does this look like graphically? Let’s draw it!

Scalar multipliers



You can multiply any vector by a scalar (constant). This will not change the direction of the vector - it will only change the magnitude of the vector.


Example: \(\vec{A} = [1,2]\)


\[\vec{B} = 3\vec{A} = [3*1, 3*2] = [3, 6]\]


What does this look like graphically? Let’s draw it!

In R:



We create vectors in R using the c() function (for combine or concatenate), and can perform operations on numeric vectors using basic operators.


# Create vectors A and B ----
A <- c(1,2)
B <- c(5,9)

Vector addition and subtraction in R



Just do it!


For \(\vec{A}=[1,2]\) and \(\vec{B}=[5,9]\):


# Addition ----
A + B
[1]  6 11
# Subtraction ----
A - B
[1] -4 -7

Scalar multiplication in R



Just do it!


For \(\vec{A}=[1,2]\), calculate \(4\vec{A}\):

4 * A
[1] 4 8

Vectors of > 3 elements



\(\vec{M}=c(2,4,1,8,6)\)


Is as valid as describing a “point” in multivariate space as a vector with two “coordinates” – it’s just difficult for us to visualize and conceptualize since our brain only happily deals with 3 dimensions.

Dot product



For vectors \(\vec a\) and \(\vec b\), their dot product is:


\[\vec a \cdot \vec b = \sum a_i b_i\]


In words: The dot product is the sum of elements of each vector multiplied together, and is a measure of how close the vectors “point” in the same direction

Dot product example



For \(\vec a=[2,-1,0]\) and \(\vec b=[9,3,-4]\):


\[\vec a \cdot \vec b = (2)(9)+(-1)(3)+(0)(-4) =15\]


This becomes very useful when describing systems of equations (tomorrow).

What happens when we have orthogonal vectors?



Sketch a quick graph, then find the dot product, of the following vector combinations:


  1. \(\vec a=[0,4]\) and \(\vec b =[6,0]\)


  1. \(\vec c=[-3,1]\) and \(\vec d=[2,6]\)


What is the value of the dot product for orthogonal vectors?

More on vector fundamentals:






Optional: watch 3Brown1Blue’s great 10 min recording on Vectors (Ch 1 Essense of Linear Algebra).


Vector addition & scalar multiplication are the basis of most linear algebra!

From Wickham’s Advanced R chapter on Data Structures:



“Under the hood, a data frame is a list of equal-length vectors.”

food <- c("banana", "apple", "carrot")
meal <- c("breakfast", "snack", "lunch")
food_mass_g <- c(14.8, 19.2, 11.5)

squirrel_meals <- data.frame(food, meal, food_mass_g) 
squirrel_meals # Returns the data frame
    food      meal food_mass_g
1 banana breakfast        14.8
2  apple     snack        19.2
3 carrot     lunch        11.5