Skip to product information
Python Time Series Forecasting Analysis
Python Time Series Forecasting Analysis
Description
Book Introduction
Learning Time Series Analysis and Applications with Field Practical Examples

In data science, analyzing change patterns over time to build various predictive models can help predict the future and make various decisions.
This book introduces a time series forecasting method that works perfectly with Python code.
We'll cover defining time series data, developing baseline models, building large-scale models using statistical models, TensorFlow, and modern deep learning tools, and even an automated forecasting library.
Master time series forecasting analysis with a variety of practical examples, including Google stock price trends, antidiabetic prescription predictions, and household electricity consumption forecasts.
  • You can preview some of the book's contents.
    Preview

index
Translator's Preface xiii
Beta Reader Review xiv
Preface xvi
Acknowledgments xviii
About this book xix
About the cover xxiii

PART I Time waits for no one

CHAPTER 1 Understanding Time Series Forecasting 3
1.1 Introduction to Time Series 4
__1.1.1 Components of a Time Series 5
1.2 Bird's-eye view of time series forecasting 8
__1.2.1 Setting the Goal 10 / 1.2.2 Deciding What to Predict to Achieve the Goal 10 / 1.2.3 Setting the Forecast Period 10 / 1.2.4 Collecting Data 10 / 1.2.5 Developing the Forecast Model 11 / 1.2.6 Deploying to a Production Environment 12 / 1.2.7 Monitoring 12 / 1.2.8 Collecting New Data 12
1.3 How Time Series Forecasting Differs from Other Regression Tasks 13
__1.3.1 Time series have an order 13 / 1.3.2 There are cases where time series have no features 14
1.4 Next Step 14
Summary 15

CHAPTER 2: Predicting the Future Simply 16

2.1 Defining the Baseline Model 18
2.2 Predicting with Past Averages 19
__2.2.1 Setting up the baseline implementation 20 / 2.2.2 Implementing the historical average-based baseline model 22
2.3 Predicting with last year's average 27
2.4 Predicting with the last measured value 29
2.5 Implementing a Simple Seasonal Forecast 31
2.6 Next Step 32
Summary 33

CHAPTER 3: Following the Probabilistic Walk 35

3.1 Stochastic Walk Process 37
__3.1.1 Simulating a Stochastic Walk Process 37
3.2 Identifying Probabilistic Walks 40
__3.2.1 Stationarity 42 / 3.2.2 Testing Stationarity 44 / 3.2.3 Autocorrelation Function 48 / 3.2.4 Putting It All Together 48 / 3.2.5 Is GOOGL a Random Walk? 52
3.3 Predicting a Stochastic Walk 55
__3.3.1 Long-term Forecasting 55 / 3.3.2 Forecasting the Next Time Step 61
3.4 Next Step 64
3.5 Exercise 65
__3.5.1 Simulating and Predicting a Stochastic Walk 65 / 3.5.2 Predicting the Daily Closing Price of GOOGL 66 / 3.5.3 Predicting the Daily Closing Price of a Directly Selected Stock 66
Summary 67

PART 2 Predicting Using Statistical Models

CHAPTER 4 MODELING THE MOVING AVERAGE PROCESS 71

4.1 Defining the Moving Average Process 73
__4.1.1 Identifying the Order of the Moving Average Process 75
4.2 Predicting the Moving Average Process 80
4.3 Next Step 90
4.4 Exercise 91
__4.4.1 MA(2) Process Simulation and Prediction 92 / 4.4.2 MA(q) Process Simulation and Prediction 92
Summary 93

CHAPTER 5: MODELING THE AUTOREVERSE PROCESS 94

5.1 Predicting Average Weekly Footfall at a Retail Store 95
5.2 Defining the Autoregressive Process 97
5.3 Finding the Order of a Normal Autoregressive Process 98
__5.3.1 Autocorrelation Function 104
5.4 Predicting Autoregressive Processes 107
5.5 Next Step 114
5.6 Exercise 114
__5.6.1 Simulating AR(2) Processes and Making Predictions 114 / 5.6.2 Simulating AR(p) Processes and Making Predictions 115
Summary 115

CHAPTER 6: MODELING COMPLEX TIME SERIES 116

6.1 Predicting Data Center Bandwidth Usage 117
6.2 Examining the Autoregressive Moving Average Process 120
6.3 Identifying Normal ARMA Processes 122
6.4 Designing a General Modeling Procedure 128
__6.4.1 Understanding the Akaike Information Criterion 130 / 6.4.2 Selecting a Model Using AIC 132 / 6.4.3 Understanding Residual Analysis 134 / 6.4.4 Performing Residual Analysis 139
6.5 Applying General Modeling Procedures 143
6.6 Predicting Bandwidth Usage 152
6.7 Next Steps 157
6.8 Exercise 157
__6.8.1 Performing Predictions on a Simulated ARMA(1,1) Process 158 / 6.8.2 Simulating an ARMA(2,2) Process and Performing Predictions 158
Summary 159

