Skip to content

Commit 5cfdf66

Browse files
authored
Add files via upload
1 parent c93e5d3 commit 5cfdf66

7 files changed

+263
-0
lines changed

1-overview return data.R

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
setwd("/Users/peimingqi/Desktop/spl")
2+
data=read.csv("DB.csv")
3+
close=data$Adj.Close
4+
5+
#calculate the return
6+
close_ln=log(close,base=exp(1))
7+
close_diff=diff(close_ln)
8+
9+
#change the data format
10+
data$Date<-as.Date(data$Date)
11+
data_date=data$Date
12+
View(data_date)
13+
14+
#draw the plot of the return data
15+
return=data.frame(data$Date[2:3897],close_diff)
16+
colnames(return)=c("date","diff")
17+
View(return)
18+
plot(return,type='l',xlab="date",ylab="return",col="dark red")
19+

2-calculate theta.R

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
setwd("/Users/peimingqi/Desktop/spl")
2+
data=read.csv("DB.csv")
3+
close=data$Adj.Close
4+
close_ln=log(close,base=exp(1))
5+
close_diff=diff(close_ln)
6+
7+
#the plot/curve of the kernel density distribution
8+
plot(density(close_diff))
9+
10+
#estimate the approximate function of kernel distribution
11+
kernel_density<-density(close_diff)
12+
kernel_fun<-approxfun(kernel_density$x,kernel_density$y)
13+
14+
#
15+
myfun=function(a){
16+
b=kernel_fun(a)*a
17+
return(b)
18+
}
19+
20+
#calculate theta(alpha=0.05)
21+
alpha=0.05
22+
23+
q=quantile(close_diff,probs=alpha)
24+
25+
inte1=integrate(myfun,-0.2,q)[[1]]
26+
inte2=integrate(myfun,q,0.2)[[1]]
27+
28+
theta=(alpha*q-inte1)/(2*inte2-(1-2*alpha)*q)
29+
theta

3-data .R

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
setwd("/Users/peimingqi/Desktop/spl")
2+
DB=read.csv("DB.csv")
3+
4+
#date
5+
DB$Date<-as.Date(data$Date)
6+
dateframe=data.frame(DB$Date[2:3897])
7+
8+
#index return
9+
index=DB$GSPC
10+
index_ln=log(index,base=exp(1))
11+
index_diff=diff(index_ln)
12+
indexframe=data.frame(index_diff)
13+
14+
#P/EPS
15+
PE=DB$Close/DB$EPS
16+
PEframe=data.frame(PE[2:3897])
17+
18+
#turnover rate
19+
turnover=DB$Volume/DB$shares
20+
turnoverframe=data.frame(turnover[2:3897])
21+
22+
#circulated stock value
23+
csv=DB$shares*10000*DB$Close
24+
ln_csv=log(csv,exp(1))
25+
csvframe=data.frame(ln_csv[2:3897])
26+
27+
#Y
28+
close=DB$Adj.Close
29+
close_ln=log(close,base=exp(1))
30+
close_diff=diff(close_ln)
31+
Yframe=data.frame(close_diff)
32+
33+
#total data frame
34+
DBframe=data.frame(dateframe,indexframe,PEframe,turnoverframe,csvframe,Yframe)
35+
colnames(DBframe)=c("date","index","PE","turnover","csv","Y")
36+
View(DBframe)
37+

4-prepare for iteration.R

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#get the initial value of beta
2+
datamatrix=cbind(DBframe$index,DBframe$PE,DBframe$turnover,DBframe$csv)
3+
colnames(datamatrix)=c("index","PE","turnover","csv")
4+
5+
6+
Y=DBframe$Y
7+
8+
result=lm(formula = Y ~ datamatrix)
9+
summary(result)
10+
11+
#test heteroscedasticity
12+
par(mfrow=c(2,2)) # init 4 charts in 1 panel
13+
plot(result)
14+
library("lmtest")
15+
restest=bptest(result)
16+
17+
#get the initial beta
18+
beta0=result$coefficients
19+
20+
#estimate alpha in ARCH
21+
res = result$resid
22+
res2=res^2
23+
res=as.matrix(res2)
24+
25+
m=length(res)
26+
U=res[4:m]
27+
v1=matrix(res[3:(m-1)],nrow=m-3)
28+
v2=matrix(res[2:(m-2)],nrow=m-3)
29+
v3=matrix(res[1:(m-3)],nrow=m-3)
30+
31+
V=cbind(v1,v2,v3)
32+
33+
lm2=lm(U~V)
34+
summary(lm2)
35+
36+
alphat=lm2$coefficients
37+
38+
#calculate sigma
39+
v4=matrix(rep(1,m-3),nrow=m-3)
40+
41+
Vt=cbind(v4,v1,v2,v3)
42+
43+
sigma=Vt%*%alphat
44+
45+
#calculate omega
46+
et=result$resid
47+
et=as.vector(et)
48+
49+
50+
et2=matrix(0,nrow=3896)
51+
mode(et2)
52+
et2[which(et<0)]=1
53+
et2[which(et==0)]=1
54+
et2[which(et>0)]=0
55+
et2=as.vector(et2)
56+
57+
58+
thetavec=rep(theta,3896)
59+
thetavec=as.vector(thetavec)
60+
61+
omega=abs(thetavec-et2)
62+
is.vector(omega)
63+
omega=omega[4:3896]
64+
65+
66+
#calculate the new beta
67+
Xt=cbind(1,datamatrix[4:3896,])
68+
j=1
69+
xxsummatrix=matrix(0,nrow=5,ncol = 5)
70+
xysummatrix=matrix(0,nrow = 5,ncol = 1)
71+
ynew=Y[4:3896]
72+
for (j in 1:3893) {
73+
xxsummatrix=xxsummatrix+omega[j]/sigma[j]*crossprod(t(Xt[j,]),Xt[j,])
74+
xysummatrix=xysummatrix+omega[j]/sigma[j]*crossprod(t(Xt[j,]),ynew[j])
75+
}
76+
77+
solve(xxsummatrix)
78+
beta1=crossprod(t(solve(xxsummatrix)),xysummatrix)
79+
80+
81+

