**By Gabriel Vasconcelos**

## Overview

There are several ways to do portfolio optimization out there, each with its advantages and disadvantages. We already discussed some techniques here. Today I am going to show another method to perform portfolio optimization that works very well in large datasets because it produces very robust weights, which results in a good out-of-sample performance. This technique is called Parametric Portfolio Policies (PPP) and it was proposed by Brandt, Santa-Clara and Valkanov in 2009 (click here to read the full article).

A portfolio is a set of weights that states the proportion of wealth in asset as time . For example, in a Markowitz portfolio we get by minimizing the portfolio variance subject to some target return. This is like estimating one weight for each asset if you think of weights as parameters of a model. If you have 1000 assets you will have to estimate 1000 parameters. Naturally, you can’t expect a good out-of-sample performance from a simple Markowitz portfolio with 1000 assets just as you can’t expect a good performance from a simple regression with 1000 variables. The Parametric Portfolio Policy (PPP) is made exactly to this high-dimensional case. It parametrizes the weights on some asset characteristics in a way that we have to estimate only one parameter for each characteristic. Consider the following portfolio policy:

Here, the portfolio is a function of a base portfolio , which will be a simple equal weighted portfolio in my example. is a vector of characteristics for asset at time , is the vector of parameters and is the number of available assets. Note that is standardized cross-sectionally to make sure the sum of the weights in each period is one. If we have two characteristics we only need to estimate two parameters and use the policy equation to recover all weights in the portfolio. Unfortunately using a policy like this makes the portfolio much more restricted than the Markowitz case. In a Markowitz portfolio weights can be anything as long as the sum 1 but in the PPP the portfolio must satisfy the policy equation.

Now that we have the policy we need a way to obtain . We can use utility functions and try to maximize the portfolio utility. In the equation below we have the populational case in the left and the sample case in the right.

where:

Putting all together we have:

where the utility function will be the Constant Relative Risk Aversion (CRRA) with relative risk aversion , define as:

## The data

The PPP parametrizes the weights on characteristics of the stocks, and although we can get a lot of characteristics from returns such as momentum and variance, we are likely to need something else to have good results. In the original paper Brandt Santa-Clara and Valkanov used momentum, book-to-market ratio and market capitalization (mktcap) . I will use only a momentum measure and the mktcap because they are easier to get and need no complex treatment.

The code below uses the package tidyquant to download data from all S&P500 stocks from Yahoo Finance. The code takes some time to download all data. If you want to replicate the exact example I will show you can also download the data from here. My data starts at 2008-01-02 and ends at 2018-02-06. If you use a different time you will obviously find different results.

library(tidyverse) library(reshape2) library(lubridate) library(tidyquant) prices=tq_index("sp500") %>% tq_get(get = "stock.prices", complete_cases = TRUE)

## # A tibble: 6 x 12 ## symbol company weight sector share… date open high low close ## <chr> <chr> <dbl> <chr> <dbl> <date> <dbl> <dbl> <dbl> <dbl> ## 1 MSFT Microso… 0.0306 Inform… 9.22e⁷ 2008-01-02 45.9 46.1 44.9 35.2 ## 2 MSFT Microso… 0.0306 Inform… 9.22e⁷ 2008-01-03 45.2 45.7 44.7 35.4 ## 3 MSFT Microso… 0.0306 Inform… 9.22e⁷ 2008-01-04 45.1 45.2 43.7 34.4 ## 4 MSFT Microso… 0.0306 Inform… 9.22e⁷ 2008-01-07 44.3 44.6 43.9 34.6 ## 5 MSFT Microso… 0.0306 Inform… 9.22e⁷ 2008-01-08 44.5 44.5 42.8 33.5 ## 6 MSFT Microso… 0.0306 Inform… 9.22e⁷ 2008-01-09 42.8 44.3 42.8 34.4 ## # ... with 2 more variables: volume <dbl>, adjusted <dbl>

