Professional Documents
Culture Documents
Code Doc
Code Doc
KNN Predictions
1
Prophet Model Predictions
2
3
Neural Network Predictions
4
Loading the packages
library(tidyverse)
library(lubridate)
library(scales)
library(ggthemes)
library(prophet)
library(forecast)
library(tsfknn)
Pre processing
hcl<-hcl%>%filter(Close!="null")%>% mutate_if(is.character,as.numeric)
hdfc<-hdfc%>%filter(Close!="null")%>%mutate_if(is.character,as.numeric)
icici<-icici%>%filter(Close!="null")%>%mutate_if(is.character,as.numeric)
infosys<-infosys%>%filter(Close!="null")%>%mutate_if(is.character,as.numeric)
sbi<-sbi%>%filter(Close!="null")%>% mutate_if(is.character,as.numeric)
cognizant[2:nrow(cognizant),"Return"]<-log((cognizant[2:nrow(cognizant),5])/cognizant[1:(nrow(cognizant)
hcl[2:nrow(hcl),"Return"]<-log(hcl[2:nrow(hcl),5]/hcl[1:(nrow(hcl)-1),5])
hdfc[2:nrow(hdfc),"Return"]<-log(hdfc[2:nrow(hdfc),5]/hdfc[1:(nrow(hdfc)-1),5])
icici[2:nrow(icici),"Return"]<-log(icici[2:nrow(icici),5]/icici[1:(nrow(icici)-1),5])
5
infosys[2:nrow(infosys),"Return"]<-log(infosys[2:nrow(infosys),5]/infosys[1:(nrow(infosys)-1),5])
sbi[2:nrow(sbi),"Return"]<-log(sbi[2:nrow(sbi),5]/sbi[1:(nrow(sbi)-1),5])
cognizant_less<-cognizant%>%filter(Date>="2020-10-05")
hdfc_less<-hdfc%>%filter(Date>="2020-10-05")
icici_less<-icici%>%filter(Date>="2020-10-05")
sbi_less<-sbi%>%filter(Date>="2020-10-05")
Plots
ggplot()+geom_line(data=cognizant,aes(Date,Close),size=1.1,col="red")+
scale_x_date(date_breaks = "2 month",date_labels = "%B %Y")+
my_theme+
labs(x="Time",y="Closing Price",title = "Cognizant Closing Share Price")
ggplot()+geom_col(data=cognizant,aes(Date,Volume),size=1.1,col="#3aa832")+
scale_x_date(date_breaks = "2 month",date_labels = "%B %Y")+
my_theme+
scale_y_continuous(labels = comma)+
labs(x="Time",y="Volume",title = "Coznizant Volume of shares traded/contracted")
ggplot()+geom_line(data=cognizant_less,aes(Date,Return),color="blue",
size=1.1,alpha=0.7)+
geom_hline(yintercept = 0,col="red",size=1.1,linetype="dashed")+
scale_x_date(date_breaks = "2 month",date_labels = "%B %Y")+
my_theme+
labs(x="Time",y="Log Return Value",title = "Log Return Value of Cognizant")
6
HCL (Fig 5,6,7)
ggplot()+geom_line(data=hcl,aes(Date,Close),size=1.1,col="red")+
scale_x_date(date_breaks = "2 month",date_labels = "%B %Y")+
scale_y_continuous(breaks = seq(800,1300,100))+
my_theme+
labs(x="Time",y="Closing Price",title = "HCL Closing Share Price")
ggplot()+geom_col(data=hcl,aes(Date,Volume),size=1.1,col="#3aa832")+
scale_x_date(date_breaks = "2 month",date_labels = "%B %Y")+
my_theme+
scale_y_continuous(labels = comma)+
labs(x="Time",y="Volume",title = "HCL Volume of shares traded/contracted")
ggplot()+geom_line(data=hcl,aes(Date,Return),color="red",size=1.1,alpha=0.7)+
geom_hline(yintercept = 0,col="darkblue",size=1.1,linetype="dashed")+
scale_x_date(date_breaks = "2 month",date_labels = "%B %Y")+
my_theme+
labs(x="Time",y="Log Return Value",title = "Log Return Value of HCL")
ggplot()+geom_line(data=hdfc,aes(Date,Close),size=1.1,col="red")+
scale_x_date(date_breaks = "2 month",date_labels = "%B %Y")+
my_theme+
labs(x="Time",y="Closing Price",title = "HDFC Closing Share Price")
ggplot()+geom_col(data=hdfc,aes(Date,Volume),size=1.1,col="#3aa832")+
scale_x_date(date_breaks = "2 month",date_labels = "%B %Y")+
my_theme+
scale_y_continuous(labels = comma)+
labs(x="Time",y="Volume",title = "HDFC Volume of shares traded/contracted")
ggplot()+geom_line(data=hdfc_less,aes(Date,Return),color="#fc9003",
size=1.1,alpha=0.7)+
scale_x_date(date_breaks = "2 month",date_labels = "%B %Y")+
geom_hline(yintercept = 0,col="red",size=1.1,linetype="dashed")+
my_theme+
labs(x="Time",y="Log Return Value",title = "Log Return Value of HDFC")
ggplot()+geom_line(data=icici,aes(Date,Close),size=1.1,col="red")+
scale_x_date(date_breaks = "2 month",date_labels = "%B %Y")+
my_theme+
labs(x="Time",y="Closing Price",title = "ICICI Closing Share Price")
7
ggplot()+geom_col(data=icici,aes(Date,Volume),size=1.1,col="#3aa832")+
scale_x_date(date_breaks = "2 month",date_labels = "%B %Y")+
my_theme+
scale_y_continuous(labels = comma)+
labs(x="Time",y="Volume",title = "ICICI Volume of shares traded/contracted")
ggplot()+geom_line(data=icici_less,aes(Date,Return),color="#9003fc",
size=1.1,alpha=0.7)+
scale_x_date(date_breaks = "2 month",date_labels = "%B %Y")+
geom_hline(yintercept = 0,col="red",size=1.1,linetype="dashed")+
my_theme+
labs(x="Time",y="Log Return Value",title = "Log Return Value of ICICI")
ggplot()+geom_line(data=infosys,aes(Date,Close),size=1.1,col="red")+
scale_x_date(date_breaks = "2 month",date_labels = "%B %Y")+
my_theme+
scale_y_continuous(breaks = seq(1000,1700,100))+
labs(x="Time",y="Closing Price",title = "Infosys Closing Share Price")
ggplot()+geom_col(data=infosys,aes(Date,Volume),size=1.1,col="#3aa832")+
scale_x_date(date_breaks = "2 month",date_labels = "%B %Y")+
my_theme+
scale_y_continuous(labels = comma)+
labs(x="Time",y="Volume",title = "Infosys Volume of shares traded/contracted")
ggplot()+geom_line(data=infosys,aes(Date,Return),color="brown",
size=1.1,alpha=0.7)+
scale_x_date(date_breaks = "2 month",date_labels = "%B %Y")+
geom_hline(yintercept = 0,col="darkblue",size=1.1,linetype="dashed")+
my_theme+
labs(x="Time",y="Log Return Value",title = "Log Return Value of Infosys")
ggplot()+geom_line(data=sbi,aes(Date,Close),size=1.1,col="red")+
scale_x_date(date_breaks = "2 month",date_labels = "%B %Y")+
my_theme+
scale_y_continuous(breaks = seq(100,500,100),limits = c(100,500))+
labs(x="Time",y="Closing Price",title = "SBI Closing Share Price")
ggplot()+geom_col(data=sbi,aes(Date,Volume),size=1.1,col="#3aa832")+
scale_x_date(date_breaks = "2 month",date_labels = "%B %Y")+
my_theme+
scale_y_continuous(labels = comma)+
labs(x="Time",y="Volume",title = "SBI Volume of shares traded/contracted")
8
ggplot()+geom_line(data=sbi_less,aes(Date,Return),color="#03cefc",
size=1.1,alpha=0.7)+
scale_x_date(date_breaks = "2 month",date_labels = "%B %Y")+
geom_hline(yintercept = 0,col="red",size=1.1,linetype="dashed")+
my_theme+
labs(x="Time",y="Log Return Value",title = "Log Return Value of SBI")
Filtering out only date and close column and renaming them
cognizant_data<-cognizant[,c(1,5)]
hcl_data<-hcl[,c(1,5)]
hdfc_data<-hdfc[,c(1,5)]
icici_data<-icici[,c(1,5)]
infosys_data<-infosys[,c(1,5)]
sbi_data<-sbi[,c(1,5)]
colnames(cognizant_data)<-c("ds","y")
colnames(hcl_data)<-c("ds","y")
colnames(hdfc_data)<-c("ds","y")
colnames(icici_data)<-c("ds","y")
colnames(infosys_data)<-c("ds","y")
colnames(sbi_data)<-c("ds","y")
Prophet model
f_1<-prophet(cognizant_data)
f_2<-prophet(hcl_data)
f_3<-prophet(hdfc_data)
f_4<-prophet(icici_data)
f_5<-prophet(infosys_data)
f_6<-prophet(sbi_data)
9
Forecasting plots of Prophet model
Fig 21
ggplot()+geom_line(data=cognizant_data,aes(ds,y,col="Actual"))+
geom_line(data=forecast_cognizant[nrow(cognizant_data+1):nrow(forecast_cognizant),],
aes(as.Date(ds),yhat,col="Forecast"))+
scale_x_date(date_breaks = "3 month",date_labels = "%B %Y")+
scale_color_manual(labels=c("Actual","Forecast"),values = c("black","red"))+
my_theme+
labs(x="Time",y="Close Value",title = "Forecast share prices(Cognizant)")
Fig 24
ggplot()+geom_line(data=sbi_data,aes(ds,y,col="Actual"))+
geom_line(data=forecast_sbi[nrow(sbi_data+1):nrow(forecast_sbi),],
aes(as.Date(ds),yhat,col="Forecast"))+
scale_x_date(date_breaks = "2 month",date_labels = "%B %Y")+
scale_color_manual(labels=c("Actual","Forecast"),values = c("black","red"))+
my_theme+
labs(x="Time",y="Close Value",title = "Forecast share prices(SBI)")
Fig 27
ggplot()+geom_line(data=hdfc_data,aes(ds,y,col="Actual"))+
geom_line(data=forecast_hdfc[nrow(hdfc_data):nrow(forecast_hdfc),],
aes(as.Date(ds),yhat,col="Forecast"))+
scale_x_date(date_breaks = "3 month",date_labels = "%B %Y")+
scale_color_manual(labels=c("Actual","Forecast"),values = c("black","red"))+
my_theme+
labs(x="Time",y="Close Value",title = "Forecast share prices(HDFC)")
Fig 31
prophet_plot_components(f_1, forecast_cognizant)
KNN model
10
predknn_icici <- knn_forecasting(icici_data$y, h = 60, lags = 1:30, k = 40, msas = "MIMO")
Fig 20
autoplot(predknn_cognizant)+my_theme+
labs(x="Time",y="Close Value",title = "Forecast Share Prices(Cognizant)")
Fig 23
autoplot(predknn_sbi)+my_theme+
labs(x="Time",y="Close Value",title = "Forecast Share Prices(SBI)")
Fig 26
autoplot(predknn_hdfc)+my_theme+
labs(x="Time",y="Close Value",title = "Forecast Share Prices(HDFC)")
Neural Network
• Setting up
Models
11
lambda_1 <- BoxCox.lambda(cognizant_data)
dnn_pred_cognizant <- nnetar(cognizant_data$y, size= hn_1, lambda = lambda_1)
dnn_forecast_cognizant <- forecast(dnn_pred_cognizant, h= 60, PI = TRUE)
Fig 22
plot(dnn_forecast_cognizant,xlab="Time",ylab="Close Values",
main="Forecast share values(Cognizant)")
Fig 25
plot(dnn_forecast_sbi,xlab="Time",ylab="Close Values",
main="Forecast share values(SBI)")
Fig 28
plot(dnn_forecast_hdfc,xlab="Time",ylab="Close Values",
main="Forecast share values(HDFC)")
12
Fig 29 & 30
cognizant_data$dnn<-dnn_forecast_cognizant$fitted
hcl_data$dnn<-dnn_forecast_hcl$fitted
hdfc_data$dnn<-dnn_forecast_hdfc$fitted
ggplot()+geom_line(data=cognizant,aes(Date,Close,col="Actual"),size=1.1,alpha=0.7)+
geom_line(data=forecast_cognizant[1:nrow(cognizant_data),],
aes(as.Date(ds),yhat,col="Prophet Model"),
size=1.1)+
geom_line(data=cognizant_data,aes(ds,dnn,col="Neural Network"),size=1.1,alpha=0.6)+
scale_x_date(date_breaks = "2 month",date_labels = "%B %Y")+
scale_color_manual(labels=c("Actual","Neural Network","Prophet Model"),
values=c("red","blue","#49eb34"))+
my_theme+
labs(x="Time",y="Close Value",title ="Actual Vs Fitted Data(Cognizant)" )
ggplot()+geom_line(data=hdfc,aes(Date,Close,col="Actual"),size=1.1,alpha=0.7)+
geom_line(data=forecast_hdfc[1:nrow(hdfc_data),],
aes(as.Date(ds),yhat,col="Prophet Model"),
size=1.1)+
geom_line(data=hdfc_data,aes(ds,dnn,col="Neural Network"),size=1.1,alpha=0.6)+
scale_x_date(date_breaks = "2 month",date_labels = "%B %Y")+
scale_color_manual(labels=c("Actual","Neural Network","Prophet Model"),
values=c("red","blue","#49eb34"))+
my_theme+
labs(x="Time",y="Close Value",title ="Actual Vs Fitted Data(HDFC)" )
Accuracy Measure
Prophet RMSE
cognizant_data$prediction<-forecast_cognizant[(1:nrow(cognizant_data)),"yhat"]
accuracy(cognizant_data$prediction,cognizant_data$y)
sbi_data$prediction<-forecast_sbi[(1:nrow(sbi_data)),"yhat"]
accuracy(sbi_data$prediction,sbi_data$y)
hdfc_data$prediction<-forecast_hdfc[(1:nrow(hdfc_data)),"yhat"]
accuracy(hdfc_data$prediction,hdfc_data$y)
KNN RMSE
13
ro_sbi <- rolling_origin(predknn_sbi)
ro_sbi$global_accu
accuracy(cognizant_data$dnn,cognizant_data$y)
accuracy(dnn_forecast_sbi$fitted,sbi_data$y)
accuracy(dnn_forecast_hdfc$fitted,hdfc_data$y)
14