# 公司财务-投资组合Stata实现-Berk-Corporate-Finance-Chapter-11-Examples

Stata 连享会   主页 || 视频 || 推文

## 第 11 章 表 11.1

``````
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
``````

## 相关课程

http://lianxh.duanshu.com

### 课程一览

Stata数据清洗 游万海 直播, 2 小时，已上线

Note: 部分课程的资料，PPT 等可以前往 连享会-直播课 主页查看，下载。

#### 关于我们

• Stata连享会 由中山大学连玉君老师团队创办，定期分享实证分析经验。直播间 有很多视频课程，可以随时观看。
• 连享会-主页知乎专栏，300+ 推文，实证分析不再抓狂。
• 公众号推文分类： 计量专题 | 分类推文 | 资源工具。推文分成 内生性 | 空间计量 | 时序面板 | 结果输出 | 交乘调节 五类，主流方法介绍一目了然：DID, RDD, IV, GMM, FE, Probit 等。
• 公众号关键词搜索/回复 功能已经上线。大家可以在公众号左下角点击键盘图标，输入简要关键词，以便快速呈现历史推文，获取工具软件和数据下载。常见关键词：`课程, 直播, 视频, 客服, 模型设定, 研究设计, stata, plus, 绘图, 编程, 面板, 论文重现, 可视化, RDD, DID, PSM, 合成控制法`

✏ 连享会学习群-常见问题解答汇总：
https://gitee.com/arlionn/WD