Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages

Category Archives: Uncategorized

  • 0

asrol Cumulative Returns Over 12-months, skipping the Current month


Anomaly Hunters has asked: How Can I find cumulative returns from month T-1 to T-12, i.e. not including the current month in the calculation. I want to do it on a rolling window for each firm.

Let us use a small dataset. We shall create this dataset in Stata with the following code

 input byte firm_id str6 date float ret
 1 "200310"         .
 1 "200311"  -.049505
 1 "200312"  .0121528
 1 "200401"  .0360206
 1 "200402" -.0480132
 1 "200403"  .0434783
 1 "200404"  .0933333
 1 "200405"  .0121951
 1 "200406"  .0060241
 1 "200407"  .0883234
 1 "200408" -.0261348
 1 "200409" -.0819209
 1 "200410"      -.02
 1 "200411" -.0062794
 1 "200412"  .0458136
 1 "200501"  .0891239
 1 "200502"  .0443828
 1 "200503"

* Convert the date to Stata format

tostring date, replace
gen month = mofd(date(date, "YM"))
format month %tm

* Find cumulative returns in a 12-month rolling window

bys firm_id : asrol ret, stat(product) add(1) window(month 12)

* Lag the returns by one month to skip the current month returns from the calculation

tsset firm_id month
gen ret_skip1 = L.product12_ret
list month ret product12_ret ret_skip1

*And appreciate the results


     |   month        ret   product1~t   ret_skip1 |
  1. | 2003m10          .            .           . |
  2. | 2003m11   -.049505     -.049505           . |
  3. | 2003m12    .012153   -.03795382    -.049505 |
  4. |  2004m1    .036021   -.00330034   -.0379538 |
  5. |  2004m2   -.048013   -.05115508   -.0033003 |
  6. |  2004m3    .043478   -.00990092   -.0511551 |
  7. |  2004m4    .093333    .08250829   -.0099009 |
  8. |  2004m5    .012195    .09570959    .0825083 |
  9. |  2004m6    .006024    .10231025    .0957096 |
 10. |  2004m7    .088323    .19967004    .1023103 |
 11. |  2004m8   -.026135     .1683169      .19967 |
 12. |  2004m9   -.081921    .07260733    .1683169 |
 13. | 2004m10       -.02    .05115519    .0726073 |
 14. | 2004m11   -.006279     .0989585    .0511552 |
 15. | 2004m12    .045814    .13550617    .0989585 |
 16. |  2005m1    .089124     .1937088    .1355062 |
 17. |  2005m2    .044383    .30956536    .1937088 |
 18. |  2005m3   -.005312     .2483334    .3095654 |

  • 0

Step-by-Step: Portfolio Risk in Stata and Excel


Portfolio Risk in Excel

To build our concept of the portfolio risk, we shall calculate it first manually in EXCEL, then we shall replicate the results using matrix notations in Stata. 
Consider the following set of returns for two assets, i.e asset A and B.    

  A        B   
.249917 .819483
.739069 .821416
.895491 .276843
.902722 .001586
.078344 .714815
.429804 .027261
.239556 .736011

As we know, the portfolio standard deviation under the modern portfolio theory is calculated as = 

Thus, we need the standard deviation of the two assets, the proportion of investment in each asset (weights), and the covariance term between the two assets. In Excel, we can find the standard deviation by


And the formula for covariance is

=COVARIANCE.S(range1, range2)

Thus, standard deviation of asset A = 0.3387         

the standard deviation of asset B = 0.3713         

Covariance between the two = -0.0683

And if we invest equally in both assets, then

the weight of A = 0.5

and weight of B = 0.5

Portfolio SD =( (0.5^2*0.3387^2)+(0.5^2*0.3713^2)+(2*0.5*0.5*-0.0683))^0.5

Excel sheet showing the above example can be downloaded from here.

Portfolio Risk in Stata

Finding portfolio standard deviation under the Modern Portfolio theory using matrix algebra requires three matrices
1. Weights of the assets in the portfolio, in row format = W
2. Variance-Covariance matrix of assets returns = S
3. Weights of the assets in the portfolio, in column format = W’

Portfolio SD = W * S * W'

NOTE: In order to find the variance-covariance matrix, you can install varrets program from ssc with:

 ssc install mvport 

Step 1 : Copy the example data to stata

You can do it either by copying the data from the excel file and pasting it to the stata editor. Alternatively, you can copy the following and past it in the stata command line, or download the data file from here.

 input a b
