Home  /  Products  /  Stata 19  /  Financial portfolio generation

← See Stata 19's new features

Highlights

  • Portfolios with equal weights

  • Portfolios with minimum variance

  • Portfolios with maximum Sharpe ratio

  • Portfolios with custom weights

  • Portfolios with and without short selling

  • Minimum-variance portfolios with target level of returns

  • See more in financial statistics features

With the new finportfolio command, you can easily create a portfolio of assets and examine this portfolio's properties. Optimize your portfolio to minimize variance or maximize the Sharpe ratio, also known as the return-to-risk ratio. This feature is a part of StataNow™.

A conventional investment strategy combines different assets to create a portfolio. Portfolio construction depends on the type of investment strategy you want to pursue. The new finportfolio command allows you to construct portfolios using a weighted average of assets. The weights may all be equal or selected optimally to minimize variance or maximize the Sharpe ratio. Weights may also be chosen ex ante and provided to finportfolio. For the minimum-variance weighted portfolio, you may even target a specific value for the returns.

Let's see it work

Below, we use data on the asset prices of 25 fictional companies. Our data consist of 780 monthly observations containing the end-of-month values for these asset prices.

. webuse finex
(Fictional stock price data)

We tell Stata that we have time-series data by using tsset on the date variable, datem:

. tsset datem

Time variable: datem, 1955m1 to 2019m12
        Delta: 1 month

Next we construct a set of simple returns based on five of the asset prices in our data by typing

. finreturns acme-tyr, simple(r_) multiply(100) nopreview
(all returns multiplied by 100)

Simple returns generated for variables acme, bat, iron, dune, and tyr.

We have created five simple returns (percentage changes in asset prices). We used the simple() option to specify that the returns have a name that starts with r_ (for example r_acme) instead of the generic name that the command provides. Also, we multiply the returns by 100 for interpretation as percentage changes instead of proportional changes.

We now construct a portfolio with returns r_acme through r_tyr. We first assign each portfolio equal weights.

. finportfolio equal r_acme-r_tyr

Equally weighted portfolio

Number of obs = 779
Sample: 1955m2 thru 2019m12
Weight
ACME .2
BAT .2
IRON .2
DUNE .2
TYR .2
Portfolio return  = 0.8281 Portfolio std. dev.  = 4.5342 Risk-free rate  = 0.0000 Sharpe ratio  = 0.1826

We get a monthly return of 0.83% with a standard deviation of 4.53%. The Sharpe ratio is the quotient of these two numbers. It tells us the return per unit of risk. Ideally, we would like to minimize variance and maximize return. Of course, we can do one or the other or a ratio of both. Below, we explore these possibilities. Let's see what would happen if we wanted to minimize variance.

. finportfolio minvariance r_acme-r_tyr

Global minimum variance portfolio
Short selling allowed

Number of obs = 779
Sample: 1955m2 thru 2019m12
Weight
ACME .8985511
BAT .0918437
IRON -.1387537
DUNE -.130712
TYR .2790709
Portfolio return  = 0.4050 Portfolio std. dev.  = 1.0869 Risk-free rate  = 0.0000 Sharpe ratio  = 0.3726

The return and the standard deviation have decreased, and we now have a higher Sharpe ratio. Another important consideration is that two of the weights in our portfolio are negative. This means that we are short selling these assets. In other words, we sell the assets today and agree to purchase them back later. We are betting that the asset's price is going to be lower in the future. If we do not want to pursue such a strategy, we could use the noshort option and recompute the optimal portfolio forcing all weights to be positive.

We now explore the possibility of maximizing the Sharpe ratio.

. finportfolio maxsharpe r_acme-r_tyr

Maximum Sharpe ratio portfolio
Short selling allowed

Number of obs = 779
Sample: 1955m2 thru 2019m12
Weight
ACME .9157845
BAT .1024231
IRON -.1234373
DUNE -.0289488
TYR .1341785
Portfolio return  = 0.4439 Portfolio std. dev.  = 1.1380 Risk-free rate  = 0.0000 Sharpe ratio  = 0.3901

Returns are higher than when we minimized variance, and by construction, the Sharpe ratio is higher.

We could look for a target return level while simultaneously minimizing variance. Maybe we are willing to allow a bit more risk to attain a particular monthly return, say, 0.5%. We use the target() option and type

. finportfolio minvariance r_acme-r_tyr, target(.5)

Minimum variance portfolio with fixed return
Short selling allowed

Number of obs = 779
Sample: 1955m2 thru 2019m12
Weight
ACME .9405779
BAT .1176436
IRON -.1014019
DUNE .1174566
TYR -.0742762
Portfolio return  = 0.5000 (fixed return) Portfolio std. dev.  = 1.3629 Risk-free rate  = 0.0000 Sharpe ratio  = 0.3669

We increased the variance and reduced the Sharpe ratio, as expected. We did this mainly by increasing the fraction of ACME that we hold in our portfolio and shifting our short-selling strategy.

So far, we have just looked at the portfolio weights that arise from an optimization problem or from using fixed weights. We can also specify the generate() option to create variables that contain each of our portfolios.

. finportfolio equal        r_acme-r_tyr, generate(equal)
. finportfolio minvariance  r_acme-r_tyr, generate(minvar)
. finportfolio maxsharpe    r_acme-r_tyr, generate(maxsharpe)
. finportfolio minvariance  r_acme-r_tyr, target(.5) generate(minvar_noshort)

Now we can use the portfolio variables for further analyses. Perhaps we want to see how they behave relative to a benchmark market and adjust for a particular rate of return; we can use finsummarize and finregress. Maybe we want to see how risky the portfolios are in terms of extreme events; we can compute their values at risk with finvalrisk. Or we may want to model the portfolios using Stata's time-series tools. See Financial statistics for an overview of these and other commands for analyzing financial data.

Tell me more

Read more about financial portfolio creation in [FIN] finportfolio in the Stata Financial Statistics Reference Manual.

Learn more about Stata's financial statistics features.

View all the new features in Stata 19 and, in particular, new in financial statistics.

Ready to get started?

Experience powerful statistical tools, reproducible workflows, and a seamless user experience—all in one trusted platform.