Digital Library

Time Series Forecasting in Python (Final Release) (Marco Peixeiro)(Z-Library)

Marco Peixeiro

Time Series Forecasting in Python (Final Release) (Marco Peixeiro)(Z-Library)

Author Marco Peixeiro

python
Language English

Build predictive models from time-based patterns in your data. Master statistical models including new deep learning approaches for time series forecasting. 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. Time Series Forecasting in Python teaches you to apply time series forecasting and get immediate, meaningful predictions. You’ll learn both traditional statistical and new deep learning models for time series forecasting, all fully illustrated with Python source code. Test your skills with hands-on projects for forecasting air travel, volume of drug prescriptions, and the earnings of Johnson & Johnson. By the time you’re done, you’ll be ready to build accurate and insightful forecasting models with tools from the Python ecosystem.

Format PDF
Size 22.5 MB
22
Views
0
Downloads
0.00
Total Donations
(First 20 pages)

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Page 1
M A N N I N G Marco Peixeiro
Page 2
Core concepts for time series forecasting Continues on inside back cover Core concept Chapter Section Defining time series 1 1.1 Time series decomposition 1 1.1 Forecasting project lifecycle 1 1.2 Baseline models 2 2.1 Random walk model 3 3.1 Stationarity 3 3.2.1 Differencing 3 3.2.1 Autocorrelation function (ACF) 3 3.2.3 Forecasting a random walk 3 3.3 Moving average model: MA(q) 4 4.1 Reading the ACF plot 4 4.1.1 Forecasting with MA(q) 4 4.2 Autoregressive model: AR(p) 5 5.2 Partial autocorrelation function (PACF) 5 5.3.1 Forecasting with AR(p) 5 5.4 ARMA(p,q) model 6 6.2 General modeling procedure 6 6.4 Akaike Information Criterion (AIC) 6 6.4.1 Q-Q plot 6 6.4.3 Ljung-Box test 6 6.4.3 Residual analysis 6 6.4.4 Forecasting with ARMA(p,q) 6 6.6 ARIMA(p,d,q) model 7 7.1 Forecasting with ARIMA 7 7.3
Page 3
Time Series Forecasting in Python
Page 4
(This page has no text content)
Page 5
Time Series Forecasting in Python MARCO PEIXEIRO MANN I NG SHELTER ISLAND
Page 6
For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email: orders@manning.com ©2022 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. The author and publisher have made every effort to ensure that the information in this book was correct at press time. The author and publisher do not assume and hereby disclaim any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from negligence, accident, or any other cause, or from any usage of the information herein. Manning Publications Co. Development editor: Bobbie Jennings 20 Baldwin Road Technical development editor: Al Krinker PO Box 761 Review editor: Adriana Sabo Shelter Island, NY 11964 Production editor: Andy Marinkovich Copy editor: Andy Carroll Proofreader: Katie Tennant Technical proofreader: Karsten Strøbaek Typesetter: Dennis Dalinnik Cover designer: Marija Tudor ISBN: 9781617299889 Printed in the United States of America
Page 7
To my wife, my parents, and my sister, even though you will probably never read it.
Page 8
(This page has no text content)
Page 9
brief contents PART 1 TIME WAITS FOR NO ONE...............................................1 1 ■ Understanding time series forecasting 3 2 ■ A naive prediction of the future 14 3 ■ Going on a random walk 30 PART 2 FORECASTING WITH STATISTICAL MODELS ....................59 4 ■ Modeling a moving average process 61 5 ■ Modeling an autoregressive process 81 6 ■ Modeling complex time series 101 7 ■ Forecasting non-stationary time series 140 8 ■ Accounting for seasonality 156 9 ■ Adding external variables to our model 180 10 ■ Forecasting multiple time series 197 11 ■ Capstone: Forecasting the number of antidiabetic drug prescriptions in Australia 216vii
Page 10
BRIEF CONTENTSviiiPART 3 LARGE-SCALE FORECASTING WITH DEEP LEARNING ......231 12 ■ Introducing deep learning for time series forecasting 233 13 ■ Data windowing and creating baselines for deep learning 248 14 ■ Baby steps with deep learning 270 15 ■ Remembering the past with LSTM 287 16 ■ Filtering a time series with CNN 305 17 ■ Using predictions to make more predictions 320 18 ■ Capstone: Forecasting the electric power consumption of a household 329 PART 4 AUTOMATING FORECASTING AT SCALE........................359 19 ■ Automating time series forecasting with Prophet 361 20 ■ Capstone: Forecasting the monthly average retail price of steak in Canada 396 21 ■ Going above and beyond 410
Page 11
contents preface xvii acknowledgments xix about this book xx about the author xxiv about the cover illustration xxv PART 1 TIME WAITS FOR NO ONE .....................................1 1 Understanding time series forecasting 3 1.1 Introducing time series 4 Components of a time series 5 1.2 Bird’s-eye view of time series forecasting 8 Setting a goal 9 ■ Determining what must be forecast to achieve your goal 9 ■ Setting the horizon of the forecast 10 ■ Gathering the data 10 ■ Developing a forecasting model 10 ■ Deploying to production 11 ■ Monitoring 11 ■ Collecting new data 11 1.3 How time series forecasting is different from other regression tasks 12 Time series have an order 12 ■ Time series sometimes do not have features 13 1.4 Next steps 13ix
Page 12
CONTENTSx2 A naive prediction of the future 14 2.1 Defining a baseline model 16 2.2 Forecasting the historical mean 17 Setup for baseline implementations 17 ■ Implementing the historical mean baseline 19 2.3 Forecasting last year’s mean 23 2.4 Predicting using the last known value 25 2.5 Implementing the naive seasonal forecast 26 2.6 Next steps 28 3 Going on a random walk 30 3.1 The random walk process 31 Simulating a random walk process 32 3.2 Identifying a random walk 35 Stationarity 36 ■ Testing for stationarity 38 ■ The autocorrelation function 41 ■ Putting it all together 42 ■ Is GOOGL a random walk? 45 3.3 Forecasting a random walk 47 Forecasting on a long horizon 48 ■ Forecasting the next timestep 52 3.4 Next steps 55 3.5 Exercises 56 Simulate and forecast a random walk 56 ■ Forecast the daily closing price of GOOGL 57 ■ Forecast the daily closing price of a stock of your choice 57 PART 2 FORECASTING WITH STATISTICAL MODELS ..........59 4 Modeling a moving average process 61 4.1 Defining a moving average process 63 Identifying the order of a moving average process 64 4.2 Forecasting a moving average process 69 4.3 Next steps 78 4.4 Exercises 79 Simulate an MA(2) process and make forecasts 79 ■ Simulate an MA(q) process and make forecasts 80
Page 13
CONTENTS xi5 Modeling an autoregressive process 81 5.1 Predicting the average weekly foot traffic in a retail store 82 5.2 Defining the autoregressive process 84 5.3 Finding the order of a stationary autoregressive process 85 The partial autocorrelation function (PACF) 89 5.4 Forecasting an autoregressive process 92 5.5 Next steps 98 5.6 Exercises 99 Simulate an AR(2) process and make forecasts 99 ■ Simulate an AR(p) process and make forecasts 100 6 Modeling complex time series 101 6.1 Forecasting bandwidth usage for data centers 102 6.2 Examining the autoregressive moving average process 105 6.3 Identifying a stationary ARMA process 106 6.4 Devising a general modeling procedure 111 Understanding the Akaike information criterion (AIC) 113 Selecting a model using the AIC 114 ■ Understanding residual analysis 116 ■ Performing residual analysis 121 6.5 Applying the general modeling procedure 125 6.6 Forecasting bandwidth usage 132 6.7 Next steps 136 6.8 Exercises 137 Make predictions on the simulated ARMA(1,1) process 137 Simulate an ARMA(2,2) process and make forecasts 137 7 Forecasting non-stationary time series 140 7.1 Defining the autoregressive integrated moving average model 142 7.2 Modifying the general modeling procedure to account for non-stationary series 143 7.3 Forecasting a non-stationary times series 145 7.4 Next steps 154 7.5 Exercises 154 Apply the ARIMA(p,d,q) model on the datasets from chapters 4, 5, and 6 154
Page 14
CONTENTSxii8 Accounting for seasonality 156 8.1 Examining the SARIMA(p,d,q)(P,D,Q)m model 157 8.2 Identifying seasonal patterns in a time series 160 8.3 Forecasting the number of monthly air passengers 163 Forecasting with an ARIMA(p,d,q) model 165 ■ Forecasting with a SARIMA(p,d,q)(P,D,Q)m model 171 ■ Comparing the performance of each forecasting method 176 8.4 Next steps 178 8.5 Exercises 178 Apply the SARIMA(p,d,q)(P,D,Q)m model on the Johnson & Johnson dataset 178 9 Adding external variables to our model 180 9.1 Examining the SARIMAX model 182 Exploring the exogenous variables of the US macroeconomics dataset 183 ■ Caveat for using SARIMAX 185 9.2 Forecasting the real GDP using the SARIMAX model 186 9.3 Next steps 195 9.4 Exercises 195 Use all exogenous variables in a SARIMAX model to predict the real GDP 195 10 Forecasting multiple time series 197 10.1 Examining the VAR model 199 10.2 Designing a modeling procedure for the VAR(p) model 201 Exploring the Granger causality test 201 10.3 Forecasting real disposable income and real consumption 203 10.4 Next steps 214 10.5 Exercises 214 Use a VARMA model to predict realdpi and realcons 214 Use a VARMAX model to predict realdpi and realcons 215 11 Capstone: Forecasting the number of antidiabetic drug prescriptions in Australia 216 11.1 Importing the required libraries and loading the data 218 11.2 Visualizing the series and its components 219
Page 15
CONTENTS xiii11.3 Modeling the data 220 Performing model selection 222 ■ Conducting residual analysis 224 11.4 Forecasting and evaluating the model’s performance 225 11.5 Next steps 229 PART 3 LARGE-SCALE FORECASTING WITH DEEP LEARNING .......................................................231 12 Introducing deep learning for time series forecasting 233 12.1 When to use deep learning for time series forecasting 234 12.2 Exploring the different types of deep learning models 234 12.3 Getting ready to apply deep learning for forecasting 237 Performing data exploration 237 ■ Feature engineering and data splitting 241 12.4 Next steps 246 12.5 Exercise 246 13 Data windowing and creating baselines for deep learning 248 13.1 Creating windows of data 249 Exploring how deep learning models are trained for time series forecasting 249 ■ Implementing the DataWindow class 253 13.2 Applying baseline models 260 Single-step baseline model 260 ■ Multi-step baseline models 263 Multi-output baseline model 266 13.3 Next steps 268 13.4 Exercises 269 14 Baby steps with deep learning 270 14.1 Implementing a linear model 271 Implementing a single-step linear model 272 ■ Implementing a multi-step linear model 274 ■ Implementing a multi-output linear model 275
Page 16
CONTENTSxiv14.2 Implementing a deep neural network 276 Implementing a deep neural network as a single-step model 278 Implementing a deep neural network as a multi-step model 281 Implementing a deep neural network as a multi-output model 282 14.3 Next steps 284 14.4 Exercises 285 15 Remembering the past with LSTM 287 15.1 Exploring the recurrent neural network (RNN) 288 15.2 Examining the LSTM architecture 290 The forget gate 291 ■ The input gate 292 ■ The output gate 294 15.3 Implementing the LSTM architecture 295 Implementing an LSTM as a single-step model 295 Implementing an LSTM as a multi-step model 297 Implementing an LSTM as a multi-output model 299 15.4 Next steps 302 15.5 Exercises 303 16 Filtering a time series with CNN 305 16.1 Examining the convolutional neural network (CNN) 306 16.2 Implementing a CNN 309 Implementing a CNN as a single-step model 310 ■ Implementing a CNN as a multi-step model 314 ■ Implementing a CNN as a multi-output model 315 16.3 Next steps 317 16.4 Exercises 318 17 Using predictions to make more predictions 320 17.1 Examining the ARLSTM architecture 321 17.2 Building an autoregressive LSTM model 322 17.3 Next steps 327 17.4 Exercises 328 18 Capstone: Forecasting the electric power consumption of a household 329 18.1 Understanding the capstone project 330 Objective of this capstone project 331
Page 17
CONTENTS xv18.2 Data wrangling and preprocessing 333 Dealing with missing data 334 ■ Data conversion 335 Data resampling 335 18.3 Feature engineering 338 Removing unnecessary columns 338 ■ Identifying the seasonal period 339 ■ Splitting and scaling the data 341 18.4 Preparing for modeling with deep learning 342 Initial setup 342 ■ Defining the DataWindow class 343 Utility function to train our models 346 18.5 Modeling with deep learning 346 Baseline models 346 ■ Linear model 349 ■ Deep neural network 350 ■ Long short-term memory (LSTM) model 351 Convolutional neural network (CNN) 351 ■ Combining a CNN with an LSTM 354 ■ The autoregressive LSTM model 355 Selecting the best model 356 18.6 Next steps 358 PART 4 AUTOMATING FORECASTING AT SCALE..............359 19 Automating time series forecasting with Prophet 361 19.1 Overview of the automated forecasting libraries 362 19.2 Exploring Prophet 363 19.3 Basic forecasting with Prophet 365 19.4 Exploring Prophet’s advanced functionality 370 Visualization capabilities 370 ■ Cross-validation and performance metrics 374 ■ Hyperparameter tuning 379 19.5 Implementing a robust forecasting process with Prophet 381 Forecasting project: Predicting the popularity of “chocolate” searches on Google 381 ■ Experiment: Can SARIMA do better? 389 19.6 Next steps 393 19.7 Exercises 394 Forecast the number of air passengers 394 ■ Forecast the volume of antidiabetic drug prescriptions 394 ■ Forecast the popularity of a keyword on Google Trends 394
Page 18
CONTENTSxvi20 Capstone: Forecasting the monthly average retail price of steak in Canada 396 20.1 Understanding the capstone project 397 Objective of the capstone project 397 20.2 Data preprocessing and visualization 398 20.3 Modeling with Prophet 400 20.4 Optional: Develop a SARIMA model 404 20.5 Next steps 409 21 Going above and beyond 410 21.1 Summarizing what you’ve learned 411 Statistical methods for forecasting 411 ■ Deep learning methods for forecasting 412 ■ Automating the forecasting process 413 21.2 What if forecasting does not work? 413 21.3 Other applications of time series data 415 21.4 Keep practicing 416 appendix Installation instructions 418 index 421
Page 19
preface Working at a bank, I quickly realized how time is an important factor. Interest rates vary over time, people’s spending varies over time, asset prices vary over time. Yet I found most people, including me, were uncomfortable with time series. So I decided to learn time series forecasting. It turned out to be harder than expected because every resource I found was in R. I am comfortable with Python, and Python is undoubtedly the most popular language for data science in the industry. While R constrains you to statistical computing, Python allows you to code websites, perform machine learning, deploy models, build servers, and more. Therefore, I had to translate a lot of R code into Python to learn time series forecasting. That’s when I recognized the gap, and I was lucky enough to be given the opportunity to write a book about it. With this book, I hope to create a one-stop reference for time series forecasting with Python. It covers both statistical and machine learning models, and it also discusses automated forecasting libraries, as they are widely used in the industry and often act as baseline models. This book greatly emphasizes a hands-on, practical approach, with various real-life scenarios. In real life, data is messy, dirty, and sometimes missing, and I wanted to give readers a safe space to experiment with those difficulties, learn from them, and easily transpose those skills into their own projects. This book focuses on time series forecasting. Of course, with time series data, we can also perform classification or anomaly detection, but this book addresses only forecasting to keep the scope manageable.xvii
Page 20
PREFACExviii In each chapter, you will find exercises you can use to practice and hone your skills. Each exercise comes with a full solution on GitHub. I strongly suggest that you take the time to complete them, as you will gain important practical skills. They offer a great way to test your knowledge, see what you need to revisit in a given chapter, and apply modeling techniques in new scenarios. After reading the chapters and completing the exercises, you will have all the nec- essary tools to tackle any forecasting project with confidence and great results. Hope- fully, you will also gain the curiosity and motivation to go beyond this book and become a time series expert.
The above is a preview of the first 20 pages. Register to read the complete e-book.

Support Author

0.00
Total Amount (¥)
0
Donation Count
Please enter an amount Minimum ¥1

You will be redirected to Alipay to complete payment, then return here.

Recommended for You

Loading recommended books...
Failed to load, please try again later
Back to List