hello and welcome again to the data
science and analytics track here at
pycon iu 2021
next up from mysore india
is gaziandra deshpande
they held a masters in technology in
computer science and engineering from
um
oh damn i got the thing wrong
and also a postgraduate diploma in cyber
law and cyber forensics from national
law school of india university in
bengaluru
they have presented talks and papers and
been reviewer and program committee at
more conference and more continents that
i can count
and gazendra is also a github certified
campus advisor and they lead the paidada
bellagavi chapter as well as the owaspe
agave chapter
in the talk today gajendra reminds us
that time series is one of the important
as well as lesser known fields of
machine learning
in time series forecasting we analyze
the time dependent data for predicting
long-term
and seasonally trends
please give a warm welcome to gazendra
deshpande and the presentation on time
series forecasting techniques in python
thank you for the nice introduction it
is wonderful to
speak at pycon conferences always
this is my second talk at pycon
australia online
so today i will be presenting a talk on
time series for casting using
python so it will be a basic talk
so we will not be covering uh any
advanced topics here
so in my talk today we will today i will
discuss introduction to time series
forecasting techniques then basic steps
in forecasting
then time series forecasting techniques
then python libraries for
time says forecasting and finally we'll
summarize and we'll take up some
questions
so
we need to first define what is
time series
so it's a collection of data points at
constant time intervals
analyzed to determine a long term trend
so you can see here on this slide
there is a picture which is
showing the temperature data so
temperature collected at
various
hours
and based on the historical data
we are going to predict the future
trends say for example we have collected
uh temperature data for say around
one month
okay then we are going to predict what
will be the temperature
next day
or maybe we are
collecting the temperature data
say for the month of september over the
years and we are going to predict what
will be the temperature temperature and
what will be the weather
for september 2022
so this is what we are going to do in
time series forecasting of course it is
not only related to
weather forecasting anything related to
forecasting and which is time dependent
we are going to forecast
and python has a has got great support
for these libraries
now there are some application areas
which i have uh listed
i may not be able to explain everything
but we will
but i will explain few
say for example
weather forecasting i have just
explained so it may
be used in agricultural industry may be
used by farmers also
so
before
sowing these seeds
a farmer wants to know what will be the
weather forecast for
next few days
so that accordingly he can
plan his farming activities
then similarly earthquake prediction
so
there will be
patterns of earthquakes so what will be
the
area whatever the environment conditions
are earthquakes are happening in that
area
frequently so based on that they try to
predict when the earth earthquake may
happen
then statistics
at least has got lots of
libraries and lots of applications
related to
forecasting
then also in pattern recognition
then signal processing control
engineering
then mathematical finance astronomy
so
many applications are there
maybe if i have to give
one more example then
say for example there's a website and i
want to predict the
number of
visitors in
next few days based on the previous data
so historical data is very very
important for us so based on that only
we have to
forecast
now there are
common time series patterns so whenever
we use
time series it is very important to
go for
visualization of the data so that
visualization of the data
will
give us
uh
the patterns in the
data
okay
so
you can see here there are five
time series
common time series patterns
so first one is level then second one is
trend
then
next one is um
seasonal or periodic
then cyclical then
irregular
fluctuations
so first one is irregular fluctuations
irregular fluctuations are nothing but
they are random in nature
and most of the times this data will be
random so we can't say that the data
will always going to increase our data
will always going to decrease of course
there will be a pattern
in that
but irregular fluctuations is a random
data
there is a lot of randomness
in in data
the next one is the
trend
so trend
shows whether the data is increasing or
decreasing so on this slide you can see
that there is a line here
so which is
growing so that means the trend is
increasing of course trend may be
decreasing also
but in this example it is not shown
then next is the seasonal or periodic
say for example in india we have got
three seasons one is rainy season and
second one is winter season and third
one is
summer season
so there's a lot of temperature
variation and there is
say forever in summer it will be a hot
season then in
rainy season there will be a lot of rain
and in winter season there will be lot
of cold
so
there will be different temperature in
these seasons
then similarly
these
seasons there are different periods say
for example some pattern may repeat
daily some pattern may repeat weekly
some pattern may repeat monthly
and some pattern may repeat
yearly
okay
in astronomy in astronomy also there are
some patterns say for example
uh
uh maybe
the moving cycles or the
uh
the constellations
they have got some pattern of occurrence
the next is the
cyclical so in cyclical
we are going to have
both patterns increasing as well as
decreasing
but it will not be a just
one phase there will be multiple
repetitive
phases
so you can see here that there is a
upward trend then downward and then
again upward trend so this is cyclical
then again uh downward trend so this is
cyclical so irregular fluctuations we
have discussed that it's a
random one
so these are the common time series
patterns and of course this also affects
our
our results
then there are some basic steps in
performing forecasting
so first one is the defining a problem
then collect the information
then perform preliminary
explorative analysis
then choose and
fit model then use and evaluate
a forecasting
model
so first one
is the defining a problem which is very
very important so
we need a problem to work on
we need a date
we need a problem to work on and here we
need to define what is our problem and
what we want to
achieve
so problem and goal should be defined
here the next is
collect the information so here we are
building the
data set by collecting the
historical data
so we also need
inputs from the domain experts
if your domain expert you can directly
work on it then perform a preliminary
explorative
analysis
so in preliminary explorative analysis
you are going to use tools like um
visualization tools
to identify the
kind of
data pattern
and
once you identify this data pattern then
you can
select a model to
fit
okay so in building a preliminary
explorative analysis you are basically
trying to find out the
pattern in the data by performing
several operations
such as visualization and so on
then choose and fit a model
here you are going to experiment with
different models so the one which gives
you
less error
compared to the other
model you are going to select that
then use and evaluate
forecasting model in your application so
finally you are going to
deploy it
in your application
now these are the
common time series
methods uh supported by stats models
package in python
so first one is the auto regression then
moving average
so these are the
two basic
time series methods
but
if you see the
rest
then
they are the combinations of first two
and apart from that
the
different parameters such as seasonality
has been added to the
basic methods
okay
so first one is auto regression then
moving average then auto regressive
moving average auto regressive
integrated
moving average then seasonal auto
regressive integrated moving average
then seasonal autoregressive integrated
moving average with exogenous regressors
and vector auto regression moving
average
then hold winston's exponential
smoothing and finally the diki fuller
test
what happens in auto regression is that
we are going to predict the
future based on the past data
so this past data is given as input to
the regression
then it will be
then it will try to predict the
uh
values
okay so we can just
try to test this
application
okay so before that
so let's see the source code so what we
are doing here is we are trying to
import the
uh package
import the modulator right then we are
also
importing the random module because we
want to generate the data set random
this is just for
an example
but you can really go for real-world
data set
then
fit the model
so here we are using the
autoric function
and it takes
two parameters data and lags
so here lags is the
pattern so here we are predict we want
to put the one value
then fit the model
then make the prediction
and print the
prediction
okay so next is uh
the moving average
so in moving average what happens is we
are going to
uh the average so based on
selected data points
say last n data points are taken to
perform the
moving average
or to calculate the to calculate the
moving average
so here we are importing the
arima model
uh
because we don't have any direct
moving average module here
but the first parameter is
set as uh
zero so that is going to make it as
moving
average so
we are making first parameters in order
that is auto regression and integrated
as zero
so
it will compute the moving average on
the random data
the next is auto regressive moving
average
it is the combination of
of previous two
models
that is the
auto regress auto regression and
moving average
and third one is
auto regressive integrated moving
average arima again it's a combination
of previous two but here the
additional parameter has been added
so that is the
integrating feature
so the integrating feature
is related to the
seasonality
trends
here
uh not seasonality sorry it's it's
related to the
stationarity
so what happens is um
if your data is not stationary then you
have to make it stationary whenever you
are performing
time series
forecasting
so that is what happens in arima model
then in
sarima model the seasonality values are
are added
so it directly supports the
it directly combines the arima model
with the ability of again auto
regression then
differencing then moving average
and at the seasonal level
so it is suitable for again univariate
time series
or trend or seasonal components so again
all previous methods whatever we have
discussed they are all related to or
they are all suitable for
uh
they are also double for univariate time
series analysis so there are some
methods which are suitable for
multivariate analysis which
we will see later
now all these methods in
inside they use a kalman filter
for
fitting the values
using the predict
method
now next one is
cerimo model with exogenous
regressors
now here you can see here that
we are using two
data sets
so data one and
data two
so
we are saying that
in ceramics
function the first parameter is the
data and second parameter is the
exogenous regressor
values
so
they are exogenous
variables are also called as the
covariates
and they can be
thought of as a
parallel input sequences
so that have observations in
at the same steps at the original
original series so that means
at the same time
there are
multiple values which are
being observed
so again this method is suitable for
univariate
time series
now vector auto regression moving
average
this uses
auto regression and moving average
methods
combined with
vectors
so this is suitable for this is suitable
for multivariate
time analysis multivariate time series
time series forecasting
so that's why you can see here that in
this code the list is created then
the first vector is
generated randomly where the second
vectors values are dependent on the
first vector
so basically you need here two sets of
values
so a row will be having both the values
vector one as well as vector two now
note here that vector two's value is
always dependent on the vector
one so this is suitable for
multivariate time series
forecasting
but
without
but without
trend and seasonal components
the next one is the holtminster's
exponential
smoothing
so this using
this method uses the
smoothing techniques
but specifically
holtwinster's exponential smoothing uses
triple exponential smoothing method
and it models the
next time step as the exponentially
weighted linear function
of observations
at prior time steps
then taking trends and seasonality into
the
account
so again it is suitable for univariate
time series but with
trend or
seasonal
components
then next is the
package called as
profit it is by
by facebook
so it's a open source software
addressed by facebook
core data science team
so it's a procedure for forecasting
time series data based on an additive
model
where non-linear trends are
fit with yearly weekly and daily
seasonality
plus holiday effects
it works best with
time series that have strong seasonal
effects
and several seasons of historical data
it's a robust
to missing data and shifts
in in the trend and typically handles
outliers
well
so you can visit this url for more
information
it is also it also supports
r
so it has been widely used in
facebook and its
applications
and the next package is the
darts
so it's a python library for
easy manipulation and
forecasting of
time series
so it contains very variety of models
from
classics such as arima to deep neural
networks
the models can all be used in the
same way using
fit and predict functions similar to
scikit lab
then it also makes it
easy to backtest models and combine
predictions of several models
and external
regressors
so it supports both univariate and
multivariate time series and models
then neural networks can be trained on
multiple time series and some
of the models offer the probabilistic
forecasts
now if you see this slide then it shows
the various capabilities of
a darts um
library
so for discovery it supports
seasonality and trend checks
visualizations
then new models
then for preprocessing it supports
normalization then interpolation of
missing values
then seasonality or
or trend
removal
then up
or down sampling
then it supports classic forecasting
models so which we have
seen in our slides
such as
auto regression moving average
arima ceremony max
etc
then it also supports it also integrates
the facebook's
profit library
and
many others
then for model selection and evaluation
it supports back testing
then residual analysis then research
and it also supports various
metrics
so in summary we have discussed about
basics of time series forecasting
then different times is forecasting
techniques
then different python libraries for
performing time series forecasting
quickly of course these are not the only
libraries we have also got other
libraries such as you can also go for
tensorflow so there is a nice
documentation available
so depending on your application
depending on your
depending on the
pattern in the data
you should be selecting the model and
when you are selecting the model of
course there are different options
so you need to select that option which
is
giving you less error as well as
the one which is
performing
better
compared to the others
so thank you everyone for attending my
talk
thank you
thank you kashendra
um we have a couple of questions for you
from from the audience i'm gonna read
them to you the first one is
what advice do you have for someone
starting a time series project for the
first time
okay so if you are starting for
if you are starting
a time series project for the first time
then
first learn about the
statistics and different statistical
methods which we have discussed
then you should have a
basic understanding of
these statistics and mathematics
then learn the basic libraries
in python
so pandas data frames
numpy's scipy
stat models they will help then you can
learn a specific
python package
which is which satisfies your
requirement of course we have got
many options but you should see which
satisfies your requirements better
and then
you have to select a domain
in which you want to
uh perform forecasting so if you have a
domain knowledge then it's fine if you
don't have a domain knowledge then you
need to collaborate with someone
and collect the data and once you have
the data use the packages and you can
start
the forecasting or even start the time
series analysis
um thank you another question they have
is about profit or darts
about performance
do you have any benchmark for the
performance of each
uh library which
um is more performant or better
maybe has a better use in other cases
uh i'm not aware of the uh benchmarks
but yeah maybe
we may use metrics and test
but i'm not aware of the benchmarks
so i think this is it and thank you so
much kajindra and i want to ask the
venulist people to give a big virtual
applause and thank you
yeah thank you
next up on this stage in about 15
minutes at 2 30
um eastern european time we'll have mali
and mangami talking about a beginner's
guide to gpus for pythonistas see you
then