library(PerformanceAnalytics)
## Warning: package 'PerformanceAnalytics' was built under R version 4.1.3
## Loading required package: xts
## Warning: package 'xts' was built under R version 4.1.3
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.1.3
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
##
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
##
## legend
library(xts)
library(lubridate)
## Warning: package 'lubridate' was built under R version 4.1.2
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
if(!require('base')) {
install.packages('base')
library('base')
}
# load data and create an xts dataset
fund<-read.csv("contrafund.csv")
fund$Date<-mdy(fund$Date)
fund2<-fund[order(fund$Date),]
#create an xts dataset
All.dat<-xts(fund2[,-1],order.by=fund2[,1],)
Return.cumulative(All.dat, geometric =TRUE)
## ContraRet Market.Return Risk.Free
## Cumulative Return 141.5677 69.29182 4.072008
chart.CumReturns(All.dat, wealth.index =FALSE, geometric = TRUE)

SharpeRatio(All.dat$ContraRet,All.dat$Risk.Free)
## ContraRet
## StdDev Sharpe (Rf=0.4%, p=95%): 0.19010682
## VaR Sharpe (Rf=0.4%, p=95%): 0.12900607
## ES Sharpe (Rf=0.4%, p=95%): 0.05971594
TreynorRatio(All.dat$ContraRet,All.dat$Market.Return,All.dat$Risk.Free)
## [1] 0.1005838
All.dat<-transform(All.dat,MktExcess=Market.Return-Risk.Free,FundExcess=ContraRet-Risk.Free)
print("for some reason, the aliases above for FundExcess & MktExcess didn't work, had to insert the following from the dataframe as shown by")
## [1] "for some reason, the aliases above for FundExcess & MktExcess didn't work, had to insert the following from the dataframe as shown by"
head(All.dat)
## ContraRet Market.Return Risk.Free Market.Return.1 ContraRet.1
## 1980-01-31 0.024391 0.0631 0.0080 0.0551 0.016391
## 1980-02-29 -0.016874 -0.0033 0.0089 -0.0122 -0.025774
## 1980-03-31 -0.089431 -0.1169 0.0121 -0.1290 -0.101531
## 1980-04-30 0.017857 0.0523 0.0126 0.0397 0.005257
## 1980-05-31 0.078947 0.0607 0.0081 0.0526 0.070847
## 1980-06-30 0.011743 0.0367 0.0061 0.0306 0.005643
print("ContraRet.1 & Market.Return.1")
## [1] "ContraRet.1 & Market.Return.1"
head(All.dat)
## ContraRet Market.Return Risk.Free Market.Return.1 ContraRet.1
## 1980-01-31 0.024391 0.0631 0.0080 0.0551 0.016391
## 1980-02-29 -0.016874 -0.0033 0.0089 -0.0122 -0.025774
## 1980-03-31 -0.089431 -0.1169 0.0121 -0.1290 -0.101531
## 1980-04-30 0.017857 0.0523 0.0126 0.0397 0.005257
## 1980-05-31 0.078947 0.0607 0.0081 0.0526 0.070847
## 1980-06-30 0.011743 0.0367 0.0061 0.0306 0.005643
Alpha=lm(ContraRet.1 ~ Market.Return.1,data=All.dat)
summary(Alpha)
##
## Call:
## lm(formula = ContraRet.1 ~ Market.Return.1, data = All.dat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.087553 -0.009588 0.001373 0.010343 0.060880
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.0021850 0.0008362 2.613 0.00927 **
## Market.Return.1 0.9013625 0.0188580 47.797 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.01777 on 460 degrees of freedom
## Multiple R-squared: 0.8324, Adjusted R-squared: 0.832
## F-statistic: 2285 on 1 and 460 DF, p-value: < 2.2e-16