24.index_R

24.index_R - University of California, Los Angeles...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
University of California, Los Angeles Department of Statistics Statistics C183/C283 Instructor: Nicolas Christou Constructing the optimal portfolios - Single index model R commands In this example we use data from 5 stocks plus the S P 500 index. Read the data: data1 <- read.table("http://www.stat.ucla.edu/~nchristo/statistics_c183_c283/ stocks_5_ret.txt", header=TRUE) Read the data in matrix form: b <- as.matrix(data1) Initialize the vectors and matrices: x <- rep(0, 30) xx <- matrix(x, ncol=6, nrow=5) stock <- rep(0,5) alpha <- rep(0,5) beta <- rep(0,5) mse <- rep(0,5) Rbar <- rep(0,5) Ratio <- rep(0,5) col1 <- rep(0,5) col2 <- rep(0,5) col3 <- rep(0,5) col4 <- rep(0,5) col5 <- rep(0,5) Risk free rate: rf <- 0.001 Perform regression of each stock on the index and record α,β,σ 2 ± i : for(i in 1:5){ alpha[i] <- lm(data=data1,formula=data1[,i] ~ data1[,6])$coefficients[1] beta[i] <- lm(data=data1,formula=data1[,i] ~ data1[,6])$coefficients[2] Rbar[i] <- alpha[i]+beta[i]*mean(b[,6]) mse[i] <- sum(lm(data=data1, formula=data1[,i] ~ data1[,6])$residuals^2)/(nrow(b)-2)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 2

24.index_R - University of California, Los Angeles...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online