
New! 搜推文,找资料,用
lianxh
命令:
安装:ssc install lianxh, replace
使用:lianxh 合成控制
lianxh DID + 多期, w


下面是公司财务课程教材中部分范例的 Stata 实现代码,以便大家对相关的概念和计算方法有更深入的理解。
教材: Berk, Jonathan, and Peter DeMarzo, 2014. Corporate finance (4th, Golbal Edition), Pearson Press.
第 11 章 表 11.1
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

扩展阅读
- 史津宇、连玉君, 2021, Stata:投资组合有效边界, Blog No.595.


资源共享
- 连享会资料 ……
- 在线视频:lianxh-class.cn
- Stata 33 讲,100 万+ 播放,Stata 入门必备,公开课
- 直击面板数据模型,10 万+ 播放,白话面板模型,公开课
- … more …
- 论文复现和数据
- 主题分类
- 热门推文

尊敬的老师 / 亲爱的同学们:
连享会致力于不断优化和丰富课程内容,以确保每位学员都能获得最有价值的学习体验。为了更精准地满足您的学习需求,我们诚挚地邀请您参与到我们的课程规划中来。
请您在下面的问卷中,分享您 感兴趣的学习主题或您希望深入了解的知识领域 。您的每一条建议都是我们宝贵的资源,将直接影响到我们课程的改进和创新。
我们期待您的反馈,因为您的参与和支持是我们不断前进的动力。感谢您抽出宝贵时间,与我们共同塑造更加精彩的学习旅程!https://www.wjx.cn/vm/YgPfdsJ.aspx# 再次感谢大家宝贵的意见!

关于我们
- Stata连享会 由中山大学连玉君老师团队创办,定期分享实证分析经验。more……
- 扫码加入连享会微信群,提问交流更方便