CHAPTER 7 Forecasting Nonstationary Time Series 161

7.1 Defining the Autoregressive Cumulative Moving Average Model 164
7.2 Modifying General Modeling Procedures to Apply to Nonstationary Time Series 165
7.3 Forecasting Nonstationary Time Series 167
7.4 Next Steps 177
7.5 Exercise 177
__7.5.1 Applying the ARIMA(p,d,q) Model to the Data Sets from Chapters 4, 5, and 6 177
Summary 178

CHAPTER 8 CONSIDERING SEASONALITY 179

8.1 Examining the SARIMA(p,d,q)(P,D,Q)m Model 180
8.2 Identifying Seasonal Patterns in Time Series 183
8.3 Forecasting Monthly Airline Passenger Counts 187
__8.3.1 Forecasting using the ARIMA(p,d,q) model 190 / 8.3.2 Forecasting using the SARIMA(p,d,q)(P,D,Q)m model 196 / 8.3.3 Comparing the performance of each forecasting method 200
8.4 Next Steps 203
8.5 Exercise 203
__8.5.1 Applying the SARIMA(p,d,q)(P,D,Q)m Model to the Johnson & Johnson Data Set 203
Summary 204

CHAPTER 9 Adding Exogenous Variables to the Model 205

9.1 Examining the SARIMAX Model 207
__9.1.1 Exploring Exogenous Variables in the U.S. Macroeconomic Data Set 208 / 9.1.2 Considerations When Using SARIMAX 211
9.2 Forecasting Real GDP Using the SARIMAX Model 212
9.3 Next Steps 221
9.4 Exercise 222
__9.4.1 Forecasting Real GDP Using All Exogenous Variables in a SARIMAX Model 222
Summary 222

CHAPTER 10 Forecasting Multiple Time Series 223

10.1 Examining the VAR Model 225
10.2 Designing a Modeling Procedure for a VAR(p) Model 227
__10.2.1 Examining the Granger Causality Test 229
10.3 Forecasting Real Disposable Income and Real Consumption 230
10.4 Next Steps 242
10.5 Exercise 243
__10.5.1 Predicting realdpi and realcons using the VARMA model 243 /10.5.2 Predicting realdpi and realcons using the VARMAX model 244
Summary 244

CHAPTER 11 Capstone Project: Predicting Antidiabetic Prescriptions in Australia 245

11.1 Importing required libraries and loading data 247
11.2 Visualizing Sequences and Their Components 248
11.3 Modeling with Data 250
__11.3.1 Performing Model Selection 253 / 11.3.2 Performing Residual Analysis 254
11.4 Performing Predictions and Evaluating Model Performance 256
11.5 Next Step 260

PART 3: Leveraging Deep Learning for Large-Scale Prediction

CHAPTER 12: Introducing Deep Learning for Time Series Forecasting 263


12.1 When to Use Deep Learning for Time Series Forecasting 264
12.2 Examining Different Types of Deep Learning Models 265
12.3 Preparing to Apply Deep Learning for Prediction 268
__12.3.1 Performing Data Exploration 268 / 12.3.2 Feature Engineering and Data Partitioning 272
12.4 Next Steps 277
12.5 Exercise 277
Summary 278

CHAPTER 13: Windowing Data and Building Baseline Models for Deep Learning 279

13.1 Creating a Data Window 280
__13.1.1 A Look at Training a Deep Learning Model for Time Series Forecasting 280
__13.1.2 Implementing the DataWindow Class 284
13.2 Applying the Baseline Model 292
__13.2.1 Single-Step Baseline Model 292
__13.2.2 Multi-stage baseline model 295
__13.2.3 Multi-Output Baseline Model 299
13.3 Next Step 303
13.4 Exercise 303
Summary 304

CHAPTER 14: First Steps in Deep Learning 305

14.1 Implementing a Linear Model 306
__14.1.1 Implementing a Single-Step Linear Model 307 / 14.1.2 Implementing a Multi-Step Linear Model 309 / 14.1.3 Implementing a Multi-Output Linear Model 311
14.2 Implementing a Deep Neural Network 312
__14.2.1 Implementing a Deep Neural Network as a Single-Stage Model 314 / 14.2.2 Implementing a Deep Neural Network as a Multi-Stage Model 317 / 14.2.3 Implementing a Deep Neural Network as a Multi-Output Model 319
14.3 Next Step 320
14.4 Exercise 321
Summary 322

