(GP-MaunaLoa)=

Gaussian Process for CO2 at Mauna Loa

:::{post} April, 2022 :tags: gaussian process, CO2 :category: intermediate :author: Bill Engels, Chris Fonnesbeck :::

This Gaussian Process (GP) example shows how to:

  • Design combinations of covariance functions
  • Use additive GPs whose individual components can be used for prediction
  • Perform maximum a-posteriori (MAP) estimation

Since the late 1950's, the Mauna Loa observatory has been taking regular measurements of atmospheric CO$_2$. In the late 1950's Charles Keeling invented a accurate way to measure atmospheric CO$_2$ concentration. Since then, CO$_2$ measurements have been recorded nearly continuously at the Mauna Loa observatory. Check out last hours measurement result here.

Not much was known about how fossil fuel burning influences the climate in the late 1950s. The first couple years of data collection showed that CO$_2$ levels rose and fell following summer and winter, tracking the growth and decay of vegetation in the northern hemisphere. As multiple years passed, the steady upward trend increasingly grew into focus. With over 70 years of collected data, the Keeling curve is one of the most important climate indicators.

The history behind these measurements and their influence on climatology today and other interesting reading:

Let's load in the data, tidy it up, and have a look. The raw data set is located here. This notebook uses the Bokeh package for plots that benefit from interactivity.

Preparing the data

:::{include} ../extra_installs.md :::

This data might be familiar to you, since it was used as an example in the Gaussian Processes for Machine Learning book by {cite:t}rasmussen2003gaussian. The version of the data set they use starts in the late 1950's, but stops at the end of 2003. So that our PyMC example is somewhat comparable to their example, we use the stretch of data from before 2004 as the "training" set. The data from 2004 to 2022 we'll use to test our predictions.

Bokeh plots are interactive, so panning and zooming can be done with the sidebar on the right hand side. The seasonal rise and fall is plainly apparent, as is the upward trend. Here is a link to an plots of this curve at different time scales, and in the context of historical ice core data.

The 400 ppm level is highlighted with a dashed line. In addition to fitting a descriptive model, our goal will be to predict the first month the 400 ppm threshold is crossed, which was May, 2013. In the data set above, the CO$_2$ average reading for May, 2013 was about 399.98, close enough to be our correct target date.

Modeling the Keeling Curve using GPs

As a starting point, we use the GP model described in {cite:t}rasmussen2003gaussian. Instead of using flat priors on covariance function hyperparameters and then maximizing the marginal likelihood like is done in the textbook, we place somewhat informative priors on the hyperparameters and use optimization to find the MAP point. We use the gp.Marginal since Gaussian noise is assumed.

The R&W {cite:p}rasmussen2003gaussian model is a sum of three GPs for the signal, and one GP for the noise.

  1. A long term smooth rising trend represented by an exponentiated quadratic kernel.
  2. A periodic term that decays away from exact periodicity. This is represented by the product of a Periodic and a Matern52 covariance functions.
  3. Small and medium term irregularities with a rational quadratic kernel.
  4. The noise is modeled as the sum of a Matern32 and a white noise kernel.

The prior on CO$_2$ as a function of time is,

f(t)GPslow(0,k1(t,t))+GPmed(0,k2(t,t))+GPper(0,k3(t,t))+GPnoise(0,k4(t,t))f(t) \sim \mathcal{GP}_{\text{slow}}(0,\, k_1(t, t')) + \mathcal{GP}_{\text{med}}(0,\, k_2(t, t')) + \mathcal{GP}_{\text{per}}(0,\, k_3(t, t')) + \mathcal{GP}_{\text{noise}}(0,\, k_4(t, t'))

Hyperparameter priors

We use fairly uninformative priors for the scale hyperparameters of the covariance functions, and informative Gamma parameters for lengthscales. The PDFs used for the lengthscale priors is shown below:

  • ℓ_pdecay: The periodic decay. The smaller this parameter is, the faster the periodicity goes away. I doubt that the seasonality of the CO$_2$ will be going away any time soon (hopefully), and there's no evidence for that in the data. Most of the prior mass is from 60 to >140 years.

  • ℓ_psmooth: The smoothness of the periodic component. It controls how "sinusoidal" the periodicity is. The plot of the data shows that seasonality is not an exact sine wave, but its not terribly different from one. We use a Gamma whose mode is at one, and doesn't have too large of a variance, with most of the prior mass from around 0.5 and 2.

  • period: The period. We put a very strong prior on $p$, the period that is centered at one. R&W fix $p=1$, since the period is annual.

  • ℓ_med: This is the lengthscale for the short to medium long variations. This prior has most of its mass below 6 years.

  • α: This is the shape parameter. This prior is centered at 3, since we're expecting there to be some more variation than could be explained by an exponentiated quadratic.

  • ℓ_trend: The lengthscale of the long term trend. It has a wide prior with mass on a decade scale. Most of the mass is between 10 to 60 years.

  • ℓ_noise: The lengthscale of the noise covariance. This noise should be very rapid, in the scale of several months to at most a year or two.

