# Import data file
rm(list=ls())
firm_data <- read.csv("3firmExample_data1.csv")
str(firm_data)
## 'data.frame':    59 obs. of  4 variables:
##  $ date     : chr  "Mar-95" "Apr-95" "May-95" "Jun-95" ...
##  $ Nordstrom: num  -0.03615 -0.0568 0.07821 -0.00302 -0.02757 ...
##  $ Starbucks: num  0.00521 -0.02105 0.21244 0.2036 0.04797 ...
##  $ Microsoft: num  0.1213 0.13923 0.03529 0.06501 0.00138 ...
library(xts)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
date <- lubridate::my(firm_data[,1])
firm_data.xts <- as.xts(firm_data[, -1], order.by = date)
class(firm_data.xts)
## [1] "xts" "zoo"
#
library(quantmod)
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr   1.1.2     ✔ readr   2.1.4
## ✔ forcats 1.0.0     ✔ stringr 1.5.0
## ✔ ggplot2 3.4.2     ✔ tibble  3.2.1
## ✔ purrr   1.0.1     ✔ tidyr   1.3.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::first()  masks xts::first()
## ✖ dplyr::lag()    masks stats::lag()
## ✖ dplyr::last()   masks xts::last()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(tidyquant)
## Loading required package: PerformanceAnalytics
## 
## Attaching package: 'PerformanceAnalytics'
## 
## The following object is masked from 'package:graphics':
## 
##     legend
library(fBasics)
## 
## Attaching package: 'fBasics'
## 
## The following objects are masked from 'package:PerformanceAnalytics':
## 
##     kurtosis, skewness
## 
## The following object is masked from 'package:TTR':
## 
##     volatility
Sigma <- cov(firm_data[, 2:4])
ones <- rep(1, 3)
one.vec <- matrix(ones, ncol =1)
a <- inv(Sigma)%*%one.vec
b <- t(one.vec)%*%a
mvp.w <- a / as.numeric(b)
# Given constraint: Portfolio return is specified as 0.045 annual rate.  
# Try to find the optimal weights for the minimum variance portfolio. 
# Reference: p18 of slide "Mean-Variance Portfolio Theory_2023.ppt"
#=============================================================================
mu <- 0.045/12
return <- firm_data[,2:4]
Ax <- rbind(2*cov(return), colMeans(return), rep(1, ncol(return)))
Ax <- cbind(Ax, rbind(t(tail(Ax, 2)), matrix(0, 2, 2)))
b0 <- c(rep(0, ncol(return)), mu, 1)
out <- solve(Ax, b0)
wgt <- out[1:3]
wgt
##   Nordstrom   Starbucks   Microsoft 
##  0.90766373  0.11201021 -0.01967394
sum(wgt)
## [1] 1
# Verfity the return
ret.out<-sum(wgt*colMeans(return))
ret.out.annual<-ret.out*12
ret.out.annual
## [1] 0.045
# Compute the standard deviation or risk of the portfolio
std.out<-sqrt(t(wgt)%*%cov(return)%*%wgt)
std.out.annual<-std.out*sqrt(12)
std.out.annual
##           [,1]
## [1,] 0.3444032