CHAPTER 15: Remembering the Past with LSTMs 323

15.1 Examining Recurrent Neural Networks 324
15.2 Examining the LSTM Architecture 326
__15.2.1 Forget Gate 327 / 15.2.2 Input Gate 329 / 15.2.3 Output Gate 330
15.3 Implementing the LSTM Architecture 332
__15.3.1 Implementing LSTM as a Single-Stage Model 332 / 15.3.2 Implementing LSTM as a Multi-Stage Model 335 / 15.3.3 Implementing LSTM as a Multi-Output Model 338
15.4 Next Step 341
15.5 Exercise 342
Summary 343

CHAPTER 16 Filtering Time Series with CNNs 344

16.1 CNN Overview 345
16.2 Implementing CNNs 349
__16.2.1 Implementing CNN as a Single-Stage Model 350 / 16.2.2 Implementing CNN as a Multi-Stage Model 354 / 16.2.3 Implementing CNN as a Multi-Output Model 356
16.3 Next Step 359
16.4 Exercise 359
Summary 361

CHAPTER 17: Predicting More with Predictions 362

17.1 A Look at the ARLSTM Architecture 363
17.2 Building an Autoregressive LSTM Model 364
17.3 Next Step 370
17.4 Exercise 371
Summary 371

CHAPTER 18 CAPSTONE PROJECT: ESTIMATE HOME POWER CONSUMPTION 372

18.1 Understanding the Capstone Project 373
__18.1.1 Capstone Project Objectives 375
18.2 Data Wrangling and Preprocessing 376
__18.2.1 Handling Missing Data 377 / 18.2.2 Data Transformation 379 / 18.2.3 Resampling Data 379
18.3 Feature Engineering 382
__18.3.1 Removing Unnecessary Columns 383 / 18.3.2 Identifying Seasonal Periods 383 / 18.3.3 Partitioning and Scaling Data 386
18.4 Preparing to Model with Deep Learning 387
__18.4.1 Initial Setup 387 / 18.4.2 Defining the DataWindow Class 389 / 18.4.3 Utility Functions for Model Training 391
18.5 Modeling with Deep Learning 392
__18.5.1 Baseline Model 392 / 18.5.2 Linear Model 396 / 18.5.3 Deep Neural Network 397 / 18.5.4 Long Short-Term Memory Model 398 / 18.5.5 Convolutional Neural Network 399 / 18.5.6 Combining CNN and LSTM 401 / 18.5.7 Autoregressive LSTM Model 402 / 18.5.8 Selecting the Optimal Model 404
18.6 Next Step 406

PART 4 ​​Automating Large-Scale Forecasting

CHAPTER 19 Automating Time Series Forecasting with Prophet 409


19.1 Overview of Automated Prediction Libraries 410
19.2 Examining the Prophet 412
19.3 Making Basic Predictions Using Prophet 414
19.4 Explore Prophet's Advanced Features 420
__19.4.1 Visualization Features 421 / 19.4.2 Cross-Validation and Performance Metrics 425 / 19.4.3 Hyperparameter Tuning 429
19.5 Implementing a Robust Forecasting Procedure with Prophet 432
__19.5.1 Prediction Project: Predicting the Popularity of 'Chocolate' Searches on Google 434 / 19.5.2 Experiment: Could SARIMA Be Better? 442
19.6 Next Step 446
19.7 Exercise 447
__19.7.1 Predicting the number of airline passengers 447 / 19.7.2 Predicting the number of antidiabetic prescriptions 447 / 19.7.3 Predicting keyword popularity in Google Trends 447
Summary 448

CHAPTER 20 CAPSTONE PROJECT: ESTIMATE THE AVERAGE MONTHLY RETAIL PRICE OF STEAK IN CANADA 449

20.1 Understanding the Capstone Project 450
__20.1.1 Capstone Project Objective 450
20.2 Data Preprocessing and Visualization 451
20.3 Modeling with Prophet 453
20.4 Optional: Developing a SARIMA Model 459
20.5 Next Step 464

CHAPTER 21 Going One Step Further 466

21.1 Summarizing What You've Learned 467
__21.1.1 Statistical Methods for Prediction 467 / 21.1.2 Deep Learning Methods for Prediction 468 / 21.1.3 Automating the Prediction Process 469
21.2 What to do if your prediction fails? 470
21.3 Other Applications of Time Series Data 472
21.4 Continue practicing 473

APPENDIX A Installation Instructions 475

Search 479

Detailed image
Detailed Image 1

