Files
2025-10-18 15:35:31 +02:00

85 lines
48 KiB
Plaintext
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
Hands-on Time Series Analysis with Python
From Basics to Bleeding Edge Techniques
B V Vishwas Ashish Patel
Hands-on Time Series Analysis with Python: From Basics to Bleeding Edge Techniques
B V Vishwas Infosys Bengaluru, India
Ashish Patel Cygnet Infotech Pvt Ltd Ahmedabad, India
ISBN-13 (pbk): 978-1-4842-5991-7 ISBN-13 (electronic): 978-1-4842-5992-4 https://doi.org/10.1007/978-1-4842-5992-4
Copyright © 2020 by B V Vishwas and Ashish Patel
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.
Trademarked names, logos, and images may appear in this Book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.
While the advice and information in this Book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: Celestin Suresh John Development Editor: James Markham Coordinating Editor: Aditee Mirashi
Cover designed by eStudioCalamar
Cover image designed by Pixabay
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail booktranslations@springernature.com; for reprint, paperback, or audio rights, please e-mail bookpermissions@springernature.com.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this Book is available to readers on GitHub via the Books product page, located at www.apress.com/ 978-1-4842-5991-7. For more detailed information, please visit www.apress.com/source-code.
Printed on acid-free paper
Table of Contents
About the Authorsxi About the Technical Reviewerxiii Acknowledgments xv Introduction xvii
Chapter 1: T ime-Series Characteristics1 Types of Data2 Time-Series Data2 Cross-Section Data4 Panel Data/Longitudinal Data4 Trend 6 Detecting Trend Using a Hodrick-Prescott Filter6 Detrending a Time Series7 Seasonality 11 Multiple Box Plots12 Autocorrelation Plot13 Deseasoning of Time-Series Data14 Seasonal Decomposition15 Cyclic Variations16 Detecting Cyclical Variations17 Errors, Unexpected Variations, and Residuals18 Decomposing a Time Series into Its Components18 Summary 21
iii
Table of Contents
Chapter 2: D ata Wrangling and Preparation for Time Series23 Loading Data into Pandas24 Loading Data Using CSV24 Loading Data Using Excel25 Loading Data Using JSON25 Loading Data from a URL26 Exploring Pandasql and Pandas Side by Side27 Selecting the Top Five Records27 Applying a Filter28 Distinct (Unique)29 IN 30 NOT IN32 Ascending Data Order33 Descending Data Order34 Aggregation 35 GROUP BY36 GROUP BY with Aggregation38 Join (Merge)39 INNER JOIN41 LEFT JOIN43 RIGHT JOIN46 OUTER JOIN48 Summary of the DataFrame51 Resampling 52 Resampling by Month53 Resampling by Quarter53 Resampling by Year53
iv
Table of Contents
Resampling by Week54 Resampling on a Semimonthly Basis55 Windowing Function55 Rolling Window56 Expanding Window57 Exponentially Weighted Moving Window57 Shifting 58 Handling Missing Data60 BFILL 62 FFILL 62 FILLNA 63 INTERPOLATE 64 Summary 64
Chapter 3: S moothing Methods65 Introduction to Simple Exponential Smoothing66 Simple Exponential Smoothing in Action68 Introduction to Double Exponential Smoothing76 Double Exponential Smoothing in Action78 Introduction to Triple Exponential Smoothing86 Triple Exponential Smoothing in Action87 Summary 97
Chapter 4: R egression Extension Techniques for Time-­Series Data99 Types of Stationary Behavior in a Time Series99 Making Data Stationery101 Using Plots101 Using Summary Statistics101 Using Statistics Unit Root Tests102
v
Table of Contents
Interpreting the P-value104 Augmented Dickey-Fuller Test104 Kwiatkowski-Phillips-Schmidt-Shin Test105 Using Stationary Data Techniques106
Differencing 106 Random Walk107 First-Order Differencing (Trend Differencing)108 Second-Order Differencing (Trend Differencing)109 Seasonal Differencing110 Autoregressive Models111 Moving Average113 Autocorrelation and Partial Autocorrelation Functions116 Introduction to ARMA117 Autoregressive Model118 Moving Average119 Introduction to Autoregressive Integrated Moving Average119 The Integration (I)121 ARIMA in Action122 Introduction to Seasonal ARIMA129 SARIMA in Action131 Introduction to SARIMAX143 SARIMAX in Action144 Introduction to Vector Autoregression154 VAR in Action155 Introduction to VARMA172 VARMA in Action172 Summary 184
vi
Table of Contents
Chapter 5: B leeding-Edge Techniques185 Introduction to Neural Networks185 Perceptron 186 Activation Function188 Binary Step Function188 Linear Activation Function189 Nonlinear Activation Function190 Forward Propagation195 Backward Propagation196 Gradient Descent Optimization Algorithms199 Learning Rate vs. Gradient Descent Optimizers199 Recurrent Neural Networks202 Feed-Forward Recurrent Neural Network204 Backpropagation Through Time in RNN206 Long Short-Term Memory210 Step-by-Step Explanation of LSTM212 Peephole LSTM214 Peephole Convolutional LSTM215 Gated Recurrent Units216 Convolution Neural Networks219 Generalized CNN Formula222 One-Dimensional CNNs223 Auto-encoders 224 Summary 226
vii
Table of Contents
Chapter 6: B leeding-Edge Techniques for Univariate Time Series227 Single-Step Data Preparation for Time-Series Forecasting227 Horizon-Style Data Preparation for Time-Series Forecasting229 LSTM Univariate Single-Step Style in Action230 LSTM Univariate Horizon Style in Action242 Bidirectional LSTM Univariate Single-Step Style in Action253 Bidirectional LSTM Univariate Horizon Style in Action262 GRU Univariate Single-Step Style in Action271 GRU Univariate Horizon Style in Action279 Auto-encoder LSTM Univariate Single-Step Style in Action288 Auto-encoder LSTM Univariate Horizon Style in Action297 CNN Univariate Single-Step Style in Action306 CNN Univariate Horizon Style in Action315 Summary 324
Chapter 7: B leeding-Edge Techniques for Multivariate Time Series 325 LSTM Multivariate Horizon Style in Action325 Bidirectional LSTM Multivariate Horizon Style in Action337 Auto-encoder LSTM Multivariate Horizon Style in Action346 GRU Multivariate Horizon Style in Action356 CNN Multivariate Horizon Style in Action365 Summary 374
viii
Table of Contents
Chapter 8: Prophet375 The Prophet Model375 Implementing Prophet376 Adding Log Transformation381 Adding Built-in Country Holidays386 Adding Exogenous variables using add_regressors(function)389 Summary 394
Index395
ix
About the Authors
B V Vishwas is a Data Scientist, AI researcher and AI Consultant, Currently living in Bengaluru(INDIA). His highest qualification is Master of Technology in Software Engineering from Birla Institute of Technology & Science, Pilani, India and his primary focus and inspiration is Data Warehousing, Big Data, Data Science (Machine Learning, Deep Learning, Timeseries, Natural Language Processing, Reinforcement Learning, and Operation Research). He has over seven years of IT experience currently working at Infosys as Data Scientist & AI Consultant. He has also worked on Data Migration, Data Profiling, ETL & ELT, OWB, Python, PL/SQL, Unix Shell Scripting, Azure ML Studio, Azure Cognitive Services, and AWS.
Ashish Patel is a Senior Data Scientist, AI researcher, and AI Consultant with over seven years of experience in the field of AI, Currently living in Ahmedabad(INDIA). He has a Master of Engineering Degree from Gujarat Technological University and his keen interest and ambition to research in the following domains such as (Machine Learning, Deep Learning, Time series, Natural Language Processing, Reinforcement Learning, Audio Analytics, Signal Processing, Sensor
xi
About the Authors Technology, IoT, Computer Vision). He is currently working as Senior Data Scientist for Cynet infotech Pvt Ltd. He has published more than 15 + Research papers in the field of Data Science with Reputed Publications such as IEEE. He holds Rank 3 as a kernel master in Kaggle. Ashish has immense experience working on cross-domain projects involving a wide variety of data, platforms, and technologies.
xii
About the Technical Reviewer
Over his nearly three-decade career, Alexey Panchekha, PhD, CFA, has spent 10 years in academia, where he focused on nonlinear and dynamic processes; 10 years in the technology industry, where he specialized in program design and development; and eight years in financial services. In the latter arena, he specialized in applying mathematical techniques and technology to risk management and alpha generation. For example, Panchekha was involved in the equity derivative trading technology platform at Goldman Sachs, and he led the creation of the multi-asset multigeographies portfolio risk management system at Bloomberg. He also served as the head of research at Markov Process International, a leader in portfolio attribution and analytics. Most recently, Panchekha cofounded Turing Technology Associates, Inc., with Vadim Fishman. Turing is a technology and intellectual property company that sits at the intersection of mathematics, machine learning, and innovation. Its solutions typically service the financial technology (fintech) industry. Turing primarily focuses on enabling technology that supports the burgeoning ensemble active management (EAM). Prior to Turing, Panchekha was managing director at Incapital, and head of research at F-Squared Investments, where he designed innovative volatility-based risk-sensitive investment strategies. He is fluent in multiple computer programming languages and software and database programs and is certified in deep learning software. He earned a PhD from Kharkiv Polytechnic University with studies in physics and mathematics as well as an MS in physics. Panchekha is a CFA charterholder.
xiii
Acknowledgments
This being my first book, I found transforming idea and real-world experience into its current shape to be a strenuous task. I am grateful to almighty God for blessing and guiding me in all endeavors. I would like to thank my parents (Vijay Kumar and Rathnamma), brother (Shreyas), other family, and friends for helping me sail though the sea of life.
—B V Vishwas First and foremost, praises and thanks to God, the almighty, for His showers of blessings throughout the book-writing process. I would like to express my deep and sincere gratitude to my parents (Dinesh Kumar N Patel and Javnika ben D Patel), my sister (Nisha Patel), and my family and friends (Shailesh Patel, Sanket Patel, Nikit Patel, Mansi Patel, Khushboo Shah) for their support and valuable prayers.
—Ashish Patel Special thanks to Celestin Suresh John, Aditee Mirashi, James Markham, Alexey Panchekha, and the Apress team for bringing this book to life.
xv
Introduction
This book explains the concepts of time series from traditional to bleeding-edge techniques with full-fledged examples.
The book begins by covering time-series fundamentals and their characteristics, Structure & Components of time series data, preprocessing, and ways of crafting features through data wrangling. Next, it covers traditional time-series techniques such as the smoothing methods ARMA, ARIMA, SARIMA, SARIMAX, VAR, and VARMA using trending frameworks such as Statsmodels and Pmdarima.
Further covers how to leverage advanced deep learning-based techniques such as ANN, CNN, RNN, LSTM, GRU, and Autoencoder to solve time-­series problems using Tensorflow. It concludes by explaining how to use the popular framework fbprophet for modeling time-series analysis.
After completion of the book, the reader will have thorough knowledge of concepts and techniques to solve time-series problems. All the code presented in this book is available in Jupyter Notebooks; this allows readers to do hands-on experiments and enhance them in exciting ways.
xvii