################################## # Buffett: Berkshire Hathaway ################################## library(fImport) Y = yahooSeries("BRK-A", from = "1990-01-01") # take a look of the data head(Y) plot(Y[,6],type="l",col=20,main="BRKA Share Price", ylab="Price",xlab="Time") # Calculate Returns y = rev(Y$`BRK-A.Adj.Close`) n <- length(y) rets <- y[-1]/y[-n]-1 summary(rets) # Two plots in one # par(mfrow=c(1,2)) plot(rets,pch=20,col=24) abline(0,0) # summarise returns mean(rets) sd(rets) skewness(rets) hist(rets,breaks=50,freq=FALSE,main="Berkshire Returns",col="red") lines(seq(-1,1,0.001),dnorm(seq(-1,1,0.001),mean(rets),sd(rets)),lwd=2.5,col="green") legend("topright",c("Actual Distribution","Normal Distribution"), lty=c(1,1),lwd=c(2.5,2.5),col=c("red","green")) # to save the plot,click "Export" ans "save as image" # ts.plot(y,main="BRK-A price time series",ylab="price",xlab="day") ################################## # Buffett Regressiuon ################################## buffett = read.table("http://faculty.chicagobooth.edu/nicholas.polson/teaching/41000/buffett.txt",header=T) attach(buffett) # Plot the data plot(Market, Buffett, xlab="Market Return", ylab="Buffett Return",pch=20) legend(x="topleft",legend=c("Buffett","Market"),pch=15,col=c(2,4),bty="n") # correlation matrix cor(cbind(Buffett,Market)) # Fit the least-squares line and superimpose this line on the plot model = lm(Buffett~Market) abline(model) title("Buffett vs Market") # Extract the model coefficients coef(model) summary(model) # Improvement in Fit sd(model$residuals) sd(Buffett) # To remove datapoint 10 # buffett_10 = buffett[-10,]