.249917 .819483
.739069 .821416
.895491 .276843
.902722 .001586
.078344 .714815
.429804 .027261
.239556 .736011

Step 2: Make variance-covariance matrix

varrets a bmat S = r(cov) 

Step 3: Make a weight matrix

Assuming that we assign equal weights, we define matrix W

mat W = (0.5, 0.5) 

Step 4 : Multiply the weight and variance-covariance matrix

mat VAR = W * S * W' 

Step 5: Show variance of the portfolio

mat list VAR 

Complete .do file of the example can be downloaded from here.

  • 7

tabstat with asdoc in Stata


asdoc makes some elegant tables when used with tabstat command. There are several custom-made routines in asdoc that creates clean tables from tabstat command. asdoc fully supports the command structure and options of tabstat. And, yes asdoc allows one additional statistics, that is, t-statistics alongside the allowed statistics in tabstat. For reporting purposes, asdoc categorizes tabstat commands in two groups:

(1) stats without a grouping variable

(2) stats over a grouping variable.


Tabstat Without-by

If statistics are less than variables, the table is transposed, i.e. statistics are shown in columns, while variables are shown in rows


Example 49 : One variable, many stats, including t-statistics

sysuse auto, clear  
asdoc tabstat price , stat(min max mean sd median p1 p99 tstat) replace 


Example 50 : Many variables, one statistic

asdoc tabstat price mpg rep78 headroom trunk weight length foreign , stat( mean) replace


Example 51 : Many variables, many statistics

asdoc tabstat price mpg rep78 headroom trunk weight length foreign , /// 
stat( max mean sd median p1 p99 tstat) replace


Tabstat with-by


Example 52 :

bysort foreign: asdoc tabstat price mpg rep78 headroom trunk weight length, stat(mean) replace


asdoc tabstat price mpg rep78 headroom trunk weight length, ///
stat(mean) by(foreign) replace


Example 53 : By with many variables and many statistics

bysort foreign: asdoc tabstat price mpg rep78 headroom trunk weight length, ///
stat(mean sd p1 p99 tstat) replace


  • 2

Fama and MacBeth regression with Shanken correction using asreg


If you are not yet familiar with asreg, here is a quick start. Implementing the Fama and MacBeth regression using asreg is super-fast and easy. Here are a few posts related to this implementation.

FMB regressions with asreg

FMB regression – what, how and where

FMB regressions with 25-portfolios – An example

The Shanken Correction

In applying standard OLS formulas to a cross-sectional regression, we assume that the right-hand variables β are fixed. The β in the cross-sectional regressions are not fixed, of course, but are estimated in the time-series regression.  Therefore, there might be a sampling error in the estimates of β.  Shanken (1992) suggested a correction to the standard errors of the estimates.

How to do it?

The focus in this post is on the Fama and MacBeth implementation with Shanken () correction. Like with many other commands using asreg, the Shanken correction is fairly easy. The following steps are needed:

1. Find a covariance matrix among the right hand-side variables and write it to a matrix. Suppose variables inour dataset inlcude rm_rf smb and hml, then to find the covariance and write it to a matrix, we would do the following:

cor rm_rf smb hml, cov
matrix S = r(C)  

2. Find the first stage lambda of the RHS variables.

bys portfolios: asreg excess_returns rm_rf smb hml
* Remove uncessary variables
 drop _Nobs _R2 _adjR2 _b_cons 

3. Fama and MacBeth regression: In this last stage, we would use the fmb and shanken option. The shanken option requires the covariance matrix that we created in step 1 above

asreg excess_returns _b_mmrf _b_smb _b_hml , fmb shanken(S)



The asreg program is a freeware and can be downloaded from SSC. The Shanken correction is available for $100/model, plus a $50 for raw data processing (in case the data is not in Stata format and variables are not already constructed). For further details, please contact us at:


See our full list of completed projects


  1. Fama, E. F., & MacBeth, J. D. (1973). Risk, return, and equilibrium: Empirical tests. Journal of political economy81(3), 607-636.
  2. Shanken, J. (1992). On the estimation of beta-pricing models. The review of financial studies5(1), 1-33.

  • 0

Fama – MacBeth (1973) procedure: What, how and where | asreg in Stata


Fama and MacBeth (1973) procedure can be used in testing asset pricing models and in other areas. In this post, my primary focus is on its use in testing asset pricing models.

FMB in asset pricing models

It is actually a three-step process. We would divide the time period into three parts.

1. The first step is to find the assets/portfolios betas in the first period. Some researchers would use these betas to classify assets into portfolios.

