41,99 €
Step by Step guide filled with real world practical examples.
This book is for anyone who wants to analyze data over time and/or frequency. A statistical background is necessary to quickly learn the analysis methods.
Time Series Analysis allows us to analyze data which is generated over a period of time and has sequential interdependencies between the observations. This book describes special mathematical tricks and techniques which are geared towards exploring the internal structures of time series data and generating powerful descriptive and predictive insights. Also, the book is full of real-life examples of time series and their analyses using cutting-edge solutions developed in Python.
The book starts with descriptive analysis to create insightful visualizations of internal structures such as trend, seasonality and autocorrelation. Next, the statistical methods of dealing with autocorrelation and non-stationary time series are described. This is followed by exponential smoothing to produce meaningful insights from noisy time series data. At this point, we shift focus towards predictive analysis and introduce autoregressive models such as ARMA and ARIMA for time series forecasting. Later, powerful deep learning methods are presented, to develop accurate forecasting models for complex time series, and under the availability of little domain knowledge. All the topics are illustrated with real-life problem scenarios and their solutions by best-practice implementations in Python.
The book concludes with the Appendix, with a brief discussion of programming and solving data science problems using Python.
This book takes the readers from the basic to advance level of Time series analysis in a very practical and real world use cases.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 217
Veröffentlichungsjahr: 2017
BIRMINGHAM - MUMBAI
Copyright © 2017 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: September 2017
Production reference: 2041017
ISBN 978-1-78829-022-7
www.packtpub.com
Authors
Dr. Avishek Pal
Dr. PKS Prakash
Copy Editor
Tasneem Fatehi
Reviewer
Prabhanjan Tattar
Project Coordinator
Manthan Patel
Commissioning Editor
Veena Pagare
Proofreader
Safis Editing
Acquisition Editor
Aman Singh
Indexer
Tejal Daruwale Soni
Content Development Editor
Snehal Kolte
Graphics
Tania Dutta
Technical Editor
Danish Shaikh
Production Coordinator
Deepika Naik
Dr. Avishek Pal, PhD, is a software engineer, data scientist, author, and an avid Kaggler living in Hyderabad, the City of Nawabs, India. He has a bachelor of technology degree in industrial engineering from the Indian Institute of Technology (IIT) Kharagpur and has earned his doctorate in 2015 from University of Warwick, Coventry, United Kingdom. At Warwick, he studied at the prestigious Warwick Manufacturing Centre, which functions as one of the centers of excellence in manufacturing and industrial engineering research and teaching in UK.
In terms of work experience, Avishek has a diversified background. He started his career as a software engineer at IBM India to develop middleware solutions for telecom clients. This was followed by stints at a start-up product development company followed by Ericsson, a global telecom giant. During these three years, Avishek lived his passion for developing software solutions for industrial problems using Java and different database technologies.
Avishek always had an inclination for research and decided to pursue his doctorate after spending three years in software development. Back in 2011, the time was perfect as the analytics industry was getting bigger and data science was emerging as a profession. Warwick gave Avishek ample time to build up the knowledge and hands-on practice on statistical modeling and machine learning. He applied these not only in doctoral research, but also found a passion for solving data science problems on Kaggle.
After doctoral studies, Avishek started his career in India as a lead machine learning engineer for a leading US-based investment company. He is currently working at Microsoft as a senior data scientist and enjoys applying machine learning to generate revenue and save costs for the software giant.
Avishek has published several research papers in reputed international conferences and journals. Reflecting back on his career, he feels that starting as a software developer and then transforming into a data scientist gives him the end-to-end focus of developing statistics into consumable software solutions for industrial stakeholders.
Dr. PKS Prakash is a data scientist and author. He has spent the last 12 years in developing many data science solutions in several practice areas within the domains of healthcare, manufacturing, pharmaceutical, and e-commerce. He is working as the data science manager at ZS Associates. ZS is one of the world's largest business services firms, helping clients with commercial success by creating data-driven strategies using advanced analytics that they can implement within their sales and marketing operations in order to make them more competitive, and by helping them deliver an impact where it matters.
Prakash's background involves a PhD in industrial and system engineering from Wisconsin-Madison, US. He has earned his second PhD in engineering from University of Warwick, UK. His other educational qualifications involve a masters from University of Wisconsin-Madison, US, and bachelors from National Institute of Foundry and Forge Technology (NIFFT), India. He is the co-founder of Warwick Analytics spin-off from University of Warwick, UK.
Prakash has published articles widely in research areas of operational research and management, soft computing tools, and advance algorithms in leading journals such as IEEE-Trans, EJOR, and IJPR among others. He has edited an issue on Intelligent Approaches to Complex Systems and contributed in books such as Evolutionary Computing in Advanced Manufacturing published by WILEY and Algorithms and Data Structures using R and R Deep Learning Cookbook published by PACKT.
Prabhanjan Tattar is currently working as a Senior Data Scientist at Fractal Analytics Inc. He has 8 years of experience as a statistical analyst. Survival analysis and statistical inference are his main areas of research/interest, and he has published several research papers in peer-reviewed journals and also authored two books on R: R Statistical Application Development by Example, Packt Publishing, and A Course in Statistics with R, Wiley. The R packages, gpk, RSADBE, and ACSWR are also maintained by him.
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www.packtpub.com/mapt
Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review.
If you'd like to join our team of regular reviewers, you can e-mail us at [email protected]. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products!
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
Introduction to Time Series
Different types of data
Cross-sectional data
Time series data
Panel data
Internal structures of time series
General trend
Seasonality
Run sequence plot
Seasonal sub series plot
Multiple box plots
Cyclical changes
Unexpected variations
Models for time series analysis
Zero mean models
Random walk
Trend models
Seasonality models
Autocorrelation and Partial autocorrelation
Summary
Understanding Time Series Data
Advanced processing and visualization of time series data
Resampling time series data
Group wise aggregation
Moving statistics
Stationary processes
Differencing
First-order differencing
Second-order differencing
Seasonal differencing
Augmented Dickey-Fuller test
Time series decomposition
Moving averages
Moving averages and their smoothing effect
Seasonal adjustment using moving average
Weighted moving average
Time series decomposition using moving averages
Time series decomposition using statsmodels.tsa
Summary
Exponential Smoothing based Methods
Introduction to time series smoothing
First order exponential smoothing
Second order exponential smoothing
Modeling higher-order exponential smoothing
Summary
Auto-Regressive Models
Auto-regressive models
Moving average models
Building datasets with ARMA
ARIMA
Confidence interval
Summary
Deep Learning for Time Series Forecasting
Multi-layer perceptrons
Training MLPs
MLPs for time series forecasting
Recurrent neural networks
Bi-directional recurrent neural networks
Deep recurrent neural networks
Training recurrent neural networks
Solving the long-range dependency problem
Long Short Term Memory
Gated Recurrent Units
Which one to use - LSTM or GRU?
Recurrent neural networks for time series forecasting
Convolutional neural networks
2D convolutions
1D convolution
1D convolution for time series forecasting
Summary
Getting Started with Python
Installation
Python installers
Running the examples
Basic data types
List, tuple, and set
Strings
Maps
Keywords and functions
Iterators, iterables, and generators
Iterators
Iterables
Generators
Classes and objects
Summary
This book is about an introduction to time series analysis using Python. We aim to give you a clear overview of the basic concepts of the discipline and describe useful techniques that would be applicable for commonly-found analytics use cases in the industry. With too many projects requiring trend analytics and forecasting based on past data, time series analysis is an important tool in the knowledge arsenal of any modern data scientist. This book will equip you with tools and techniques, which will let you confidently think through a problem and come up with its solution in time series forecasting.
Why Python? Python is rapidly becoming a first choice for data science projects across different industry sectors. Most state-of-the art machine learning and deep learning libraries have a Python API. As a result, many data scientists prefer Python to implement the entire project pipeline that consists of data wrangling, model building, and model validation. Besides, Python provides easy-to-use APIs to process, model, and visualize time series data. Additionally, Python has been a popular language for the development of backend for web applications and hence has an appeal to a wider base of software professionals.
Now, let's see what you can expect to learn from every chapter this book.
Chapter 1, Introduction to Time Series, starts with a discussion of the three different types of datasets—cross-section, time series, and panel. The transition from cross-sectional to time series and the added complexity of data analysis is discussed. Special mathematical properties that make time series data special are described. Several examples demonstrate how exploratory data analysis can be used to visualize these properties.
Chapter 2, Understanding Time Series Data, covers three topics, advanced preprocessing and visualization of time series data through resampling, group-by, and calculation of moving averages; stationarity and statistical hypothesis testing to detect stationarity in a time series; and various methods of time series decomposition for stationarizing a non-stationary time series.
Chapter 3, Exponential Smoothing based Methods, covers smoothing-based models using the Holt-Winters approach for first order to capture levels, second order to smoothen levels and trend, and higher order smoothing is illustrated, which captures level, trend, and seasonality within a time series dataset.
Chapter 4, Auto-Regressive Models, discusses autoregressive models for forecasting. The chapter covers a detailed implementation for moving average (MA), autoregressive (AR), Auto Regressive Moving Average (ARMA), and Auto Regressive Integrated Moving Average (ARIMA) to capture different levels of nuisance within time series data during forecasting.
Chapter 5, Deep Learning for Time Series Forecasting, discusses recent deep learning algorithms that can be directly adapted to develop forecasting models for time series data. Recurrent Neural Networks (RNNs) are a natural choice for modeling sequence in data. In this chapter, different RNNs such as Vanilla RNN, Gated Recurrent Units, and Long Short Term Memory units are described to develop forecasting models on time series data. The mathematical formulations involved in developing these RNNs are conceptually discussed. Case studies are solved using the ‘keras’ deep learning library of Python.
Appendix, Getting Started with Python, you will find a quick and easy introduction to Python. If you are new to Python or looking for how to get started with the programming language, reading this appendix will help you get through the initial hurdles.
You will need the Anaconda Python Distribution to run the examples in this book and write your own Python programs for time series analysis. This is freely downloadable from https://www.continuum.io/downloads.
The code samples of this book have been written using the Jupyter Notebook development environment. To run the Jupyter Notebooks, you need to install Anaconda Python Distribution, which has the Python language essentials, interpreter, packages used to develop the examples, and the Jupyter Notebook server.
The topics in this book are expected to be useful for the following people:
Data scientists, professionals with a background in statistics, machine learning, and model building and validation
Data engineers, professionals with a background in software development
Software professionals looking to develop an expertise in generating data-driven business insights
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
A block of code is set as follows:
import osimport pandas as pd%matplotlib inlinefrom matplotlib import pyplot as pltimport seaborn as sns
In-text code is highlighted in font and color as here: pandas.DataFrame. File and folder names are also shown in the same style, for example, Chapter_1_Models_for_Time_Series_Analysis.ipynb or datasets/DJIA_Jan2016_Dec2016.xlsx
At several places in the book, we have referred to external URLs to cite source of datasets or other information. A URL would appear in the following text style: http://finance.yahoo.com
New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "In order to download new modules, we will go toFiles|Settings|Project Name|Project Interpreter."
Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of. To send us general feedback, simply email [email protected], and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files emailed directly to you. You can download the code files by following these steps:
Log in or register to our website using your email address and password.
Hover the mouse pointer on the
SUPPORT
tab at the top.
Click on
Code Downloads & Errata
.
Enter the name of the book in the
Search
box.
Select the book for which you're looking to download the code files.
Choose from the drop-down menu where you purchased this book from.
Click on
Code Download
.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
WinRAR / 7-Zip for Windows
Zipeg / iZip / UnRarX for Mac
7-Zip / PeaZip for Linux
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Practical-Time-Series-Analysis. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title. To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy of copyrighted material on the internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the internet, please provide us with the location address or website name immediately so that we can pursue a remedy. Please contact us at [email protected] with a link to the suspected pirated material. We appreciate your help in protecting our authors and our ability to bring you valuable content.
If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.
The recent few years have witnessed the widespread application of statistics and machine learning to derive actionable insights and business value out of data in almost all industrial sectors. Hence, it is becoming imperative for business analysts and software professionals to be able to tackle different types of datasets. Often, the data is a time series in the form of a sequence of quantitative observations about a system or process and made at successive points in time. Commonly, the points in time are equally spaced. Examples of time series data include gross domestic product, sales volumes, stock prices, weather attributes when recorded over a time spread of several years, months, days, hours, and so on. The frequency of observation depends on the nature of the variable and its applications. For example, gross domestic product, which is used for measuring annual economic progress of a country, is publicly reported every year. Sales volumes are published monthly, quarterly or biyearly, though figures over longer duration of time might have been generated by aggregating more granular data such as daily or weekly sales. Information about stock prices and weather attributes are available at every second. On the other extreme, there are several physical processes which generate time series data at fraction of a second.
Successful utilization of time series data would lead to monitoring the health of the system over time. For example, the performance of a company is tracked from its quarterly profit margins. Time series analysis aims to utilize such data for several purposes that can be broadly categorized as:
To understand and interpret the underlying forces that produce the observed state of a system or process over time
To forecast the future state of the system or process in terms of observable characteristics
To achieve the aforementioned objectives, time series analysis applies different statistical methods to explore and model the internal structures of the time series data such as trends, seasonal fluctuations, cyclical behavior, and irregular changes. Several mathematical techniques and programming tools exist to effectively design computer programs that can explore, visualize, and model patterns in time series data.
However, before taking a deep dive into these techniques, this chapter aims to explain the following two aspects:
Difference between time series and non-time series data
Internal structures of time series (some of which have been briefly mentioned in the previous paragraph)
For problem solving, readers would find this chapter useful in order to:
Distinguish between time series and non-time series data and hence choose the right approach to formulate and solve a given problem.
Select the appropriate techniques for a time series problem. Depending on the application, one may choose to focus on one or more internal structures of the time series data.
At the end of this chapter, you will understand the different types of datasets you might have to deal with in your analytics project and be able to differentiate time series from non-time series. You will also know about the special internal structures of data which makes it a time series. The overall concepts learnt from this chapter will help in choosing the right approach of dealing with time series.
This chapter will cover the following points:
Knowing the different types of data you might come across in your analytics projects
Understanding the internal structures of data that makes a time series
Dealing with auto-correlation, which is the single most important internal structure of a time series and is often the primary focus of time series analysis
Business analysts and data scientists come across many different types of data in their analytics projects. Most data commonly found in academic and industrial projects can be broadly classified into the following categories:
Cross-sectional data
Time series data
Panel data
Understanding what type of data is needed to solve a problem and what type of data can be obtained from available sources is important for formulating the problem and choosing the right methodology for analysis.
Cross-sectional data or cross-section of a population is obtained by taking observations from multiple individuals at the same point in time. Cross-sectional data can comprise of observations taken at different points in time, however, in such cases time itself does not play any significant role in the analysis. SAT scores of high school students in a particular year is an example of cross-sectional data. Gross domestic product of countries in a given year is another example of cross-sectional data. Data for customer churn analysis is another example of cross-sectional data. Note that, in case of SAT scores of students and GDP of countries, all the observations have been taken in a single year and this makes the two datasets cross-sectional. In essence, the cross-sectional data represents a snapshot at a given instance of time in both the cases. However, customer data for churn analysis can be obtained from over a span of time such as years and months. But for the purpose of analysis, time might not play an important role and therefore though customer churn data might be sourced from multiple points in time, it may still be considered as a cross-sectional dataset.
Often, analysis of cross-sectional data starts with a plot of the variables to visualize their statistical properties such as central tendency, dispersion, skewness, and kurtosis. The following figure illustrates this with the univariate example of military expenditure as a percentage of Gross Domestic Product of 85 countries in the year 2010. By taking the data from a single year we ensure its cross-sectional nature. The figure combines a normalized histogram and a kernel density plot in order to highlight different statistical properties of the military expense data.
As evident from the plot, military expenditure is slightly left skewed with a major peak at roughly around 1.0 %. A couple of minor peaks can also be observed near 6.0 % and 8.0 %.
Exploratory data analysis such as the one in the preceding figure can be done for multiple variables as well in order to understand their joint distribution. Let us illustrate a bivariate analysis by considering total debt of the countries' central governments along with their military expenditure in 2010. The following figure shows the joint distributions of these variables as kernel density plots. The bivariate joint distribution shows no clear correlation between the two, except may be for lower values of military expenditure and debt of central government.
