Professional Documents
Culture Documents
rm(list=ls())
#Loading Data
setwd("D:\\CMS\\")
data1=read.csv("Nifty.csv", header=T)
data2=read.csv("SP.csv", header=T)
data3=read.csv("USDINR.csv", header=T)
data4=read.csv("WTI.csv", header=T)
#View Data
head(data1)
tail(data1)
#Extract Prices
nifty=data1[,2]
sp=data2[,2]
usdinr=data3[,2]
wti=data4[,2]
#Plot Prices
plot(nifty,type="l",main="Nifty 50 Index",xlab="Time",ylab="Price")
plot(sp,type="l",main="S&P 500 Index",xlab="Time",ylab="Price")
plot(usdinr,type="l",main="USD/INR Exchange rate",xlab="Time",ylab="Price")
plot(wti,type="l",main="WTI Crude Oil",xlab="Time",ylab="Price")
#Comparative Plots
old.par <- par(mfrow=c(2,2))
plot(nifty,type="l",main="Nifty 50 Index",xlab="Time",ylab="Price")
plot(sp,type="l",main="S&P 500 Index",xlab="Time",ylab="Price")
plot(usdinr,type="l",main="USD/INR Exchange rate",xlab="Time",ylab="Price")
plot(wti,type="l",main="WTI Crude Oil",xlab="Time",ylab="Price")
par(old.par)
#Test of Stationarity
adf.test(nifty)
adf.test(sp)
adf.test(usdinr)
adf.test(wti)
#Calculate Returns
ret_nifty=diff(log(nifty))
ret_sp=diff(log(sp))
ret_usdinr=diff(log(usdinr))
ret_wti=diff(log(wti))
#Plot Returns
plot(ret_nifty,type="l",main="Nifty 50 Index",xlab="Time",ylab="Return")
plot(ret_sp,type="l",main="S&P 500 Index",xlab="Time",ylab="Return")
plot(ret_usdinr,type="l",main="USD/INR Exchange rate",xlab="Time",ylab="Return")
plot(ret_wti,type="l",main="WTI Crude Oil",xlab="Time",ylab="Return")
#Comparative Plots
old.par <- par(mfrow=c(2,2))
plot(ret_nifty,type="l",main="Nifty 50 Index",xlab="Time",ylab="Return")
plot(ret_sp,type="l",main="S&P 500 Index",xlab="Time",ylab="Return")
plot(ret_usdinr,type="l",main="USD/INR Exchange rate",xlab="Time",ylab="Return")
plot(ret_wti,type="l",main="WTI Crude Oil",xlab="Time",ylab="Return")
par(old.par)
#Test of Stationarity
adf.test(ret_nifty)
adf.test(ret_sp)
adf.test(ret_usdinr)
adf.test(ret_wti)
#Descriptive Statistics
all_data=cbind(ret_nifty,ret_sp,ret_usdinr,ret_wti)
library(fBasics)
basicStats(all_data)
#Test of Normality
jarque.bera.test(ret_nifty)
jarque.bera.test(ret_sp)
jarque.bera.test(ret_usdinr)
jarque.bera.test(ret_wti)
#Random walk
RW<-cumsum(white_noise)
ts.plot(RW)
#one path of RW
rn=rnorm(1000,0,1)
a=c(100,rn)
rwalk1=cumsum(a)
plot(rwalk1,type="l")
# Random Walk
m=100 #No of Paths
n=1000 #No. of steps in each path
Rwalk=matrix(nrow=m,ncol=n+1)
for (i in 1:m){
randnum=rnorm(n,0,1)
rw=cumsum(c(100,randnum))
Rwalk[i,]=rw
}
matplot(t(Rwalk),type="l")
#MA
#ARMA
#ARIMA