You are on page 1of 4

#Project econometrics.

setwd("C:/Users/RamiroPC/Desktop/lmef/II term/econometrics/Project")
--------------------------------

##Problem 1:

Data<-read.csv("http://www.decg.it/pcoretto/didattica/mef/data/sp500.csv",
header=TRUE)

#I want to know is the objet Data is actually a data.frame.
is.data.frame(Data)
#we obtain TRUE as result, meaning that it's indeed a data.frame.

##Problem 1: Part 1A.

#I will create a matrix where each observation is going to be TRUE if obs=NA and
FALSE otherwise.
#I am also taking off the first row (the dates).

c<-apply(Data[,-(1)],2,is.na)

#with this function we count the number of missing values.
apply(c,2,sum)
#--------------------------------------------------------------
##Problem 1: Part 1B.

## using the diff funtion basically we are computing the difference between the
#observation today and the observation in the previous period.

logretMHS<-diff(log(Data$MHS))

?qqnorm

qqnorm(logretMHS)
qqline(logretMHS)

#the qq plot shows basically how close is the distribution of the variable
#to the normal distribution.
#we can see that the central values of the distribution are pretty close to the
#normal distribution.
#---------------------------------------------------------------

##Problem 1: Part 1C.

LOGRET<-matrix(nrow=1509,ncol=101)
for(t in 1:1508) { for(i in 1:101)
LOGRET[t+1,i]=log(Data[t+1,i+1])-log(Data[t,i+1])}

View(LOGRET)
?write.csv

write.csv(LOGRET, file="logret_sp500.csv.")

#---------------------------------------------------------------
##Problem 1: Part 1D.

