mgf 690 f2f special topics: financial analysis with r ...yany/doc/syllabusmgf690.pdf · software:...
TRANSCRIPT
-
1
MGF 690 F2F
Special Topics: Financial Analysis with R
Course Syllabus
Department of Finance and Managerial Economics
University at Buffalo Fall 2015 Instructor: Paul Yan
Office: 359 Jacobs Management Center (Tuesday only)
Emails: [email protected] and [email protected]
Phones: 716 645 3277 (Tuesday), 716 888 2604 (other weekdays)
Textbook: Financial Modeling using R by Yuxing Yan
Publisher: Tate Publishing, ISBN: 978-1-68187-530-9
https://www.tatepublishing.com/bookstore/book.php?w=9781681875309
References: An Introduction to R http://canisius.edu/~yany/doc/R-intro.pdf
The R Language Definition http://canisius.edu/~yany/doc/R-lang.pdf
Web sites: https://ublearns.buffalo.edu/ and http://canisius.edu/~yany/R.shtml
> source("http://canisius.edu/~yany/mgf690") Note: I will explain this line in
week 2
Software: R, open source statistical and computational software1. Students are expected to
spend at least 1 hour per day on R outside the classroom.
Prerequisites: Minimum two finance courses, such as Corporate Finance and Portfolio Analysis
Lecture: Tuesdays: 8:00am ~ 10:50am @ Jacobs B30 (a computer lab)
Office hours: Tuesdays 11:00am ~ 1:00pm @ 359 Jacobs Management Center
Three objectives:
1) Learn/review basic financial concepts such as Ratio Analysis, Portfolio Theory,
CAPM, Fama-French-Carhart Factor Model, Monte Carlo simulation, Options Theory,
VaR (Value at Risk) and Market microstructure
2) Learn and apply R to finance
3) Focus on publicly available financial data such as Yahoo Finance, Google Finance,
Prof. French's Data Library and Federal Reserve Economic Data Library (FRED).
4) Use CRSP and/or TAQ (Trade and Quote) databases.
Note: CRSP and TAQ are for the term-project if some groups or individual student want
to use CRSP or TAQ databases for their projects.
1 www.r-project.org
https://www.tatepublishing.com/bookstore/book.php?w=9781681875309http://canisius.edu/~yany/doc/R-intro.pdfhttp://canisius.edu/~yany/doc/R-lang.pdfhttps://ublearns.buffalo.edu/http://canisius.edu/~yany/R.shtmlhttp://www.r-project.org/
-
2
Teaching Method:
Each class will be consist of two parts: lecture (including discussion of homework) and
hands-on.
Grading Policy:
Homework 30%
Midterm 20%
Final exam 25%
Group project 10%
Group presentation 5%
Class participation 10%
----------------------------------------------
Total 100%
Mid-term and Final:
All exams (midterm and final) will be conducted in the computer lab. Those are open
book exams with three types of questions: related to 1) finance; 2) R or 3) financial data.
Group project
Each group can have up to three members. A topic should be closely associated with this
course. The maximum number of pages of your report is 15 with 12-point font. Please
discuss with me your topic before you start to work on it. Some basic criterions are listed
below.
Real world topics are especially encouraged. Three parts are essential:
1) theory and background of the topic,
2) R programs with a short explanation of the codes,
3) final data set (plus the codes to process the data, the source of raw data)
Note: please do not send me your raw data.
The second type of projects is to study one of R packages. Three parts are critical:
1) why this specific package is useful in finance
2) a summary of all or most important functions offered by the package
3) examples to use them
Note: see a list of potential topics, at the end of the syllabus, for the group projects.
-
3
Tentative schedule # Date Topics Description (F for Finance)
1
9/1
Syllabus
discussion,
introduction to
R
A short survey, self-intro, syllabus, course structure, mid-term and final
F: Review of basic financial concepts, financial formulae, risk vs. return,
how to measure risk, time value of money, present value (PV), future
value (FV), PV(annuity), etc.
R: Installation, assignment, basic math functions: mean(),
min(),max(), median(), sd() and use R as a scientific calculator
2
9/8
Review of basic
finance concepts
and formulae
(functions)
F: How to estimate returns? PV(bond), simple and compound
interest, conversion of returns for different frequencies
R: How to write an R function? double_f(), pv_f(), fv_f(), IRR(),
pv_annuity(), fv_annuity(), pv(perpetuity), pv(perpetuity_due)
How to call your functions? several ways to input data, matrix,
differential operator and use R as a financial calculator
3
9/15
Options
F: Black-Scholes-Merton option model, trading strategies with
options, implied volatility, Blacks approximation for an
American option, Greeks, put-call parity, hedging strategy, risk-
neutral evaluation
R: pnorm() [cumulative standard normal distribution], bs_f.R,
Implied_vol.R, greeks.R
4
9/22
Financial
Statement
Analysis
F: Financial statement analysis, ratio analysis, probability ratio:
operating margin, net profit margin, ROA, ROE, current
ratio, book debt-equity ratio
R: Retrieve BS, IS, CF, current ratio, debt equity ratio,
5
9/29
Open data
[Yahoo finance
etc.]
F: daily vs. monthly returns, Yahoo finance, Rf, Frenchs Data Library
R: How to input data from a text file? simple programs to download
historical price data from Yahoo finance, download data from Prof.
Frenchs data library , download risk-free rate
6
10/6
CAPM
F: CAPM, estimation, rolling/portfolio , hedging portfolio market risk
R Several functions: as.Date, week_day_effect.R , data.frame,
,beta.R, rolling_beta.R, replicate
7
10/13
Multi-factor
models, Sharpe
Treynor ratios
F: Fama-French 3-factor model, momentum strategy, Sharpe ratio,
Treynors ratio, 52-weeks high, Jensens ,
R: ff3factor, ff4, weekday/January effect. sharpe.R, Treynor.R
8 10/20 Midterm
9
10/27
T-test, F-test,
Autocorrelation,
Causality
F: T-test for significance, equality of means, F-test for difference of
volatility, Granger causality test, Durbin-Watson autocorrelation test
R: t.test(), var.test(), dwtest(), Wilcoxon.test(), granger_test().
Note: post two dozen topics related to term projects (see Appendices B
and C for two examples)
10
11/3
Monte Carlo
Simulation
F: simulation and assumptions, normality test, estimate variance-
covariance matrix, conversion variances between different frequencies,
path dependent options, sensitivity analysis, scenario analysis
R: rnorm(x), random number from normal, uniform distribution Several
functions: as.Date, week_day_effect.R,In class-exercise: find 1 C among
500 Os and Monte Carlo Simulation to price European and Asian options
-
4
Continued Lecture Date Topic F (finance) and R
11
11/10
CRSP for
teaching using R
F: What is CRSP? CRSP monthly, daily time series data, event
data (more topics for term projects)
R: stockMonthly, indexMonthly, indexDaily, stockD1925 to
stockD2014, various R program to retrieve/process data efficiently
12
11/17
Portfolio theory
F: variance, standard deviation, correlation, return matrix, portfolio
return, portfolio volatility of 2-stock (n-stock) portfolio, variance-
covariance matrix, portfolio optimization
R: package fPortfolio
13
11/24
Value at Risk
F: introduction to VaR, (standard) normal distribution, thick tail
distribution
R: standard normal distribution, VaR_01.R, VaR_02.R,
introduction to packages in R.
14 12/1 Group
Presentation
3 to 4 groups
15 12/8 Group
Presentation
All other groups
Extra
TAQ for
teaching using R
TAQ (Trade and Quote) are high frequency database by NYSE
MTAQ (up to second) DTAQ (up to millisecond)
R data sets: TAQct, TAQcq, DTAQ22ct, DTAQ22cq
TORQct, TORQcq, TORQcd, TORQsod
R: loadTAQ, filterCT, filterCQ, spread, relativeSpread,leeRead
Extra
Credit risk
F: Credit rating, credit score, probability of default, Z-score for
predicting Bankruptcy, Moodys, S&P, Fitch, Best crediting
systems, credit transition matrix
R: prob_default.R, Z_score.R, KMV.R
Extra
Spread
estimation from
low-frequency
F: spread estimation from low frequency data, Roll (1984), Corwin
and Schultz (2012) high-low spread
R: Roll.R, Corwin_Schultz.R
Extra
Liquidity
measure
F: Amihud (2002) illiquidity measure, Pastor and Stambaugh
(2002) liquidity measure
R: Amihud.R, PS.R
TBA Final Final-exam
-
5
Appendix A: A list of potential topics for term projects
Warm
up
1 Financial statement analysis
2 An interned connected financial calculator (Yan, 2012)
3 Correlations among stocks in US, UK, Canada, France, China, Japan and Australia
4 A Business cycle indicator (Yan and Zhang, 2015)
5 Use journal ranking data efficiently (SCImago Journal and Country Ranking)
6 Find an optimal portfolio
7 How much you need when you retire? Social Security Benefit calculator
8 Which party, Republican or Democratic, could manage the economy better?
9 Monte Carlo Simulation (standard normal distribution, one variables) VaR
10 PCA (Principal Component Analysis)
Data
Pu
bli
c d
ata
11 Generate R data sets for 200 stocks, CPI, GDP, Unemployment rate etc.
12 Generate R data sets for Fama-French 3-factors, 5 factors etc.
13 Generate R data sets for all SEC 10Q and 10K index files from SEC (1993-2015)
14 Generate R data sets for TORQ (Trade, Order, Report and Quote) database
15 Generate R data sets for TDAQ (millisecond by millisecond transaction data)
16 Parse 10K data from SEC filings, generate related R data sets
CR
SP
/TA
Q
17 Generate R data sets for one months TAQ data (MTAQ)
18 Generate R data sets for crspInfo, stockMonthly, indexMonthly for CRSP
19 Generate R data sets for stockDaily, indexDaily for CRSP
20 Generate R data sets for TDAQ for several months
Usi
ng p
ub
lic
data
21 Are annual beta mean reversion?
22 Test the January and weekday effects
23 Does size effect exist?
24 Tracking errors
25 Z-score (bankruptcy prediction, Altman, 1968)
26 52-week High trading strategy using more than 200 stocks
27 estimate Roll (1984) spread from daily data (Roll, 1984)
28 Assessment of multiple choice questions using R
29 Monte Carlo Simulation (capital budgeting, replicate a Slot Machine)
30 Monte Carlo Simulation (one variables) VaR, n correlated stocks
Usi
ng C
RS
P o
r T
AQ
31 Replicate S&P500 EW (equal-weighted) and VW (value-weighted) returns
32 Is liquidity factor priced? (Amihud, 2002)
33 What is the color of your firm, blue or red? (Yan, 2014)
34 Which model is the best, CAPM, FF3, FFC4, or FF5?
35 Estimate spread, relative spread, expected spread etc. by using TAQ
36 Process TAQ efficiently, how to process 30 year MTAQ data efficiently ?
37 Replicate momentum trading strategy (Jegadeesh and Titman, 1993)
38 Replicate industry momentum trading strategy (Moskowitz and Grinblatt, 1999)
39 Replicate 52-week high trading strategy (George and Huang, 2004)
40 Replicate max- trading strategy (Bali, Cakici and Whitelaw, 2011)
41 Impact of business cycle on the above four trading strategies, (Yan and Zhang , 2015)
-
6
References (most of them are for term projects)
Altman, Edward, Altman, E. (1968), Financial ratios, discriminant analysis and the prediction of
corporate bankruptcy, Journal of Finance, 23(4), pp 598-608.
Amihud, Yakov, 2002, Illiquidity and Stock returns, Journal of Financial Markets 5, 31-56.
Bali, Turan G., Nusret Cakici, and Robert F. Whitelaw, 2011, Maxing Out: Stocks as Lotteries and the
Cross-Section of Expected Returns, Journal of Financial Economics 99 427-446.
George, Thomas J, and Chuan-Yang Huang, 2004, The 52-week High and Momentum Investing, Journal
of Finance 54, 5, 2145-2176.
Jegadeesh, N., and S. Titman, 1993, Returns to Buying Winners and Selling Losers: Implications for
Stock Market Efficiency, Journal of Finance 48, 65-91.
Moskowitz, Tobias, and Mark Grinblatt, 1999, Do industries explain momentum? Journal of Finance 54,
2017-2069.
Pastor, L. & Stambaugh, R., 2003, Liquidity risk and expected stock returns, Journal of Political
Economy 111, 642-685.
Roll, Richard, 1984, A simple implicit measure of the effective bid-ask spread in an efficient market,
Journal of Finance 39, 1127-1139.
Yan, Yuxing, An internet connected financial calculator, 2012, Journal of Accounting and Finance 12(5),
59-70.
Yan, Yuxing, 2015, Red vs. Blue Stocks: Politics and Profitability of Firms, Journal of Business and
Policy Research 10 (1), 117-138.
Yan, Yuxing and Shaojun Zhang, 2015, Business cycle, investors preferences and trading strategies
working paper, http://www3.canisius.edu/~yany/doc/paper26max.pdf
http://www3.canisius.edu/~yany/doc/paper26max.pdf
-
7
Appendix B: Topic #31: Replicate S&P500 EW and VW returns
Objectives:
1) Understand the meanings of equal-weighted and valued portfolio returns
2) Know how to write an R program to estimate portfolio returns
3) Replicate S&P500 EWRETD and VWRETD returns
Data sets needed:
1) stockMonthly (individual stock monthly data)
2) sp500add (shows when a stock is added and deleted from S&P500 index)
3) sp500monthly (served as a benchmark, EVRETD and VWRETD)
Below is a simple illustration showing how to estimate EW and VW portfolio returns for a 3-stock
portfolio for just one day.
EW VW
=
3
=1
=
3
=1
Assume that we have three stocks in our portfolio: Walmart , IBM and Apple. We want estimate their
equal-weighted and value weighted portfolio returns on 4/30/2009. Usually, the lagged market
capitalization is used as the weight. First, find out the PERMNO for those three firms.
> crspInfo[grep("IBM",crspInfo$TICKER),]
PERMNO PERMCO CUSIP FIRMNAME TICKER EXCHANGE BEGDATE ENDDATE
2469 12490 20990 45920010 INTERNATIONAL BUSINESS MACHS CO IBM 1 1925-12-31 2014-12-31
3633 13811 37493 46429B58 ISHARES TRUST IBMG 4 2013-03-28 2014-12-31
4270 14480 37493 46429B56 ISHARES TRUST IBMH 4 2014-02-28 2014-12-31
4637 14853 37493 46434V57 ISHARES TRUST IBMI 4 2014-08-29 2014-12-31
30435 93211 37493 46428927 ISHARES TRUST IBMF 4 2010-01-29 2014-12-31
30436 93212 37493 46428931 ISHARES TRUST IBME 4 2010-01-29 2014-12-31
30437 93213 37493 46428933 ISHARES TRUST IBMD 4 2010-01-29 2014-12-31
The PERNO for IBM is 12490. Similarly, we could find out that the PERMNOs for Wal-Mart and Apple
(55976 and 14593). Assume that we want to estimate EW and VW of this 3-stock portfolio returns on
4/30/2009. We have the following information, see Appendix A for more details.
PERMNO Date Return Volume Price SharesOutStanding
12490 2009-03-31 0.052809 2861506 96.89 1341678
12490 2009-04-30 0.065229 2137517 103.21 1321397
55976 2009-03-31 0.063617 5453261 52. 1 3922552
55976 2009-04-30 -0.032630 4505615 50.4 3912873
14593 2009-03-31 0.177024 5728764 105.12 892110
14593 2009-04-30 0.197013 4340566 125.83 892110
The results are shown below.
> ret lag_cap ret
[1] 0.065229 -0.032630 0.197013
> lag_cap
[1] 12999518142 207996329 112254201
> mean(ret)
-
8
[1] 0.07653733
> weighted.mean(ret,lag_cap) # > sum(ret*lag_cap/sum(lag_cap))
[1] 0.0648115 # [1] 0.0648115
Appendix A: choose two days and show their returns, price and shares outstanding
>tradingDaysCRSPmonthly date1 date2 subset(stockMonthly, stockMonthly$PERMNO==12490 &
(stockMonthly$Date==date1 | stockMonthly$Date==date2))
PERMNO Date Return Volume Price SharesOutStanding
357635 12490 2009-03-31 0.052809 2861506 96.89 1341678
357636 12490 2009-04-30 0.065229 2137517 103.21 1321397
Appendix B: a few lines from sp500add
> load("sp500add.RData")
> head(sp500add)
PERMNO Added Deleted
1 10006 1957-03-01 1984-07-18
2 10030 1957-03-01 1969-01-08
3 10049 1925-12-31 1932-10-01
4 10057 1957-03-01 1992-07-02
5 10078 1992-08-20 2010-01-28
6 10102 1957-03-01 1975-02-05
Appendix C: a few lines from sp500monthly R data set and the definitions of variables
> load("sp500monthly.RData")
> head(sp500monthly,3)
DATE VWRETD VWRETX EWRETD EWRETX VALUE N VAL500 N500 SP500INDEX SP500RET
1 1925-12-31 15236830 89 12.46 2 1926-01-30 -0.001783 -0.003980 0.006457 0.003250 15277664 89 15236829.50 79 12.74 0.022472 3 1926-02-27 -0.033296 -0.037876 -0.039979 -0.042451 14712895 89 15277664.00 81 12.18 -0.043956
-
9
Appendix C: Topic #36: Momentum strategy
We could use a simple phrase to summarize the so-called momentum trading strategy: buy winners and
sell losers. Here, we have an implied assumption: within a short-term (between 3 months and 12 months),
the winner will remain a winner while a loser would continue to be a loser. Two related questions: 1) how
to define a winner from a loser? 2) how to conduct a test?
Objectives of this term project:
1) Understand the CRSP database
2) Understand how to use R to retrieve and process data using CRSP monthly stock data
3) Prove or disapprove so-called momentum strategy by replicating Table 1 of Jegadeesh and
Titman (1993)
Prerequisites: access to an R data set called stockMonthly.RData (I will supply this data set)
Basic logic: According to Jegadeesh and Titman (1993) it is a profitable trading strategy if we buy
the past winners and sell the past losers.
Notations: Check the past K-month returns, and then form a portfolio for L months,
Where K=3,6,9 and 12 and L=3, 6, 9 and 12. Below we use K=L=6 as an example.
Trading strategy: Estimate all stocks past 6- month returns and sort stocks into 10 groups (deciles)
according to their 6-month total returns. Long the top decile (winners) and short the
bottom decile (losers) for the next 6 months.
Procedure:
Step 0: Starting month: January 1927
Step 1: Retrieve CRSP data (PERMNO, DATE and RET)
Step 2: Estimate past 6-month cumulative returns 6
Step 3: Sort all stocks into deciles according to their cumulative 6-month returns
Step 4: Long winners (best return group) and short losers for the next 6-month
Step 5: Estimate portfolio returns
Step 6: Move to the next month and repeat the above steps until the last month (July,
2013 since the last month for the current CRSP monthly data is December 2014)
References
Jegadeesh Narasimhan and Sheridan Titman, 1993, Returns to Buying Winners and Selling Losers:
Implications for Stock Market Efficiency, Journal of Finance 48 (1), 65-91.
Appendix A: Table 1 from Jegadeesh and Titman (1993).
-
10
-
11
Appendix D: A list of R data sets related to term projects
*-------------------------------------------------------*
* List of R data sets for CRSP *
*-------------------------------------------------------*
* crspInfo # CRSP header file *
* tradingDaysM # trading days for monthly data *
* tradingDaysD # trading days for daily data *
* stockMonthly # monthly stock data *
* indexMonthly # index monthly data *
* sp500add # sp500 constitutients *
* sp500monthly # monthly S&P500 returns *
* sp500daily # daily S&P500 daily returns *
* indexDaily # daily index *
* d1925 # daily stock for 1925 *
* d1926 # daily stock for 1926 *
* ..... *
* d2013 # daily stock for 2013 *
* d2014 # daily stock for 2014 *
* stockDailyBefore1980 # daily stock (1926 -- 1979) *
* stockDailyBefore1990 # stock daily (1980 -- 1989) *
* stockDailyBefore2000 # stock daily (1990 -- 1999) *
* stockDailyBefore2015 # stock daily (2000 -- 2014) *
* eventDaily # daily event data set *
* eventMonthly # monthly event data set *
*-------------------------------------------------------*
* List of R data sets for Fama-French *
*-------------------------------------------------------*
* ffMonthly # Fama-Fench monthly 3 factors *
* ffcMonthly # Fama-French-Carhart 4 factors *
* ffMonthly5 # Fama-French-Carhart 5 factors *
* ffDaily # Fama-French daily 3 factors *
* ffcDaily # Fama-French-Carhart daily 3 factors *
* ffDaily5 # Fama-French 5 daily factors *
*-------------------------------------------------------*
* List of R data sets for TAQ (MTAQ and DTAQ) *
*-------------------------------------------------------*
* TAQct # CT (Consolidated Trade) *
* TAQcq # CQ (Consolidated Quote) *
* DTAQct # CT (Ultra-high frequency,millisecond) *
* DTAQcq # CQ (Ultra-high frequency) *
* TORQct # CT from TORQ *
*TORQcq # CQ from TORQ *
* TORQcd # CD (audit) from TORQ *
* TORQcod # COD from TORQ *
*-------------------------------------------------------*
* Others *
*-------------------------------------------------------*
* firmData # firms accounting data *
* sec10K # SEC quarterly indices (1993-2015) *
*-------------------------------------------------------*
In total, over 120 R data sets were generated to support various term projects.
-
12
Appendix E: one of useful programs for some term project topics
loserWinner_f