## Empirical Methods in Finance Using R Programming

## Building a Simple auto Correlation-based Forecasting Model

## Non-stationarity and Regression Models

set.seed(1)

##############################Q2 part 1

mu<-0.5

sigma<-4

t<-600

N<-10000

beta<-numeric(N)

r1t<-numeric(t)

r2t<-numeric(t)

for ( i in 1:N)

{

r1t<-mu+sigma*rnorm(t,0,1)

r2t<-mu+sigma*rnorm(t,0,1)

mod<-lm(r2t~r1t)

beta[i]<-mod$coefficients[2]

}

mean(beta)

sd(beta)

hist(beta)

t.test(beta, mu = 0, alternative = "two.sided")

##############################Q2 part 2

p1_vec<-function(t)

{

r1t<-mu+sigma*rnorm(600,0,1)

y1<-numeric(t+1)

y1[1]<-0

for(i in 2:t+1)

{

#since for y1 i=1 is same as p1,0 but in case of r1t i=1 is same as t=1 since y1 array starts from

# 1 and goes upto t+1 but r1t goes from i=1 to i=t

y1[i]<-y1[i-1]+r1t[i-1]

}

return(y1)

}

p2_vec<-function(t)

{

r2t<-mu+sigma*rnorm(t,0,1)

y2<-numeric(t+1)

y2[1]<-0

for(i in 2:t+1)

{

#since for y2 i=1 is same as p1,0 but in case of r2t i=1 is same as t=1 since y2 array starts from

# 1 and goes upto t+1 but r2t goes from i=1 to i=t

y2[i]<-y2[i-1]+r2t[i-1]

}

return(y2)

}

betas<-numeric(N)

for ( i in 1:N)

{

p1<-p1_vec(t)

p2<-p2_vec(t)

mod<-lm(p1~p2)

betas[i]<-mod$coefficients[2]

}

mean(betas)

sd(betas)

hist(betas)

plot(mod)

t.test(betas, mu = 0, alternative = "two.sided")

set.seed(1)

########################Q1

df<-read.csv("F-F_Research_Data_5_Factors_2x3.csv",skip=2)

df<-df[1:688,]

library(anytime)

df$X<-anydate(df$X)

names(df)[names(df)=="X"]<-"Date"

tse<-ts(df$RMW,start=c(1963, 7), end=c(2020, 10), frequency=12)

plot.ts(tse)

row.names(df) <- levels(df$Date)[df$Date]

library(PerformanceAnalytics)

Return.annualized(df$RMW,scale=12)

sum(is.na(df$RMW))

df$RMW<-as.numeric(df$RMW)

a<-acf(df$RMW,lag.max = 60)

plot(a)

cum_acf<-a$acf[2:61]

cum_acf_calculated<-cumsum(cum_acf)

cum_acf

x<-1:60

plot(x,cum_acf_calculated,xlab = "lag",ylab = "Cummulative ACF")

Box.test(df$RMW,lag=6)

##############################Q2 part 1

mu<-0.5

sigma<-4

t<-600

N<-10000

beta<-numeric(N)

r1t<-numeric(t)

r2t<-numeric(t)

for ( i in 1:N)

{

r1t<-mu+sigma*rnorm(t,0,1)

r2t<-mu+sigma*rnorm(t,0,1)

mod<-lm(r2t~r1t)

beta[i]<-mod$coefficients[2]

}

mean(beta)

sd(beta)

hist(beta)

t.test(beta, mu = 0, alternative = "two.sided")

##############################Q2 part 2

p1_vec<-function(t)

{

r1t<-mu+sigma*rnorm(600,0,1)

y1<-numeric(t+1)

y1[1]<-0

for(i in 2:t+1)

{

#since for y1 i=1 is same as p1,0 but in case of r1t i=1 is same as t=1 since y1 array starts from

# 1 and goes upto t+1 but r1t goes from i=1 to i=t

y1[i]<-y1[i-1]+r1t[i-1]

}

return(y1)

}

p2_vec<-function(t)

{

r2t<-mu+sigma*rnorm(t,0,1)

y2<-numeric(t+1)

y2[1]<-0

for(i in 2:t+1)

{

#since for y2 i=1 is same as p1,0 but in case of r2t i=1 is same as t=1 since y2 array starts from

# 1 and goes upto t+1 but r2t goes from i=1 to i=t

y2[i]<-y2[i-1]+r2t[i-1]

}

return(y2)

}

betas<-numeric(N)

for ( i in 1:N)

{

p1<-p1_vec(t)

p2<-p2_vec(t)

mod<-lm(p1~p2)

betas[i]<-mod$coefficients[2]

}

mean(betas)

sd(betas)

hist(betas)

plot(mod)

t.test(betas, mu = 0, alternative = "two.sided")