FINANCIAL MATHEMATICS Multiple Asset Optimization

1 Efficient Three-Asset Portfolios

1.1 Excercise 1

Consider three assets S1,S2,S3 with annual mean returns 7%,12%,18% and volatilities 12%,21%,28% respectively. Assume that short sell is permitted. The correlation matrix is given by \[\begin{pmatrix} 1.00 & -0.25 & -0.05 \\ -0.25 & 1.00 & 0.45 \\ -0.05 & 0.45 & 1.00 \end{pmatrix}\]

Find the efficient portfolio consisting of these assets with an annual mean return r=8.86% and compute the volatility of the obtained efficient portfolio.

# r = annual mean return of Port
min_variance = function(sigma, rho , mean_return ,r ) {
  COV = c(sigma[1]*sigma[2]*rho[1],
          sigma[2]*sigma[3]*rho[2],
          sigma[3]*sigma[1]*rho[3])
  
  VCOV = matrix(c(sigma[1]^2,COV[1],COV[3], 
                  COV[1],sigma[2]^2,COV[2], 
                  COV[3],COV[2],sigma[3]^2),
                3, 3, byrow = T)
  dvec =  c(0, 0, 0) # 3 assets
  b0 = c(1, r)
  Amat = cbind(c(1, 1, 1), mean_return)
  solution = solve.QP(2 * VCOV, dvec, Amat, bvec = b0, meq = 2)
  return(solution)
}

sigma = c(0.12, 0.21, 0.28)
rho = c(-0.25, 0.45, -0.05)
mean_return = c(0.07, 0.12, 0.18)
min_var <- min_variance(sigma,rho,mean_return, r = 0.0886)
min_var
## $solution
## [1] 0.68422177 0.26892676 0.04685147
## 
## $value
## [1] 0.008343562
## 
## $unconstrained.solution
## [1] 0 0 0
## 
## $iterations
## [1] 3 0
## 
## $Lagrangian
## [1] 0.01417472 0.02835674
## 
## $iact
## [1] 1 2

The desired efficient variance portfolio has and the risk of the portfolio is 0.0913431008885749.

1.2 Problem 2

Consider three assets A,B,C with annual mean returns 10.73%, 7.37%, 6.27% and volatilities 16.67%, 10.55%, 3.4% respectively.

Their correlation coefficients are: \(\rho_{AB}=0.2199\),\(\rho_{AC}=0.0366\),\(\rho_{BC}=−0.0545\).

Assume that short sell is not permitted.

  1. Establish the corresponding quadratic programming to find the efficient portfolio consisting of these assets with an annual mean return of at least 10.5%.

  2. Write an R code to find and plot the efficient portfolios.

# r = annual mean return of Port
min_variance = function(sigma, rho , mean_return ,r ) {
  COV = c(sigma[1]*sigma[2]*rho[1],
          sigma[2]*sigma[3]*rho[2],
          sigma[3]*sigma[1]*rho[3])
  
  VCOV = matrix(c(sigma[1]^2, COV[1], COV[3], 
                  COV[1], sigma[2]^2, COV[2], 
                  COV[3], COV[2], sigma[3]^2),
                3, 3, byrow = T)
  dvec =  c(0, 0, 0) # 3 assets
  b0 = c(1, r, 0, 0, 0)
  Amat = cbind(c(1, 1, 1), mean_return , c(1, 0, 0), c(0, 1, 0), c(0, 0, 1))
  sol = solve.QP(2 * VCOV, dvec, Amat, bvec = b0, meq = 1)
  return(sol)
}

mean_return = c(0.1073, 0.0737, 0.0627)
rho = c(0.2199, 0.2199, (-0.0545))
sigma = c(0.1667, 0.1055, 0.034)
min_var <- min_variance(sigma,rho,mean_return, r = 0.105)
min_var
## $solution
## [1] 0.93154762 0.06845238 0.00000000
## 
## $value
## [1] 0.02466004
## 
## $unconstrained.solution
## [1] 0 0 0
## 
## $iterations
## [1] 4 0
## 
## $Lagrangian
## [1] 0.086847999 1.296838827 0.000000000 0.000000000 0.005068692
## 
## $iact
## [1] 2 1 5

The desired efficient variance portfolio has and the risk of the portfolio is 0.157035151685753

#### Step 2: CalculateVariance-Covariance(VCOV) Matrix of Returns
VCOV<- matrix(c(0.02778889,0.0038673000,0.0002074400,0.00386730,0.0111302500,-0.0001954915,0.00020744,-0.0001954915,0.0011560000),3,3)


### Step 3: Construct the Target Portfolio ReturnVector
avg.ret=c(0.1073,0.0737,0.0627)
min.ret<-min(avg.ret)

max.ret<-max(avg.ret)

increments=100
tgt.ret<-seq(min.ret,max.ret,length=increments)
head(tgt.ret)
## [1] 0.06270000 0.06315051 0.06360101 0.06405152 0.06450202 0.06495253
## Step 4: Construct Dummy Portfolio Standard DeviationVector
tgt.sd<-rep(0,length=increments)
tgt.sd
##   [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
##  [38] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
##  [75] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#### Step 5: Construct Dummy Portfolio Weights Vector
wgt<-matrix(0,nrow=increments,ncol=length(avg.ret))
### Step 6: Run the quadprog Optimizer
Dmat <- 2*VCOV
dvec <-  c(rep(0,length(avg.ret)))
Amat <- cbind(rep(1,length(avg.ret)),avg.ret,
              + diag(1,nrow=ncol(Dmat)))
for (i in 1:increments){
  bvec <- c(1,tgt.ret[i],rep(0,ncol(Dmat)))
  soln <- solve.QP(Dmat,dvec,Amat,bvec=bvec,meq=2)
  tgt.sd[i] <- sqrt(soln$value)
  wgt[i,] <- soln$solution}
head(wgt)
##               [,1]         [,2]      [,3]
## [1,]  0.000000e+00 4.996004e-16 1.0000000
## [2,] -1.734723e-18 4.095500e-02 0.9590450
## [3,]  0.000000e+00 8.191001e-02 0.9180900
## [4,]  5.952534e-03 9.873019e-02 0.8953173
## [5,]  1.563101e-02 1.004434e-01 0.8839256
## [6,]  2.530949e-02 1.021565e-01 0.8725340
head(tgt.sd)
## [1] 0.03400000 0.03265828 0.03193201 0.03175399 0.03170891 0.03175446
colnames(wgt)<-paste(c("wgt.SA","wgt.SB","wgt.SC"))

### Step 7: CombinePortfolio Returns, Portfolio Standard Deviations, andPortfolio
### Weights

tgt.port<-data.frame(cbind(tgt.ret,tgt.sd,wgt))
head(tgt.port)
##      tgt.ret     tgt.sd        wgt.SA       wgt.SB    wgt.SC
## 1 0.06270000 0.03400000  0.000000e+00 4.996004e-16 1.0000000
## 2 0.06315051 0.03265828 -1.734723e-18 4.095500e-02 0.9590450
## 3 0.06360101 0.03193201  0.000000e+00 8.191001e-02 0.9180900
## 4 0.06405152 0.03175399  5.952534e-03 9.873019e-02 0.8953173
## 5 0.06450202 0.03170891  1.563101e-02 1.004434e-01 0.8839256
## 6 0.06495253 0.03175446  2.530949e-02 1.021565e-01 0.8725340
#### Step 8: Identify the MinimumVariance Portfolio
minvar.port<-subset(tgt.port,tgt.port$tgt.sd==min(tgt.port$tgt.sd))
minvar.port
##      tgt.ret     tgt.sd     wgt.SA    wgt.SB    wgt.SC
## 5 0.06450202 0.03170891 0.01563101 0.1004434 0.8839256
par(mfrow=c(1,1))
weight_minvar <- cbind(minvar.port$wgt.SA,minvar.port$wgt.SB,minvar.port$wgt.SC)
colnames(weight_minvar) <-paste(c("AA","BB","CC"))
pie.values<-as.numeric(weight_minvar)
pie.labels<-colnames(weight_minvar)
pct<-round(pie.values*100)
pie.labels<-paste(pie.labels,pct) # Add Pct to Labels
pie.labels<-paste(pie.labels,"%",sep="") # Add % Sign
pie(pie.values,labels=pie.labels,col=rainbow(length(pie.labels)),main="Global minimum variance portfolio Weighting")