5-iterationfunction.R

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
3+
itefun=function(beta){
4+
5+
#calculate residual
6+
Xt=cbind(1,datamatrix)
7+
n=nrow(Xt)
8+
et=Y-crossprod(t(Xt),beta)
9+
ets=et^2
10+
U=ets[4:n]
11+
v1=matrix(ets[3:(n-1)],nrow=n-3)
12+
v2=matrix(ets[2:(n-2)],nrow=n-3)
13+
v3=matrix(ets[1:(n-3)],nrow=n-3)
14+
V=cbind(v1,v2,v3)
15+
16+
lm2=lm(U~V)
17+
summary(lm2)
18+
19+
alphat=lm2$coefficients
20+
21+
#calculate sigma
22+
v4=matrix(rep(1,n-3),nrow=n-3)
23+
24+
Vt=cbind(v4,v1,v2,v3)
25+
26+
sigma=Vt%*%alphat
27+
28+
#calculate omega
29+
et2=matrix(0,nrow = n)
30+
et2[which(et<0)]=1
31+
et2[which(et==0)]=1
32+
et2[which(et>0)]=0
33+
et2=as.vector(et2)
34+
35+
thetavec=rep(theta,n)
36+
thetavec=as.vector(thetavec)
37+
38+
omega=abs(thetavec-et2)
39+
omega=omega[4:n]
40+
41+
j=1
42+
xxsummatrix=matrix(0,nrow=5,ncol = 5)
43+
xysummatrix=matrix(0,nrow = 5,ncol = 1)
44+
ynew=Y[4:n]
45+
for (j in 1:(n-3)) {
46+
xxsummatrix=xxsummatrix+omega[j]/sigma[j]*crossprod(t(Xt[j,]),Xt[j,])
47+
xysummatrix=xysummatrix+omega[j]/sigma[j]*crossprod(t(Xt[j,]),ynew[j])
48+
}
49+
50+
beta=crossprod(solve(xxsummatrix),xysummatrix)
51+
return(beta)
52+
}
53+
54+
55+

6-iteration loop.R

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#calculate the max diff of betas as the condition to end the iteration
2+
difference1=beta1-beta0
3+
difference1=abs(difference1)
4+
maxdifference1=max(difference1)
5+
6+
#establish a matrix to put all the obtained betas
7+
betamatrix=cbind(beta0,beta1)
8+
9+
#give the initial values in the loop
10+
beta=beta1
11+
maxdiff=1000
12+
13+
#the iteration loop
14+
while(maxdiff>0.0001){
15+
k=beta
16+
beta=itefun(beta)
17+
difference=abs(beta-k)
18+
maxdiff=max(difference)
19+
betamatrix=cbind(betamatrix,beta)
20+
}
21+
22+
betafinal=beta
23+
24+
View(betafinal)
25+
View(betamatrix)
26+

7-results.R

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#calculate VaR
2+
alpha=0.05
3+
q=quantile(close_diff,probs=alpha)
4+
5+
xmean=apply(Xt,2,mean)
6+
7+
#calculatevtheta
8+
vtheta=xmean%*%betafinal
9+
10+
#calculate ES
11+
ES=-(1+theta/((1-2*theta)*alpha))*vtheta+theta/((1-2*theta)*alpha)*mean(Y)
12+
13+
#the final results
14+
datafinal=data.frame(alpha,theta,q,ES)
15+
colnames(datafinal)=c("alpha","theta","VaR","ES")
16+
View(datafinal)

0 commit comments

Comments
 (0)