sjart_dm0070
sjart_dm0070
AgEcon Search
http://ageconsearch.umn.edu
aesearch@umn.edu
Papers downloaded from AgEcon Search may be used for non-commercial purposes and personal study only.
No other use, including posting to another Internet site, is permitted without permission from the copyright
owner (not AgEcon Search), or as allowed under the provisions of Fair Use, U.S. Copyright Act, Title 17 U.S.C.
No endorsement of AgEcon Search or its fundraising activities by the author(s) of the following work or their
employer(s) is intended or implied.
The Stata Journal
Editors
H. Joseph Newton Nicholas J. Cox
Department of Statistics Department of Geography
Texas A&M University Durham University
College Station, Texas Durham, UK
editors@stata-journal.com editors@stata-journal.com
Associate Editors
Christopher F. Baum, Boston College Frauke Kreuter, Univ. of Maryland–College Park
Nathaniel Beck, New York University Peter A. Lachenbruch, Oregon State University
Rino Bellocco, Karolinska Institutet, Sweden, and Jens Lauritsen, Odense University Hospital
University of Milano-Bicocca, Italy Stanley Lemeshow, Ohio State University
Maarten L. Buis, WZB, Germany J. Scott Long, Indiana University
A. Colin Cameron, University of California–Davis Roger Newson, Imperial College, London
Mario A. Cleves, University of Arkansas for Austin Nichols, Urban Institute, Washington DC
Medical Sciences Marcello Pagano, Harvard School of Public Health
William D. Dupont, Vanderbilt University Sophia Rabe-Hesketh, Univ. of California–Berkeley
Philip Ender, University of California–Los Angeles J. Patrick Royston, MRC Clinical Trials Unit,
David Epstein, Columbia University London
Allan Gregory, Queen’s University Philip Ryan, University of Adelaide
James Hardin, University of South Carolina Mark E. Schaffer, Heriot-Watt Univ., Edinburgh
Ben Jann, University of Bern, Switzerland Jeroen Weesie, Utrecht University
Stephen Jenkins, London School of Economics and Ian White, MRC Biostatistics Unit, Cambridge
Political Science Nicholas J. G. Winter, University of Virginia
Ulrich Kohler, University of Potsdam, Germany Jeffrey Wooldridge, Michigan State University
The Stata Journal publishes reviewed papers together with shorter notes or comments, regular columns, book
reviews, and other material of interest to Stata users. Examples of the types of papers include 1) expository
papers that link the use of Stata commands or programs to associated principles, such as those that will serve
as tutorials for users first encountering a new field of statistics or a major new technique; 2) papers that go
“beyond the Stata manual” in explaining key features or uses of Stata that are of interest to intermediate
or advanced users of Stata; 3) papers that discuss new commands or Stata programs of interest either to
a wide spectrum of users (e.g., in data management or graphics) or to some large segment of Stata users
(e.g., in survey statistics, survival analysis, panel analysis, or limited dependent variable modeling); 4) papers
analyzing the statistical properties of new or existing estimators and tests in Stata; 5) papers that could
be of interest or usefulness to researchers, especially in fields that are of practical importance but are not
often included in texts or other journals, such as the use of Stata in managing datasets, especially large
datasets, with advice from hard-won experience; and 6) papers of interest to those who teach, including Stata
with topics such as extended examples of techniques and interpretation of results, simulations of statistical
concepts, and overviews of subject areas.
The Stata Journal is indexed and abstracted by CompuMath Citation Index, Current Contents/Social and Behav-
ioral Sciences, RePEc: Research Papers in Economics, Science Citation Index Expanded (also known as SciSearch,
Scopus, and Social Sciences Citation Index.
For more information on the Stata Journal, including information for authors, see the webpage
http://www.stata-journal.com
Subscriptions are available from StataCorp, 4905 Lakeway Drive, College Station, Texas 77845, telephone
979-696-4600 or 800-STATA-PC, fax 979-696-4601, or online at
http://www.stata.com/bookstore/sj.html
Subscription rates listed below include both a printed and an electronic copy unless otherwise mentioned.
1-year university library subscription $125 1-year university library subscription $165
2-year university library subscription $215 2-year university library subscription $295
3-year university library subscription $315 3-year university library subscription $435
http://www.stata.com/bookstore/sjj.html
Individual articles three or more years old may be accessed online without charge. More recent articles may
be ordered online.
http://www.stata-journal.com/archives.html
The Stata Journal is published quarterly by the Stata Press, College Station, Texas, USA.
Address changes should be sent to the Stata Journal, StataCorp, 4905 Lakeway Drive, College Station, TX
77845, USA, or emailed to sj@stata.com.
Copyright Statement: The Stata Journal and the contents of the supporting files (programs, datasets, and
help files) are copyright c by StataCorp LP. The contents of the supporting files (programs, datasets, and
help files) may be copied or reproduced by any means whatsoever, in whole or in part, as long as any copy
or reproduction includes attribution to both (1) the author and (2) the Stata Journal.
The articles appearing in the Stata Journal may be copied or reproduced as printed copies, in whole or in part,
as long as any copy or reproduction includes attribution to both (1) the author and (2) the Stata Journal.
Written permission must be obtained from StataCorp if you wish to make electronic copies of the insertions.
This precludes placing electronic copies of the Stata Journal, in whole or in part, on publicly accessible websites,
fileservers, or other locations where the copy may be accessed by anyone other than the subscriber.
Users of any of the software, ideas, data, or other materials published in the Stata Journal or the supporting
files understand that such use is made without warranty of any kind, by either the Stata Journal, the author,
or StataCorp. In particular, there is no warranty of fitness of purpose or merchantability, nor for special,
incidental, or consequential damages such as loss of profits. The purpose of the Stata Journal is to promote
free communication among Stata users.
The Stata Journal (ISSN 1536-867X) is a publication of Stata Press. Stata, , Stata Press, Mata, ,
and NetCourse are registered trademarks of StataCorp LP.
The Stata Journal (2013)
13, Number 3, pp. 603–617
1 Introduction
Diversification, portfolio selection, hedging, and the capital asset pricing model (CAPM)
(Sharpe 1964; Lintner 1965) are integral parts of an undergraduate and a graduate
finance curriculum. Most textbooks provide detailed explanations about how to diver-
sify, how to evaluate different financial securities, and how to fit a CAPM. Considering
the importance of “learning by doing” or a “hands-on” approach, financial calculations
and CAPM estimations are performed in classrooms and given as assignments. There
are abundant resources about these financial calculations and CAPM estimations mostly
using Microsoft Excel. However, there seem to be limited resources for students to
do financial calculations and CAPM estimations using an econometric software such as
Stata. Considering the speed and ease of repeatability of Stata, this is a drawback in the
finance curriculum: teaching should focus more on theory and interpretation of results
than on long and tiresome steps of calculations and estimations.
Microsoft Excel is one of the primary software in the industry and an asset for finance
students, who should be able to use it to fit a CAPM and other financial calculations.
However, automated tasks such as fitting a CAPM for multiple stocks would require
them to learn either Excel macro programming or some type of econometric software.
Recently, Stata has become the popular choice of academics and students, perhaps
because of its user friendliness and abundant resources.
This article is of interest to finance instructors, students, and investors.1 It entails
lecture notes to teach different criteria for portfolio selection, diversification, and op-
tions. It shows how some of the most common statistics are calculated. It provides
fast and easy commands to repeat these tasks during a study session or a lecture using
Stata. It enables finance instructors to assign projects using real-life data and to spend
time on interpretations and on methods proportional to their importance. The provided
procedures are also useful for investors (for educational purposes): using real financial
data, they can compare their investment choices to achieve portfolio objectives.
The initial step is to obtain a list of stocks that make up the Dow Jones Com-
posite Index. For this task, a new Stata command is used: fetchcomponents. The
second step is to download historic prices for these stocks using the Stata command
fetchyahooquotes (Dicle and Levendis 2011). Using the same command, I download
Fama–French factors to fit a multifactor CAPM (Fama and French 1992, 1993). The
third step is to calculate the average daily returns, total returns, standard deviation of
daily returns, Sharpe (1970) measure, and Treynor (1965) measure. In this step, multi-
factor CAPM following Fama and French (1992, 1993) is also estimated for each stock.
Another new Stata command is used to automate this process: fetchportfolio. The
fourth step is to interpret the results. The final step is to introduce the idea of hedging
portfolio risk by using fetchyahoooptions to download options data for a few stocks.
Their implied volatilities are calculated and graphed.
1. This article and associated Stata codes are for educational use. There is no direct or implied
financial advice. While every effort is made for accuracy and reliability, the data and the results
may not be accurate or reliable.
2. Available through http://finance.yahoo.com/q/cp?s=ˆDJA+Components.
M. F. Dicle 605
2.1 Definition
fetchcomponents downloads the list of components for an index. A list of stocks and
components is provided by Yahoo! Finance 50 at a time. Each page of 50 is defined by
the option page.
2.2 Syntax
fetchcomponents, symbol(string) page(#)
2.3 Options
symbol(string) specifies the symbol of the index for which the user wants to download
the components (that is, ^NYA). There can be only one symbol defined, which must
be an index. symbol() is required.
page(#) specifies the number of pages for the list of components (starts from zero).
Because the list of components is provided in batches of 50, it may take several pages
to download the entire list (that is, for ^NYA, there are 0 to 38 pages).
A new command, fetchportfolio, fits the multifactor CAPM and other financial
statistics for all the stocks that constitute the DJA. CAPM estimations are based on the
daily percentage change of dividend- and split-adjusted closing prices.
4.1 Definition
fetchportfolio estimates and calculates financial statistics to compare financial se-
curities for portfolio selection. fetchyahooquotes is needed for fetchportfolio to
run.
4.2 Syntax
namelist is a list of ticker symbols for which the statistics are calculated and a CAPM
is fit. Symbols are separated by spaces.
4.3 Option
year(numlist) is a list of years for which the statistics are calculated and a CAPM is fit.
Years are separated by spaces.
M. F. Dicle 609
5 Interpreting results
5.1 Multifactor CAPM Beta
. list Symbol Beta* R2*
(output omitted )
The analysis is performed on the list of stocks that make up the DJA. There are a
total of 65 stocks. For the purpose of compactness, results are provided for 10 of these
65 stocks. In the table above, there are two columns for each year: Beta and R2. Beta
is the Beta estimated with (1). R2 refers to the R-squared for the same regression. A
CAPM is fit for each stock separately for 2010 and 2011. Higher Beta5 means higher
market risk for each stock. Higher R-squared means more of the variation of daily
stock returns is explained by the controlled independent variables: market risk, small-
minus-big, and high-minus-low. As Beta increased from 2010 to 2011, we can conclude
that the stocks’ market risk increased. As R-squared increased from 2010 to 2011, we
can conclude that the uncontrolled factors (that is, company-specific risks) became less
important factors to explain the variation of daily returns.
1. AA -5.674569 -43.91911
2. AEP 13.75836 25.34019
3. AES -10.90641 -5.622488
4. ALK 62.59323 29.71666
5. AXP 8.655738 12.01655
(output omitted )
Total return is the percentage change in split- and dividend-adjusted closing prices
from the first day to the last day of the period plus the dividend yield for the year.
The figures are in percentages (that is, AA’s return for 2010 was −5.67%, whereas it was
−43.92% for 2011).
1. AA 2.349188 2.711182
2. AEP 1.045383 1.196054
3. AES 2.305197 2.168423
4. ALK 2.611011 2.442996
5. AXP 2.066332 1.918625
(output omitted )
Total risk is the standard deviation of daily returns. Standard deviation of daily
returns is based on the daily percentage change of dividend- and split-adjusted closing
prices. It is interpreted as the combination of systemic and unsystemic risks.
1. AA -.0001682 -.0693872
2. AEP .0343362 .0683895
3. AES -.008775 .0056541
4. ALK .0870246 .0578227
5. AXP .0226696 .0323365
(output omitted )
The formula for the Sharpe measure is as follows: Sharpe = (mean daily return −
mean risk-free rate)/standard deviation of daily returns. Mean daily returns are based
on the daily percentage change of dividend- and split-adjusted closing prices. Therefore,
the higher the ratio, the higher the mean return per level of standard deviation (total
risk).
1. AA -.0002812 -.1217243
2. AEP .04519 .1283311
3. AES -.0160798 .0097086
4. ALK .1547454 .1513523
5. AXP .0374454 .0532964
(output omitted )
The formula for the Treynor measure is as follows: Treynor = (mean daily return −
mean risk-free rate)/multifactor CAPM Beta. As in the Sharpe measure, the higher the
ratio, the higher the mean return for the level of Beta (market risk).
6.1 Syntax
fetchyahoooptions namelist, m(string) iv(real)
namelist specifies a list of ticker symbols for which the options are parsed and down-
loaded from Yahoo! Finance’s options webpage. Symbols are separated by spaces.
6.2 Options
m(string) specifies the month in which the options expire (that is, 2011-12). Multiple
maturity months can be included (that is, 2011-12 2012-01). m() is required.
iv(real) specifies the calculated implied volatility using Black and Scholes’s (1973)
option pricing formula (that is, iv(0.0001)) following (2), (3), (4), and (5). It uses
a trial-and-error method to loop through levels of volatilities to calculate a call or
put option price that matches the ask price. User-supplied increment is used for the
loop. As the increment gets smaller, the loop will take longer. Implied volatility is
calculated separately for each strike price.
n 2
o
ln(S0 /K) + (rf + σ2 )T
d1 = √ (2)
σ T
√
d2 = d1 − σ T (3)
c = S0 N (d1 ) − Ke−rf T N (d2 ) (4)
−rf T
p = Ke N (−d2 ) − S0 N (−d1 ) (5)
In these equations, S0 refers to the spot price of the underlying security, K to the
strike price, rf to the risk-free rate,8 σ to the standard deviation, and T to the years to
maturity.
8. For the risk-free rate, fetchyahoooptions downloads the current ˆIRX, the 13-week U.S. Treasury
Bill yield index, from Yahoo! Finance.
9. For example, http://finance.yahoo.com/q/op?s=IBM+Options.
614 fetchportfolio, fetchcomponents, and fetchyahoooptions
The following Mata function parses the current price of the underlying asset for the
option.
Options data are within the HTML table. At this point, the options table needs to
be parsed.
string get_table (string scalar raw, string scalar tag, string scalar aranan)
{
kalan = strlower(raw)
while (strpos(kalan, "<" + strlower(tag)) < strpos(kalan, strlower(aranan))) {
kalan = substr (kalan, strpos(kalan, "<" + strlower(tag)) + 2
+ strlen(tag), .)
}
kalan = "<" + strlower(tag) + " " + kalan
son_pos = strpos(kalan, "</" + strlower(tag))
kalan=substr(kalan, 1, son_pos + 2 + strlen(tag))
return (kalan)
}
M. F. Dicle 615
The remaining string is parsed for individual HTML tags such as <td>, <tr>, <br>,
etc. Parsing is lengthy and can be accessed through the fetchyahoooptions.ado file.
Parsing is not shown here to conserve space.
The entire HTML source code downloaded from Yahoo! Finance is parsed into a single
string using Mata. The string table that contains the options data has HTML tag <td>,
which can be used as a line break that can then be converted into a Stata observation.
The following Stata code is used for this string split.
The resulting dataset contains eight variables: Strike, Symbol, Last, Change, Bid,
Ask, Volume, and Open Interest.
1
.8
Implied Volatility
.6
.4
.2
7 Acknowledgments
I thank Jiad Alqotob, College of Business, Loyola University, New Orleans, for his valu-
able comments during the creating of the fetchyahoooptions command. I also thank
Ashton Verdery, Department of Sociology, University of North Carolina at Chapel Hill,
for suggesting an improvement (implemented as suggested) in the fetchyahoooptions
command to make it more reliable.
8 References
Black, F., and M. Scholes. 1973. The pricing of options and corporate liabilities. Journal
of Political Economy 81: 637–654.
Dicle, M. F., and J. Levendis. 2011. Importing financial data. Stata Journal 11: 620–
626.
Fama, E. F., and K. R. French. 1992. The cross-section of expected stock returns.
Journal of Finance 47: 427–465.
———. 1993. Common risk factors in the returns on stocks and bonds. Journal of
Financial Economics 33: 3–56.
M. F. Dicle 617
Lintner, J. 1965. The valuation of risk assets and the selection of risky investments in
stock portfolios and capital budgets. Review of Economics and Statistics 47: 13–37.
Sharpe, W. F. 1964. Capital asset prices: A theory of market equilibrium under condi-
tions of risk. Journal of Finance 19: 425–442.
———. 1970. Portfolio Theory and Capital Markets. New York: McGraw–Hill.