Thank you. Good morning, everyone. Hello, please confirm if you can see my screen and you are able to hear me so that we can start the session, please write in the chat box. Hello, good morning, please confirm if I am audible. Please confirm if I am audible and also if my screen is visible. And also if my screen is visible. Okay, thanks for confirming. Okay, thanks for confirming. Okay, so we can start. So we can start. So today we will talk about the time series. So we will talk about the time series. So we will talk about the time series. So we will talk about the time series. So we will talk about the time series. So we will talk about the time series. So we will talk about the time series data. and what is time series data right so what is time series so as the name comes time series so it is a series of data which is collected over time of the same event so it is collected sequentially over time for example uh the number of kilometers you are going to walk in uh every day so you collect this data day by day monday tuesday wednesday thursday friday right so this will create a time series of your daily kilometers similarly if you have your sleep data so every time your sleep quality is measured as a say suppose our score is given 80 90 70 percent or number of hours so that is collected every day every night so you can also say it is a time series data right so the most important thing which makes the crime series different from uh yeah good morning everyone yeah so the most important thing uh to understand about time series and the thing which it makes it different from all other uh data is it possesses certain trends certain seasonality certain variations and some noise so for example uh if i talk about the trend so suppose the amount number of kilometers which every day you are walking is being increased every day in a week and then it decreased suddenly on sunday then again it increased till uh sun uh till uh saturday and on sunday you are on rest so you have not walked much so you can see the data which you are having it is going to increase uh i mean uh the value of your kilometers which you are going to walk is going to increase and then suddenly reduces so this says there is an increasing trend till someday okay now uh so a time series data may have a combination of trend so a trend can be upward or downward also then it has a combination of a seasonality like all of you works right so if you work in the weekdays so the number of work a number of hours you walk in weekdays is lesser than number of uh kilometers which you walk on maybe on weekends so you will see uh seasonality like monday to friday will have a same behavior and on saturday sunday you will see there is an increase or you may also see like if you are going work from home on uh two days a week that is monday and uh tuesday rest all you are going to work from the office so in that case also your data pattern will be different so you can see apart from uh the regular data which we see in the machine learning time series has certain classifications certain patterns which are termed as trend seasonality and there are also cyclical variations like long-term variation of a stock market we cannot say in the stock market that every uh year the same pattern repeats but it may happen after three four years you will get a pattern and again the occurrence of that pattern is not fixed right uh but for example if you consider a temperature or temperature throughout the year may follow a same similar trend for example the uh temperature which you see in the month of april every year it follows the same trend like uh more or less 42 degree plus minus two or so but if you see the same thing in october it will be like a 30 degree plus minus two three degree celsius so you will see it is a seasonal seasonal means every year it is reoccurring at the same time with some variations so that is the seasonality so that's why uh that's why you need to consider all those things and that's why the time series data becomes important the last part is noise or irregularity when you have all the things mixed together they will add to a noise or irregularity for example someday you fall sick and you have not walked at all right someday due to some decision from the shock market stock market crashed so that will become a noise and irregularity so you need to model that also as well in your time series so we will see all these things how we can do so just to uh make sure so you may see my pointer in the screen and at times i will be using a marker also so that will be if you if you are uh uh you just make it more interactive and to make it more uh concepts more uh clear so overall we will see about uh what makes time series data different how we understand there are stationary there are different uh trends seasonality cycles noise and dependence over time and we'll build one of this model we'll not build all but we'll build one of the model to see how it works am i not audible ganesh not able to hear me if anyone else who can hear me right right so uh ganesh please just check your audio connection if you can uh if it helps some settings may be uh there which may not be able okay so in this case in the case when uh there is a some uh uh issues in my audio on in my screen so please point me although i will try to be audible loud and clear okay so yeah thank you very much for the confirmation right so uh coming back to our discussion so you will see a component of a time series it will have a trend it can have a seasonality it can have a cyclicality cyclicality which is not seasonal but still you will see the patterns are being repeated but you are not sure about or after what interval irregular means it is having a noise you cannot model it clearly right so the time series best is best in modeling trend seasonality and cyclicality okay so going forward so this figures are trend is like this is an increasing trend it can be a decreasing trend also it can be a straight line also uh so you can say it can be a modeled as a linear or it can be a little exponential also but it can be modeled seasonality is like you can see this peak pattern is repeating after some set of interval if this is the time lines cyclicality you see that this is intervening repeating but not in equal interval of time and irregular means it contains some shocks it contains some noise which needs to be modeled okay so just to give you example this is an upward trend but it is a non-linear the blue one is a linear trend which can be modeled but it is an upward trend as you see the uh values are going to increase and this is right so uh so this uh what is data everybody of you must be aware but let's see from the point of view of time series so if you collect any data data which is nothing but it is an observation of some uh event at a particular time or uh phenomena you can say so a time series data is the observed value of the same variable or same event which is collected over time for example you take a daily temperature for one city but a cross-sectional data is like if you take a mass of 100 students at the same time roughly at the same time the mass they get in the examination so it becomes a cross-sectional data which you cannot model over the time so you cannot say it is a time model so first of all you need to understand where to fit time series data analysis so for this the the primary uh requirement is that data should be observed over time okay so one more critical observation which you need to have in time is today's value may depend upon yesterday's value observations can be correlated across time and the time itself can have trend and seasonality which can repeat over time so a classical example of this is the temperature so today's value definitely will depend upon yesterday's value but suppose if you say today's value is it depend upon the six months uh earlier value so that would say you will say that it was a different season and in that definitely not because summers are way warmer than the uh autumn or uh winter so you can see it may be one month it will only depend upon the last one month but not the whole year so that's why time series is special we are not considering all the data but we will see there is a seasonality is involved where we need to consider certain period of time to predict so if you say uh predict the temperature of tomorrow if you can take the average of last seven days it may give you a correct value but if you say take up average of a last three months or four months you may not get the correct value okay so that's why you yt yt yt is the time which you have today is dependent on the yesterday or day before yesterday but not always there may be a trend or season till where it is dependent so there is a classical examples of time series data or weather data collected over date over every day or rainfall corrected again week or day pressure winds business you collect the sales information demand revenue inventory to predict your future requirement that for example the next month how many units you need to need in your inventory similarly for energy energy may also have a seasonal pattern for example you want to predict the consumption in a city so it will slightly depend upon the month so you may see there is a increase in consumption when there are high winters due to use of uh that is very cold temperature due to use of geysers and use of room heaters and also uh there is a dependence on high temperature if there is a temperature is very high you may need to have more air conditioner and all right finance you have to see that how much prices are uh fluctuating you have to plan your investments and all in healthcare you see the daily values of the patients and based on that it can be predicted also it can be predicted that number of admissions based on the number of patients released in your hospital today they may come tomorrow or day after tomorrow so they may have a similar trends if it is increasing trend so that may be because of some epidemic or some uh in a uh small period of time and a lot of increasing trend so that may be due to some uh small epidemic or some type of disease which is prevailing and communicable as you have might have observed in the covet 19 so similarly you have to predict if example the transport like number of the passengers are going from a city at a particular uh square at certain uh location maybe one hour you have to see that 8 to 9 a.m you may relate that the traffic is lesser that's why you plan to leave early at 8 to 9 a.m rather than 10 to 11 a.m which is the peak traffic hours similarly in the evening time if you see the four to five four to six window is or five to seven is very peak so you may plan to leave at 4 30 or so so that's why this things which are which are seasonal they are repeating every day it's not like your machine learning can just predict okay this is the traffic you will get so you cannot generalize right so those things require an additional considerations and that are handled using time series analysis so we can uh yeah so that is what i have told that how much traffic should you expect yes so uh there is a question what is difference between cyclical and seasonal right so as i have given you an example that uh seasonal is a example like uh which you are a behavior which you are observing every week for example you are observing that is in the fixed time that is every saturday or every sunday you observe that your number of kilometers which you walk are more than usual so that is seasonal right right if you see a stock market so suppose a small uh stock market made a dip it has an increasing trend and after that it is dipping so sometimes it dips after two years sometimes it has a recession kind of thing after every four c four years so it is dependent upon some other thing which is not seasonal but still it is cyclical because it is happening after some time but that some time is not fixed in seasonal you will see it is happening after fixed time like it is repeating every week right clear action is it okay so moving to our uh next point so how we observe so we observe the past and we predict the uh uh future values in the time series so our time series if you look at it so it will show you the observed values uh so suppose uh you may start from the beginning so why one may be your first collection or first sample then it will have one two three it is the increasing timeline so the time index t which we are referring here it may be an hourly daily weekly monthly or quarterly for example the temperature you observe is hourly then what are the daily values so there can be a daily like uh sun sunrise or sunset values weekly weekly is like uh you can also have a amount of uh your uh working hours suppose eight hours a week you uh eight hours a day you work someday more someday less so you can say that weekly this many months hours hours you have stream time and all so monthly is like amount of expenditure and all you can plan right so you will see the time the data is being uh congenerated or collected after every cycle maybe it is a hybrid sorry every period which may be a daily weekly monthly also so the time series forecasting means with the help of values which are given to us from uh beginning to the end we are forecasting what would be the value estimate as yt plus one yt plus one means till today if we have the data what would be the value of that event tomorrow okay so the concept of notation of the lagged value so suppose we say today we observe using yt and yesterday is yt minus one yt minus two periods then we have a yt minus three suppose we are recording the number of kilometers every day which we are walking and we calculate lag one lag two lag lag one is the value which you calculate it with a day the value which is uh associated with the previous day the trend which you follow lag two is if you have a two days difference so if it is a value 200 and if it is a lag one you will see the same value right previous day if you see there is a two days difference so you will have a 200 and then you will have it as a t minus two also 200 value now if i am talking about uh kilometers and if i am talking about that lag six that is or lag seven that means i am talking about the week day right and i am talking about the number of days in a week so if i am talking about uh monday here so lag t minus seven will also give me here the monday and if i see the value of uh today and t minus seven that is that value which i observed last week the same day is similar then means it is very high correlation with this value and there is a trend of a seasonality that the value is being repeated after every seven days okay so just to know about an example that how there can be an increase of trend or a decrease trend so you can see uh this is a five days consumption of electricity 8 28 50 so you can see monday tuesday wednesday thursday friday so there is a increasing trend so friday is always higher so this is one week trend if you see another week also same thing if you see another week also same thing so you will see that this is the trend is follow if not then there may be a noise like uh is there any uh friday which is usually hotter comparatively due to which the amount of uh amount of energy which was required was more right yeah yeah so this we have already discussed this uh cross-sectional versus time series suppose as mars of the student will move forward so if we want to form uh uh forecasting problem so from observations so we need to analyze the historical data uh explore patterns choose model fit model forecast and evaluate so you will see this is the thing historical data machine learning algorithms mostly dependent on the historical data and the explore patterns this is done implicitly but in the time series you see there are explicit pattern that's why you need to model it differently with the help of some different type of uh statistical methods okay we'll see so our trend let's come back to the trend and look at it at a deeper at a more depth so suppose there is a long-term increase or decrease in that level of series for example there is a population growth sales or technology technology adoption so the most importantly if you see if their trend is linear it can be modeled over time like using a uh using a line or a linear equation that is a plus bx where x is the intercept here it will become the x value will be like the previous values right so a is the your intercept and b is the slope okay so value of y when x equals to zero that is your initial condition okay and slope is calculated using sigma xy sum of this both the values divided by x square that is the value which is you are having at a particular t so what is important here to note is that when we are analyzing the trend we are not going to calculate like a linear regression all the values which were there but we will see only part of it that's why it becomes a linear for example the population growth only follows for last three years the correct trend otherwise the trend was different so in that case your model may not be correctly model your value so just to calculate if you want to calculate with what pattern if it is a linear trend so if you are given a data of annual sales for five years and for year 21 to 25 the sales is uh 10 12 15 18 20 and this you centered across a mean or a center value so you will get x as minus 1 minus 2 and then you go further to 1 and 2 so this will become your 24 and 25 years so you can calculate the x y and you can calculate x and with this uh slope and intercept you will find that the trend which is fitting this uh uh data is uh yt equals to 15 plus 2.6 x so now you keep any value that is uh yt will give you the yearly sales and this is the one x will be your value which is the in terms of which is here so it will be minus 2 to 2. so if you put each in here you will see that it is approximately directly forecasting the sales so this is modeling this trend so this is how you can model a trend in a time series so as we discussed that seasonality can be a regular monthly sales or something which can repeat over time for example the energy consumption or a monthly retail sales so which repeats over time because due to due to festival suppose uh in month of october you will observe there the sales are more if there is an electricity you will see in month of summers or in month of uh some uh winters you will see that the sale is more for example the sales of ice cream so that is more in the summer season in comparison to the winter season so you will see there is a trend like it is going up and then going winter down then going up and down so you will see there is a pattern here like this so suppose i will use this like a pan so suppose you will see the trend it is going like this and going up going up so the peaks which you will see so these peaks will be corresponding to the summers and this will be winter so the if you see the sales of a ice cream right so it have a seasonal value so now to understand this seasonal term uh we need to understand what is the correlation between the current value and our value which is recorded previously so we need uh to know that what is the seasonal value that for example if i taking if i'm taking the sales of a monthly sales over a year so in that case i have to see the lag 12 to get the same seasonal value or same month value so auto correlations checks whether the series is similar by shifting into the k time steps so we compare it with the observation so yt would be compared from the yt minus k the k maybe here will be 12 for monthly for uh for a monthly data you have to repeat till 12 so it may be a seasonal okay uh we will see all these terms what is this rho k so how this correlation you calculate so why it is auto correlation so auto regression is predicting from itself so auto term itself so don't confuse about regression you will predict the value but auto is about the self so that's why it is auto regression correlation correlation also it is with the self but the previous recorded value so previously t minus one t minus two values all of its same uh observations or the same parameter so you can define the auto correlation using the variance and using the mean okay so you can calculate auto correlation as a covariance under root variation of x t that is the t equals to when lag is zero and when t minus k okay t minus k means you will calculate the correlation okay t minus k means you will calculate the correlation at xt of xt with xt minus k and this k will vary from k will vary from this k will vary from k equals to 1 to suppose it is a seasonal so monthly uh if it is a monthly data so you may need to repeat it well but it may uh you can do till k and that k you can determine using the rho k value okay so whenever the rho k value that is a auto correlation function so this value is around 1 that means it is highly correlated that means xt and xt minus k are highly correlated if it is a negative then that means that is a highly opposite so suppose this is your correlation and this is correlation so for this both uh intervals you will see this value will be correlated with this one so this is when the rho k will be one when this and this is there that means the correlation will be around minus one so you will have the maximum one to minus one values okay so going further so let's see this with the help of the example suppose this is the time series data which you have for 16 values of four years and each value is collected quarterly that means this is the value of q1 q2 q3 q4 year one then year two q1 q2 q3 q4 then you have q3 q3 q4 okay so what we need to do to compute q variance because you need to compute the mean then you can subtract the mean and then you can calculate the variance and once you compare this with you get the variance with like zero this is the value you will get you may try yourself also once i can share the slide but these are the calculated value very basic maths using the statistical mean and variance which you are studying from long time then you can compute the auto k covariance at each leg okay so it is denoted by ck and you will see it will repeat it from t equals to 1 to t equals to n minus k okay so this is repeated from t equals to 1 to t equals to n minus k where you will have a lag between t plus k and dt so for k equals to 4 so you will have d with d5 d1 you will compare d1 with d5 if d2 with d6 d3 with d7 and so on so for each of the values you will calculate and you will get for example if it is 2 then you will calculate with d1 and d3 so and you will calculate all these values so you will see the correlation that is ck and rk rk is the rho k which is divided by this is at your lag 0 and this is at lag k okay so you will see there is a correlation at lag 0 1 2 3 7 and now you see the lag 0 there is a correlation 1 after that you see there is a positive correlation correlation of 0.7 again you will see there is a positive correlation auto correlation at season 8. so with this you can have a idea that the seasons which you are getting the seasonality is 4 because that means in every quarter suppose it is a month uh quarterly sales and the q3 sales is always higher right so you will see there is a there is a positive correlation with the seasonality 4 so you can say the s equals to 4 is the value so what we need to see for the seasonality we have to see three things so we have to see the peaks at multiples of a fixed lag so we see r4 was 0.7 r8 was 0.43 so it was a positive so it gives us the observation that the seasonality can be 4. okay roughs at half period that means this is a negative correlation with 0.6 and 0.5 which are negative so near zero values at the end of the odd lag this is what a cyclic trend will look like because every time you will see after that it will again follow the same thing so lag 2 4 6 or they will approach so this is the value you will get in the if you average it okay so how you conclude what would be the seasonality so as we see there are frequent uh uh frequent uh you know uh peaks at uh period of four so there are quarterly you will see the period of four after four period there is a uh seasonal behavior okay then you can compare it with a some market analysis that why it is so then you can correlate with it and if it uh confirms with your uh market uh understanding that uh suppose your sales are higher in the winters or your sales are higher in the summer due to some reason if it is a year condition conditioner definitely the sales will be higher in the summers and low in the winter so there the the lower values will be having a correlation with the lowest values in the winter so you will see every year it is repeating for a q3 you will have a sales a spike okay you can also do that you can again do a yt minus yt minus 4 and compute the ac4 uh acf that is autocorrelation function again by having a differencing so you compare you subtract the current value with the value which was there in the e minus 4 and see what is the value so you will see that this type of a spike which you will see that is disappeared because now once you defer it there will be us some normal value so just to explain i was saying you the seasonality so suppose you subtract this value with the this value then you will left with a very less values right so that's why it is so you will not find a peaks in the autocorrelation function so that confirms that the serial serial seasonality is not there so or you can conclude and confirm with this so this is a very basic code which you can try you can import the numpy and you can try this uh acf model with the quarterly sales values which we have used and you can print so you will see this is how the variation is okay and if you plot the correlation function you will see there is a positive correlation there is a lag for positive correlation there is a lag for always there is a positive correlation that means it confirms the there is a seasonality why we are having here uh eight so the n lags means number of legs which we are going to our total number of legs which we are trying it is total lags eight but you find the best or the most value because this now the trend is going down so anyway you will not have so that's why we have the n lag as eight you may also try with 16 and you will see there will be peaks four eight will be having a little lesser peak 12 again you will have a little lesser peak because you will see the seasonal value of one year is also correlated with the value of the last year not from the last to last year so there will be a slight uh less because it is also following an increasing trend okay to sum up what you have to do to understand seasonality you collect the series then you have to see the even spacing there are some four plus cycles you can find after plotting okay okay now if there is any trend you need to uh remove if it is present so what how you can do remove the trend that we will see we can remove it by differencing then you center the series by subtracting it from the mean and then apply the auto covariance and auto correlation you calculate at these as each leg you get the values and you see so there is another test known as a lunch box junk box text so if your p value is less than zero zero point five then it means there is a uh it is fine so there is a auto there is a correlation and you find the seasonality if it is a uh greater than zero zero point five then that means there are noise so this is just to reconfirm although the main thing which you judge based on that is the auto correlation function values so it peaks at multiple of s s will give you the seasonal okay so the question is does the increasing trend with a specific pattern for a short period as shown that can be referred as a cyclical as it's not repeating following right so again for cyclical that modeling cyclical is little different for cyclical you need to model using uh different model you don't you cannot use just a seasonal variation first the the understanding is we need to check step by step that there is an increasing trend then you model using increasing trend and remove that increasing trend then if there is a seasonal behavior you model that seasonal behavior and remove that after that you see there is a cyclical trend so for the short time right so if you follow that our time series is following a cyclic trend for x to z then z to uh some other uh say t plus 20 it is following some trend then again it is following season in that case it is random you cannot say it is just a uh cyclical right but when we are going to model a time series we are going to consider all these aspects that is cyclical seasonal and trend i will show you in some time the the model the final model which you are going to make from the time series will have a combination of all these things it's not the one thing so all those trends will be covered okay so just hold on for some time after that we can see if you still have this question then there is a cycle so which have a long-term behavior and also irregular noise which you are not able to model okay so uh you will see a time series can have a components uh prediction model of the time series should have certain combo all of you should be agree at the time with me that there may be a trend there can be a seasonality there can be a cycle and there can be a random uh behavior also that came using some shock so all these things needs to be considered while modeling a correct time series right for correct forecasting or accurate forecasting so we will see our ideal model should have a trend analysis should have a seasonality analysis cycle and random also so how you do that so to capture this part this is a random part you will have a moving average okay so moving average is defined as the average of last uh five values for example i have given you an example of the temperature so suppose the uh seasonality is about following a a pattern which is when the data is highly correlated with a weak data which is of the previous week but in case of uh uh moving average or if it is not a uh seasonal or a trend if it is a cyclical then in that case you need to see uh it can follow till certain k values so moving average will give you example for example uh for temperature last four days temperature may be highly correlated with the tomorrow's temperature but if you just see yesterday's temperature you may not be able to predict and if you see that average of one month temperature that also may not be accurate but if you take the last five months or last five days uh sorry last five days temperature then it is a highly correlated so we calculate this using this correlation partial correlation function and the uh moving average means we are just taking the values which are last four or five days or last t minus till t minus q okay but each value till uh t equals to zero to lag t minus q we will consider so that's why it is known as moving average so c is a constant mean of the series which is constant because the series is stationary and epsilon not t is the noise terms the times the time series and are the theta one theta two are the constant so once we have more uh data and uh we model that our time series we need to apply and check the moving average so these parameters value you can estimate so suppose you just want to take at the lag one uh value at the lag one so you don't you need to just compute the xd as c plus epsilon not t so you will not have theta two terms theta three and t minus three t minus eight terms okay so uh if you want to uh plot this so for that you need to have a plot acf you need to plot auto correlation function and partial auto correlation function that i will explain you so you can see this is the history of the uh market data so it is following this type of data so you will see how moving average is able to uh predict the values of last five days using last five days so you will see there is a very small correlation you will see that there is no uh single thing there are multiple things there are multiple things this series is mix of many things so now if you calculate a rolling average plot rolling average is the average where you take a 10 day average rolling value 10 day value okay so if you take a 10 days value and try to model this 10 day value as a rolling average and see you will see your the 10 day average is very close to the one which you are going to actual series but suppose if you make it in a 15 day average will it be good no it will just become a straight line like this okay so if it is a 50 day average then it will be not very good so you see that what is the value but now the biggest question is how to analyze this day then how would you know the 10 day is the value you have to see you have to see the last 10 days values only why not 15 days why not 12 days and why not 8 days so the answer you will get from the partial auto correlation function okay so uh that's the relation between so once you plot this uh acf and pacf so uh acf will give you the values at a particular lag so you can see your trend is going from one to uh so you at the last yt it is highly correlated and then this correlation is going down okay so it is although not seasonal but you can see each value with each lab view there is a correlation so that means there is some part of the cyclic variation which is you are able to model using the moving average average so you can see here you have to consider till what you have to consider because after this the terms become less than 0.2 okay so you can see till here the lag equals to from where you have seen the lag goes to zero to lag equals to 70 you have uh you have plotted the partial auto correlation and you have seen that auto correlation goes down after this so you need to consider till only seven legs okay till 70 legs if it is further going down very going down so then that means it is not correlated so you don't need to take the moving average beyond that okay so what is the value of the partial auto correlation function extend uh x uh tells you so then how it is correlated with the values at previous like like equals to one like goes to three like goes to four like also you see all there is a there is a certain certain certain uh uh certain correlation is there but after that you will see so there is some positive some negative some positive some negative okay so this partial autocorrelation will give you the value of the covariance at t with t minus k from t1 to tk plus 1 so you can see you have to check for all so that's why the partial correlation function gives you the direct like k relation from 1 to k minus 1 okay so now how to judge whether to take that autocorrelation function value is enough or not so those autocorrelation function value will give you that idea that behind that there is very minimum correlation okay from that line so pacf isolates the direct correlation between the current observation and the observation at like k so controlling for the influences of lags in between so you will see after that there is a no correlation correlation and at p value where auto correlation uh p p means the lag 70 it often shows a sharp cutoff after that there is no further so it will tend to follow the value near zero okay now once you model all these things so once you so once you model all these things you need to see now or you need to understand the stationary also also stationarity so a series to come to the prime series before analyzing we need to make sure that it follows a stationary behavior that means means should not live systematically and variance should remain seasonally reasonably stable okay so how you can calculate you can see if our series if you are having a increasing trend and you will see the average will not be be if you say last 20 values average which should be around last 50 values or or last uh next 10 values so that average will not be a constant it will keep changing okay so for that to understand that whether you have a seasonality or not you have a stationary you need to see whether your time series is following a stationarity so to check stationarity you have to plot okay and with the help of differencing we can remove so differencing can remove the trends also and strong 10 which can create a which can create a further uh you know auto correlation so we need to difference to check the correct stationarity so you will see if there is a time series given at yt and there is a observation at yt minus one if there is a increasing trend you can subtract it with the value of yt to yt minus one so this will remove the increasing trend and at the first uh yes so when uh so please hold on we will see all the steps they will come together so uh when we solve up formal model we will see that we apply all these steps one by one before final computing the prediction okay so just hold on we will do that after that you can ask puneet so you can you need to do a differencing so that you will see there is a there is a fixed pattern which is coming okay so there is a linear increasing trend that will be lost so now if you take a second differencing second differencing means again we are value whatever we got uh yt that is a delta yt is delta y is uh delta y is yt minus yt man still you will have see that there is a trend then you can again differentiate delta yt minus delta y minus t minus one and it will give you a delta square so usually t equals to zero or d equals to one is common while d equals to two is sometimes appropriate so we may not always need to go beyond d equals to three okay now here comes the solution puneet so we are discussing the arima model so we have seen auto regressive where we have calculated the auto correlation between the current values and the lag values so that will be captured using a auto regressive and a parameter is given which is p so this will give you the lag value that you want to calculate at lag till lag p so this will give you a seasonality integrated this is for the trend you use differencing to address non-stationarity you make the series stationary by making the differences and then moving average so this mostly handles the cyclic or past forecast errors so moving average so use past forecast errors and shocks okay so this is the moving average we'll use so we have seen all these three things separately how differencing works how auto regressive works using auto correlation function and moving average so that's why the model to forecast the time series which you use is known as arima arima that is auto regressive integrated moving average arima model so this is one of the classical model which is used for the time series forecasting and it consists of ar auto regressive i integrated which removes the trend using differentiating differentiating or differencing and then moving average okay so why do we need arima so yesterday value may contain a values about today's value a recent unexpected shock may affect the next observation so it may be a cyclic or it may be a uh uh uh uh it may be a you know uh some raw changes a trend is there which it makes the stationary series non-stationary so the curry arima model combines these three ideas into a single flexible forecasting framework so that's why we will be able to calculate or forecast correctly with the help of arima so which is one of the most uh used model so it contains auto regression auto regression means the ar model so you see there is a constant and then there is a this is the constant for uh previous term so you will see how yt minus 1 yt minus 2 will t minus p it is related okay so if you use ar1 so phi is positive that means there is a positive correlation at e minus 1 okay then moving average so this is the c value this is the average and then epsilon so this will become on the moving average 1 2 and 3 like q okay so what ar does ar uses the past observation and ma uses the past shocks so both describe the temporal dependencies but in different ways okay because uh stationary uh auto correlation is more with the with a some type of uh pattern and then i in arima is integrated that is differencing so if d equals to 0 there is a no differencing in the series that means there are no trends if d equals to 1 that is first difference you can subtract the yt to yt minus 1 d equals to 2 okay so the b is known as backshift operator that we will use for simplicity okay so if you have a arima model 1 1 1 means you have phi 1 so this is the auto regressive term and then this is the moving average term okay this is the mean constant term okay and differencing and differencing equals to 1 that's why you have a delta here delta yt minus 1 so you are modeling delta y rather than yt minus 1. okay so back shift it just give you after the differencing so this is the general arima equation will become okay so this will give you the differencing and this will give you the value now the question is p d q right so you have seen here in the arima we are having uh where is that yeah so arima is denoted by p d and q so q is for moving average i is in for integrated so how to find this correct value of p d and q right so d is for differencing so let me first complete this uh model yeah so what is the use of auto correlation function to calculate p what exactly moving you average used to calculate q and uh yes so right so moving average will tell us that how if if our series contain a dependency or a behavior that is handled using moving average we have seen that moving average separately so till where the partial auto correlation function is there yes so let me just first complete this uh choosing p dq value and then we will discuss more about this okay so with this you will understand so suppose you want to get the p dq values as modeling decisions okay so you know the p value is required for auto regressive d is for differencing and q requires moving average so this the answer to your question is the answer to your question is this slide so this is the primary request site or the request site where your the request site or the request site or the request site or the request site or the request site or the request so if your values are correlated with today's values and also there are unexpected shock and also there is a trend which is non-stationary so for all these things if it is there then your arima model fits in right so the next question is how to identify this p d and q value so so have you seen as you've seen the p value requires the autocorrelation function so we see the value of autocorrelation function and partial autocorrelation function to to calculate p and q values and d is for differencing okay so once you uh once you apply this and calculate the uh plot acf and dcf so you would first of all the first thing the very first thing you have to see the differencing so you determine whether the uh series is appearing uh stationary or not if it is not appearing stationary how you will get calculate whether it is stationary or not by seeing that your average uh is so it has a constant mean and constant variance if it is having constant variance and variance that means it is stationary if it is not a stationary then you have to apply the differencing that differencing maybe d equals to one you can start with and you can see if you get the again the mean variance is right then you can move forward with the d equals to one most of the time d equals to one is sufficient d equals to two will come in later on if for the complex model then you have to see the plot acf and pacf these are the auto correlation function and pc pacf this will give you what p and q values are good okay then you need to estimate the model using information criteria so uh and then you have to check for residuals which are the constants in your equation and this you calculate using some method of calculating error like root mean square or you can use a mean average uh or least square errors you can calculate the other one you can select there is a model you can select yourself your other thing there is a model you can select automatic once you select the automatic arima itself selects the best model and give you the output so you can follow either this or this but for the understanding this one is the best so we should follow this one okay so let's uh go to the time series so the first panel shows the time series here so you can see there is a increasing trend it's not stationary right so you can see there is a repeat of the trend so you may try with the d equals to one so you have to check stationary so stationarity series typically have constant mean and constant variance okay so you can see here the series is not stationary and you can also check this by augmented decade fuller function so if the adf static value is uh negative or it is uh less than the critical value then you can see or you need to have a p value the other one is you have to see the rolling mean and uh and you need to check the standard divisions you can see here there is a rolling mean okay which is increasing so that's why the value is not less than zero zero point five so you need to apply d equals to one so when you apply d equals to one so this is the first differencing yt equals to yt minus one so this is your original observation so after differencing you will get 10 10 okay so d equals to one is enough so you don't need to go to the second differencing now you need to check the autocorrelation functions using uh the values of rho k so suppose this is the data and you are calculating the mean uh you are calculating the uh autocorrelation function so this will suggest till what uh value you need to calculate the moving average so this will give you the value of this will give you the value of q so you will see and uh when you calculate the partial autocorrelation function so till there the seasonality is there so you need to see the where the pacf uh goes down to the zero so you have to calculate till here the lag okay so once you have a value of p and q identified so uh you can uh so suppose let's go further so here like pattern you will see so for for autocorrelation function you see in this series uh these are the positive at the lag one at lag two lag three lag four lag five it is going to increase and with the help of partial autocorrelation function you can see after 15 lags or after this it is very less so you need to see till here and what is the best lag you get here so the p value will be determined where acf tails off okay so where acf tails off you see here right so after 10 lags there are no particular lands then uh you can determine q as the lag where your pacf tails off or your acf cut off so tails off means this is one the value where you will see so you will see that that we have plotted till q so accordingly you can select the p and q values and then you can fit the candidate using arima model so you can use those values and inside arima model you will see there is a alkaline information criterion and bic which is basian information criterion which is used to calculate the aic and bic value so if you have the lowest aic and bic value after fitting the pd and q then you have to uh select those values so you can see from here it is a uh case number of estimated parameters and l is the likelihood how model fits and n is the number of sample size okay so uh you can try that after that the next thing is compare the model and diagonalized residuals so you have some uh error term in that where you do not have any value so you may start with some values and you start calculating mean average error and root mean square error with the yt which is the actual value and yt is the predicted y cap t and with that you can see which one fits best and accordingly you can do the uh this error calculation and see which one fits best okay and accordingly you can uh upgrade the value of pdq so yeah so this lunchbox test this is for the initially autocorrelation function but p value will be less anyway wherever you will select the correct line okay so just to summarize now here all the points so you have to see the plot the raw series so you see there are certain uh uh certain uh uh non-stationary content if it is non-stationary that means mean is not constant and variation variance is not constant so you have to see that p value will be less than 0.5 using adf function auto uh this function which is there or you see that there is a rolling mean and standing standard mean which is not constant so you can apply with the d equals to zero your series was like this and if you apply d equals to one that is first difference it will look like this second differencing will uh lead to errors because you may have too many uh fluctuations so it may it may it is not recommended if you get a d equals to one and after that you get the constant average and variance then it is fine then you have to see the plot the acf and pacf plot auto correlation function and uh pacf so the pacf plot will suggest you the uh place where after where this is zero so you don't need to take the values after that so this will give you the uh p values that after that you need to cut off so you just need to take the p till here and you will also see the acf cutoff so where acf cutoff so you will see their acf cutoff so you can check these values so you will have i this is for differencing and then you have ar and ma for that you find it using a acf and pcf plot and now you try you see here p equals to one p equals to two so with the p equals to one and p equals to two so the value which you will take from here okay so this will give you the most probable values that means after this you don't need to consider but now again you have to see which one gives you the best output okay so you see two is the p value d equals to one here the first differencing and q is the value which is the second q is the value for the moving average so now once you fit in this arima model you calculate you calculate you will see aic and bic values so these are the parameters which are calculated based on the arima model we are not going details on why aic and bic give use the minimum value but this is what they use uh considering uh the method so the one model which will so you will see the one which will give you the best values for aic and bic curve so that is the lowest value so you see the combination which is giving you the lowest value that is what you will select okay so then you can compare the values and you can see the output and after that once you diagnose that means you are comparing with the residual analysis and finding the values of the parameters okay so these parameters are uh uh which will be learned okay so the question is how to uh on board tail-off part okay so uh so for p so you will see the value which need to see for the p uh where is my mouse here so the value which you need to see which is having the highest correlation so it was zero but you cannot take zero so there will be one and two so you can see after one and two there is a very cut off cut off means you can see the auto correlation values are further going down okay and for moving average you will see after moving average that is a one moving average is one then after that you see this psc plot again the values are going very low side okay so there is another question on uh if we are using linear regression or this to make time series stationary okay so or linear regression so linear regression itself is about non-stationarity so if there is a non-stationary behavior and that's all you have that can be best model using linear regression you cannot need to have time series but time series you are having multiple things which you are uh multiple type of uh uh you know uh pattern which are best mod model using the parameters which we have studied or which the patterns which we have studied so time series have this many parameters only or like seasonality trend or some type of cyclicality if it is not there then it is just a uh random values which you can just model using machine learning it does not follow any trend because machine learning it the problem with the machine learning and the time series is the time series is more specific to the uh specific type of pattern but machine learning is a general term which you can use anywhere but in time series you need to have certain pattern and they are repeating if you apply machine learning it will just give you a uh it will learn a lot of features but then you have to have a lot of features here you just have a single value that is uh being given you don't given so many values so that machine learning then that case requires so many parameters so many observation but with the help of time series you are able to do that so yes so uh yeah so maybe you missed my pointer but i anyway i was just saying that you need to see that where these values are going down and accordingly wherever the values are greater than uh where they are larger much larger that gives you the correlation so the the acf and pcf plot just give you idea about the correlation you can select whichever you want and you see what gives you the best aic and bic values okay so you have to understand how it is code moving average means you are making it to link to last few uh uh last few uh observation and same thing with the lag k means you are making to fit to the exactly that particular lag case okay for linear regression you don't need to make the data stationary now you can just uh apply whatever you have uh which uh if you want to i'm not not here teaching you linear regression so linear regression don't need to stationary you don't need to apply the time series uh principles or time series forecasting to the linear regression linear regression itself is enough and it takes uh different parameters time series is different but my i'm trying to explain you how it is different and how you will get better prediction in case of uh using time series forecasting than linear regression so we are not talking about the linear regression so just let me finish a few more things are there so uh if there are any further questions we can take again so arima you can complete reasoning workflow plot stationary you have to see differencing so you need to have a acf pcf fit and then you have to diagnose and then you can do forecast so for example you can also use the pandas and stat models which is already present in the uh python uh so you can use from the arima model okay stat model is there and then you can use the model fit and you get the model uh fit and then you can just print the value so you can use the forecast evaluation using mean average error room mean square error mean average uh this uh this uh this uh for forecast evaluation whether your forecast is uh correct or not and then you can uh see uh what are the challenges with the arima so most of the time we uh make mistakes like we use arima without understanding whether it will fit data or not uh we may defer the series over like we may use d equals to two even if we uh not required we use choose p and q arbitrarily but actually we need to plot acf and pcaf and we have to see what will be the correct value which will work with this and uh yeah so using training as a so we just use the training error to learn the parameter we ignore the seasonality so if now there is a very high seasonality is there then there is another model which you need to use is sarima model as a rima okay so it may not be and very accurate okay if your pattern also have that so the forecast we need to make sure that so there is a one more uh algorithm known as sarima so that use uh some specific we have pdq p is seasonal auto regressive order so that looks at the lag from previous season example from last india but seasonally seasonality what it does it tries for every seasonal cycle okay and uh so pdq we have used but there will be the extra extra term that is as length of the season cycle which they will take okay so that is how as anima will model more on that okay so what example weather forecasting you can use and this is the suppose this is the data you have and you can apply the differencing okay so some questions are there i will answer in few minutes so i'm just showing you if you want to put the temperature trend then you can forecast using the five steps uh just you need to fit it in the arima and you can see the values of what is the acf and pcf value you can use the d equals to one if it is a linear differencing okay and then you can apply the arima okay so then a question is the question is uh uh okay so the question of why value hence you sir can okay okay okay okay got your point so do you know neural networks so what is the problem with the neural network you know what is the problem with the neural network suppose you are given you want to suppose you want to go from here and you want to go to the market to buy vegetables okay which is just uh 50 meter or 100 meters away from your home what you will take you have a bicycle you will have a you can walk or you can take a car out of a garage and take 10 minutes and then you go there and you come back what would you choose you would choose side bicycle or just a bike right because the mod the the data is not that much parameters we are giving we are not giving uh too many parameters like sequence to liquids like you are not given it follows similar but it will use the seasonal parameter also one more extra we have not used seasonal parameter with two uh so sorry my model it uses so we are not going to study that so uh to answer to this first let me first answer this rnn part and all so the answer is very simple if you can model the problem with a very simple solution which does not require so much a computation does not require so much training does not require so much calculation which can be just a pretty straightforward so that is the one with the help of the time series model why need to apply sequence to sequence model complex rnn model which in fact takes a good time for inferencing it's not very as fast as this arima model or time series focusing so why you want to use all those things with so many parameters if you use a linear regression then you need to have more number of parameters to capture trend cyclarity seasonal so many different different value of temperature pressure and all so in that case it will become a multivariate linear regression and uh again but here we are able to do all those things with very simply enough so definitely you can use more complex method like rnn it may give good result or bad it may be there but still you can still use sequence to sequence model but still you see the complexity the amount of training and amount of uh uh uh complexity will be more right you can use any data in the kegels so you can just search in the times uh so there is a aeroplane flight uh data in the para in the kegel you can try you can uh just search uh in the you know you can just search in the uh kegel you can try there will there are a lot of uh data set and the code will also be available on that but most important thing uh which i want to explain it the choice of the model even if you study machine learning if you for a very simple task if you start using cnn will it be optimal no right it if it is not required why you will go by uh why you will suppose there is already a temperature good enough why you will need a ac although still ac will do that and you have a certain uh good ventilation the answer is you need to use the appropriate model for your appropriate solve to solve the appropriate problem it doesn't mean that for every time when you your work is done with a simple uh pickup when you are why you need to bring a truck right so that is the basic understanding to predict yes you can use it for the stock price prediction you can use it uh for the and the data is anyway it is continuous across time you need in the time series so there are some trend basic basic characteristics with time series data which has which i have already you i have shown you so the basic characteristics of the time series data so what is let's go to the first slide where you understand where to use and why to use so the basic characteristic of the time series data is you have a trend seasonality variations but still you will keep yes so you can use for the stock price prediction arima you can use if it does not have a seasonality so it does not have a seasonality because uh uh because you do not know that every season it behaves like every season so arima can be used definitely you can use right so to coming back to the time series data time series data we are not using complex neural network we are not taking thousands of features if you give a deep learning a cnn model okay you give just a try giving just one parameter it will not scale it will not give you the correct predictions it will not model it will just give you the average of the values which you will get unless until you have more number of parameters then only it will give just based on the yt based on the yt minus one it is very difficult from a cnn and all to get the correct values even it is a sequence to sequence model and all so that's why in the time series data we we are able to with the help of the signal barrier itself we are able to model trend seasonality cyclic variation and all okay so that's the basic difference you need to keep in mind if you find such type of data it is best uh if you have such type of data which is having the uh this type of pattern then you can best use the time series so okay one so this is the last question we will see so this is the last uh question we will see in uh okay series of model when you have multiple input steps it must be yes yes so yeah so there are different consideration if you talk about the lstm and rnn you don't need to do this much pre-processing but for example there are uh uh not giving correct uh data then you can see again that uh with what pattern or with what type of modification you can do so that may be your contribution in that part okay so all all models are there you need to understand why you need to need more study more model to understand which type of data will work in which scenario which type of data will be good for this scenario so that is why you are reading now you are not reading just one model that is lstm and apply everything in lstm right so that's why we need to know multiple models that so that we can solve our problem optimally we can predict and we can classify okay so uh so i think we can end our session if there are no further questions so i hope uh it is uh uh you you have learned the basic concept so i would be uh sharing these slides i will give the slides uh you will be getting it soon through through the course coordinators okay so anything else yeah so thank you very much thank you very much so i will stop recording here and