I will build the portfolio optimization on the adjusted prices. We also need to select close prices and shares_held to calculate the market capitalization. Additionally, I will optimize the portfolio on monthly returns and the data must be aggregated.

## == Treat Data == ## prices = prices %>% select(symbol, date, adjusted, close ,shares_held) %>% group_by(year(date), month(date), symbol) %>% summarize( adjusted = tail(adjusted, 1), close = tail(close, 1), mktcap = tail(close * shares_held, 1)) %>% mutate(date = make_date(`year(date)`, `month(date)`))

Next we need to transform the data into time-series panel with the function `acast`

and calculate log-returns from the adjusted prices. Note that the characteristics in are used to build a portfolio for . Therefore, the characteristics must be lagged one month from the returns. I also removed stocks with NAs. The PPP works fine in unbalanced panels but I will use them balanced to keep it simple. Just keep in mind that if you use it in unbalanced panels and the base portfolio must be adjusted.

## == Reshape variables to matrices == ## # = dropped first observatio in the mktcap because we = # # = loose one observatio to calculate the returns = # mktcap=acast(prices,date~symbol,value.var = "mktcap")[-1,] prices=acast(prices,date~symbol,value.var = "adjusted") ## == Calculate log returns == ## ret=diff(log(prices)) ## == Remove Stocks with NAs == ## drop = which( is.na(colSums(ret)) | is.na(colSums(mktcap)) ) ret = ret[, - drop] mktcap = mktcap[ , - drop] ## == Remove last observation of mktcap because it has == ## ## == to be lagged one month from the returns == ## mktcap = mktcap[- nrow(mktcap), ]

The next step is to generate a variable that is the cumulative return of the last 12 months that will be used as a momentum characteristic. We loose some observations in this procedure and the rest of the data must be adjusted. The characteristics must also be standardized cross-sectionally to have mean zero and variance one. The next chunk also defines the risk aversion coefficient as 5. Feel free to play with the risk aversion. If you increase it to much the portfolio will be the same as the equal weighted and if you decrease the risk aversion you are likely to have portfolios with more variance and leverage.

## == Generate 12 month cumulative returns == ## ## == I used sub because returns are in log == ## m12 = matrix(NA, nrow(ret), ncol(ret)) for(i in 13:nrow(m12)){ m12[i, ] = apply(ret[(i - 12):(i - 1), ],2 , sum ) } ## == Remove last 12 observations because we == ## ## == don't the first 12 m12. == ## ret = ret[-c(1:12), ] m12 = m12[-c(1:12), ] # = only 11 in the mktcap because we already = # # = removed one = # mktcap = mktcap[-c(1:11), ] ## == This is the weigth of each stock in a == # ## == equal weighted portfolio == # nt = wb = 1/ncol(ret) ## = Risk aversion == ## rr = 5 ## == Some plots == ## par(mfrow = c(2, 2)) matplot(ret[ ,1:10], type = "l", ylab = "monthly returns") matplot(m12[ ,1:10], type = "l", ylab = "12 month returns") matplot(mktcap[ ,1:10], type = "l", ylab = "market captalization") ## == Characteristics must be standardized == ## ## == on the crossection == ## m12=t(apply(m12,1,scale)) mktcap=t(apply(mktcap,1,scale))

## Optimizing

Now let’s define the optimization function and use it to find our portfolio. I will use the function in 60 expanding windows to simulate a portfolio that is re-estimated and updated every month. The interpretation is that in the end of every month we optimize with all the data we have until then and build our portfolio for the next month.

## == Portfolio Optimization Function == ## pps = function(x, wb, nt, ret, m12, mktcap, rr){ wi = wb + nt * (x[1]*m12 + x[2]*mktcap) wret = rowSums(wi*ret) ut = ((1 + wret)^(1 - rr))/(1 - rr) u = -mean(ut) return(u) } ## == Loop the function in 60 expanding windows == ## res_save = matrix(NA, 60, 2) weights = matrix(NA, 60, ncol(ret)) for(i in 1:60){ opt = optim(c(0, 0), pps, wb = wb, nt = nt, ret = ret[1:(48 + i), ], m12 = m12[1:(48 + i), ], mktcap = mktcap[1:(48 + i), ], rr = rr, method = "BFGS") res = opt$par w = wb + nt*(res[1]*m12[i + 49, ] + res[2]*mktcap[i + 49, ]) weights[i, ] = w res_save[i, ] = res }

