#homework1
#EX1a--------------------------------------------
library(fBasics)
## Loading required package: timeDate
## Loading required package: timeSeries
ex1a=read.table("d-axp3dx-0111.txt",header=T)
head(ex1a)
## date axp vw ew sp
## 1 20010904 0.000824 -0.001658 -0.005708 -0.000565
## 2 20010905 0.007682 -0.003242 -0.008926 -0.001059
## 3 20010906 -0.039477 -0.020733 -0.014187 -0.022390
## 4 20010907 -0.019274 -0.017769 -0.011476 -0.018637
## 5 20010910 0.011850 0.003513 -0.007368 0.006226
## 6 20010917 -0.135961 -0.050714 -0.042398 -0.049216
mmm=ex1a[,2]
basicStats(mmm)
## mmm
## nobs 2535.000000
## NAs 0.000000
## Minimum -0.175949
## Maximum 0.206485
## 1. Quartile -0.009672
## 3. Quartile 0.010540
## Mean 0.000534
## Median 0.000000
## Sum 1.353560
## SE Mean 0.000524
## LCL Mean -0.000493
## UCL Mean 0.001561
## Variance 0.000695
## Stdev 0.026368
## Skewness 0.459773
## Kurtosis 9.592053
mean(mmm)
## [1] 0.0005339487
var(mmm)
## [1] 0.0006952929
stdev(mmm) # standard deviation
## [1] 0.02636841
# Simple tests
t.test(mmm) # Testing mean return = 0
##
## One Sample t-test
##
## data: mmm
## t = 1.0195, df = 2534, p-value = 0.308
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
## -0.0004930041 0.0015609015
## sample estimates:
## mean of x
## 0.0005339487
s3=skewness(mmm)
T=length(mmm) # Sample size
T
## [1] 2535
t3=s3/sqrt(6/T) # Skewness test
t3
## [1] 9.450552
## attr(,"method")
## [1] "moment"
pp=2*(1-pnorm(t3)) # p-value
pp
## [1] 0
## attr(,"method")
## [1] "moment"
s4=kurtosis(mmm)
t4=s4/sqrt(24/T) # Kurtosis test
t4
## [1] 98.58139
## attr(,"method")
## [1] "excess"
normalTest(mmm,method="jb") # JB-test
##
## Title:
## Jarque - Bera Normalality Test
##
## Test Results:
## STATISTIC:
## X-squared: 9827.8615
## P VALUE:
## Asymptotic p Value: < 2.2e-16
##
## Description:
## Thu Sep 26 22:56:27 2019 by user: TS
####EX1btransformtologreturns
library(fBasics)
da=read.table("d-axp3dx-0111.txt",header=T)
head(da)
## date axp vw ew sp
## 1 20010904 0.000824 -0.001658 -0.005708 -0.000565
## 2 20010905 0.007682 -0.003242 -0.008926 -0.001059
## 3 20010906 -0.039477 -0.020733 -0.014187 -0.022390
## 4 20010907 -0.019274 -0.017769 -0.011476 -0.018637
## 5 20010910 0.011850 0.003513 -0.007368 0.006226
## 6 20010917 -0.135961 -0.050714 -0.042398 -0.049216
axp=log(da$axp+1) # Transform to log returns
vw=log(da$vw+1)
ew=log(da$ew+1)
sp=log(da$sp+1)
tdx=c(1:nrow(da))/12+2001 # Create time index
par(mfcol=c(2,1))
#Axp
basicStats(axp)
## axp
## nobs 2535.000000
## NAs 0.000000
## Minimum -0.193523
## Maximum 0.187711
## 1. Quartile -0.009719
## 3. Quartile 0.010484
## Mean 0.000188
## Median 0.000000
## Sum 0.476584
## SE Mean 0.000522
## LCL Mean -0.000836
## UCL Mean 0.001212
## Variance 0.000691
## Stdev 0.026294
## Skewness 0.020992
## Kurtosis 9.020499
mean(axp)
## [1] 0.0001880014
var(axp)
## [1] 0.0006913948
stdev(axp) # standard deviation
## [1] 0.02629439
# Simple tests
t.test(axp) # Testing mean return = 0
##
## One Sample t-test
##
## data: axp
## t = 0.35999, df = 2534, p-value = 0.7189
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
## -0.0008360686 0.0012120714
## sample estimates:
## mean of x
## 0.0001880014
s3=skewness(axp)
T=length(axp) # Sample size
T
## [1] 2535
t3=s3/sqrt(6/T) # Skewness test
t3
## [1] 0.4314821
## attr(,"method")
## [1] "moment"
pp=2*(1-pnorm(t3)) # Compute p-value
pp
## [1] 0.6661179
## attr(,"method")
## [1] "moment"
s4=kurtosis(axp)
t4=s4/sqrt(24/T) # Kurtosis test
t4
## [1] 92.7073
## attr(,"method")
## [1] "excess"
normalTest(axp,method="jb") # JB-test
##
## Title:
## Jarque - Bera Normalality Test
##
## Test Results:
## STATISTIC:
## X-squared: 8612.9221
## P VALUE:
## Asymptotic p Value: < 2.2e-16
##
## Description:
## Thu Sep 26 22:56:27 2019 by user: TS
plot(tdx,axp,xlab='year',ylab='lrtn',type='l')
title(main="(a) AXP returns")
cor(axp,sp) # Obtain sample correlation
## [1] 0.7872114
m1=lm(axp~ sp)
summary(m1)
##
## Call:
## lm(formula = axp ~ sp)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.095121 -0.007060 -0.000323 0.007186 0.162519
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.0001891 0.0003221 0.587 0.557
## sp 1.5009877 0.0233631 64.246 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.01622 on 2533 degrees of freedom
## Multiple R-squared: 0.6197, Adjusted R-squared: 0.6196
## F-statistic: 4128 on 1 and 2533 DF, p-value: < 2.2e-16
plot(sp,axp,cex=0.8) # Obtain scatter plot
abline(0.008,.807)
title(main="(a) SP, AXP scatter")

