Home  /  Products  /  Stata 19  /  Financial statistics

← See Stata 19's new features

Highlights

Analysis of financial data is now easier using the new suite of finance commands. Generate returns, build portfolios, analyze portfolio characteristics, build financial regression models, and assess value at risk (VaR). Go deeper with the powerful statistical tools already in Stata. These features are part of StataNow™.

Stata's new financial statistics suite of commands provides tools to manage and analyze financial data. The new commands simplify workflows and expand on the data management, statistics, reporting, and graphics tools that you already know.

For example, the new finreturns command takes price series and creates commonly used types of returns. If we type

. finreturns asset1-asset200, logs annual

we get log returns and annualized returns for 200 assets.

We can also construct portfolios optimally. The new finportfolio command allows us to construct a portfolio selecting weights for different returns. We can minimize variance, maximize the Sharpe ratio, use equal weights, or provide our own weights. If we type

. finportfolio minvariance r1-r10, generate(myportfolio)

we get a minimum-variance weighted portfolio constructed from returns r1 through r10.

To get relevant summary statistics about our portfolios or returns, we can use the new finsummarize command. If we type

. finsummarize portfolio1-portfolio10, ...

we get the mean, standard deviation, Sharpe ratio, Treynor index, Jensen's alpha, and market beta for 10 portfolios.

If we want to compute VaR, we type

. finvalrisk r1-r10

Commonly used financial regressions like the capital asset pricing model (CAPM),

. finregress capm r1-r10 = rmarket

or Fama–Macbeth regression,

. finregress fmb r1-r10 = rmarket

are also available.

In addition, we added custom business calendars for the major stock exchanges around the world. They can be accessed using the new bcal webcopy command. To get the business calendar for the New York Stock Exchange (NYSE) between January 2, 2024, and December 31, 2028, we type

. bcal webcopy nyse

which downloads a copy of the business calendar into our working directory. We can then use the business calendar to create a date variable that includes NYSE trading days only. This makes it easy to tsset data collected on the corresponding dates and use any time-series or financial statistics command.

As you can see, it is now easier to do financial research using Stata.

Commands for financial statistics

finreturnsGenerate financial returns
finportfolioFinancial portfolio selection
finregress capmCapital asset pricing model
finregress fmbFama–MacBeth regression
finsummarizeFinancial summary statistics
finvalriskValue at risk

Let's see it work

Below, we use data on the asset prices of 25 fictional companies, along with data on a stock market index sp500. Our data consist of 780 monthly observations containing the end-of-month values for these asset prices.

. webuse finex
(Fictional stock price data)

The first thing we do is to tell Stata we have time-series data using tsset on our date variable, datem:

. tsset datem

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

We have monthly data starting in 1955 with lags defined in one-month increments.

We would like to construct a set of portfolios based on the asset prices in our data and compare them with the market index in our data, the sp500. To create a market return, we type

. finreturns sp500, simple(rmkt) multiply(100)
(all returns multiplied by 100)

Simple returns generated for variable sp500:

datem sp500 rmkt
1. 1955m1 35.6 .
2. 1955m2 36.79 3.3426966
3. 1955m3 36.5 -.78825768
4. 1955m4 37.76 3.4520548
5. 1955m5 37.6 -.42372881
6. 1955m6 39.78 5.7978723
7. 1955m7 42.69 7.3152338
8. 1955m8 42.43 -.60904193
9. 1955m9 44.34 4.5015319
10. 1955m10 42.11 -5.0293189

finreturns created a simple return (a percentage change in the asset price). We used the simple() option to give the return the name rmkt rather than the generic default name. Also, we multiplied the return by 100 to obtain percentage changes instead of proportional changes. This simple market return is going to come in handy later on.

We would now like to form a portfolio based on the returns of the other assets in our data. First, we generate simple returns for all of them by typing

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

Simple returns generated for variables acme, bat, iron, dune, tyr, glo,
spa, wgt, bar, yum, aaa, afh, ard, cph, das, dil, ege, epg, goa, jml, khc,
krg, kth, nhb, and tks.

