Time Series Forecasting in Python

Book description

Build predictive models from time-based patterns in your data. Master statistical models including new deep learning approaches for time series forecasting.

In Time Series Forecasting in Python you will learn how to:

  • Recognize a time series forecasting problem and build a performant predictive model
  • Create univariate forecasting models that account for seasonal effects and external variables
  • Build multivariate forecasting models to predict many time series at once
  • Leverage large datasets by using deep learning for forecasting time series
  • Automate the forecasting process

Time Series Forecasting in Python teaches you to build powerful predictive models from time-based data. Every model you create is relevant, useful, and easy to implement with Python. You’ll explore interesting real-world datasets like Google’s daily stock price and economic data for the USA, quickly progressing from the basics to developing large-scale models that use deep learning tools like TensorFlow.

About the Technology
You can predict the future—with a little help from Python, deep learning, and time series data! Time series forecasting is a technique for modeling time-centric data to identify upcoming events. New Python libraries and powerful deep learning tools make accurate time series forecasts easier than ever before.

About the Book
Time Series Forecasting in Python teaches you how to get immediate, meaningful predictions from time-based data such as logs, customer analytics, and other event streams. In this accessible book, you’ll learn statistical and deep learning methods for time series forecasting, fully demonstrated with annotated Python code. Develop your skills with projects like predicting the future volume of drug prescriptions, and you’ll soon be ready to build your own accurate, insightful forecasts.

What's Inside
  • Create models for seasonal effects and external variables
  • Multivariate forecasting models to predict multiple time series
  • Deep learning for large datasets
  • Automate the forecasting process


About the Reader
For data scientists familiar with Python and TensorFlow.

About the Author
Marco Peixeiro is a seasoned data science instructor who has worked as a data scientist for one of Canada’s largest banks.

Quotes
The importance of time series analysis cannot be overstated. This book provides key techniques to deal with time series data in real-world applications. Indispensable.
- Amaresh Rajasekharan, IBM

Marco Peixeiro presents concepts clearly using interesting examples and illustrative plots. You’ll be up and running quickly using the power of Python.
- Ariel Andres, MD Financial Management

What caught my attention were the practical examples immediately applicable to real life. He explains complex topics without the excess of mathematical formalism.
- Simone Sguazza, University of Applied Sciences and Arts of Southern Switzerland