#VW
basicStats(vw)
## vw
## nobs 2535.000000
## NAs 0.000000
## Minimum -0.094049
## Maximum 0.108755
## 1. Quartile -0.005489
## 3. Quartile 0.006193
## Mean 0.000131
## Median 0.000848
## Sum 0.331452
## SE Mean 0.000272
## LCL Mean -0.000402
## UCL Mean 0.000663
## Variance 0.000187
## Stdev 0.013670
## Skewness -0.300352
## Kurtosis 7.880082
mean(vw)
## [1] 0.0001307504
var(vw)
## [1] 0.0001868787
stdev(vw) # standard deviation
## [1] 0.01367036
# Simple tests
t.test(vw) # Testing mean return = 0
##
## One Sample t-test
##
## data: vw
## t = 0.48156, df = 2534, p-value = 0.6302
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
## -0.0004016600 0.0006631607
## sample estimates:
## mean of x
## 0.0001307504
s3=skewness(vw)
T=length(vw) # Sample size
T
## [1] 2535
t3=s3/sqrt(6/T) # Skewness test
t3
## [1] -6.173683
## attr(,"method")
## [1] "moment"
pp=2*(1-pnorm(t3)) # Compute p-value
pp
## [1] 2
## attr(,"method")
## [1] "moment"
s4=kurtosis(vw)
t4=s4/sqrt(24/T) # Kurtosis test
t4
## [1] 80.98677
## attr(,"method")
## [1] "excess"
normalTest(vw,method="jb") # JB-test
##
## Title:
## Jarque - Bera Normalality Test
##
## Test Results:
## STATISTIC:
## X-squared: 6611.3226
## P VALUE:
## Asymptotic p Value: < 2.2e-16
##
## Description:
## Thu Sep 26 22:56:27 2019 by user: TS
plot(tdx,vw,xlab='year',ylab='lrtn',type='l')
title(main="(b) VW returns")
cor(axp,vw)
## [1] 0.7822369
m2=lm(axp~ vw)
summary(m2)
##
## Call:
## lm(formula = axp ~ vw)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.099779 -0.007272 -0.000527 0.007365 0.159407
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -8.726e-06 3.254e-04 -0.027 0.979
## vw 1.505e+00 2.381e-02 63.195 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.01638 on 2533 degrees of freedom
## Multiple R-squared: 0.6119, Adjusted R-squared: 0.6117
## F-statistic: 3994 on 1 and 2533 DF, p-value: < 2.2e-16
plot(vw,axp,cex=0.8) # Obtain scatter plot
abline(0.008,.807)
title(main="(b) VW, AXP scatter")