We use the stub r_ to obtain 25 simple returns named r_acme through r_tks corresponding to the price variables acme through tks.

The first portfolio we construct puts equal weight on all the asset returns. We type

. finportfolio equal r_acme-r_tks, generate(eweighted, label(eweighted))

The generate() option creates a variable containing the returns of our equally weighted portfolio. Otherwise, we would see only the following output:

Equally weighted portfolio

Number of obs = 779
Sample: 1955m2 thru 2019m12
Weight
ACME .04
BAT .04
IRON .04
DUNE .04
TYR .04
GLO .04
SPA .04
WGT .04
BAR .04
YUM .04
AAA .04
AFH .04
ARD .04
CPH .04
DAS .04
DIL .04
EGE .04
EPG .04
GOA .04
JML .04
KHC .04
KRG .04
KTH .04
NHB .04
TKS .04
Portfolio return = 0.6697 Portfolio std. dev. = 2.7483 Risk-free rate = 0.0000 Sharpe ratio = 0.2437

We see that the average portfolio return is 0.67% per month with a standard deviation of 2.75%. Because we did not specify a risk-free rate, these returns are not compared with any benchmark interest rate. Finally, we see the Sharpe ratio. It is a ratio of the mean return to the standard deviation, which gives us a measure of return per unit of risk.

Next, we create two other portfolios, a global minimum-variance portfolio and a portfolio that maximizes the Sharpe ratio.

. quietly finportfolio minvariance r_acme-r_tks, generate(minvar, label(minvar))

. quietly finportfolio maxsharpe   r_acme-r_tks, generate(maxsharpe, label(maxsharpe))

See Financial portfolio generation to learn more about creating and optimizing portfolios.

We compare the three portfolios we created using finsummarize. We would like to compare their returns, relative to the benchmark market returns we created above.

. finsummarize eweighted minvar maxsharpe, benchmark(rmkt)

Financial summary statistics

Number of obs = 779
Sample: 1955m2 thru 2019m12

Sharpe Treynor Jensen's
Mean Std. dev. ratio index alpha beta
eweighted 0.6697 2.7483 0.2437 0.8563 0.1701 0.7820
minvar 0.4263 0.4732 0.9008 26.7156 0.4161 0.0160
maxsharpe 0.4635 0.4934 0.9393 17.4849 0.4465 0.0265
Note: Variable rmkt used as benchmark asset.

We see that the equally weighted portfolio has the highest average return, but it has more than five times the standard deviation than the other two portfolios.

The Sharpe ratio reflects this by stating that the return per unit of risk is better for our minimum variance and maximum Sharpe ratio portfolios.

The last column is beta, which indicates how much the portfolios vary with the benchmark asset specified in benchmark(). For instance, if beta is negative, it implies that the portfolio does well when the benchmark does poorly (a hedge); if beta is above 1, the portfolio reacts more than proportionally to changes in the benchmark. We see that the equally weighted portfolio moves with the market but not one to one, whereas the other two portfolios with betas around zero seem to be unrelated to the benchmark, meaning that these portfolios are insulated from swings in the benchmark.

The Treynor index is the ratio between mean portfolio return and beta. A higher value implies a high return per unit of risk, where risk is measured in terms of how strongly the portfolio varies with the benchmark.

Jensen's alpha is the constant in a regression of the asset or portfolio's returns on the benchmark's returns. Positive values indicate returns on average that are not captured by beta, that is, returns that can be expected even when the market return is zero. Higher values indicate greater returns.

In the table above, underlying Jensen's alpha and the market beta, there is a CAPM. CAPMs provide an assessment of the performance of an asset with respect to a benchmark market return and a risk-free interest rate (in the example above, it is zero, but often it is the interest rate offered by a central bank, like the federal funds rate in the US). These regressions can be conducted for multiple assets. For our portfolios, we would type

. finregress capm eweighted minvar maxsharpe = rmkt

Capital asset pricing model

Sample: 1955m2 thru 2019m12                                Number of obs = 779