Table of contents

  1. inside front cover
  2. Time Series Forecasting in Python
  3. Copyright
  4. dedication
  5. contents
  6. front matter
    1. preface
    2. acknowledgments
    3. about this book
      1. Who should read this book?
      2. How this book is organized: A roadmap
      3. About the code
      4. liveBook discussion forum
      5. Author online
    4. about the author
    5. about the cover illustration
  7. Part 1. Time waits for no one
  8. 1 Understanding time series forecasting
    1. 1.1 Introducing time series
      1. 1.1.1 Components of a time series
    2. 1.2 Bird’s-eye view of time series forecasting
      1. 1.2.1 Setting a goal
      2. 1.2.2 Determining what must be forecast to achieve your goal
      3. 1.2.3 Setting the horizon of the forecast
      4. 1.2.4 Gathering the data
      5. 1.2.5 Developing a forecasting model
      6. 1.2.6 Deploying to production
      7. 1.2.7 Monitoring
      8. 1.2.8 Collecting new data
    3. 1.3 How time series forecasting is different from other regression tasks
      1. 1.3.1 Time series have an order
      2. 1.3.2 Time series sometimes do not have features
    4. 1.4 Next steps
    5. Summary
  9. 2 A naive prediction of the future
    1. 2.1 Defining a baseline model
    2. 2.2 Forecasting the historical mean
      1. 2.2.1 Setup for baseline implementations
      2. 2.2.2 Implementing the historical mean baseline
    3. 2.3 Forecasting last year’s mean
    4. 2.4 Predicting using the last known value
    5. 2.5 Implementing the naive seasonal forecast
    6. 2.6 Next steps
    7. Summary
  10. 3 Going on a random walk
    1. 3.1 The random walk process
      1. 3.1.1 Simulating a random walk process
    2. 3.2 Identifying a random walk
      1. 3.2.1 Stationarity
      2. 3.2.2 Testing for stationarity
      3. 3.2.3 The autocorrelation function
      4. 3.2.4 Putting it all together
      5. 3.2.5 Is GOOGL a random walk?
    3. 3.3 Forecasting a random walk
      1. 3.3.1 Forecasting on a long horizon
      2. 3.3.2 Forecasting the next timestep
    4. 3.4 Next steps
    5. 3.5 Exercises
      1. 3.5.1 Simulate and forecast a random walk
      2. 3.5.2 Forecast the daily closing price of GOOGL
      3. 3.5.3 Forecast the daily closing price of a stock of your choice
    6. Summary
  11. Part 2. Forecasting with statistical models
  12. 4 Modeling a moving average process
    1. 4.1 Defining a moving average process
      1. 4.1.1 Identifying the order of a moving average process
    2. 4.2 Forecasting a moving average process
    3. 4.3 Next steps
    4. 4.4 Exercises
      1. 4.4.1 Simulate an MA(2) process and make forecasts
      2. 4.4.2 Simulate an MA(q) process and make forecasts
    5. Summary
  13. 5 Modeling an autoregressive process
    1. 5.1 Predicting the average weekly foot traffic in a retail store
    2. 5.2 Defining the autoregressive process
    3. 5.3 Finding the order of a stationary autoregressive process
      1. 5.3.1 The partial autocorrelation function (PACF)
    4. 5.4 Forecasting an autoregressive process
    5. 5.5 Next steps
    6. 5.6 Exercises
      1. 5.6.1 Simulate an AR(2) process and make forecasts
      2. 5.6.2 Simulate an AR(p) process and make forecasts
    7. Summary
  14. 6 Modeling complex time series
    1. 6.1 Forecasting bandwidth usage for data centers
    2. 6.2 Examining the autoregressive moving average process
    3. 6.3 Identifying a stationary ARMA process
    4. 6.4 Devising a general modeling procedure
      1. 6.4.1 Understanding the Akaike information criterion (AIC)
      2. 6.4.2 Selecting a model using the AIC
      3. 6.4.3 Understanding residual analysis
      4. 6.4.4 Performing residual analysis
    5. 6.5 Applying the general modeling procedure
    6. 6.6 Forecasting bandwidth usage
    7. 6.7 Next steps
    8. 6.8 Exercises
      1. 6.8.1 Make predictions on the simulated ARMA(1,1) process
      2. 6.8.2 Simulate an ARMA(2,2) process and make forecasts
    9. Summary
  15. 7 Forecasting non-stationary time series
    1. 7.1 Defining the autoregressive integrated moving average model
    2. 7.2 Modifying the general modeling procedure to account for non-stationary series
    3. 7.3 Forecasting a non-stationary times series
    4. 7.4 Next steps
    5. 7.5 Exercises
      1. 7.5.1 Apply the ARIMA(p,d,q) model on the datasets from chapters 4, 5, and 6
    6. Summary
  16. 8 Accounting for seasonality
    1. 8.1 Examining the SARIMA(p,d,q)(P,D,Q)m model
    2. 8.2 Identifying seasonal patterns in a time series
    3. 8.3 Forecasting the number of monthly air passengers
      1. 8.3.1 Forecasting with an ARIMA(p,d,q) model
      2. 8.3.2 Forecasting with a SARIMA(p,d,q)(P,D,Q)m model
      3. 8.3.3 Comparing the performance of each forecasting method
    4. 8.4 Next steps
    5. 8.5 Exercises
      1. 8.5.1 Apply the SARIMA(p,d,q)(P,D,Q)m model on the Johnson & Johnson dataset
    6. Summary
  17. 9 Adding external variables to our model
    1. 9.1 Examining the SARIMAX model
      1. 9.1.1 Exploring the exogenous variables of the US macroeconomics dataset
      2. 9.1.2 Caveat for using SARIMAX
    2. 9.2 Forecasting the real GDP using the SARIMAX model
    3. 9.3 Next steps
    4. 9.4 Exercises
      1. 9.4.1 Use all exogenous variables in a SARIMAX model to predict the real GDP
    5. Summary
  18. 10 Forecasting multiple time series
    1. 10.1 Examining the VAR model
    2. 10.2 Designing a modeling procedure for the VAR(p) model
      1. 10.2.1 Exploring the Granger causality test
    3. 10.3 Forecasting real disposable income and real consumption
    4. 10.4 Next steps
    5. 10.5 Exercises
      1. 10.5.1 Use a VARMA model to predict realdpi and realcons
      2. 10.5.2 Use a VARMAX model to predict realdpi and realcons
    6. Summary
  19. 11 Capstone: Forecasting the number of antidiabetic drug prescriptions in Australia
    1. 11.1 Importing the required libraries and loading the data
    2. 11.2 Visualizing the series and its components
    3. 11.3 Modeling the data
      1. 11.3.1 Performing model selection
      2. 11.3.2 Conducting residual analysis
    4. 11.4 Forecasting and evaluating the model’s performance
    5. Next steps
  20. Part 3. Large-scale forecasting with deep learning
  21. 12 Introducing deep learning for time series forecasting
    1. 12.1 When to use deep learning for time series forecasting
    2. 12.2 Exploring the different types of deep learning models
    3. 12.3 Getting ready to apply deep learning for forecasting
      1. 12.3.1 Performing data exploration
      2. 12.3.2 Feature engineering and data splitting
    4. 12.4 Next steps
    5. 12.5 Exercise
    6. Summary
  22. 13 Data windowing and creating baselines for deep learning
    1. 13.1 Creating windows of data
      1. 13.1.1 Exploring how deep learning models are trained for time series forecasting
      2. 13.1.2 Implementing the DataWindow class
    2. 13.2 Applying baseline models
      1. 13.2.1 Single-step baseline model
      2. 13.2.2 Multi-step baseline models
      3. 13.2.3 Multi-output baseline model
    3. 13.3 Next steps
    4. 13.4 Exercises
    5. Summary
  23. 14 Baby steps with deep learning
    1. 14.1 Implementing a linear model
      1. 14.1.1 Implementing a single-step linear model
      2. 14.1.2 Implementing a multi-step linear model
      3. 14.1.3 Implementing a multi-output linear model
    2. 14.2 Implementing a deep neural network
      1. 14.2.1 Implementing a deep neural network as a single-step model
      2. 14.2.2 Implementing a deep neural network as a multi-step model
      3. 14.2.3 Implementing a deep neural network as a multi-output model
    3. 14.3 Next steps
    4. 14.4 Exercises
    5. Summary
  24. 15 Remembering the past with LSTM
    1. 15.1 Exploring the recurrent neural network (RNN)
    2. 15.2 Examining the LSTM architecture
      1. 15.2.1 The forget gate
      2. 15.2.2 The input gate
      3. 15.2.3 The output gate
    3. 15.3 Implementing the LSTM architecture
      1. 15.3.1 Implementing an LSTM as a single-step model
      2. 15.3.2 Implementing an LSTM as a multi-step model
      3. 15.3.3 Implementing an LSTM as a multi-output model
    4. 15.4 Next steps
    5. 15.5 Exercises
    6. Summary
  25. 16 Filtering a time series with CNN
    1. 16.1 Examining the convolutional neural network (CNN)
    2. 16.2 Implementing a CNN
      1. 16.2.1 Implementing a CNN as a single-step model
      2. 16.2.2 Implementing a CNN as a multi-step model
      3. 16.2.3 Implementing a CNN as a multi-output model
    3. 16.3 Next steps
    4. 16.4 Exercises
    5. Summary
  26. 17 Using predictions to make more predictions
    1. 17.1 Examining the ARLSTM architecture
    2. 17.2 Building an autoregressive LSTM model
    3. 17.3 Next steps
    4. 17.4 Exercises
    5. Summary
  27. 18 Capstone: Forecasting the electric power consumption of a household
    1. 18.1 Understanding the capstone project
      1. 18.1.1 Objective of this capstone project
    2. 18.2 Data wrangling and preprocessing
      1. 18.2.1 Dealing with missing data
      2. 18.2.2 Data conversion
      3. 18.2.3 Data resampling
    3. 18.3 Feature engineering
      1. 18.3.1 Removing unnecessary columns
      2. 18.3.2 Identifying the seasonal period
      3. 18.3.3 Splitting and scaling the data
    4. 18.4 Preparing for modeling with deep learning
      1. 18.4.1 Initial setup
      2. 18.4.2 Defining the DataWindow class
      3. 18.4.3 Utility function to train our models
    5. 18.5 Modeling with deep learning
      1. 18.5.1 Baseline models
      2. 18.5.2 Linear model
      3. 18.5.3 Deep neural network
      4. 18.5.4 Long short-term memory (LSTM) model
      5. 18.5.5 Convolutional neural network (CNN)
      6. 18.5.6 Combining a CNN with an LSTM
      7. 18.5.7 The autoregressive LSTM model
      8. 18.5.8 Selecting the best model
    6. 18.6 Next steps
  28. Part 4. Automating forecasting at scale
  29. 19 Automating time series forecasting with Prophet
    1. 19.1 Overview of the automated forecasting libraries
    2. 19.2 Exploring Prophet
    3. 19.3 Basic forecasting with Prophet
    4. 19.4 Exploring Prophet’s advanced functionality
      1. 19.4.1 Visualization capabilities
      2. 19.4.2 Cross-validation and performance metrics
      3. 19.4.3 Hyperparameter tuning
    5. 19.5 Implementing a robust forecasting process with Prophet
      1. 19.5.1 Forecasting project: Predicting the popularity of “chocolate” searches on Google
      2. 19.5.2 Experiment: Can SARIMA do better?
    6. 19.6 Next steps
    7. 19.7 Exercises
      1. 19.7.1 Forecast the number of air passengers
      2. 19.7.2 Forecast the volume of antidiabetic drug prescriptions
      3. 19.7.3 Forecast the popularity of a keyword on Google Trends
    8. Summary
  30. 20 Capstone: Forecasting the monthly average retail price of steak in Canada
    1. 20.1 Understanding the capstone project
      1. 20.1.1 Objective of the capstone project
    2. 20.2 Data preprocessing and visualization
    3. 20.3 Modeling with Prophet
    4. 20.4 Optional: Develop a SARIMA model
    5. 20.5 Next steps
  31. 21 Going above and beyond
    1. 21.1 Summarizing what you’ve learned
      1. 21.1.1 Statistical methods for forecasting
      2. 21.1.2 Deep learning methods for forecasting
      3. 21.1.3 Automating the forecasting process
    2. 21.2 What if forecasting does not work?
    3. 21.3 Other applications of time series data
    4. 21.4 Keep practicing
  32. Appendix. Installation instructions
    1. Installing Anaconda
    2. Python
    3. Jupyter Notebooks
    4. GitHub Repository
    5. Installing Prophet
    6. Installing libraries in Anaconda
  33. index
  34. inside back cover

Product information

  • Title: Time Series Forecasting in Python
  • Author(s): Marco Peixeiro
  • Release date: October 2022
  • Publisher(s): Manning Publications
  • ISBN: 9781617299889