Case Study: Breaking Records

How often should you expect to have a truly record breaking year? Suppose you have 100 years of weather data indicating how much precipitation fell in each year. How many records to you expect to be set for wettest or driest years on record? For example, according to NOAA, St. Louis' official driest year had just 20.7 inches of precipitation in 1953, and our wettest year had 61.2 inches in 2015. On average, we get about 43.4 inches of precipitation in a year.

SLU precipitation data

In this class we will simulate weather data and compute how many record highs and lows are seen. To do this, we build upon our exercise from last time, the peak-finding function.

Finding peaks

Given a vector, we have discussed the basic algorithm for fiding the maximum, by keep track of the maximum thus far while iterating through the vector.

Question: how many different elements qualify as being the maximum thus far during that process?

Goal: Develop a function peaks(v) that returns the number of elements that have value surpassing all previous elements in the vector. For example, peaks([5 1 8 3 8 11 10]) should return 3, as the first element, the third element and the sixth elements are new maximums (we do not count the fifth, because that value 8 did not surpass the previously visited maximum of 8).

Goal: Rewrite the function so that it can provide a second return value, which is a vector of indices achieving those peaks (akin to the second return value of the built-in max function). Thus peaks([5 1 8 3 8 11 10]) should return the pair [ 3 [1 3 6] ].


Originally by Michael Goldwasser