logreturns<-function(price,lag,zero.tol){
#setting the default values

"UPS"]))>(0. #Problem 2: Part 2A.ncol=(ncol(price))) colnames(RESULT)<-colnames(price) #the first column is going to be equal since it's basically the dates for(t in 1:((nrow(price)-lag))) { for(k in 1:(ncol(price)-1)) if(isTRUE(price[t+lag.k+1]=NA}} RESULT<-as. length=65) HatSigUps<-vector(mode="numeric"."SP500"]))>(0.k+1]=(log(as.frame'){stop('price must be either a matrix or a data frame.frame(RESULT) RESULT[.25*66))){ #IF IT'S TRUE THAT MORE THAN THE 25% OF OBS ARE NA THEN REPLACE THE ESTIMATION WITH NA. if(missing(zero.j])) {price[i.tol & !is.25*66))| isTRUE(sum((c[(1+(j-1)*22):(22*(j-1)+(66)).na(price[i.lm(u) summary(u)$coefficients #------------------------------------------------------------------- ##Problem 2: Part 2B.j]<=zero.tol)!='numeric'){stop('zero. length=65) c<-is. HatBetaUps[j]<-NA}else{ ."Date"]=price[-lag.integer(lag)-lag)!=0)){stop('lag must be an integer')} #here compare the price with the threshold.k+1]!=0)){ RESULT[t.k+1]!=0) & isTRUE(price[t.data.j]=0}} #create the matrix were the data is stored RESULT<-matrix(nrow=(nrow(price)-lag).numeric(price[t+lag.numeric(price[t. HatBetaUps<-vector(mode="numeric".')} if(class(zero."Date"] return(RESULT) } logreturn<-logreturns(Data) #----------------------------------------------------------------- #----------------------------------------------------------------- #PROBLEM 2 #first we create a subset of the data including only the first 66 obs.k+1]))- log(as.tol)){zero. for(i in 1:nrow(price)){ for(j in 2:ncol(price))if(price[i.k+1])))} else {RESULT[t.tol=1e-8} if(missing(lag)){lag=1} #impose condition in the type of arguments that the funtion admits if(class(price)!='matrix'&class(price)!='data. subset=1:66) coef(u)["SP500"] summary.tol must be a numeric argument')} if(isTRUE((as. u<-lm(logreturn$UPS~1+logreturn$SP500.na(logreturn) for (j in 1:65) if(isTRUE(sum((c[(1+(j-1)*22):(22*(j-1)+(66)).

integer(window)-window)!=0)){stop('window must be an integer')} if(isTRUE(length(ret.asset.main="Estimated B for UPS". HatSigUps[j]<-NA}else{ var<-vector(mode="numeric".subset=((1+(j-1)*22)): (22*(j-1)+66)))[2]} if(isTRUE(sum((c[(1+(j-1)*22):(22*(j-1)+(66)).25*window)))| (isTRUE(sum((c2[(1+(j-1)*period):(period*(j-1)+(window))]))>(0. 2)) boxplot(HatBetaUps.25*66))| isTRUE(sum((c[(1+(j-1)*22):(22*(j-1)+(66)).na(ret.subset=((1+(j-1)*22)): (22*(j-1)+66)))) HatSigUps<-sqrt(var)}} ##PLOTS par(mfrow=c(1.market)-window)/period)) for (j in 1:(length=((length(ret.market) c2<-is.ret.asset) beta<-vector(length=((length(ret.market.window){ if(missing(period)){period=22} if(missing(window)){window=66} if(isTRUE((as. main="Estimated B for UPS") acf(HatBetaUps) acf(HatSigUps) #----------------------------------------------------------------- ##Problem 2: Part 2C.25*window)))){ #IF IT'S TRUE THAT MORE THAN THE 25% OF OBS ARE NA THEN REPLACE THE ESTIMATION WITH NA.length = 65) for (j in 1:65){ var[j]<-var(residuals(lm(logreturn$UPS~1+logreturn$SP500.market)-window)/period))) if((isTRUE(sum((c1[(1+(j-1)*period):(period*(j-1)+(window))]))>(0.integer(period)-period)!=0)){stop('period must be an integer')} if(isTRUE((as.subset=((1+(j-1)*period)):((period*(j- 1)+window))))[2]} for (j in 1:(length=((length(ret.asset~1+ret. HatBetaUps[j]<-coef(lm(logreturn$UPS~1+logreturn$SP500.market.market)-window)/period)) sigma<-vector(length=((length(ret. beta[j]<-NA}else{ beta[j]<-coef(lm(ret.period.market)-window)/period))) if((isTRUE(sum((c1[(1+(j-1)*period):(period*(j-1)+(window))]))>(0. main="Estimated Sigma for UPS".25*window)))| (isTRUE(sum((c2[(1+(j-1)*period):(period*(j-1)+(window))]))>(0.ts(HatBetaUps.subset=((1+(j- 1)*period)):(period*(j-1)+window)))))} .market!=length(ret.25*window)))){ sigma[j]<-NA}else{ sigma[j]<-sqrt(var(residuals(lm(ret."UPS"]))>(0.market.na(ret.asset)))){stop('the series have different lenght')} c1<-is. col=2) plot. col=5) boxplot(HatSigUps."SP500"]))>(0. caprisk<-function(ret.asset~1+ret.25*66))){ #IF IT'S TRUE THAT MORE THAN THE 25% OF OBS ARE NA THEN REPLACE THE ESTIMATION WITH NA.

-1:-2] BETA<-matrix(nrow=65.l]<-caprisk(logreturn$SP500.return[.ncol = 100) for (l in 1:100) BETA[.l])[[2]] colnames(SIGMA)<-colnames(return) . return<-logreturn[.ncol=100) for (l in 1:100) SIGMA[. RESULTS<-list(beta.logreturn$UPS) #the results are equal!!! ##----------------------------------------------------------------- ##Problem 2: Part 2D.sigma) return(RESULTS) } caprisk(logreturn$SP500.l])[[1]] colnames(BETA)<-colnames(return) SIGMA<-matrix(nrow=65.l]<-caprisk(logreturn$SP500.return[.