Robust
Coefficient std. err. z P>|z| [95% conf. interval]
eweighted
rmkt .7819972 .0049403 158.29 0.000 .7723143 .7916801
_cons .1701223 .0176187 9.66 0.000 .1355903 .2046543
minvar
rmkt .0159574 .0055898 2.85 0.004 .0050016 .0269131
_cons .4161162 .0175597 23.70 0.000 .3816998 .4505326
maxsharpe
rmkt .0265074 .0055318 4.79 0.000 .0156652 .0373495
_cons .4465457 .0181703 24.58 0.000 .4109326 .4821589

The coefficients on rmkt are the same beta values reported by finsummarize.

CAPM regressions give us information about every asset or portfolio individually. We can also assess the performance of all of them simultaneously. In particular, we can assess how their return is related to risk. To do this, we can use a Fama–MacBeth regression. We would type

. finregress fmb eweighted minvar maxsharpe = rmkt

Fama–MacBeth regression

Sample: 1955m2 thru 2019m12                            Number of obs     = 779
Method: OLS                                            Number of depvars =   3

depvar_means Coefficient Std. err. z P>|z| [95% conf. interval]
beta
rmkt .2959044 .1263486 2.34 0.019 .0482657 .5435431
_cons .4384932 .0172122 25.48 0.000 .4047578 .4722285
Dependent variables: eweighted minvar maxsharpe

These results indicate that for our three sample portfolios, as beta risk rises by one unit, average return rises by 0.3 percentage points per month. If we wanted to see how average returns are related to beta risk for the whole sample of assets, we would type

. finregress fmb r_acme-r_tks = rmkt

See Financial regression to learn more about CAPMs and Fama–MacBeth regression.

We might also be interested in tail behavior. How bad is a bad day with these portfolios? Let's define a bad day as the negative return (the loss) we would expect to obtain only 5% of the time or less. We can use finvalrisk to assess this threshold using both historical and normal-based methods:

. finvalrisk eweighted minvar maxsharpe

Value-at-risk percentiles

Number of obs = 779
Sample: 1955m2 thru 2019m12

Empirical Normal
5%
eweighted -3.869312 -3.850921
minvar -.3166386 -.3520867
maxsharpe -.3372051 -.348142

The equally weighted portfolio generates a return of -3.87%, or worse, 5% of the time. Meanwhile, the minimum-variance portfoilo generates a return of -0.32%, or worse, 5% of the time. Thus, the minimum-variance portfolio has much more mild tail behavior than the equally weighted portfolio; that is to say, it is far less risky. Meanwhile, the maximum Sharpe ratio portfolio is comparable with the minimum variance portfolio, albeit slightly riskier, as would be expected.

See Value at risk to learn more about computing VaR.

Of course, there are many other ways to analyze financial data, leveraging tools that are already available in Stata. We could model the variance of our portfolios using autoregressive conditional heteroskedasticity (ARCH) and generalized ARCH (GARCH) models, predict conditional variances, and then visualize them:

. arch maxsharp, arch(1) garch(1)
. predict varshp, variance

. arch minvar, arch(1) garch(1)
. predict varmv, variance

. tsline varmv varshp, legend(off)

We could explore stationarity of the asset returns by using a modified Dickey–Fuller test by typing

. dfgls r_acme
. dfgls r_tks

Or we can use any other univariate time-series command, such as the arima command to fit an autoregressive integrated moving-average (ARIMA) model.

. arima r_acme, ar(1 2 3) ma(1 2 3)

We might also model asset return conditional variance jointly:

. mgarch ccc (r_acme r_tks = L.r_acme L.r_tks, nocons), arch(1) garch(1)

Or we can use any multivariate time-series command to study the joint relationship of the asset returns over time.  For instance, we can fit a vector autoregressive model and evaluate the impulse–response functions by typing

. var r_acme r_tks
. irf create ...
. irf graph oirf ...

We can produce forecasts using the forecast command:

. forecast create ...
. forecast estimates ...
. forecast solve ...

In sum, the arsenal of tools for analyzing financial data at your disposal is large and has expanded.

Tell me more

Read more about financial statistics in the Stata Financial Statistics Reference Manual, and see Financial portfolio generation, Value at risk, and Financial regression.

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.