We know beforehand which GP components should have a larger magnitude, so we include this information in the scale parameters.

For all of the scale priors we use distributions that shrink the scale towards zero. The seasonal component and the long term trend have the least mass near zero, since they are the largest influences in the data.

  • η_per: Scale of the periodic or seasonal component.
  • η_med: Scale of the short to medium term component.
  • η_trend: Scale of the long term trend.
  • σ: Scale of the white noise.
  • η_noise: Scale of correlated, short term noise.

The model in PyMC

Below is the actual model. Each of the three component GPs is constructed separately. Since we are doing MAP, we use Marginal GPs and lastly call the .marginal_likelihood method to specify the marginal posterior.

At first glance the results look reasonable. The lengthscale that determines how fast the seasonality varies is about 126 years. This means that given the data, we wouldn't expect such strong periodicity to vanish until centuries have passed. The trend lengthscale is also long, about 50 years.

Examining the fit of each of the additive GP components

The code below looks at the fit of the total GP, and each component individually. The total fit and its $2\sigma$ uncertainty are shown in red.

The fit matches the observed data very well. The trend, seasonality, and short/medium term effects also are cleanly separated out. If you zoom so the seasonal process fills the plot window (from x equals 1955 to 2004, from y equals 310 to 320), it appears to be widening as time goes on. Lets plot the first year of each decade:

This plot makes it clear that there is a broadening over time. So it would seem that as there is more CO$_2$ in the atmosphere, the absorption/release cycle due to the growth and decay of vegetation in the northern hemisphere becomes more slightly more pronounced.

What day will the CO2 level break 400 ppm?

How well do our forecasts look? Clearly the observed data trends up and the seasonal effect is very pronounced. Does our GP model capture this well enough to make reasonable extrapolations? Our "training" set went up until the end of 2003, so we are going to predict from January 2004 out to the end of 2022.

Although there isn't any particular significance to this event other than it being a nice round number, our side goal was to see how well we could predict the date when the 400 ppm mark is first crossed. This event first occurred during May, 2013 and there were a few news articles about other significant milestones.

The mean prediction and the $2\sigma$ uncertainty is in red. A couple samples from the marginal posterior are also shown on there. It looks like our model was a little optimistic about how much CO2 is being released. The first time the $2\sigma$ uncertainty crosses the 400 ppm threshold is in May 2015, two years late.

One reason this is occurring is because our GP prior had zero mean. This means we encoded prior information that says that the function should go to zero as we move away from our observed data. This assumption probably isn't justified. It's also possible that the CO$_2$ trend is increasing faster than linearly -- important knowledge for accurate predictions. Another possibility is the MAP estimate. Without looking at the full posterior, the uncertainty in our estimates is underestimated. How badly is unknown.

Having a zero mean GP prior is causing the prediction to be pretty far off. Some possibilities for fixing this is to use a constant mean function, whose value could maybe be assigned the historical, or pre-industrial revolution, CO$_2$ average. This may not be the best indicator for future CO$_2$ levels though.

Also, using only historical CO$_2$ data may not be the best predictor. In addition to looking at the underlying behavior of what determines CO$_2$ levels using a GP fit, we could also incorporate other information, such as the amount of CO$_2$ that is released by fossil fuel burning.

Next, we'll see about using PyMC's GP functionality to improve the model, look at full posteriors, and incorporate other sources of data on drivers of CO$_2$ levels.

Authors

  • Authored by Bill Engels in September, 2017 (pymc#2444)
  • Updated by Chris Fonnesbeck in December, 2020
  • Re-executed by Danh Phan in May, 2022 (pymc-examples#316)
  • Updated to PyMC 5 by Chris Fonnesbeck in April, 2025

References

:::{bibliography} :filter: docname in docnames :::

:::{include} ../page_footer.md :::