Stata 现场培训报名中
下面是公司财务课程教材中部分范例的 Stata 实现代码,以便大家对相关的概念和计算方法有更深入的理解。
- 教材:Berk, Jonathan, and Peter DeMarzo, 2014. Corporate finance (4th, Golbal Edition), Pearson Press.
global path "D:\stata15\ado\personal\CF10\chp11"
cd "$path"
*----------------------
*- Table 11.1
* Returns for Three Stocks, and Portfolios of Pairs of Stocks
*----------------------
clear
input year NorthAir WestAir TexOil
2007 0.21 0.09 -0.02
2008 0.30 0.21 -0.05
2009 0.07 0.07 0.09
2010 -0.05 -0.02 0.21
2011 -0.02 -0.05 0.30
2012 0.09 0.30 0.07
end
tsset year
*-Stock Returns
sum N W T
*-Portfolio Returns
gen P_NW = 0.5*N + 0.5*W
gen P_WT = 0.5*W + 0.5*T
list, sep(0) noobs
sum P_NW P_WT
save "CF_tab_11_1.dta", replace
结果:
. list, sep(0) noobs
+---------------------------------------------------+
| year NorthAir WestAir TexOil P_NW P_WT |
|---------------------------------------------------|
| 2007 .21 .09 -.02 .15 .035 |
| 2008 .3 .21 -.05 .255 .08 |
| 2009 .07 .07 .09 .07 .08 |
| 2010 -.05 -.02 .21 -.035 .095 |
| 2011 -.02 -.05 .3 -.035 .125 |
| 2012 .09 .3 .07 .195 .185 |
+---------------------------------------------------+
.
. sum P_NW P_WT
Variable | Obs Mean Std. Dev. Min Max
-------------+---------------------------------------------------------
P_NW | 6 .1 .1207477 -.035 .255
P_WT | 6 .1 .0507937 .035 .185
Table 11.2 Computing the Covariance and Correlation between Pairs of Stocks
*----------------------
*-Table 11.2
*----------------------
use "CF_tab_11_1.dta", clear
*-Covariance
corr North West, cov
corr West Tex , cov
*-Correlation
corr North West
corr West Tex
*-verify by hand
egen N_avg = mean(North)
gen N_div = North - N_avg
egen W_avg = mean(West)
gen W_div = West - W_avg
gen N_x_W = N_div*W_div
sum N_x_W
scalar Cov_NW = r(sum)/(6-1)
dis Cov_NW
*-Extention: Graphing the effects of protfolio
twoway (connect North year, lw(thick) lc(blue)) ///
(connect West year, lw(thick) lc(black)) ///
(connect Tex year, lw(thick) lc(yellow*2)) ///
, legend(row(1))
graph export Tab11_1_Fig_NWT.png, replace
twoway (connect North year, lw(thick) lc(blue)) ///
(connect West year, lw(thick) lc(black)) ///
(connect P_NW year, lw(vthick) lc(red)) ///
, legend(row(1))
graph export Tab11_1_Fig_NWP.png, replace
twoway (connect Tex year, lw(thick) lc(yellow)) ///
(connect West year, lw(thick) lc(black)) ///
(connect P_WT year, lw(vthick) lc(red)) ///
, legend(row(1))
graph export Tab11_1_Fig_WTP.png, replace
*-correlation matrix
logout, save(corr0) excel replace: ///
pwcorr_c North West Tex, format(%3.2f)
Figure 11.2 Volatility of an Equally Weighted Portfolio Versus the Number of Stocks
*----------------------
*-Figure 11.2
* Volatility of an Equally Weighted Portfolio
* Versus the Number of Stocks
*----------------------
local sd1 = 0.40
local sd2 = 0.40
local rho = 0.28 // 更改这里的相关系数 -0.2,0.5,1.1
local N = 50 // 股票数量
clear
set obs `N'
gen n = _n
gen SD_Rp = sqrt(1/n*`sd1'^2 + (1-1/n)*(`rho'*`sd1'*`sd2'))
line SD_Rp n, ylabel(,angle(0) format(%4.2f)) xmtick(##5) ymtick(##5) ///
lw(*2.5) xline(30, lc(green) lw(*1.5) lp(dash)) ///
text(0.3 `=`N'-10' "rho = `rho'", size(*1.5)) xlabel(,angle(0))
graph export Fig_11_2.png, replace
输出结果:
Table 11.4 Expected Returns and Volatility for Different Portfolios of Two Stocks
*----------------------
*-Table 11.4
*----------------------
clear all
scalar RI = 26
scalar RC = 6
scalar sdI = 50
scalar sdC = 25
forvalues xi = 1(-0.1)0{ // weight of Intel
local xc = 1-`xi' // weight of Coca-cala
local Rp = `xi'*RI + `xc'*RC // the return of portfolio
local SDp = sqrt(`xi'^2*sdI^2 + `xc'^2*sdC^2 + 0)
// the valotility of portfolio
mat A = (nullmat(A) \ `xi', `xc', `Rp', `SDp')
}
*mat list A
svmat A, names(x)
renvars x1-x4 / xI xC Rp SDp
format x* %2.1f
format SDp %3.1f
list, sep(0) noobs
save data_Tab11_4.dta, replace
Figure 11.3 Volatility Versus Expected Return for Portfolios of Intel and Coca-Cola Stock
*----------------------
*-Figure 11.3
* Volatility Versus Expected Return
* for Portfolios of Intel and Coca-Cola Stock
*----------------------
use data_Tab11_4.dta, clear
format SDp %3.0f
cap drop label
gen str10 label = "(0" + string(xI) + ", 0" + string(xC) + ")"
replace label = "(1, 0)" if xI==1
replace label = "(0, 1)" if xC==1
twoway (connect Rp SDp if Rp<=10, lcolor(blue) lw(*2) mlabel(label)) ///
(connect Rp SDp if Rp>=10, lcolor(red ) lw(*2) mlabel(label)) ///
, ///
xlabel(0(10)60, angle(0)) ///
ylabel(0(5)30) ///
subtitle("Figure 11.3", position(11) box) ///
xtitle("Volatility (standard deviation), %") ///
xscale(titlegap(3)) ///
ytitle("Expected Return (%)") ///
yscale(titlegap(3)) ///
text(28 50 "Intel") ///
text( 4 24 "Coca-Cola") ///
text( 7 18 "Inefficicent" "Portfolios", size(*0.8) color(blue)) ///
text(23 35 " Efficicent " "Portfolios", size(*0.8) color(red)) ///
legend(off)
graph export "Fig11_3a.png", replace
Figure 11.4 Effect on Volatility and Expected Return of Changing the Correlation between Intel and Coca-Cola Stock
*----------------------
*-Figure 11.4
* Effect on Volatility and Expected Return of Changing the
* Correlation between Intel and Coca-Cola Stock
*----------------------
clear all
set obs 101
scalar RI = 26
scalar RC = 6
scalar sdI = 50
scalar sdC = 25
gen xi = (_n-1)/100
gen xc = 1 - xi
gen Rp = xi*RI + xc*RC
forvalues rho = -1.0(0.1)1.0{
if `rho'<0{
local rr "_Neg_`=int(abs(`rho')*100)'"
}
else{
local rr "_Pos_`=ceil(`rho'*100)'"
}
gen SDp`rr' = sqrt(xi^2*sdI^2 + xc^2*sdC^2 ///
+ 2*xi*xc*`rho'*sdI*sdC)
}
twoway (line Rp SDp_Neg_100, color(blue*2.0) lw(*1.5)) ///
(line Rp SDp_Neg_80, color(blue*1.2) lw(*1.5)) ///
(line Rp SDp_Neg_60, color(blue*0.6) lw(*1.5)) ///
(line Rp SDp_Neg_20, color(blue*0.2) lw(*1.5)) ///
(line Rp SDp_Neg_0 , color(red) lw(*2)) ///
(line Rp SDp_Pos_10, color(red*0.2) lw(*1.5)) ///
(line Rp SDp_Pos_50, color(red*0.4) lw(*1.5)) ///
(line Rp SDp_Pos_100, color(yellow*1.5) lw(*2.5)) ///
, ///
xlabel(0(10)60) ///
ylabel(0(5)30, angle(0)) ///
subtitle("Figure 11.4", position(11) box) ///
xtitle("Volatility (standard deviation), %") ///
xscale(titlegap(3)) ///
ytitle("Expected Return (%)") ///
yscale(titlegap(3)) ///
text(28 50 "Intel") ///
text( 4 24 "Coca-Cola") ///
text(17 7 "Corrlation= -1", size(*0.9) color(black)) ///
text(13 40 "Corrlation= +1", size(*0.9) color(black)) ///
plotregion(margin(0)) ///
legend(off)
Figure 11.5 Portfolios of Intel and Coca-Cola Allowing for Short Sales
*----------------------
*-Figure 11.5
* Portfolios of Intel and Coca-Cola
* Allowing for Short Sales
*----------------------
clear all
scalar RI = 26
scalar RC = 6
scalar sdI = 50
scalar sdC = 25
forvalues xi = 1.5(-0.1)-0.6{ // weight of Intel
local xc = 1-`xi' // weight of Coca-cala
local Rp = `xi'*RI + `xc'*RC // the return of portfolio
local SDp = sqrt((`xi')^2*sdI^2 + (`xc')^2*sdC^2 + 0)
// the valotility of portfolio
mat A = (nullmat(A) \ `xi', `xc', `Rp', `SDp')
}
svmat A, names(x)
renvars x1-x4 / xI xC Rp SDp
format x* %2.1f
format SDp %3.0f
twoway (line Rp SDp if Rp<=6, lc(blue*0.5) lw(*2) lp(dot)) ///
(connect Rp SDp if Rp<=10&Rp>=6, lc(blue) lw(*2)) ///
(connect Rp SDp if Rp>=10&Rp<=26, lc(red ) lw(*2)) ///
(line Rp SDp if Rp>=26, lc(green) lw(*2.5) lp(dot)) ///
, ///
xlabel(0(10)80) ///
ylabel(-5(5)40, angle(0)) ///
subtitle("Figure 11.5", position(11) box) ///
xtitle("Volatility (standard deviation), %") ///
xscale(titlegap(3)) ///
ytitle("Expected Return (%)") ///
yscale(titlegap(3)) ///
text(27 47 "Intel") ///
text( 5 18 "Coca-Cola") ///
text( 4 40 "Short Intel, " "Long Coca-Cola", size(*0.8) color(blue)) ///
text(15 37 "Long Intel, " "Long Coca-Cola", size(*0.8) color(red)) ///
text(27 69 "Long Intel, " "Short Coca-Cola", size(*0.8) color(pink)) ///
legend(off)
graph export "Fig11_5.png", replace
关于我们
- 【Stata 连享会(公众号:StataChina)】由中山大学连玉君老师团队创办,旨在定期与大家分享 Stata 应用的各种经验和技巧。
- 公众号推文同步发布于 【-Stata连享会】 和 【知乎-连玉君Stata专栏】。可以在和知乎中搜索关键词
Stata
或Stata连享会
后关注我们。 - 推文中的相关数据和程序,以及 Markdown 格式原文 可以在 【Stata连享会-码云】 中获取。【Stata连享会-码云】 中还放置了诸多 Stata 资源和程序。如 Stata命令导航 || stata-fundamentals || Propensity-score-matching-in-stata || Stata-Training 等。
联系我们
- 欢迎赐稿: 欢迎将您的文章或笔记投稿至
Stata连享会(公众号: StataChina)
,我们会保留您的署名;录用稿件达五篇
以上,即可免费获得 Stata 现场培训 (初级或高级选其一) 资格。 - 意见和资料: 欢迎您的宝贵意见,您也可以来信索取推文中提及的程序和数据。
- 招募英才: 欢迎加入我们的团队,一起学习 Stata。合作编辑或撰写稿件五篇以上,即可免费获得 Stata 现场培训 (初级或高级选其一) 资格。
- 联系邮件: [email protected]
Stata 现场培训报名中