2. The second step is to find betas of these portfolios in the second period.

3. The third step is to find the portfolio returns in the third period and test whether the betas from the second period can explain these returns? This step involves:
(i) cross-sectional regressions of the portfolio returns on the portfolio betas in each period.
(ii) averaging coefficients from the cross-sectional regressions across time. The standard errors are adjusted for cross-sectional dependence.

What does asreg do in the above process

asreg with fmb option performs step 3(i) and 3(ii). 

asreg can also help in step (1) where individual betas need to be calculated for each stock. The command might look like

bys company: asreg returns market_returns if period == 1

This means that for typical asset pricing tests, the researcher has to do step (1) and (2) and arrange the data in a panel format, listing portfolio returns and betas as variables in columns. And then use asreg with fmb option, e.g.

keep if period == 3
xtset company month
asreg returns betas, fmb

Where else FMB regression can be used?

Fama and MacBeth (1973) procedure (i.e step 3(i) and (ii)) is also used in areas other than testing the asset pricing models. You can see one example in my paper, Table 3, column 8, page 264

Shah, Attaullah & Shah, Hamid Ali & Smith, Jason M. & Labianca, Giuseppe (Joe), 2017. “Judicial efficiency and capital structure: An international study,” Journal of Corporate Finance, Elsevier, vol. 44(C), pages 255-274.

  • 13

Export correlation table to Word with stars and significance level using asdoc


The updated version of asdoc can now create a table of correlation with significance levels starred at different levels. The new version can be installed by typing the following line in Stata.

Installation of the new version