#EW
basicStats(ew)
## ew
## nobs 2535.000000
## NAs 0.000000
## Minimum -0.081470
## Maximum 0.102035
## 1. Quartile -0.004641
## 3. Quartile 0.006382
## Mean 0.000553
## Median 0.001428
## Sum 1.400780
## SE Mean 0.000240
## LCL Mean 0.000081
## UCL Mean 0.001024
## Variance 0.000146
## Stdev 0.012100
## Skewness -0.427315
## Kurtosis 8.017712
mean(ew)
## [1] 0.0005525758
var(ew)
## [1] 0.0001463991
stdev(ew) # standard deviation
## [1] 0.01209955
# Simple tests
t.test(ew) # Testing mean return = 0
##
## One Sample t-test
##
## data: ew
## t = 2.2994, df = 2534, p-value = 0.02156
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
## 8.134269e-05 1.023809e-03
## sample estimates:
## mean of x
## 0.0005525758
s3=skewness(ew)
T=length(ew) # Sample size
T
## [1] 2535
t3=s3/sqrt(6/T) # Skewness test
t3
## [1] -8.783378
## attr(,"method")
## [1] "moment"
pp=2*(1-pnorm(t3)) # Compute p-value
pp
## [1] 2
## attr(,"method")
## [1] "moment"
s4=kurtosis(ew)
t4=s4/sqrt(24/T) # Kurtosis test
t4
## [1] 82.40125
## attr(,"method")
## [1] "excess"
normalTest(ew,method="jb") # JB-test
##
## Title:
## Jarque - Bera Normalality Test
##
## Test Results:
## STATISTIC:
## X-squared: 6881.9447
## P VALUE:
## Asymptotic p Value: < 2.2e-16
##
## Description:
## Thu Sep 26 22:56:28 2019 by user: TS
plot(tdx,ew,xlab='year',ylab='lrtn',type='l')
title(main="(c) EW index")
cor(axp,ew)
## [1] 0.7392449
m3=lm(axp~ ew)
summary(m3)
##
## Call:
## lm(formula = axp ~ ew)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.111473 -0.008253 -0.000648 0.007942 0.154226
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.0006997 0.0003521 -1.987 0.047 *
## ew 1.6065053 0.0290786 55.247 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.01771 on 2533 degrees of freedom
## Multiple R-squared: 0.5465, Adjusted R-squared: 0.5463
## F-statistic: 3052 on 1 and 2533 DF, p-value: < 2.2e-16
plot(ew,axp,cex=0.8) # Obtain scatter plot
abline(0.008,.807)
title(main="(c) EW,AXP scatter")

#SP
basicStats(sp)
## sp
## nobs 2535.000000
## NAs 0.000000
## Minimum -0.094695
## Maximum 0.109572
## 1. Quartile -0.005815
## 3. Quartile 0.006098
## Mean -0.000001
## Median 0.000700
## Sum -0.001898
## SE Mean 0.000274
## LCL Mean -0.000538
## UCL Mean 0.000536
## Variance 0.000190
## Stdev 0.013790
## Skewness -0.206357
## Kurtosis 8.322826
mean(sp)
## [1] -7.48638e-07
var(sp)
## [1] 0.0001901755
stdev(sp) # standard deviation
## [1] 0.01379041
# Simple tests
t.test(sp) # Testing mean return = 0
##
## One Sample t-test
##
## data: sp
## t = -0.0027333, df = 2534, p-value = 0.9978
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
## -0.0005378347 0.0005363374
## sample estimates:
## mean of x
## -7.48638e-07
s3=skewness(sp)
T=length(sp) # Sample size
T
## [1] 2535
t3=s3/sqrt(6/T) # Skewness test
t3
## [1] -4.241626
## attr(,"method")
## [1] "moment"
pp=2*(1-pnorm(t3)) # Compute p-value
pp
## [1] 1.999978
## attr(,"method")
## [1] "moment"
s4=kurtosis(sp)
t4=s4/sqrt(24/T) # Kurtosis test
t4
## [1] 85.53703
## attr(,"method")
## [1] "excess"
normalTest(sp,method="jb") # JB-test
##
## Title:
## Jarque - Bera Normalality Test
##
## Test Results:
## STATISTIC:
## X-squared: 7350.3209
## P VALUE:
## Asymptotic p Value: < 2.2e-16
##
## Description:
## Thu Sep 26 22:56:28 2019 by user: TS
plot(tdx,sp,xlab='year',ylab='lrtn',type='l')
title(main="(d) SP index")
m4=lm(ew~ sp) # Fit the Market Model (linear model)
summary(m4)
##
## Call:
## lm(formula = ew ~ sp)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.034051 -0.002429 0.000183 0.002678 0.029833
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.0005532 0.0000945 5.854 5.43e-09 ***
## sp 0.8067350 0.0068539 117.705 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.004758 on 2533 degrees of freedom
## Multiple R-squared: 0.8454, Adjusted R-squared: 0.8454
## F-statistic: 1.385e+04 on 1 and 2533 DF, p-value: < 2.2e-16
plot(ew,sp,cex=0.8) # Obtain scatter plot
abline(0.008,.807) # Add the linear regression line
title(main="(d) EW,SP scatter")

#Ex1cAXPstockmean0
library(fBasics)
hist(axp,nclass=30) # Histogram

d1=density(axp) # Obtain density estimate
range(axp)
## [1] -0.1935229 0.1877112
x=seq(-.5,.5,.005)
y1=dnorm(x,mean(axp),stdev(axp))
plot(d1$x,d1$y,xlab="rtn",ylab="density",type="l")
lines(x,y1,lty=2)
