Using the attached data file (3firmExample_data1.xlsx or 3firmExample_data1.csv):

X3firmExample_data1 <- read_excel("C:/Users/admin/Documents/3firmExample_data1.xlsx")
firm_data <- X3firmExample_data1
View(firm_data)
str(firm_data)
## tibble [59 × 4] (S3: tbl_df/tbl/data.frame)
##  $ date     : POSIXct[1:59], format: "1995-03-01" "1995-04-01" ...
##  $ Nordstrom: num [1:59] -0.03615 -0.0568 0.07821 -0.00302 -0.02757 ...
##  $ Starbucks: num [1:59] 0.00521 -0.02105 0.21244 0.2036 0.04797 ...
##  $ Microsoft: num [1:59] 0.1213 0.13923 0.03529 0.06501 0.00138 ...
library(xts)
## Warning: package 'xts' was built under R version 4.2.2
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.2.2
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## ################################### WARNING ###################################
## # We noticed you have dplyr installed. The dplyr lag() function breaks how    #
## # base R's lag() function is supposed to work, which breaks lag(my_xts).      #
## #                                                                             #
## # If you call library(dplyr) later in this session, then calls to lag(my_xts) #
## # that you enter or source() into this session won't work correctly.          #
## #                                                                             #
## # All package code is unaffected because it is protected by the R namespace   #
## # mechanism.                                                                  #
## #                                                                             #
## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning.  #
## #                                                                             #
## # You can use stats::lag() to make sure you're not using dplyr::lag(), or you #
## # can add conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop   #
## # dplyr from breaking base R's lag() function.                                #
## ################################### WARNING ###################################
as.Date(firm_data$date,format = "%Y/%m/%d",tz = "NZ")
##  [1] "1995-03-01" "1995-04-01" "1995-05-01" "1995-06-01" "1995-07-01"
##  [6] "1995-08-01" "1995-09-01" "1995-10-01" "1995-11-01" "1995-12-01"
## [11] "1996-01-01" "1996-02-01" "1996-03-01" "1996-04-01" "1996-05-01"
## [16] "1996-06-01" "1996-07-01" "1996-08-01" "1996-09-01" "1996-10-01"
## [21] "1996-11-01" "1996-12-01" "1997-01-01" "1997-02-01" "1997-03-01"
## [26] "1997-04-01" "1997-05-01" "1997-06-01" "1997-07-01" "1997-08-01"
## [31] "1997-09-01" "1997-10-01" "1997-11-01" "1997-12-01" "1998-01-01"
## [36] "1998-02-01" "1998-03-01" "1998-04-01" "1998-05-01" "1998-06-01"
## [41] "1998-07-01" "1998-08-01" "1998-09-01" "1998-10-01" "1998-11-01"
## [46] "1998-12-01" "1999-01-01" "1999-02-01" "1999-03-01" "1999-04-01"
## [51] "1999-05-01" "1999-06-01" "1999-07-01" "1999-08-01" "1999-09-01"
## [56] "1999-10-01" "1999-11-01" "1999-12-01" "2000-01-01"
  1. Compute the optimal weights for the global minimum variance portfolio based on three assets.
library(fBasics)
## Warning: package 'fBasics' was built under R version 4.2.2
library(quantmod)
## Warning: package 'quantmod' was built under R version 4.2.2
## Loading required package: TTR
## Warning: package 'TTR' was built under R version 4.2.2
## 
## Attaching package: 'TTR'
## The following object is masked from 'package:fBasics':
## 
##     volatility
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✔ ggplot2 3.3.6     ✔ purrr   0.3.4
## ✔ tibble  3.1.7     ✔ dplyr   1.0.9
## ✔ tidyr   1.2.0     ✔ stringr 1.4.0
## ✔ readr   2.1.2     ✔ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::first()  masks xts::first()
## ✖ dplyr::lag()    masks stats::lag()
## ✖ dplyr::last()   masks xts::last()
library(tidyquant)
## Warning: package 'tidyquant' was built under R version 4.2.2
## Loading required package: lubridate
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
## Loading required package: PerformanceAnalytics
## Warning: package 'PerformanceAnalytics' was built under R version 4.2.2
## 
## Attaching package: 'PerformanceAnalytics'
## The following objects are masked from 'package:fBasics':
## 
##     kurtosis, skewness
## The following object is masked from 'package:graphics':
## 
##     legend
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)
mvp.w
##                [,1]
## Nordstrom 0.3635964
## Starbucks 0.1936574
## Microsoft 0.4427462
  1. For a given portfolio return of 4.5%, compute the optimal weights for the minimum variance portfolio based on the three assets and portfolio risk (standard deviation).
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.90766134  0.11201674 -0.01967808
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.344404