You are on page 1of 14

Code Documenation (Final DataSet)

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)

Reading the data

cognizant<-read_csv("Cognizant share prices 2019_2021.csv")

hcl<-read_csv("HCL Technologies share prices 2019_2021.csv")

hdfc<-read_csv("HDFC Bank Share Prices 2019_2021.csv")

icici<-read_csv("ICICI Bank Share Prices 2019_2021.csv")

infosys<-read_csv("Infosys Share Prices 2019_2021.csv")

sbi<-read_csv("SBI Share Prices 2019_2021.csv")

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)

Making the return column


• For plot making data for all companies, the data has been taken ffrom same dates, so some data is
filtered.

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")

Setting up the theme

my_theme<-theme_fivethirtyeight()+theme(plot.title = element_text(hjust = 0.5,size=20),


axis.title = element_text(size=20),
axis.text = element_text(size=14),
plot.subtitle = element_text(hjust=0.5),
legend.position = "top",
legend.title = element_text(size=15),
legend.text = element_text(size=15),
axis.text.x = element_text(size=14,angle = 45,hjust=1))

Plots

Cognizant (Fig 2,3,4)

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")

HDFC (Fig 8,9,10)

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")

ICICI (Fig 11,12,13)

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")

Infosys (Fig 14,15,16)

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")

SBI (Fig 17,18,19)

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

# Only taking close value

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)

future_1 <- make_future_dataframe(f_1, periods = 60)


future_2 <- make_future_dataframe(f_2, periods = 60)
future_3 <- make_future_dataframe(f_3, periods = 60)
future_4 <- make_future_dataframe(f_4, periods = 60)
future_5 <- make_future_dataframe(f_5, periods = 60)
future_6 <- make_future_dataframe(f_6, periods = 60)

forecast_cognizant <- predict(f_1, future_1)


forecast_hcl <- predict(f_2, future_2)
forecast_hdfc <- predict(f_3, future_3)
forecast_icici <- predict(f_4, future_4)
forecast_infosys <- predict(f_5, future_5)
forecast_sbi <- predict(f_6, future_6)

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

predknn_cognizant <- knn_forecasting(cognizant_data$y, h = 60, lags = 1:30, k = 40, msas = "MIMO")

predknn_hcl <- knn_forecasting(hcl_data$y, h = 60, lags = 1:30, k = 40, msas = "MIMO")

predknn_hdfc <- knn_forecasting(hdfc_data$y, h = 60, lags = 1:30, k = 40, msas = "MIMO")

10
predknn_icici <- knn_forecasting(icici_data$y, h = 60, lags = 1:30, k = 40, msas = "MIMO")

predknn_infosys <- knn_forecasting(infosys_data$y, h = 60, lags = 1:30, k = 40, msas = "MIMO")

predknn_sbi <- knn_forecasting(sbi_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

alpha <- 1.5ˆ(-10)

hn_1 <- length(cognizant_data)/(alpha*(length(cognizant_data)+30))

hn_2 <- length(hcl_data)/(alpha*(length(hcl_data)+30))

hn_3 <- length(hdfc_data)/(alpha*(length(hdfc_data)+30))

hn_4 <- length(icici_data)/(alpha*(length(icici_data)+30))

hn_5 <- length(infosys_data)/(alpha*(length(infosys_data)+30))

hn_6 <- length(sbi_data)/(alpha*(length(sbi_data)+30))

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)

lambda_2 <- BoxCox.lambda(hcl_data)


dnn_pred_hcl <- nnetar(hcl_data$y, size= hn_2, lambda = lambda_2)
dnn_forecast_hcl <- forecast(dnn_pred_hcl, h= 60, PI = TRUE)

lambda_3 <- BoxCox.lambda(hdfc_data)


dnn_pred_hdfc <- nnetar(hdfc_data$y, size= hn_3, lambda = lambda_3)
dnn_forecast_hdfc <- forecast(dnn_pred_hdfc, h= 60, PI = TRUE)

lambda_4 <- BoxCox.lambda(icici_data)


dnn_pred_icici <- nnetar(icici_data$y, size= hn_4, lambda = lambda_4)
dnn_forecast_icici <- forecast(dnn_pred_icici, h= 60, PI = TRUE)

lambda_5 <- BoxCox.lambda(infosys_data)


dnn_pred_infosys <- nnetar(infosys_data$y, size= hn_5, lambda = lambda_5)
dnn_forecast_infosys <- forecast(dnn_pred_infosys, h= 60, PI = TRUE)

lambda_6 <- BoxCox.lambda(sbi_data)


dnn_pred_sbi <- nnetar(sbi_data$y, size= hn_6, lambda = lambda_6)
dnn_forecast_sbi <- forecast(dnn_pred_sbi, 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

ro_cognizant <- rolling_origin(predknn_cognizant)


ro_cognizant$global_accu

13
ro_sbi <- rolling_origin(predknn_sbi)
ro_sbi$global_accu

ro_hdfc <- rolling_origin(predknn_hdfc)


ro_hdfc$global_accu

Neural Network RMSE

accuracy(cognizant_data$dnn,cognizant_data$y)

accuracy(dnn_forecast_sbi$fitted,sbi_data$y)

accuracy(dnn_forecast_hdfc$fitted,hdfc_data$y)

14

You might also like