EDS 212: Day 3, Lecture 2

Vectors and matrices


A rates of change example

Cella Schanabel, Bren PhD Student

Differences in green-up rates in NE Kenya


Greenness is a proxy for rangeland health. Bute is both greener than Hadado overall, and has a more rapid and more sustained green-up. A change in rate of change of greenness was the management signal we were hoping for.

Find out more!
cellaschnabel@ucsb.edu

Summation Notation

Summation notation


✏️

Summation notation


Summation notation is a compact way to write the sum of a sequence of values.

The idea is that, instead of writing long sums like

\[ x_1 + x_2 + \cdots + x_n\] we can write the same in a more compact form:

\[\large\sum_{i=1}^{n} x_i.\]

This is read as the sum of \(x_i\) from i equals one to \(n\).

There’s a few components to this notation:

  • \(\Sigma\): the Greek letter sigma indicates “sum up everything that follows”
  • \(i\): the index — a counter that steps through values
  • \(i = 1\): the lower bound — where the index starts
  • \(n\): the upper bound — where the index stops (inclusive)
  • \(x_i\): the expression evaluated and added for each \(i\)

Examples


Example 1: Evaluate \(\displaystyle\sum_{i=1}^{5} i^2\).

✏️





Example 2: Evaluate \(\displaystyle\sum_{n=1}^{3} (2n + 1)\).

✏️

Examples


Example 1: Evaluate \(\displaystyle\sum_{i=1}^{5} i^2\).

\[\sum_{i=1}^{5} i^2 = 1^2 + 2^2 + 3^2 + 4^2 + 5^2 = 1 + 4 + 9 + 16 + 25 = 55\]


Example 2: Evaluate \(\displaystyle\sum_{n=1}^{3} (2n + 1)\).

\[\sum_{n=1}^{3} (2n+1) = \bigl(2(1)+1\bigr)+\bigl(2(2)+1\bigr)+\bigl(2(3)+1\bigr) = 3 + 5 + 7 = 15\]

Exercises


✏️ Try these out individually first.

1) Expand and evaluate: \(\displaystyle\sum_{i=3}^{6} 3i\)

2) Expand and evaluate: \(\displaystyle\sum_{i=2}^{5} (i^2 - 1)\)

3) Complete the following using sigma notation:

\[x_1 + x_2^2 + x_3^3 + \cdots + x_n^n = \sum\]

4) A climate model predicts daily high temperatures \(\hat{y} = [22, 25, 20]\) °C. Observed values are \(y = [24, 23, 21]\) °C. Calculate the mean squared error (MSE) using the formula:

\[\text{MSE} = \frac{1}{n}\sum_{i=1}^{n}(\hat{y}_i - y_i)^2.\]

Solutions


✏️

1) Expand and evaluate: \(\displaystyle\sum_{i=3}^{6} 3i\)




2) Expand and evaluate: \(\displaystyle\sum_{i=2}^{5} (i^2 - 1)\)




3) Complete the following using sigma notation:

\[x_1 + x_2^2 + x_3^3 + \cdots + x_n^n = \sum\]

Solutions


✏️

4) A climate model predicts daily high temperatures \(\hat{y} = [22, 25, 20]\) °C. Observed values are \(y = [24, 23, 21]\) °C. Calculate the mean squared error (MSE) using the formula:

\[\text{MSE} = \frac{1}{n}\sum_{i=1}^{n}(\hat{y}_i - y_i)^2.\]

Solutions


1)

\[\begin{align} \sum_{i=3}^6 3i &= (3\cdot 3) + (3\cdot 4) + (3 \cdot 5) + (3 \cdot 6) \\ &= 3(3 + 4 + 5 +6) \\ &= 54 \end{align} \]

2)

\[ \begin{align} \sum_{i=2}^{5}(i^2-1) &= (4-1)+(9-1)+(16-1)+(25-1)\\ &= 3+8+15+24\\ &= 50 \end{align} \]

3)

\[x_1 + x_2^2 + \cdots + x_n^n = \sum_{i=1}^{n} x_i^i\]

4) The formula is \(\text{MSE} = \frac{1}{n}\sum_{i=1}^{n}(\hat{y}_i - y_i)^2\). Compute each squared error first:

\[ \begin{align} (\hat{y}_1 - y_1)^2 &= (22-24)^2 = 4\\ (\hat{y}_2 - y_2)^2 &= (25-23)^2 = 4\\ (\hat{y}_3 - y_3)^2 &= (20-21)^2 = 1 \end{align} \]

Then apply the formula:

\[MSE = \frac{4+4+1}{3} = 3 \text{ °C}\]

Vectors and matrices

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.

The building blocks of linear algebra


✏️

The building blocks of linear algebra


  • scalar: a value without direction, representing magnitude. For our purposes, a number:

\[8 \text{ or } \lambda.\]

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

\[(1,3,5) \text{ or } (x_1, x_2, x_3).\]

  • matrix: am array of values made up rows and columns. For example,
\[\begin{pmatrix} 1 & 2 & 3\\ 10 & 20 & 30 \end{pmatrix}\] \[\begin{pmatrix} a_{11} & a_{12} & a_{13}\\ a_{21} & a_{22} & a_{23} \end{pmatrix}\]

Applications of linear algebra in environmental sciences



  • Dimensional reduction
  • Population matrix models
  • Optimization
  • Array programming / vectorized code
  • Machine learning

Let’s start with vectors


Where are vectors in EDS?

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:

\[(32, 281)\]

Sometimes vectors are represented with an arrow over the vector name:

\[ \vec{x} = (32, 281)\]

✏️

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:

\[(32, 281)\]

Sometimes vectors are represented with an arrow over the vector name:

\[ \vec{x} = (32, 281)\]

Often, we will think of an “abstract vector” \(x\) with \(n\) coordinates:

\[x = (x_1, x_2, \ldots, x_n).\]

Vector addition & subtraction


Just add or subtract the corresponding coordinates.

✏️

Vector addition & subtraction


Just add or subtract the corresponding coordinates.


If: \(u = (1, 2)\) and \(v = (3, -1)\), then:

\[u + v = (1+3, 2-1) = (4, 1)\]


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.

✏️

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: \(u = (1,2)\)

\[w = 3u = (3*1, 3*2) = (3, 6)\]


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

Vectors with > 3 coordinates


What about a vector with more than two coordinates? More than three?

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


✏️

Dot product


For vectors

\[x = (x_1, \ldots, x_n) \text{ and } y = (y_1, \ldots, y_n)\]

their dot product is:

\[x \cdot y = \sum_{i=1}^n x_i y_i\]

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

Exercise


For vectors \(x = (x_1, \ldots, x_n)\) \(y = (y_1, \ldots, y_n)\) their dot product is:

\[x \cdot y = \sum_{i=1}^n x_i y_i\]

Find the dot product of \(a=(2,-1,0)\) and \(b= (9,3,-4)\):

✏️

Exercise


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


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


What happens when we have orthogonal vectors?



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


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


2. \(x=(-3,1)\) and \(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!

Let’s take a 5 minute break


image: Flaticon.com

Matrices

Matrices


A matrix is an array of values (has rows and columns). For example:

\[ A = \begin{pmatrix} a_{11} & a_{12} & a_{13}\\ a_{21} & a_{22} & a_{23} \end{pmatrix} \]


  • Dimensions: the size of the matrix, in rows x columns (m x n)
  • Elements: values in a matrix, often denoted symbolically with a subscript where the first number is the row and the second number is the column (e.g. \(a_{23}\) indicates the element in row 2, column 3)

Matrix algebra (add / subtract)



Add or subtract the corresponding elements (by matrix position) to create a new matrix of the same dimensions.

✏️

Matrix algebra (add / subtract)



Add or subtract the corresponding elements (by matrix position) to create a new matrix of the same dimensions.


Scalar multiplication



To multiply a matrix by a scalar, multiply each element in the matrix by the scalar to get a scaled matrix of the same dimensions.


For example:

✏️

Scalar multiplication



To multiply a matrix by a scalar, multiply each element in the matrix by the scalar to get a scaled matrix of the same dimensions.


For example:

Recall: dot product



The dot product of two vectors is the sum of their elements multiplied:


For \(u =(1,5)\) and \(v = (2,-3)\):


\[u \cdot v=(1)(2)+(5)(-3)=-13\]

Matrix multiplication



We find the dot product of row \(\cdot\) column vectors:

✏️

Matrix multiplication



We find the dot product of row \(\cdot\) column vectors:

Practice problems


✏️ Find the product of these two matrices


Matrices with unequal dimensions



What would be the dimensions of the product if you were multiplying the following?


Let’s try one!



Diagonal matrix



A diagonal matrix is (almost always) a square matrix (\(m\) = \(n\)) where only elements on the diagonal are non-zero values.


What happens when we multiply a matrix by a diagonal matrix?



A diagonal matrix is also called a scaling matrix because it scales rows proportionally, but not by the same value:

✏️

What happens when we multiply a matrix by a diagonal matrix?



A diagonal matrix is also called a scaling matrix because it scales rows proportionally, but not by the same value:


Where does this come up in EDS?


Rasters (2D arrays, addition and scalar multiplication are the same as matrices)

Source: Neon

⚠️ You can multiply two rasters coordinate by coordinate, but matrices don’t admit this operation!

Exercise: combining rasters


A raster is just a 2D array where each cell holds a value for a location on a grid. Land managers often combine several raster layers into a single index by scaling each layer (multiplying by a weight) and summing the results.

✏️ Calculate the fire risk index for each cell:

\[\text{Fire risk} = 0.6 \times \text{Temperature stress} + 0.4 \times \text{Fuel dryness}\]

Exercise: combining rasters


✏️ Calculate the fire risk index for each cell:

\[\text{Fire risk} = 0.6 \times \text{Temperature stress} + 0.4 \times \text{Fuel dryness}\]

Exercise: solution


💡 Let’s see a solution!

\[\text{Fire risk} = 0.6 \times \text{Temperature stress} + 0.4 \times \text{Fuel dryness}\]

  • Top-left: \(0.6(10)+0.4(20) = 6+8 = 14\)
  • Top-right: \(0.6(20)+0.4(10) = 12+4 = 16\)
  • Bottom-left: \(0.6(15)+0.4(25) = 9+10 = 19\)
  • Bottom-right: \(0.6(25)+0.4(15) = 15+6 = 21\)

Where do matrices come up in EDS?


Machine learning (EDS 232)

Adapted from What is the Convolutional Neural Network Architecture?

Wrapping up…

What we covered today


  • 📝 Practice all day 1 and 2 topics
  • 🌊 A bit of ODEs
  • ➕ Summation notation
  • 🔹 Vector sum and product
  • 🌓 Dot product
  • 🧮 Matrix algebra