Portfolio Theory Dr. Andrea Rigamonti andrea.rigamonti@econ.muni.cz Seminar 3 Content: • Compute mean and SD and plot them • Compute and plot wealth dynamics • Mean and variance of a portfolio • Rolling and expanding window estimation Compute mean and SD and plot them • We can load the data using “load("dataset.RData")” if it is in the same working directory. Or we can use the “load workspace” button in the environment panel on the right. • First we select only the columns with returns. • To compute the mean return of each stock, we compute the mean of each column with the “colMeans” function. • To compute the standard deviation of each stock we use “apply”. The “2” indicates we want to apply the function “sd” by column. If we wrote “1” we would apply it by row. • We can now draw the scatterplot. Compute and plot wealth dynamics • We compute the returns of an equally weighted portfolio simply by computing the mean return of each row using “rowMeans”, and we compute and plot its mean and sd. • We use “points” to plot it on the same graph we created before. We specify the color red with the “col” option. • We then create a custom function to compute the evolution of wealth of this portfolio. It uses the formula: 𝑉𝑇 = 𝑉0 + ෍ 𝑡=1 𝑇 ( 𝑉𝑡−1 𝑅𝑡) Compute and plot wealth dynamics • While the dataset already has a date column, the vector of wealth has one additional observation, because we added one with value 1 at the beginning (we invest 1 unit of wealth one month before the date of the first return). • So we create a new date vector by creating a series of first day of month dates, and then subtracting one day. This gives us a vector of end of month dates. • Then we plot. The date is used on the horizontal axis and the wealth on the vertical axis. “type= " l" ” is used to draw a line; otherwise we would get points. Mean and variance of a portfolio • We consider five of the stocks and use them to assemble a portfolio with given weights. • We can easily compute the mean of this portfolio using the formula in matrix notation µ 𝑝 = 𝒘′µ • Likewise, we compute the variance of the portfolio using 𝜎 𝑝 2 = 𝒘′∑𝒘 • If we want the standard deviation of the portfolio, we can simply compute the square root of the variance. Rolling and expanding window estimation We now compute rolling and expanding window estimates of the mean and covariance matrix • We do this using a “for” cycle. To get a rolling window we index the starting of the subset with “i”, while to get an expanding window we always start at “1”. • We store the vectors of means and the covariance matrices into list objects.