Into the book
This book focuses entirely on time series forecasting.
First, we will learn a simple prediction method that can serve as a baseline for more complex models.
Next, we will perform predictions using two statistical learning techniques: the moving average model and the autoregressive model.
These techniques will form the basis for more complex models that can handle non-stationarity, seasonal effects, and the influence of exogenous variables.
We then move from statistical learning techniques to deep learning techniques, examining a scenario where deep learning outperforms statistical learning in predicting high-dimensional and large-scale time series.

--- p.4

Let's see if we can model GOOGL's daily closing price using a stochastic walk model.
To do this, we must first check whether the process is normal.
If the process is abnormal, a transformation such as difference must be applied to make the process normal.
Then, we can plot the autocorrelation function (ACF) to see if the daily closing price of GOOGL can be approximated by a stochastic walk model.
In this chapter, we will cover both differential and autocorrelation function plots.
Finally, we will conclude this chapter by predicting the future closing price of GOOGL.

--- p.36

In this chapter, we will examine ARMA(p,q), an autoregressive moving average process.
Here, p represents the degree of the autoregressive part, and q represents the degree of the moving average part.
(Omitted) This procedure includes model selection using the Akaike information criterion (AIC), which determines the optimal combination of p and q for the time series.
Then, the validity of the model should be evaluated through residual analysis, which examines the QQ plot and density plot, which are correlation plots of the model residuals, to assess whether the model residuals are similar to white noise.
If it is determined to be valid, we can move on to predicting the time series using the ARMA(p,q) model.

--- p.117

For example, let's say we need to predict hourly temperatures.
It is reasonable to assume that there is daily seasonality, as temperatures tend to be lower at night and higher during the day, but there is also annual seasonality, as temperatures are lower in winter and higher in summer.
In these cases, deep learning can be used to make predictions by leveraging information from both seasons.
(Omitted) Ultimately, deep learning is used when fitting a statistical model takes too much time or when there are residuals that are correlated and not close to white noise.
This may be because there are other seasonal periods that the model cannot account for, or simply because there is a nonlinear relationship between the features and the target.
In these cases, deep learning models can be used to capture these nonlinear relationships, with the added benefit of being very fast to train.

--- p.264

Let's look at an advanced architecture called the long short-term memory (LSTM), which is a specific example of a recurrent neural network (RNN).
This type of neural network is used to process data arrays where order is important. One common application of RNNs and LSTMs is natural language processing.
The words in a sentence have a certain order, and changing that order can completely change the meaning of the sentence.
Therefore, this architecture is often behind text classification or text generation algorithms.
/ Another situation where the order of data is important is time series.
We know that a time series is a sequence of data occupying equal intervals in time and that the order cannot be changed.
A data point observed at 9:00 AM must be before a data point observed at 10:00 AM and after a data point observed at 8:00 AM.
Therefore, it is reasonable to apply the LSTM architecture to predict time series.

--- p.323

While manually building and tuning models provides flexibility and complete control over forecasting techniques, automating most of the forecasting process makes it easier to forecast time series and accelerates experimentation.
Understanding automated tools is important because they allow us to get predictions quickly and often facilitate the use of state-of-the-art models.
--- p.409

Publisher's Review
Time Series Data Science: Transitioning from R to Python

While R is a great language for traditional statistical analysis, replacing it with the near-universal Python opens up a wide range of applications, from statistical analysis to deep learning models and automated prediction libraries.
Marcou Peycheiro studied time series forecasting and converted a lot of code written in R to Python, and wrote this book to provide a comprehensive reference for Python-based time series forecasting.


This book covers everything from predictive analytics based on statistical models such as moving averages, autoregression, and SARIMAX using Python, to deep learning-based predictions such as LSTM and CNN architectures, and automated prediction libraries using Prophet and SARIMAX models.
In particular, it shows the process of collecting data, building a model, and finding predicted values ​​step by step through appropriate examples for each topic.
As readers follow the exercises, they will experience the predicted and actual values ​​gradually getting closer.

In data science, time variability is an important factor that cannot be ignored.
Let's learn time series forecasting analysis techniques step by step through various practical examples, such as Google stock price trends, data center bandwidth usage forecasting, monthly airline passenger count forecasting, antidiabetic prescriptions forecasting, and household electricity consumption forecasting.
After completing this book, you will learn a variety of time series data science techniques that can be applied immediately in your work.

Key Contents

● Concept of time series data and development of basic models
● Forecasting based on statistical models such as moving average, autoregression, and SARIMAX
● Deep learning-based predictions, including LSTM and CNN architectures
● Prophet, an automated prediction library using the SARIMAX model
GOODS SPECIFICS
- Date of issue: July 25, 2024
- Page count, weight, size: 508 pages | 1,004g | 188*245*25mm
- ISBN13: 9791193926314

You may also like

카테고리