The code below calculates the out-of-sample cumulative returns of our optimized portfolio and the equal weighted (EW) portfolio to use as benchmark. I also calculated the returns of a look-ahead portfolio with the best 100 stocks in the S&P500 during our period of analysis. The code ends with a plot comparing the cumulative returns of the tree portfolios.

## == Calculate out-of-sample returns == ## ret_fit = rowSums(weights*tail(ret, 60)) ret_EW = rowSums(nt*tail(ret, 60)) ## == Accumulate out-of-sample returns == ## acc_fit = cumsum(ret_fit) acc_EW = cumsum(ret_EW) ## == Calculate top 100 portfolio == ## acc_ret = apply(ret[-c(1:49), ], 2, cumsum) top100 = order(acc_ret[nrow(acc_ret), ] ,decreasing = TRUE)[1:100] acc_top100 = rowMeans(acc_ret[ ,top100]) df = data.frame(date = tail(as.Date(rownames(ret)), 60), opt = acc_fit,EW = acc_EW,top100 = acc_top100) dfm = melt(df, id.vars = "date") colnames(dfm) = c("date", "portfolio", "return") ggplot(dfm) + geom_line(aes(x = date, y = return, color = portfolio))

As you can see, our optimal portfolio is far superior than the EW portfolio and it beats the top100 portfolio most of the time. The next plot shows the parameters of the PPP in the 60 expanding windows. In general, our policy is to buy more of winners (high momentum) and buy less of bigger companies.

df = data.frame(date = tail(as.Date(rownames(ret)), 60), res_save) colnames(df)=c("date","m12","mktcap") dfm = melt(df, id.vars = "date") colnames(dfm) = c("date", "characteristic", "parameter") ggplot(dfm) + geom_line(aes(x = date, y = parameter, color = characteristic)) + facet_wrap(~ characteristic, scales = "free_y", ncol = 1)

## Important Considerations

The question now is: does the optimal portfolio I created generate realistic results??? The answer is NO! The results were very impressive, the PPP was able to beat a look-ahead portfolio, which is very unlikely to happen. The cause of this super performance is the lack of restrictions on short positions. Our portfolio assumes that we have infinite credit at no cost to borrow money and invest in stocks. Look at the plot below:

lv = apply(weights, 1, function(x) sum(x[x<0])) plot(abs(lv),type="l")

This plot shows the leverage we had in each month in the out-of-sample period, and it is a lot. In some months we had 4.5 times our wealth of borrowed money. Fortunately, it is very easy to put some restrictions in the PPP. All we have to do is add a penalty in the objective function that penalizes short positions when they are bigger than some target we choose. The function below penalizes leverage values bigger than two. The code chunk ends with the leverage comparison plot, which shows that the penalization kept the leverage very close to our target of two.

