Skip to content


Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Latest commit

Fix some minor README typos, and remove the "Desiderata" section detailing potential future improvements.

Co-authored-by: cjwallace <>

Git stats


Failed to load latest commit information.
Latest commit message
Commit time

Structural Time Series

This repo accompanies the Cloudera Fast Forward report Structural Time Series. It provides an example application of generalized additive models (via the Prophet library) to California hourly electricity demand data.

The primary output of this repository is a small application exposing a probablistic forecast and interface for asking a probabilistic question against it. The final app looks like this.

Forecasting app interface

Instructions are given both for general use (on a laptop, say), and for Cloudera CML and CDSW. We'll first describe what's here, then go through how to run everything.


The folder structure of the repo is as follows

├── apps      # Two small Streamlit applications.
├── cml       # This folder contains scripts that facilitate the project launch on CML.
├── data      # This folder contains starter data, and is where forecasts will live.
├── scripts   # This is where all the code that does something lives.
└── sts       # A small library of useful functions.

There's also an img folder that contains images for this README. That folder is unimportant and you can ignore it. Let's examine each of the important folders in turn.


This is a small Python library of utility functions useful to our problem. Its structure is as follows:

├── data
│   └──
└── models

Building a small library of problem-specific abstractions allows us to reuse them in multiple places. The code in data/, is reused in most of the scripts and applications. In this case, we have closed model details (such as the number of Fourier terms to include in a given Prophet model) into the library. It would be trivial to pass these through as arguments though, if we wanted to perform an extensive hyperparameter search for example.


These imperative scripts are where the work of the analysis is done. Side-effectful actions such as I/O and model training occur in these scripts.



Two applications accompany this project. Each has a launcher script to assist launching an Application with CDSW/CML. To launch the applications in another environment, run the code inside the launcher files, with the prefixed ! removed. You may need to specify different ports.

├──          # A model comparison and debugging assistant.
├──             # The primary forecasting interface.
├──   # Launcher script for CDSW/CML
└──      # Launcher script for CDSW/CML


The diagnostic application serves two purposes. First, it computes and reports top level metrics for any forecasts saved in the data/forecasts directory.

Diagnostic app showing model metrics

Second, it provides a few diagnostic charts, including a zoomable forecast.

Diagnostic app showing chart of forecast


The primary forecast application (pictured at the top of this README) is a prototype user interface for the forecast this analysis generates.


These scripts serve as launch instructions to facilitate the automated project setup on CML. Each script is triggered by the declarative pipeline as defined in the .project-metadata.yaml file found in the project's root directory.


Running through the analysis

To go from a fresh clone of the repo to the final state, follow these instructions in order.


The code and applications within were developed against Python 3.6.9, and are likely also to function with more recent versions of Python.

To install dependencies, first create and activate a new virtual environment through your preferred means, then pip install from the requirements file. I recommend:

python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

In CML or CDSW, no virtual env is necessary. Instead, inside a Python 3 session (with at least 2 vCPU / 4 GiB Memory), simply run

!pip3 install -r requirements.txt     # notice `pip3`, not `pip`

Next, install the sts module from this repository, with

pip3 install -e .

from inside the root directory of this repo.


We use historic California electricity demand data from the US Energy Information Administration.

A full set of data through October 12th 2020 is included as a starter. More recent data can be fetched from the EIA open data API. Doing so requires an API key, which must be set as the EIA_API_KEY environment variable for this project. To fetch new data, simply call the load_california_electricity_demand function from the module. The code is set up to work directly with the json response to the EIA API. By default, each time new data is fetched, it will overwrite the existing data. Similarly, when a new forecast is made, it will overwrite the existing forecast. It would not be hard to adapt the code to maintain a history of fetched data or forecasts if desired.


To fit models and generate forecasts, we call each script in turn from the scripts directory.

python3 scripts/
python3 scripts/
python3 scripts/
python3 scripts/

This will fit a series of models of increasing complexity and write their outputs (the mean forecast) to the data/forecasts directory. Launching the diagnostic app will show the metrics and diagnostic charts for each model.

The most compex model wins. We can view its metrics when trained on the validation data (through 2019) by running the scripts/ script. We can then generate 1000 samples from the model trained on all available training data with the scripts/ script. When those samples are written to disk, we can use the forecast app to investigate them.

The additional script,, simply fetches and writes data as a csv, which is convenient for any ad hoc analytics and interactive exploration.


Structural Time Series on US electricity demand data







No releases published


No packages published