net install asdoc, from( replace

An Example

sysuse auto, clear
asdoc pwcorr price mpg rep78 headroom trunk weight length turn , star(all) replace nonum


Just like with any other Stata command, we would write asdoc as a prefix to the Stata command. In this case, the Stata command is pwcorr which is followed by the variable names. After the comma, we added option nonum, star(all) and replace. These are explained bellow:

star(all) = This option is used to report stars to signfy significance at different levels. These are: ***

  1. *** to show significance at 1% or bellow
  2. ** to show significance at 5% or bellow
  3. * to show significance at 10% or bellow

nonum = Without using this option, asdoc will report numeric numbers as column headers

replace = This option replaces any existing file

You would be interested in this blog entry where I show several useful options of asdoc that can be used with correlation tables.

  • 10

Fama and MacBeth regression over 25 Portfolios using asreg in Stata


Antonio has asked the following question

Dear Sir,
I was wondering how to run a Fama and MacBeth regression over 25 Portfolios. In accordance with your code, the first variable needs to be the dependent variable while the following variables are considered as independent variables. Basically, I would like to calculate the risk premium of a factor over the 25 value and size-sorted portfolios. Therefore in my case, I would have more dependent variables and just one dependent variable.
Thanks for your availability


To answer your question, I have preareed a dummy dataset, which you can download by typing the following in Stata command window.

use, clear

So before running the Fama and MacBeth regressions, this is how the data needs to be structured.

The data is in a long format where the portfolios are tracked by a variable, called the panelvar. The portfolio returns are written in a separate variable, in our case, it is named as returns. The panelvar has values from 1, up to 25. The first 10 observations of the portfolios 1 and 2 look like:

. list in 1/10, noob
   |    mofd   P   returns       size        MTB |
   |  1993m6   1      .038    64.0125   5.224508 |
   |  1993m7   1     .0539   71.86839   4.505145 |
   |  1993m8   1    -.0639   27.82528   1.888283 |
   |  1993m9   1    -.0328   20.08383   7.730755 |
   | 1993m10   1     .0249   59.34985   8.961844 |
   | 1993m11   1     .0657   47.42625   3.766557 |
   | 1993m12   1     .0408   81.47429   5.148165 |
   |  1994m1   1     .0185   42.39914   5.375627 |
   |  1994m2   1     .0323   62.36839   4.882884 |
   |  1994m3   1   -.00598   64.79323   1.281697 |
 . list in 101/110, noob
   |    mofd   P   returns       size        MTB |
   |  1993m6   2     .0114   41.16883   4.549813 |
   |  1993m7   2    -.0158   10.09915   2.136258 |
   |  1993m8   2    .00616   73.43023   2.924793 |
   |  1993m9   2    -.0141   58.28651   7.608449 |
   | 1993m10   2     .0129    63.4972   1.137969 |
   | 1993m11   2    -.0223    16.1786   1.368057 |
   | 1993m12   2     .0322   64.10929   6.226629 |
   |  1994m1   2    -.0144   54.48264   7.883276 |
   |  1994m2   2     .0388   74.99379   1.362888 |
   |  1994m3   2     .0345   68.66164   7.102628 |

How to run the Fama and MacBeth regression

My asreg command is available on SSC, to download it, type:

ssc install asreg, replace

asreg can estimate three types of regressions: (1) cross-sectional regressions (2) rolling window regressions and (3) Fama and MacBeth regressions. You can read more details here.

Since our main focus here is on the Fama and MacBeth procedure, the discussion this point onwards will use option fmb of the asreg program. The syntax is:

asreg depvar indepvars, fmb

The data must be first declared as panel data with the xtset command. In our dataset, we have P as the panel variable and mofd as the time variable, therefore, to declare the data as panel data, the xtset command would be:

xtset P mofd

In our dataset, we have the variable returns as the dependent variable and size and MTB as the two independent variables. The command for the Fama and MacBeth regression would be:

. asreg returns  size MTB , fmb


retunrs = The dependent variable

size and MTB = independent variables

  • 1

asdoc Unicode issue | Stata to MS Word


For those who are not yet familiar with asdoc, asdoc can be downloaded from SSC and can be used with almost all Stata commands. Here is a short blog post that shows how asdoc can be used with any Stata command. You can also watch several YouTube videos that show the use of asdoc

asdoc installation

 ssc install asdoc

The problem

Since asdoc uses RTF file format, any Unicode character passed from Stata to the RTF file will be incorrectly shown in the output file. For example, a variable has the following value label which is correctly shown on Stata screen, but when written with asdoc, it is distorted.

Stata view: Don’t know
asdoc view: 

The solution

If you have Stata 14 or higher, then the good news is that you can use unicode command to change the file encoding. Specifically, the subcommand convertfile of unicode command can be extremely useful here. In the following example, I shall download an example dataset, called unicode.dta from my site, and then tabulate the variable M25 with asdoc, write the results to Myfile.doc and convert the coding and write the results to a new file, Myfile_new.doc

use, clear
asdoc tab M25, replace

The file generated by asdoc looks like this, the last line of the file highlights the given issue.

Now lets try the unicode solution.

unicode convertfile  Myfile.doc  Myfile_new.doc, dstencoding(Windows-1252) replace

  • 0

asdoc – custom table title | MS Word | Stata


For those who are not yet familiar with asdoc, asdoc can be downloaded from SSC and can be used with almost all Stata commands. Here is a short blog post that shows how asdoc can be used with any Stata command. You can also watch several YouTube videos that show the use of asdoc

asdoc installation

 ssc install asdoc

Customizing the table title

The focus of this blog entry is to highlight some of the tricks that can be used in asdoc to customize the table titles. asdoc produces some generic table titles with many Stata commands. However, the default table titles can be replaced by using the option title(). In the following text, I discuss this and several other features of asdoc to modify the table titles:

1. Custom titles

If custom titles are needed, just use option title( title text). See the following example.

sysuse auto
asdoc tab rep78 , title(My cutom table title) replace

2. Title not required

We can use an empty title if the table title is not required. In the following example, I am using the backslash (\) inside the title option. This will produce empty title in the document.

asdoc tab rep78 , title(\) replace

3. Title is needed in italics

We can use many RTF directives inside the title option. So to get the title in italics font, the code would be:

asdoc tab rep78 , title(\i My custom table title) replace

4. Title with bigger font size

The default in asdoc is to report table titles in boldface and font size of 10pt. If bigger or smaller font size is needed, again we can pass the RTF directive \fs# to the document. So to produce a title with 14 pt, we shall type:

asdoc tab rep78 , title(\fs28 My custom table title) replace

  • 0

The Error option matrow() not allowed” when using asdoc with tabulate command


Ronald asked the following question: I am getting the error when using asdoc with tabulate command.
option matrow() not allowed

I asked Ronald to send his dataset for a closer look. Upon inspection, it turned out that Ronald was trying to tabulate values of a string variable, and asdoc had problem with that. As a temporary solution, I suggested the following two steps.

Step 1: Convert the string variable to numeric variable with value labels. The good news is that we can do that in one line of code using the encode command. So assume that the string variable name is country, we shall encode it:

encode country, gen(country_new)

where country_new is a new is a new variable with value labels.

Step 2: Now we can use it with asdoc, so to tabulate it and send the output to MS Word, we type:

asdoc tab country_new, replace