# Finding the mean-variance efficient portfolio

A mean-variance efficient portfolio can be obtained as the solution of minimizing the portfolio variance under the constraint that the portfolio expected return equals a target return. A convenient R function for doing so is the function portfolio.optim() in the R package tseries. Its default implementation finds the mean-variance efficient portfolio weights under the constraint that the portfolio return equals the return on the equally-weighted portfolio. The only argument needed is the monthly return data on the portfolio components for which the weights need to be determined.

The variable `returns`

containing the monthly returns of the DJIA stocks is already loaded in the console.

This is a part of the course

## “Introduction to Portfolio Analysis in R”

### Exercise instructions

- Load the library
`tseries`

. - Create a mean-variance efficient portfolio of monthly returns using the default of
`portfolio.optim()`

targeting the equally-weighted portfolio return, and assign the output to the variable`opt`

. - Create a vector of weights from your optimized portfolio. Portfolio weights can be found in
`opt$pw`

. Call this`pf_weights`

. - Assign the names to the assets using the provided code.
- Select the optimum weights from
`pf_weights`

that are greater than or equal to 1%, call this`opt_weights`

. - Use barplot() to visualize the distribution of
`opt_weights`

. - Print the expect portfolio return (
`opt$pm`

) and volatility (`opt$ps`

) of the optimized portfolio.

### Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

```
# Load tseries
# Create an optimized portfolio of returns
opt <- portfolio.optim(___)
# Create pf_weights
pf_weights <- ___$pw
# Assign asset names
names(pf_weights) <- colnames(returns)
# Select optimum weights opt_weights
opt_weights <- pf_weights[___ >= 0.01]
# Bar plot of opt_weights
# Print expected portfolio return and volatility
___$pm
___$ps
```

This exercise is part of the course

## Introduction to Portfolio Analysis in R

Apply your finance and R skills to backtest, analyze, and optimize financial portfolios.

We have up to now considered the portfolio weights as given. In this chapter, you learn how to determine in R the portfolio weights that are optimal in terms of achieving a target return with minimum variance, while satisfying constraints on the portfolio weights.

Exercise 1: Modern portfolio theory of Harry MarkowitzExercise 2: Mean-variance based investing in DJIA stocksExercise 3: Exploring monthly returns of the 30 DJIA stocksExercise 4: Finding the mean-variance efficient portfolioExercise 5: Effect of the return targetExercise 6: Imposing weight constraintsExercise 7: The efficient frontierExercise 8: Computing the efficient frontier using a grid of target returnsExercise 9: Interpreting the efficient frontierExercise 10: Properties of the efficient frontierExercise 11: The minimum variance and maximum Sharpe ratio portfolioExercise 12: In-sample vs. out-of-sample evaluationExercise 13: Split-sample evaluationExercise 14: Out of sample performance evaluationExercise 15: It ain't over### What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.