## == Model with leverage penalty == ## pps1 = function(x, wb, nt, ret, m12, mktcap, rr){ wi = wb + nt * (x[1]*m12 + x[2]*mktcap) wret = rowSums(wi*ret) ut = ((1 + wret)^(1 - rr))/(1 - rr) u = -mean(ut) lv = abs(apply(wi, 1, function(x) sum(x[x<0]))) pen=abs(sum((lv>2)*lv)) return(u+pen) } ## == Loop the function in 60 expanding windows == ## res_save1 = matrix(NA, 60, 2) weights1 = matrix(NA, 60, ncol(ret)) for(i in 1:60){ opt = optim(c(0, 0), pps1, wb = wb, nt = nt, ret = ret[1:(48 + i), ], m12 = m12[1:(48 + i), ], mktcap = mktcap[1:(48 + i), ], rr = rr, method = "BFGS") res = opt$par w = wb + nt*(res[1]*m12[i + 49, ] + res[2]*mktcap[i + 49, ]) weights1[i, ] = w res_save1[i, ] = res } ## == Calculate out-of-sample returns == ## ret_fit1 = rowSums(weights1*tail(ret, 60)) ## == Accumulate out-of-sample returns == ## acc_fit1 = cumsum(ret_fit1) ## == Leverage analysis == ## lv1 = apply(weights1, 1, function(x) sum(x[x<0])) df=data.frame(date = tail(as.Date(rownames(ret)), 60),lv = abs(lv), lv_pen = abs(lv1)) dfm = melt(df, id.vars = "date") colnames(dfm) = c("date", "model", "leverage") ggplot(dfm) + geom_line(aes(x = date, y = leverage, color = model))

However, the inclusion of short selling restrictions comes at a cost. Look at the plot below! The returns in the penalizes portfolio are smaller, which is natural because we now have a more restricted model.

df = data.frame(date = tail(as.Date(rownames(ret)), 60), opt = acc_fit, opt_pen=acc_fit1, EW = acc_EW,top100 = acc_top100) dfm = melt(df, id.vars = "date") colnames(dfm) = c("date", "portfolio", "return") ggplot(dfm) + geom_line(aes(x = date, y = return, color = portfolio))

## References

Brandt, Michael W., Pedro Santa-Clara, and Rossen Valkanov. “Parametric portfolio policies: Exploiting characteristics in the cross-section of equity returns.” *The Review of Financial Studies* 22.9 (2009): 3411-3447.

Medeiros, Marcelo, Artur M Passos, and Gabriel FR Vasconcelos. “Parametric Portfolio Selection: Evaluating and Comparing to Markowitz Portfolios.” *Revista Brasileira de Finanças* 12.2 (2014).

Pingback: Parametric Portfolio Policies – Mubashir Qasim

Pingback: Distilled News | Data Analytics & R

Pingback: Parametric Portfolio Policies – Cloud Data Architect

Hi Gabriel. This is a very nice post! I really like it and it is very easy to follow your arguments. It would be very interesting to incorporate transaction costs for rebalancing the portfolio or the top 100 stocks as well as to incorporate the sp500 as the benchmark (with almost no transaction costs).

LikeLike

Thanks for your comments! Transaction costs can be easily incorporated inside the optimization by penalizing the returns when the position changes.

LikeLike

Hi Gabriel, thanks a lot for this amazing post. Really well explained!

I would be interested to know if the use of the penalty can be explained for this purpose with a formula, since within your paper it is implemented to limit individual weights. Especially the reason for “(lv>2)*lv” is not clear to me.

My 2nd question is if you could provide an example how you implement bootstrap statistics in your code.

Would appreciate your response. Thanks again for your post!

LikeLike

Hi Sam. The penalization works like this: the (lv>2) part returns a vector that has TRUE if the leverage is bigger than 2 and FALSE otherwise. If you multiply this vector by the leverage you will obtain the values bigger than 2 and 0 for all other values, which means that I will be only penalizing values bigger than 2. This is not the only way to write this penalty.

You should check the boot package for bootstrap. More specifically the ts.boot function. It is easy to use. Or you can do things the hard way and use the function sample to generate your own bootstrap samples.

LikeLiked by 1 person

Hi Gabriel,

Thank you for the post! If i wanted to look at a long only portfolio or a long/short (dollar neutral, where the sum of weights are equal to zero) portfolio, how should i add these constraints in the code?

LikeLiked by 1 person

When optimizing I seem to get the error ‘Vmmin is not finite’ after I tried to add an additional matrix of characteristics. Any ideas how to resolve this issue?

LikeLike

When optimizing (specifically creating the loop) I seem to get the error ‘Vmmin is not finite’ after I tried to add an additional matrix of characteristics. Any ideas how to resolve this issue?

LikeLike