#### Suppose the risk-free interest rate is 7%
riskfree <- 0.07

## way 1: choosing the efficient portfolio having the maximum Sharpe ratio
tgt.port$Sharpe<-(tgt.port$tgt.ret-riskfree)/tgt.port$tgt.sd
tangency.port<-subset(tgt.port,tgt.port$Sharpe==max(tgt.port$Sharpe))
tangency.port
##     tgt.ret tgt.sd wgt.SA       wgt.SB wgt.SC    Sharpe
## 100  0.1073 0.1667      1 5.551115e-16      0 0.2237552
par(mfrow=c(1,1))
weight_tangency <- cbind(tangency.port$wgt.SA,tangency.port$wgt.SB,tangency.port$wgt.SC)
colnames(weight_tangency) <-paste(c("SA","SB","SC"))
pie.values<-as.numeric(weight_tangency)
pie.labels<-colnames(weight_tangency)
pct<-round(pie.values*100)
pie.labels<-paste(pie.labels,pct) # Add Pct to Labels
pie.labels<-paste(pie.labels,"%",sep="") # Add % Sign
pie(pie.values,labels=pie.labels,col=rainbow(length(pie.labels)),main="Tangency portfolio Weighting")

#### Step 10: Identify Efficient Portfolios
eff.frontier<-subset(tgt.port,tgt.port$tgt.ret>=minvar.port$tgt.ret)


### Step 11: Plot theMVEfficient Frontier
plot(x=tgt.sd,
     xlab="Portfolio Risk",
     y=tgt.ret,
     ylab="Portfolio Return",
     col="blue",
     main="Mean-Variance Efficient Frontier of Two Assets
     Based on the Quadratic Programming Approach")
abline(h=0,lty=1)
points(x=minvar.port$tgt.sd,y=minvar.port$tgt.ret,pch=17,cex=3,col="red")
points(x=tangency.port$tgt.sd,y=tangency.port$tgt.ret,pch=19,cex=3,col="purple")
points(x=eff.frontier$tgt.sd,y=eff.frontier$tgt.ret,col="green")

1.3 Exercise 3

Minimize: \[f(\omega1,\omega2) = 0.028935\omega^2_1 + 0.013081\omega^2_2 + 0.010831\omega1\omega2 - 0.002705\omega1 - 0.003511\omega2 \]

Using Partial Derivative, obtain \[\frac{∂f(\omega_1,\omega_2)}{∂\omega_1}=2\cdot0.028935\omega_1+0.010831\omega_2−0.002705=0 (1)\]

\[\frac{∂f(\omega_1,\omega_2)}{∂\omega_2}=0.026162\omega_1+0.010831\omega_2−0.003511=0 (2)\]

Solving (1) and (2), finally we get \[(w_1,w_2,w_3) = (0.02344,0.1245,0.85206)\]

By Hessan Optimization: \[D=fw_1w_1⋅fw_2w_2−[fw_1w_2]=0.0014\] then implies Positive Definite conclude that it has a global minimizer.

#### Step 2: CalculateVariance-Covariance(VCOV) Matrix of Returns
VCOV<- matrix(c(0.02778889,0.0038673000,0.0002074400,0.00386730,0.0111302500,-0.0001954915,0.00020744,-0.0001954915,0.0011560000),3,3)
VCOV
##            [,1]          [,2]          [,3]
## [1,] 0.02778889  0.0038673000  0.0002074400
## [2,] 0.00386730  0.0111302500 -0.0001954915
## [3,] 0.00020744 -0.0001954915  0.0011560000
### Step 3: Construct the Target Portfolio ReturnVector
avg.ret=c(0.1073, 0.0737, 0.0627)
min.ret<-min(avg.ret)

max.ret<-max(avg.ret)

increments=100
tgt.ret<-seq(min.ret,max.ret,length=increments)
head(tgt.ret)
## [1] 0.06270000 0.06315051 0.06360101 0.06405152 0.06450202 0.06495253
## Step 4: Construct Dummy Portfolio Standard DeviationVector
tgt.sd<-rep(0,length=increments)
tgt.sd
##   [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
##  [38] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
##  [75] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#### Step 5: Construct Dummy Portfolio Weights Vector
wgt<-matrix(0,nrow=increments,ncol=length(avg.ret))
Dmat <- 2*VCOV
dvec <-  c(rep(0,length(avg.ret)))
Amat <- cbind(rep(1,length(avg.ret)),avg.ret, + diag(1,nrow=ncol(Dmat)))
for (i in 1:increments){
  bvec <- c(1,tgt.ret[i],rep(0,ncol(Dmat)))
  soln <- solve.QP(Dmat,dvec,Amat,bvec=bvec,meq=2)
  tgt.sd[i] <- sqrt(soln$value)
  wgt[i,] <- soln$solution}

head(wgt)
##               [,1]         [,2]      [,3]
## [1,]  0.000000e+00 4.996004e-16 1.0000000
## [2,] -1.734723e-18 4.095500e-02 0.9590450
## [3,]  0.000000e+00 8.191001e-02 0.9180900
## [4,]  5.952534e-03 9.873019e-02 0.8953173
## [5,]  1.563101e-02 1.004434e-01 0.8839256
## [6,]  2.530949e-02 1.021565e-01 0.8725340
head(tgt.sd)
## [1] 0.03400000 0.03265828 0.03193201 0.03175399 0.03170891 0.03175446
colnames(wgt)<-paste(c("wgt.SA","wgt.SB","wgt.SC"))

### Step 7: CombinePortfolio Returns, Portfolio Standard Deviations, andPortfolio
### Weights

tgt.port<-data.frame(cbind(tgt.ret,tgt.sd,wgt))
head(tgt.port)
##      tgt.ret     tgt.sd        wgt.SA       wgt.SB    wgt.SC
## 1 0.06270000 0.03400000  0.000000e+00 4.996004e-16 1.0000000
## 2 0.06315051 0.03265828 -1.734723e-18 4.095500e-02 0.9590450
## 3 0.06360101 0.03193201  0.000000e+00 8.191001e-02 0.9180900
## 4 0.06405152 0.03175399  5.952534e-03 9.873019e-02 0.8953173
## 5 0.06450202 0.03170891  1.563101e-02 1.004434e-01 0.8839256
## 6 0.06495253 0.03175446  2.530949e-02 1.021565e-01 0.8725340
#### Step 8: Identify the MinimumVariance Portfolio
minvar.port<-subset(tgt.port,tgt.port$tgt.sd==min(tgt.port$tgt.sd))
minvar.port
##      tgt.ret     tgt.sd     wgt.SA    wgt.SB    wgt.SC
## 5 0.06450202 0.03170891 0.01563101 0.1004434 0.8839256
par(mfrow=c(1,1))
weight_minvar <- cbind(minvar.port$wgt.SA,minvar.port$wgt.SB,minvar.port$wgt.SC)
colnames(weight_minvar) <-paste(c("SA","SB","SC"))
pie.values<-as.numeric(weight_minvar)
pie.labels<-colnames(weight_minvar)
pct<-round(pie.values*100)
pie.labels<-paste(pie.labels,pct) # Add Pct to Labels
pie.labels<-paste(pie.labels,"%",sep="") # Add % Sign
pie(pie.values,labels=pie.labels,col=rainbow(length(pie.labels)),main="Global minimum variance portfolio Weighting")

#### Suppose the risk-free interest rate is 7%
riskfree <- 0.07

## way 1: choosing the efficient portfolio having the maximum Sharpe ratio
tgt.port$Sharpe<-(tgt.port$tgt.ret-riskfree)/tgt.port$tgt.sd
tangency.port<-subset(tgt.port,tgt.port$Sharpe==max(tgt.port$Sharpe))
tangency.port
##     tgt.ret tgt.sd wgt.SA       wgt.SB wgt.SC    Sharpe
## 100  0.1073 0.1667      1 5.551115e-16      0 0.2237552
par(mfrow=c(1,1))
weight_tangency <- cbind(tangency.port$wgt.SA,tangency.port$wgt.SB,tangency.port$wgt.SC)
colnames(weight_tangency) <-paste(c("SA","SB","SC"))
pie.values<-as.numeric(weight_tangency)
pie.labels<-colnames(weight_tangency)
pct<-round(pie.values*100)
pie.labels<-paste(pie.labels,pct) # Add Pct to Labels
pie.labels<-paste(pie.labels,"%",sep="") # Add % Sign
pie(pie.values,labels=pie.labels,col=rainbow(length(pie.labels)),main="Tangency portfolio Weighting")

#### Step 10: Identify Efficient Portfolios
eff.frontier<-subset(tgt.port,tgt.port$tgt.ret>=minvar.port$tgt.ret)


### Step 11: Plot theMVEfficient Frontier
plot(x=tgt.sd,
     xlab="Portfolio Risk",
     y=tgt.ret,
     ylab="Portfolio Return",
     col="blue",
     main="Mean-Variance Efficient Frontier of Two Assets
     Based on the Quadratic Programming Approach")
abline(h=0,lty=1)
points(x=minvar.port$tgt.sd,y=minvar.port$tgt.ret,pch=17,cex=3,col="red")
points(x=tangency.port$tgt.sd,y=tangency.port$tgt.ret,pch=19,cex=3,col="purple")
points(x=eff.frontier$tgt.sd,y=eff.frontier$tgt.ret,col="green")

2 Portfolio efficiency

2.1 Excersise 4

Using the historical data in 5 recent years of 3 stocks to compute their yearly expected returns, volalitities, correlation between returns. From then compute and visualize efficient portfolios, global mininum variance portfolio and the tangency portfolio.

symbols  <- c( "NFLX", "AMZN", "AAPL")
        
stocks_data =tq_get(symbols, get = "stock.prices",from = "2018-01-01")
        
df_return = stocks_data  %>% 
group_by(symbol) %>%
tq_transmute(select = adjusted, mutate_fun = periodReturn,period = 'yearly', type = 'log',col_rename = 'returns')
        # Print the variance-covariance matrix
print(v_cov_matrix)
##           NFLX      AMZN      AAPL
## NFLX 0.1750229 0.1734755 0.1143520
## AMZN 0.1734755 0.1775605 0.1193606
## AAPL 0.1143520 0.1193606 0.1375245
## # A tibble: 3 × 2
##   symbol mean_return
##   <chr>        <dbl>
## 1 AAPL        0.236 
## 2 AMZN        0.0965
## 3 NFLX        0.0819
avg.ret <- unlist(mean_return$mean_return)
min.ret<-min(avg.ret)
max.ret<-max(avg.ret)
increments=100
tgt.ret<-seq(min.ret,max.ret,length=increments)
head(tgt.ret) 
## [1] 0.08193820 0.08349654 0.08505488 0.08661322 0.08817157 0.08972991
##   [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
##  [38] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
##  [75] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Dmat <- 2*v_cov_matrix
dvec <-  c(rep(0,length(avg.ret)))
Amat <- cbind(rep(1,length(avg.ret)),avg.ret,diag(1,nrow=ncol(Dmat)))
wgt <- matrix(0, nrow = increments, ncol = ncol(Dmat))
for (i in 1:increments){
   bvec <- c(1,tgt.ret[i],rep(0,ncol(Dmat)))
   soln <- solve.QP(Dmat,dvec,Amat,bvec=bvec,meq=2)
   tgt.sd[i] <- sqrt(soln$value)
   wgt[i,] <- soln$solution}
head(wgt)  
##            [,1]          [,2]      [,3]
## [1,] 0.00000000 -7.771561e-16 1.0000000
## [2,] 0.00000000  1.068860e-01 0.8931140
## [3,] 0.00000000  2.137719e-01 0.7862281
## [4,] 0.00932949  2.219359e-01 0.7687346
## [5,] 0.02048983  2.107265e-01 0.7687837
## [6,] 0.03165016  1.995170e-01 0.7688329
colnames(wgt)<-paste(c("wgt.S1","wgt.S2","wgt.S3"))
tgt.port<-data.frame(cbind(tgt.ret,tgt.sd,wgt))
minvar.port
##      tgt.ret    tgt.sd    wgt.S1 wgt.S2    wgt.S3
## 28 0.1240134 0.3621066 0.2727273      0 0.7272727
par(mfrow=c(1,1))

weight_minvar <- data.frame(minvar.port$wgt.S1, minvar.port$wgt.S2, minvar.port$wgt.S3)
colnames(weight_minvar) <- c("S1", "S2", "S3")

# Identify duplicate rows
duplicates <- duplicated(weight_minvar)

# Remove duplicate rows
weight_minvar_unique <- weight_minvar[!duplicates, ]
pie.values<-as.numeric(unlist(weight_minvar_unique))
if (any(pie.values < 0)) {
  pie.values[pie.values < 0] <- 0
}
pie.labels<-colnames(weight_minvar_unique)
pct<-round(pie.values*100)
pie.labels<-paste(pie.labels,pct) # Add Pct to Labels
pie.labels<-paste(pie.labels,"%",sep="") # Add % Sign
pie(pie.values,labels=pie.labels,col=rainbow(length(pie.labels)),main="Global minimum variance portfolio Weighting")

riskfree <- 0.07
## way 1: choosing the efficient portfolio having the maximum Sharpe ratio
tgt.port$Sharpe<-(tgt.port$tgt.ret-riskfree)/tgt.port$tgt.sd
tangency.port<-subset(tgt.port,tgt.port$Sharpe==max(tgt.port$Sharpe))
tangency.port        
##       tgt.ret    tgt.sd wgt.S1       wgt.S2 wgt.S3    Sharpe
## 100 0.2362139 0.4183574      1 -5.98328e-17      0 0.3973013
par(mar=c(1, 1, 1, 1))
weight_tangency <- cbind(tangency.port$wgt.S1,tangency.port$wgt.S2,tangency.port$wgt.S3)
colnames(weight_tangency) <-paste(c("S1","S2","S3"))
pie.values<-abs(as.numeric(weight_tangency))  # take absolute value of weights
pie.labels<-colnames(weight_tangency)
pct<-round(pie.values*100)
pie.labels<-paste(pie.labels,pct) # Add Pct to Labels
pie.labels<-paste(pie.labels,"%",sep="") # Add % Sign
pie(pie.values,labels=pie.labels,col=rainbow(length(pie.labels)),main="Tangency portfolio Weighting")

### Step 10: Identify Efficient Portfolios
eff.frontier<-subset(tgt.port,tgt.port$tgt.ret>=minvar.port$tgt.ret)

### Step 11: Plot theMVEfficient Frontier
par(mar=c(4, 4, 2, 2))  # Set plot margins
plot(x=tgt.sd,
     xlab="Portfolio Risk",
     y=tgt.ret,
     ylab="Portfolio Return",
     col="blue",
     main="Mean-Variance Efficient Frontier of Two Assets
     Based on the Quadratic Programming Approach")
abline(h=0,lty=1)
points(x=minvar.port$tgt.sd,y=minvar.port$tgt.ret,pch=17,cex=3,col="red")
points(x=tangency.port$tgt.sd,y=tangency.port$tgt.ret,pch=19,cex=3,col="purple")
points(x=eff.frontier$tgt.sd,y=eff.frontier$tgt.ret,col="green")

2.2 Exercise 5

Using the historical data in 5 recent years of 38 stocks to compute their yearly expected returns, volalitities, correlation between returns. From then compute and visualize efficient portfolios, global mininum variance portfolio and the tangency portfolio.

symbols <- c("AAPL", "MSFT", "AMZN", "GOOGL", "FB", "TSLA", "NVDA", "ADBE", "JPM", "JNJ", "V", "PG", "MA", "UNH", "HD", "BAC", "DIS", "INTC", "VZ", "CRM", "KO", "PEP", "CSCO", "PFE", "MRK", "NFLX", "CMCSA", "WMT", "NKE", "MCD")
stocks_data <- tq_get(symbols, get = "stock.prices", from = "2018-01-01")
df_return <- stocks_data %>%
  group_by(symbol) %>%
  tq_transmute(select = adjusted, mutate_fun = periodReturn,
               period = 'yearly', type = 'log',
               col_rename = 'returns')
glimpse(df_return)
## Rows: 174
## Columns: 3
## Groups: symbol [29]
## $ symbol  <chr> "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "MSFT", "MSFT"…
## $ date    <date> 2018-12-31, 2019-12-31, 2020-12-31, 2021-12-31, 2022-12-30, 2…
## $ returns <dbl> -0.07315508, 0.63635365, 0.60052018, 0.29749519, -0.30658210, …
str(df_return)
## gropd_df [174 × 3] (S3: grouped_df/tbl_df/tbl/data.frame)
##  $ symbol : chr [1:174] "AAPL" "AAPL" "AAPL" "AAPL" ...
##  $ date   : Date[1:174], format: "2018-12-31" "2019-12-31" ...
##  $ returns: num [1:174] -0.0732 0.6364 0.6005 0.2975 -0.3066 ...
##  - attr(*, "groups")= tibble [29 × 2] (S3: tbl_df/tbl/data.frame)
##   ..$ symbol: chr [1:29] "AAPL" "ADBE" "AMZN" "BAC" ...
##   ..$ .rows : list<int> [1:29] 
##   .. ..$ : int [1:6] 1 2 3 4 5 6
##   .. ..$ : int [1:6] 37 38 39 40 41 42
##   .. ..$ : int [1:6] 13 14 15 16 17 18
##   .. ..$ : int [1:6] 85 86 87 88 89 90
##   .. ..$ : int [1:6] 151 152 153 154 155 156
##   .. ..$ : int [1:6] 109 110 111 112 113 114
##   .. ..$ : int [1:6] 127 128 129 130 131 132
##   .. ..$ : int [1:6] 91 92 93 94 95 96
##   .. ..$ : int [1:6] 19 20 21 22 23 24
##   .. ..$ : int [1:6] 79 80 81 82 83 84
##   .. ..$ : int [1:6] 97 98 99 100 101 102
##   .. ..$ : int [1:6] 49 50 51 52 53 54
##   .. ..$ : int [1:6] 43 44 45 46 47 48
##   .. ..$ : int [1:6] 115 116 117 118 119 120
##   .. ..$ : int [1:6] 67 68 69 70 71 72
##   .. ..$ : int [1:6] 169 170 171 172 173 174
##   .. ..$ : int [1:6] 139 140 141 142 143 144
##   .. ..$ : int [1:6] 7 8 9 10 11 12
##   .. ..$ : int [1:6] 145 146 147 148 149 150
##   .. ..$ : int [1:6] 163 164 165 166 167 168
##   .. ..$ : int [1:6] 31 32 33 34 35 36
##   .. ..$ : int [1:6] 121 122 123 124 125 126
##   .. ..$ : int [1:6] 133 134 135 136 137 138
##   .. ..$ : int [1:6] 61 62 63 64 65 66
##   .. ..$ : int [1:6] 25 26 27 28 29 30
##   .. ..$ : int [1:6] 73 74 75 76 77 78
##   .. ..$ : int [1:6] 55 56 57 58 59 60
##   .. ..$ : int [1:6] 103 104 105 106 107 108
##   .. ..$ : int [1:6] 157 158 159 160 161 162
##   .. ..@ ptype: int(0) 
##   ..- attr(*, ".drop")= logi TRUE
df_return <- unnest(df_return)
df_return_wide <- pivot_wider(df_return, id_cols = date, names_from = symbol, values_from = returns)
v_cov <- cov(df_return_wide[, -1])
print(v_cov)
##                AAPL         MSFT          AMZN         GOOGL        TSLA
## AAPL   1.375256e-01  0.092975002  0.1193660533  1.020991e-01  0.29187245
## MSFT   9.297500e-02  0.083391805  0.1004196708  9.446712e-02  0.20010257
## AMZN   1.193661e-01  0.100419671  0.1775831945  1.054827e-01  0.37291598
## GOOGL  1.020991e-01  0.094467124  0.1054826673  1.180976e-01  0.23479435
## TSLA   2.918724e-01  0.200102573  0.3729159826  2.347943e-01  1.05145673
## NVDA   2.176627e-01  0.163101478  0.1916188807  2.091487e-01  0.49570316
## ADBE   1.043168e-01  0.089762385  0.1368880078  9.119470e-02  0.27185240
## JPM    4.955124e-02  0.041980852  0.0198484860  4.695543e-02  0.01595781
## JNJ    1.720965e-02  0.007715503 -0.0009610284  8.815468e-03  0.02248398
## V      3.548729e-02  0.024060300  0.0355770161  1.606586e-02  0.04806321
## PG     4.155001e-02  0.032097701  0.0278201021  3.315099e-02  0.05340745
## MA     4.042174e-02  0.028710782  0.0403309751  1.744931e-02  0.04881590
## UNH    1.735924e-02  0.019828738  0.0079540752  2.677152e-02  0.04389698
## HD     7.390648e-02  0.064530943  0.0553123578  8.158483e-02  0.15225936
## BAC    6.608222e-02  0.061277539  0.0254454913  7.398702e-02  0.03989876
## DIS    1.014880e-01  0.077607978  0.1246897227  7.568379e-02  0.23162697
## INTC   7.807111e-02  0.080394887  0.0939795010  8.545037e-02  0.10904409
## VZ     2.775964e-02  0.026827052  0.0419456177  2.218420e-02  0.05181939
## CRM    9.418246e-02  0.093062175  0.1515782110  1.024309e-01  0.27053631
## KO     2.687939e-03  0.002080275 -0.0085705899 -7.490218e-05 -0.02661283
## PEP    2.498534e-02  0.014983633  0.0035132080  1.825446e-02  0.01973106
## CSCO   3.142557e-02  0.047141540  0.0376722250  5.876051e-02  0.05157604
## PFE   -9.353907e-05  0.027177639  0.0095598083  4.192327e-02  0.03935348
## MRK   -5.337080e-02 -0.038493802 -0.0552379454 -5.349215e-02 -0.16354937
## NFLX   1.143540e-01  0.103797696  0.1734848275  1.100844e-01  0.36150264
## CMCSA  8.379416e-02  0.056677789  0.0766013769  6.017494e-02  0.15800469
## WMT    3.953295e-02  0.019688314  0.0269359263  1.756277e-02  0.07214132
## NKE    8.031712e-02  0.067882781  0.0972970454  7.088801e-02  0.20266663
## MCD    1.828119e-02  0.018360839  0.0132891693  2.564331e-02  0.03290021
##               NVDA          ADBE          JPM           JNJ            V
## AAPL   0.217662692  0.1043167649  0.049551242  0.0172096504  0.035487285
## MSFT   0.163101478  0.0897623850  0.041980852  0.0077155031  0.024060300
## AMZN   0.191618881  0.1368880078  0.019848486 -0.0009610284  0.035577016
## GOOGL  0.209148720  0.0911946995  0.046955425  0.0088154679  0.016065865
## TSLA   0.495703159  0.2718524050  0.015957810  0.0224839786  0.048063212
## NVDA   0.436137436  0.1545937758  0.082304291  0.0208317141  0.030953106
## ADBE   0.154593776  0.1160958273  0.031767626  0.0051671632  0.034967146
## JPM    0.082304291  0.0317676261  0.042357516  0.0102850317  0.015473379
## JNJ    0.020831714  0.0051671632  0.010285032  0.0081476125  0.003038738
## V      0.030953106  0.0349671461  0.015473379  0.0030387383  0.019761944
## PG     0.058960785  0.0323163907  0.025137639  0.0081300768  0.013708883
## MA     0.027976055  0.0425557555  0.019684466  0.0046611579  0.024640436
## UNH    0.033196603  0.0151514348  0.013803447  0.0092008067 -0.001006717
## HD     0.141553048  0.0569771725  0.040381472  0.0152320438  0.009779159
## BAC    0.120824931  0.0434653668  0.058298438  0.0168643629  0.014767406
## DIS    0.148770045  0.1018590386  0.030086147  0.0015505578  0.037269415
## INTC   0.137604553  0.0855425535  0.047025279 -0.0014443776  0.029894326
## VZ     0.031164393  0.0375053223  0.011728082 -0.0010149888  0.015634449
## CRM    0.174819873  0.1162002985  0.024465989 -0.0078241570  0.028242145
## KO    -0.004543155 -0.0004739952  0.008897269  0.0036986961  0.003376138
## PEP    0.040713785  0.0092223317  0.017355261  0.0074888748  0.005049648
## CSCO   0.074627740  0.0406610292  0.028768110  0.0034105040  0.005005804
## PFE    0.026352078  0.0183223755  0.012211149  0.0068450111 -0.011283869
## MRK   -0.116737461 -0.0383884552 -0.010986588 -0.0036080634 -0.002676744
## NFLX   0.183903545  0.1382626648  0.023285252  0.0010406074  0.033584191
## CMCSA  0.134046597  0.0642694718  0.031217808  0.0058078127  0.025255471
## WMT    0.048683092  0.0251486916  0.013143210  0.0067546762  0.013235340
## NKE    0.121802699  0.0843507033  0.026835506  0.0063709291  0.024756587
## MCD    0.044845734  0.0130246009  0.012146816  0.0027032940  0.000542400
##                 PG            MA           UNH           HD         BAC
## AAPL   0.041550011  0.0404217351  0.0173592385  0.073906483  0.06608222
## MSFT   0.032097701  0.0287107819  0.0198287377  0.064530943  0.06127754
## AMZN   0.027820102  0.0403309751  0.0079540752  0.055312358  0.02544549
## GOOGL  0.033150985  0.0174493074  0.0267715191  0.081584827  0.07398702
## TSLA   0.053407450  0.0488158995  0.0438969824  0.152259361  0.03989876
## NVDA   0.058960785  0.0279760549  0.0331966026  0.141553048  0.12082493
## ADBE   0.032316391  0.0425557555  0.0151514348  0.056977173  0.04346537
## JPM    0.025137639  0.0196844659  0.0138034470  0.040381472  0.05829844
## JNJ    0.008130077  0.0046611579  0.0092008067  0.015232044  0.01686436
## V      0.013708883  0.0246404360 -0.0010067166  0.009779159  0.01476741
## PG     0.018099627  0.0176223682  0.0107757930  0.028869668  0.03498921
## MA     0.017622368  0.0316804595  0.0012517339  0.012675877  0.02012099
## UNH    0.010775793  0.0012517339  0.0215158599  0.031305533  0.02965768
## HD     0.028869668  0.0126758772  0.0313055326  0.069590378  0.06759350
## BAC    0.034989209  0.0201209891  0.0296576790  0.067593499  0.08771966
## DIS    0.028321641  0.0427968110 -0.0007774750  0.039018606  0.03173737
## INTC   0.030257044  0.0352699655  0.0053774618  0.046940095  0.05894220
## VZ     0.010506240  0.0195090162 -0.0001307097  0.010327247  0.01213787
## CRM    0.022961371  0.0307317132  0.0024120732  0.047454695  0.03048996
## KO     0.005235000  0.0054660695  0.0041166182  0.005089142  0.01236612
## PEP    0.010949772  0.0062482262  0.0076141892  0.019382966  0.02486924
## CSCO   0.017491289  0.0078456772  0.0212347836  0.044251368  0.04886332
## PFE    0.008694881 -0.0090705804  0.0355538188  0.044289642  0.03864971
## MRK   -0.009851110  0.0002790640 -0.0076215407 -0.033990306 -0.01907723
## NFLX   0.030082066  0.0398107260  0.0168564761  0.063592136  0.03516863
## CMCSA  0.024082364  0.0276453566  0.0007115960  0.036019923  0.03566523
## WMT    0.012126874  0.0155360757  0.0019656769  0.014847668  0.01459437
## NKE    0.025808998  0.0303288533  0.0148690792  0.047789825  0.03850559
## MCD    0.006977882  0.0002629045  0.0075757685  0.019521171  0.02009294
##                DIS         INTC            VZ          CRM            KO
## AAPL   0.101487997  0.078071108  0.0277596377  0.094182459  2.687939e-03
## MSFT   0.077607978  0.080394887  0.0268270522  0.093062175  2.080275e-03
## AMZN   0.124689723  0.093979501  0.0419456177  0.151578211 -8.570590e-03
## GOOGL  0.075683787  0.085450366  0.0221842015  0.102430862 -7.490218e-05
## TSLA   0.231626966  0.109044086  0.0518193891  0.270536312 -2.661283e-02
## NVDA   0.148770045  0.137604553  0.0311643935  0.174819873 -4.543155e-03
## ADBE   0.101859039  0.085542553  0.0375053223  0.116200298 -4.739952e-04
## JPM    0.030086147  0.047025279  0.0117280819  0.024465989  8.897269e-03
## JNJ    0.001550558 -0.001444378 -0.0010149888 -0.007824157  3.698696e-03
## V      0.037269415  0.029894326  0.0156344494  0.028242145  3.376138e-03
## PG     0.028321641  0.030257044  0.0105062404  0.022961371  5.235000e-03
## MA     0.042796811  0.035269965  0.0195090162  0.030731713  5.466069e-03
## UNH   -0.000777475  0.005377462 -0.0001307097  0.002412073  4.116618e-03
## HD     0.039018606  0.046940095  0.0103272467  0.047454695  5.089142e-03
## BAC    0.031737368  0.058942201  0.0121378670  0.030489960  1.236612e-02
## DIS    0.103837690  0.084364679  0.0361210061  0.109347212 -1.734054e-03
## INTC   0.084364679  0.103242718  0.0347572970  0.103715270  3.000220e-03
## VZ     0.036121006  0.034757297  0.0163380291  0.039265589  1.103701e-03
## CRM    0.109347212  0.103715270  0.0392655893  0.147094743 -8.146189e-03
## KO    -0.001734054  0.003000220  0.0011037010 -0.008146189  4.067438e-03
## PEP    0.008846876  0.010836553  0.0012265340  0.001282391  3.945340e-03
## CSCO   0.024176771  0.048311071  0.0121890145  0.045399325  3.673592e-03
## PFE   -0.014331600  0.009326324 -0.0011988575  0.010960704  3.553957e-03
## MRK   -0.035698115 -0.025885772 -0.0045437233 -0.047600738  5.077980e-03
## NFLX   0.117678278  0.094460708  0.0418458278  0.148325953 -6.314078e-03
## CMCSA  0.070914276  0.058956753  0.0207544902  0.067411591  4.730321e-04
## WMT    0.027911554  0.014968029  0.0073365351  0.015800054  2.099784e-03
## NKE    0.072367613  0.061473922  0.0258531117  0.081345512  7.059503e-04
## MCD    0.009388842  0.016073044  0.0019985163  0.015456121  7.166669e-04
##                PEP          CSCO           PFE          MRK         NFLX
## AAPL   0.024985336  3.142557e-02 -9.353907e-05 -0.053370804  0.114353998
## MSFT   0.014983633  4.714154e-02  2.717764e-02 -0.038493802  0.103797696
## AMZN   0.003513208  3.767222e-02  9.559808e-03 -0.055237945  0.173484828
## GOOGL  0.018254461  5.876051e-02  4.192327e-02 -0.053492154  0.110084394
## TSLA   0.019731063  5.157604e-02  3.935348e-02 -0.163549372  0.361502638
## NVDA   0.040713785  7.462774e-02  2.635208e-02 -0.116737461  0.183903545
## ADBE   0.009222332  4.066103e-02  1.832238e-02 -0.038388455  0.138262665
## JPM    0.017355261  2.876811e-02  1.221115e-02 -0.010986588  0.023285252
## JNJ    0.007488875  3.410504e-03  6.845011e-03 -0.003608063  0.001040607
## V      0.005049648  5.005804e-03 -1.128387e-02 -0.002676744  0.033584191
## PG     0.010949772  1.749129e-02  8.694881e-03 -0.009851110  0.030082066
## MA     0.006248226  7.845677e-03 -9.070580e-03  0.000279064  0.039810726
## UNH    0.007614189  2.123478e-02  3.555382e-02 -0.007621541  0.016856476
## HD     0.019382966  4.425137e-02  4.428964e-02 -0.033990306  0.063592136
## BAC    0.024869244  4.886332e-02  3.864971e-02 -0.019077226  0.035168625
## DIS    0.008846876  2.417677e-02 -1.433160e-02 -0.035698115  0.117678278
## INTC   0.010836553  4.831107e-02  9.326324e-03 -0.025885772  0.094460708
## VZ     0.001226534  1.218901e-02 -1.198857e-03 -0.004543723  0.041845828
## CRM    0.001282391  4.539932e-02  1.096070e-02 -0.047600738  0.148325953
## KO     0.003945340  3.673592e-03  3.553957e-03  0.005077980 -0.006314078
## PEP    0.009629196  7.778937e-03  3.598607e-03 -0.007264499  0.004220726
## CSCO   0.007778937  4.454430e-02  4.424225e-02 -0.015027631  0.047890356
## PFE    0.003598607  4.424225e-02  7.623844e-02 -0.007638344  0.028598880
## MRK   -0.007264499 -1.502763e-02 -7.638344e-03  0.036078107 -0.052010419
## NFLX   0.004220726  4.789036e-02  2.859888e-02 -0.052010419  0.175025696
## CMCSA  0.013507649  1.585429e-02 -1.610014e-02 -0.031461914  0.069380410
## WMT    0.008088961 -7.691925e-05 -1.083536e-02 -0.010618659  0.023620300
## NKE    0.009357122  3.213978e-02  1.790946e-02 -0.030026171  0.099338628
## MCD    0.005176445  1.442483e-02  1.258936e-02 -0.011023083  0.015216991
##               CMCSA           WMT           NKE           MCD
## AAPL   0.0837941622  3.953295e-02  0.0803171173  0.0182811926
## MSFT   0.0566777889  1.968831e-02  0.0678827808  0.0183608388
## AMZN   0.0766013769  2.693593e-02  0.0972970454  0.0132891693
## GOOGL  0.0601749418  1.756277e-02  0.0708880084  0.0256433080
## TSLA   0.1580046862  7.214132e-02  0.2026666311  0.0329002068
## NVDA   0.1340465966  4.868309e-02  0.1218026990  0.0448457341
## ADBE   0.0642694718  2.514869e-02  0.0843507033  0.0130246009
## JPM    0.0312178077  1.314321e-02  0.0268355064  0.0121468165
## JNJ    0.0058078127  6.754676e-03  0.0063709291  0.0027032940
## V      0.0252554711  1.323534e-02  0.0247565873  0.0005424000
## PG     0.0240823637  1.212687e-02  0.0258089980  0.0069778815
## MA     0.0276453566  1.553608e-02  0.0303288533  0.0002629045
## UNH    0.0007115960  1.965677e-03  0.0148690792  0.0075757685
## HD     0.0360199225  1.484767e-02  0.0477898251  0.0195211713
## BAC    0.0356652307  1.459437e-02  0.0385055890  0.0200929358
## DIS    0.0709142761  2.791155e-02  0.0723676131  0.0093888423
## INTC   0.0589567531  1.496803e-02  0.0614739215  0.0160730441
## VZ     0.0207544902  7.336535e-03  0.0258531117  0.0019985163
## CRM    0.0674115911  1.580005e-02  0.0813455117  0.0154561211
## KO     0.0004730321  2.099784e-03  0.0007059503  0.0007166669
## PEP    0.0135076493  8.088961e-03  0.0093571219  0.0051764448
## CSCO   0.0158542896 -7.691925e-05  0.0321397775  0.0144248340
## PFE   -0.0161001377 -1.083536e-02  0.0179094615  0.0125893569
## MRK   -0.0314619143 -1.061866e-02 -0.0300261707 -0.0110230832
## NFLX   0.0693804097  2.362030e-02  0.0993386278  0.0152169913
## CMCSA  0.0577776941  2.441088e-02  0.0476502230  0.0100136503
## WMT    0.0244108844  1.480573e-02  0.0194813024  0.0021625803
## NKE    0.0476502230  1.948130e-02  0.0623831037  0.0111632988
## MCD    0.0100136503  2.162580e-03  0.0111632988  0.0066983098
library(dplyr)
mean_return <- df_return %>%
  group_by(symbol) %>%
  summarize(mean_return = mean(returns))
mean_return
## # A tibble: 29 × 2
##    symbol mean_return
##    <chr>        <dbl>
##  1 AAPL        0.236 
##  2 ADBE        0.125 
##  3 AMZN        0.0966
##  4 BAC         0.0159
##  5 CMCSA       0.0207
##  6 CRM         0.107 
##  7 CSCO        0.0595
##  8 DIS        -0.0102
##  9 GOOGL       0.115 
## 10 HD          0.0981
## # ℹ 19 more rows
avg.ret <- unlist(mean_return$mean_return)
min.ret<-min(avg.ret)
max.ret<-max(avg.ret)
increments=100
tgt.ret<-seq(min.ret,max.ret,length=increments)
head(tgt.ret) 
## [1] -0.0427475 -0.0388843 -0.0350211 -0.0311579 -0.0272947 -0.0234315
tgt.sd<-rep(0,length=increments)
tgt.sd      
##   [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
##  [38] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
##  [75] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
v_cov
##                AAPL         MSFT          AMZN         GOOGL        TSLA
## AAPL   1.375256e-01  0.092975002  0.1193660533  1.020991e-01  0.29187245
## MSFT   9.297500e-02  0.083391805  0.1004196708  9.446712e-02  0.20010257
## AMZN   1.193661e-01  0.100419671  0.1775831945  1.054827e-01  0.37291598
## GOOGL  1.020991e-01  0.094467124  0.1054826673  1.180976e-01  0.23479435
## TSLA   2.918724e-01  0.200102573  0.3729159826  2.347943e-01  1.05145673
## NVDA   2.176627e-01  0.163101478  0.1916188807  2.091487e-01  0.49570316
## ADBE   1.043168e-01  0.089762385  0.1368880078  9.119470e-02  0.27185240
## JPM    4.955124e-02  0.041980852  0.0198484860  4.695543e-02  0.01595781
## JNJ    1.720965e-02  0.007715503 -0.0009610284  8.815468e-03  0.02248398
## V      3.548729e-02  0.024060300  0.0355770161  1.606586e-02  0.04806321
## PG     4.155001e-02  0.032097701  0.0278201021  3.315099e-02  0.05340745
## MA     4.042174e-02  0.028710782  0.0403309751  1.744931e-02  0.04881590
## UNH    1.735924e-02  0.019828738  0.0079540752  2.677152e-02  0.04389698
## HD     7.390648e-02  0.064530943  0.0553123578  8.158483e-02  0.15225936
## BAC    6.608222e-02  0.061277539  0.0254454913  7.398702e-02  0.03989876
## DIS    1.014880e-01  0.077607978  0.1246897227  7.568379e-02  0.23162697
## INTC   7.807111e-02  0.080394887  0.0939795010  8.545037e-02  0.10904409
## VZ     2.775964e-02  0.026827052  0.0419456177  2.218420e-02  0.05181939
## CRM    9.418246e-02  0.093062175  0.1515782110  1.024309e-01  0.27053631
## KO     2.687939e-03  0.002080275 -0.0085705899 -7.490218e-05 -0.02661283
## PEP    2.498534e-02  0.014983633  0.0035132080  1.825446e-02  0.01973106
## CSCO   3.142557e-02  0.047141540  0.0376722250  5.876051e-02  0.05157604
## PFE   -9.353907e-05  0.027177639  0.0095598083  4.192327e-02  0.03935348
## MRK   -5.337080e-02 -0.038493802 -0.0552379454 -5.349215e-02 -0.16354937
## NFLX   1.143540e-01  0.103797696  0.1734848275  1.100844e-01  0.36150264
## CMCSA  8.379416e-02  0.056677789  0.0766013769  6.017494e-02  0.15800469
## WMT    3.953295e-02  0.019688314  0.0269359263  1.756277e-02  0.07214132
## NKE    8.031712e-02  0.067882781  0.0972970454  7.088801e-02  0.20266663
## MCD    1.828119e-02  0.018360839  0.0132891693  2.564331e-02  0.03290021
##               NVDA          ADBE          JPM           JNJ            V
## AAPL   0.217662692  0.1043167649  0.049551242  0.0172096504  0.035487285
## MSFT   0.163101478  0.0897623850  0.041980852  0.0077155031  0.024060300
## AMZN   0.191618881  0.1368880078  0.019848486 -0.0009610284  0.035577016
## GOOGL  0.209148720  0.0911946995  0.046955425  0.0088154679  0.016065865
## TSLA   0.495703159  0.2718524050  0.015957810  0.0224839786  0.048063212
## NVDA   0.436137436  0.1545937758  0.082304291  0.0208317141  0.030953106
## ADBE   0.154593776  0.1160958273  0.031767626  0.0051671632  0.034967146
## JPM    0.082304291  0.0317676261  0.042357516  0.0102850317  0.015473379
## JNJ    0.020831714  0.0051671632  0.010285032  0.0081476125  0.003038738
## V      0.030953106  0.0349671461  0.015473379  0.0030387383  0.019761944
## PG     0.058960785  0.0323163907  0.025137639  0.0081300768  0.013708883
## MA     0.027976055  0.0425557555  0.019684466  0.0046611579  0.024640436
## UNH    0.033196603  0.0151514348  0.013803447  0.0092008067 -0.001006717
## HD     0.141553048  0.0569771725  0.040381472  0.0152320438  0.009779159
## BAC    0.120824931  0.0434653668  0.058298438  0.0168643629  0.014767406
## DIS    0.148770045  0.1018590386  0.030086147  0.0015505578  0.037269415
## INTC   0.137604553  0.0855425535  0.047025279 -0.0014443776  0.029894326
## VZ     0.031164393  0.0375053223  0.011728082 -0.0010149888  0.015634449
## CRM    0.174819873  0.1162002985  0.024465989 -0.0078241570  0.028242145
## KO    -0.004543155 -0.0004739952  0.008897269  0.0036986961  0.003376138
## PEP    0.040713785  0.0092223317  0.017355261  0.0074888748  0.005049648
## CSCO   0.074627740  0.0406610292  0.028768110  0.0034105040  0.005005804
## PFE    0.026352078  0.0183223755  0.012211149  0.0068450111 -0.011283869
## MRK   -0.116737461 -0.0383884552 -0.010986588 -0.0036080634 -0.002676744
## NFLX   0.183903545  0.1382626648  0.023285252  0.0010406074  0.033584191
## CMCSA  0.134046597  0.0642694718  0.031217808  0.0058078127  0.025255471
## WMT    0.048683092  0.0251486916  0.013143210  0.0067546762  0.013235340
## NKE    0.121802699  0.0843507033  0.026835506  0.0063709291  0.024756587
## MCD    0.044845734  0.0130246009  0.012146816  0.0027032940  0.000542400
##                 PG            MA           UNH           HD         BAC
## AAPL   0.041550011  0.0404217351  0.0173592385  0.073906483  0.06608222
## MSFT   0.032097701  0.0287107819  0.0198287377  0.064530943  0.06127754
## AMZN   0.027820102  0.0403309751  0.0079540752  0.055312358  0.02544549
## GOOGL  0.033150985  0.0174493074  0.0267715191  0.081584827  0.07398702
## TSLA   0.053407450  0.0488158995  0.0438969824  0.152259361  0.03989876
## NVDA   0.058960785  0.0279760549  0.0331966026  0.141553048  0.12082493
## ADBE   0.032316391  0.0425557555  0.0151514348  0.056977173  0.04346537
## JPM    0.025137639  0.0196844659  0.0138034470  0.040381472  0.05829844
## JNJ    0.008130077  0.0046611579  0.0092008067  0.015232044  0.01686436
## V      0.013708883  0.0246404360 -0.0010067166  0.009779159  0.01476741
## PG     0.018099627  0.0176223682  0.0107757930  0.028869668  0.03498921
## MA     0.017622368  0.0316804595  0.0012517339  0.012675877  0.02012099
## UNH    0.010775793  0.0012517339  0.0215158599  0.031305533  0.02965768
## HD     0.028869668  0.0126758772  0.0313055326  0.069590378  0.06759350
## BAC    0.034989209  0.0201209891  0.0296576790  0.067593499  0.08771966
## DIS    0.028321641  0.0427968110 -0.0007774750  0.039018606  0.03173737
## INTC   0.030257044  0.0352699655  0.0053774618  0.046940095  0.05894220
## VZ     0.010506240  0.0195090162 -0.0001307097  0.010327247  0.01213787
## CRM    0.022961371  0.0307317132  0.0024120732  0.047454695  0.03048996
## KO     0.005235000  0.0054660695  0.0041166182  0.005089142  0.01236612
## PEP    0.010949772  0.0062482262  0.0076141892  0.019382966  0.02486924
## CSCO   0.017491289  0.0078456772  0.0212347836  0.044251368  0.04886332
## PFE    0.008694881 -0.0090705804  0.0355538188  0.044289642  0.03864971
## MRK   -0.009851110  0.0002790640 -0.0076215407 -0.033990306 -0.01907723
## NFLX   0.030082066  0.0398107260  0.0168564761  0.063592136  0.03516863
## CMCSA  0.024082364  0.0276453566  0.0007115960  0.036019923  0.03566523
## WMT    0.012126874  0.0155360757  0.0019656769  0.014847668  0.01459437
## NKE    0.025808998  0.0303288533  0.0148690792  0.047789825  0.03850559
## MCD    0.006977882  0.0002629045  0.0075757685  0.019521171  0.02009294
##                DIS         INTC            VZ          CRM            KO
## AAPL   0.101487997  0.078071108  0.0277596377  0.094182459  2.687939e-03
## MSFT   0.077607978  0.080394887  0.0268270522  0.093062175  2.080275e-03
## AMZN   0.124689723  0.093979501  0.0419456177  0.151578211 -8.570590e-03
## GOOGL  0.075683787  0.085450366  0.0221842015  0.102430862 -7.490218e-05
## TSLA   0.231626966  0.109044086  0.0518193891  0.270536312 -2.661283e-02
## NVDA   0.148770045  0.137604553  0.0311643935  0.174819873 -4.543155e-03
## ADBE   0.101859039  0.085542553  0.0375053223  0.116200298 -4.739952e-04
## JPM    0.030086147  0.047025279  0.0117280819  0.024465989  8.897269e-03
## JNJ    0.001550558 -0.001444378 -0.0010149888 -0.007824157  3.698696e-03
## V      0.037269415  0.029894326  0.0156344494  0.028242145  3.376138e-03
## PG     0.028321641  0.030257044  0.0105062404  0.022961371  5.235000e-03
## MA     0.042796811  0.035269965  0.0195090162  0.030731713  5.466069e-03
## UNH   -0.000777475  0.005377462 -0.0001307097  0.002412073  4.116618e-03
## HD     0.039018606  0.046940095  0.0103272467  0.047454695  5.089142e-03
## BAC    0.031737368  0.058942201  0.0121378670  0.030489960  1.236612e-02
## DIS    0.103837690  0.084364679  0.0361210061  0.109347212 -1.734054e-03
## INTC   0.084364679  0.103242718  0.0347572970  0.103715270  3.000220e-03
## VZ     0.036121006  0.034757297  0.0163380291  0.039265589  1.103701e-03
## CRM    0.109347212  0.103715270  0.0392655893  0.147094743 -8.146189e-03
## KO    -0.001734054  0.003000220  0.0011037010 -0.008146189  4.067438e-03
## PEP    0.008846876  0.010836553  0.0012265340  0.001282391  3.945340e-03
## CSCO   0.024176771  0.048311071  0.0121890145  0.045399325  3.673592e-03
## PFE   -0.014331600  0.009326324 -0.0011988575  0.010960704  3.553957e-03
## MRK   -0.035698115 -0.025885772 -0.0045437233 -0.047600738  5.077980e-03
## NFLX   0.117678278  0.094460708  0.0418458278  0.148325953 -6.314078e-03
## CMCSA  0.070914276  0.058956753  0.0207544902  0.067411591  4.730321e-04
## WMT    0.027911554  0.014968029  0.0073365351  0.015800054  2.099784e-03
## NKE    0.072367613  0.061473922  0.0258531117  0.081345512  7.059503e-04
## MCD    0.009388842  0.016073044  0.0019985163  0.015456121  7.166669e-04
##                PEP          CSCO           PFE          MRK         NFLX
## AAPL   0.024985336  3.142557e-02 -9.353907e-05 -0.053370804  0.114353998
## MSFT   0.014983633  4.714154e-02  2.717764e-02 -0.038493802  0.103797696
## AMZN   0.003513208  3.767222e-02  9.559808e-03 -0.055237945  0.173484828
## GOOGL  0.018254461  5.876051e-02  4.192327e-02 -0.053492154  0.110084394
## TSLA   0.019731063  5.157604e-02  3.935348e-02 -0.163549372  0.361502638
## NVDA   0.040713785  7.462774e-02  2.635208e-02 -0.116737461  0.183903545
## ADBE   0.009222332  4.066103e-02  1.832238e-02 -0.038388455  0.138262665
## JPM    0.017355261  2.876811e-02  1.221115e-02 -0.010986588  0.023285252
## JNJ    0.007488875  3.410504e-03  6.845011e-03 -0.003608063  0.001040607
## V      0.005049648  5.005804e-03 -1.128387e-02 -0.002676744  0.033584191
## PG     0.010949772  1.749129e-02  8.694881e-03 -0.009851110  0.030082066
## MA     0.006248226  7.845677e-03 -9.070580e-03  0.000279064  0.039810726
## UNH    0.007614189  2.123478e-02  3.555382e-02 -0.007621541  0.016856476
## HD     0.019382966  4.425137e-02  4.428964e-02 -0.033990306  0.063592136
## BAC    0.024869244  4.886332e-02  3.864971e-02 -0.019077226  0.035168625
## DIS    0.008846876  2.417677e-02 -1.433160e-02 -0.035698115  0.117678278
## INTC   0.010836553  4.831107e-02  9.326324e-03 -0.025885772  0.094460708
## VZ     0.001226534  1.218901e-02 -1.198857e-03 -0.004543723  0.041845828
## CRM    0.001282391  4.539932e-02  1.096070e-02 -0.047600738  0.148325953
## KO     0.003945340  3.673592e-03  3.553957e-03  0.005077980 -0.006314078
## PEP    0.009629196  7.778937e-03  3.598607e-03 -0.007264499  0.004220726
## CSCO   0.007778937  4.454430e-02  4.424225e-02 -0.015027631  0.047890356
## PFE    0.003598607  4.424225e-02  7.623844e-02 -0.007638344  0.028598880
## MRK   -0.007264499 -1.502763e-02 -7.638344e-03  0.036078107 -0.052010419
## NFLX   0.004220726  4.789036e-02  2.859888e-02 -0.052010419  0.175025696
## CMCSA  0.013507649  1.585429e-02 -1.610014e-02 -0.031461914  0.069380410
## WMT    0.008088961 -7.691925e-05 -1.083536e-02 -0.010618659  0.023620300
## NKE    0.009357122  3.213978e-02  1.790946e-02 -0.030026171  0.099338628
## MCD    0.005176445  1.442483e-02  1.258936e-02 -0.011023083  0.015216991
##               CMCSA           WMT           NKE           MCD
## AAPL   0.0837941622  3.953295e-02  0.0803171173  0.0182811926
## MSFT   0.0566777889  1.968831e-02  0.0678827808  0.0183608388
## AMZN   0.0766013769  2.693593e-02  0.0972970454  0.0132891693
## GOOGL  0.0601749418  1.756277e-02  0.0708880084  0.0256433080
## TSLA   0.1580046862  7.214132e-02  0.2026666311  0.0329002068
## NVDA   0.1340465966  4.868309e-02  0.1218026990  0.0448457341
## ADBE   0.0642694718  2.514869e-02  0.0843507033  0.0130246009
## JPM    0.0312178077  1.314321e-02  0.0268355064  0.0121468165
## JNJ    0.0058078127  6.754676e-03  0.0063709291  0.0027032940
## V      0.0252554711  1.323534e-02  0.0247565873  0.0005424000
## PG     0.0240823637  1.212687e-02  0.0258089980  0.0069778815
## MA     0.0276453566  1.553608e-02  0.0303288533  0.0002629045
## UNH    0.0007115960  1.965677e-03  0.0148690792  0.0075757685
## HD     0.0360199225  1.484767e-02  0.0477898251  0.0195211713
## BAC    0.0356652307  1.459437e-02  0.0385055890  0.0200929358
## DIS    0.0709142761  2.791155e-02  0.0723676131  0.0093888423
## INTC   0.0589567531  1.496803e-02  0.0614739215  0.0160730441
## VZ     0.0207544902  7.336535e-03  0.0258531117  0.0019985163
## CRM    0.0674115911  1.580005e-02  0.0813455117  0.0154561211
## KO     0.0004730321  2.099784e-03  0.0007059503  0.0007166669
## PEP    0.0135076493  8.088961e-03  0.0093571219  0.0051764448
## CSCO   0.0158542896 -7.691925e-05  0.0321397775  0.0144248340
## PFE   -0.0161001377 -1.083536e-02  0.0179094615  0.0125893569
## MRK   -0.0314619143 -1.061866e-02 -0.0300261707 -0.0110230832
## NFLX   0.0693804097  2.362030e-02  0.0993386278  0.0152169913
## CMCSA  0.0577776941  2.441088e-02  0.0476502230  0.0100136503
## WMT    0.0244108844  1.480573e-02  0.0194813024  0.0021625803
## NKE    0.0476502230  1.948130e-02  0.0623831037  0.0111632988
## MCD    0.0100136503  2.162580e-03  0.0111632988  0.0066983098
library(Matrix)
Dmat <- 2*v_cov
Dmat <- as.matrix((nearPD(Dmat))$mat)
dvec <-  c(rep(0,length(avg.ret)))
Amat <- cbind(rep(1,length(avg.ret)),avg.ret,diag(1,nrow=length(avg.ret)))
wgt<-matrix(0,nrow=increments,ncol=length(avg.ret))
for (i in (1+1):(increments-1)){
  bvec <- c(1,tgt.ret[i],rep(0,ncol(Dmat)))
  soln <- solve.QP(Dmat,dvec,Amat,bvec=bvec,meq=2)
  tgt.sd[i] <- sqrt(soln$value)
  wgt[i,] <- soln$solution
}

tgt.port<-data.frame(cbind(tgt.ret,tgt.sd,abs(wgt),tgt.sr=tgt.ret/tgt.sd))
minvar.port <- tgt.port[tgt.port$tgt.sd !=0,]
minvar.port <- minvar.port[which.min(minvar.port$tgt.sd),]
minvar.port_weights=as.numeric(minvar.port[1,3:(length(unique(prices$symbol))+2)])
minvar.port$tgt.ret
## [1] 0.09632774
par(mfrow=c(1,1))
weight_minvar <- cbind(minvar.port[0:(length(unique(stocks_data$symbol))+2)])
colnames(weight_minvar) <-symbols
pie.values<-as.numeric(weight_minvar)
pie.labels<-colnames(weight_minvar)
pct<-round(pie.values*100)
pie.labels<-paste(pie.labels,pct) # Add Pct to Labels
pie.labels<-paste(pie.labels,"%",sep="") # Add % Sign
pie(pie.values,labels=pie.labels,col=rainbow(length(pie.labels)),main="Global minimum variance portfolio Weighting")

eff.frontier<-subset(tgt.port,tgt.port$tgt.ret>=minvar.port$tgt.ret)
plot(x=tgt.sd,xlab="Portfolio Risk",y=tgt.ret,ylab="Portfolio Return",col="blue",main="Mean-Variance Efficient Frontier of Two Assets
    Based on the Quadratic Programming Approach")
abline(h=0,lty=1)
points(x=minvar.port$tgt.sd,y=minvar.port$tgt.ret,pch=17,cex=3,col="red")
points(x=tangency.port$tgt.sd,y=tangency.port$tgt.ret,pch=19,cex=3,col="purple")
points(x=eff.frontier$tgt.sd,y=eff.frontier$tgt.ret,col="green")