L02 现代投资组合理论

It is the part of a wise man...not to venture all his eggs in one basket. —-Miguel de Cervantes

"Don't put all your eggs in one basket" is all wrong. I tell you "put all your eggs in one basket, and then watch that basket." -- Andrew Carnegie

"Harry M. Markowitz - Facts". Nobelprize.org. Nobel Media AB 2014. Web. 2 Sep 2014.

组合管理的主要思路

  • 投资一个仔细选择的组合(portfolio)而不是某一个资产

  • 如何选择组合?

    • 在保证组合预期收益(不低于给定水平)的前提下最小化组合风险
    • 在保证组合风险(不高于给定水平)的前提下最大化组合预期收益
  • 历史背景

    • 最优化理论的发展: K-K-T条件(Kuhn & Tucker,1951; Karash, 1939)
    • 计算机(1946)的广泛应用

马科维茨与组合管理的小故事

"Harry M. Markowitz - Prize Lecture: Foundations of Portfolio Theory". Nobelprize.org. Nobel Media AB 2014. Web. 2 Sep 2014.

组合管理的均值方差分析

组合收益

收益

  • 收益的来源

    • 现金股利、利息等周期性收益
    • 资本增值或损失
  • Example
    As reported by Yahoo! Finance, the S&P 500 Index of U.S.
    stocks
    was at 903.25 on 31 December 2008. Similarly, Yahoo! Finance reported that
    the index closed on 30 July 2002 at 902.78, implying a return of close to 0 percent
    over the approximately six-and-a-half-year period. The results are very different,
    however, if the total return S&P 500 Index is considered. The index was at 1283.62 on 30 July 2002 and had risen 13.2 percent to 1452.98 on 31 December 2008, giving an annual return of 1.9 percent.

  • 用什么表示(量化)收益?

持有期收益

  • 持有期收益是在一段特定的时间内持有资产获得的收益
  • 公式

组合收益(Portfolio Return)

公式:

  • 组合收益是各资产收益的加权平均
  • 所有之和必须为
  • 对任意的, 不必一定为正

组合收益的风险

单个资产收益的方差与协方差

  • 方差, 或者风险, 是对收益的波动性或者其分散程度的度量

    • 总体方差

  • 标准差度量了随机变量偏离其期望的平均距离

投资组合收益的方差

假设为各资产占整个投资组合的权重,组合的收益为

因此,其方差可以表示为

由两个风险资产组成的组合

组合收益:

组合风险:

因此,其标准差为

取不同数值时组合收益与风险的关系

例子:组合收益 vs. 组合风险

Weight in Asset 1(%) Portfolio Return Portfolio Risk with Correlation of
1.00.50.2-1.0
015.025.025.025.025.0
1014.223.723.122.821.3
2013.422.421.320.617.6
3012.621.119.618.613.9
4011.819.817.916.610.2
5011.018.516.314.96.5
6010.217.215.013.42.8
709.415.913.812.30.9
808.614.612.911.74.6
907.813.312.211.68.3
1007.012.012.012.012.0

数值实验:两风险资产投资组合

  • imports
import numpy as np
import pandas as pd
import tushare as ts
import matplotlib 
import joblib
%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('seaborn-whitegrid')
  • load data
pro = ts.pro_api(token='')
df = pro.daily(ts_code='600519.SH,000333.SZ,000001.SZ', 
               start_date='20180101', 
               end_date='20181231')

ts_code trade_date open high low close pre_close change pct_chg vol amount
0 000001.SZ 20181228 9.31 9.46 9.31 9.38 9.28 0.10 1.0776 576604.00 541571.004
1 000333.SZ 20181228 36.75 37.40 36.31 36.86 36.80 0.06 0.1630 211676.03 782455.535
2 600519.SH 20181228 563.30 596.40 560.00 590.01 563.00 27.01 4.7975 63678.37 3705150.490
... ... ... ... ... ... ... ... ... ... ... ...
699 600519.SH 20180102 700.00 710.16 689.89 703.85 697.49 6.36 0.9100 49612.48 3482407.646

  • 提取三只股票回报率
stk1 = df.loc[df.ts_code == '600519.SH', ['ts_code','trade_date','pct_chg']]
stk2 = df.loc[df.ts_code == '000333.SZ', ['ts_code','trade_date','pct_chg']]
stk3 = df.loc[df.ts_code == '000001.SZ', ['ts_code','trade_date','pct_chg']]
stk = pd.merge(stk1,stk2,on = 'trade_date')
stk = pd.merge(stk,stk3,on = 'trade_date')
  • 计算期望回报率
stk_return = []
stk_return.append(stk.pct_chg_x.mean())
stk_return.append(stk.pct_chg_y.mean())
stk_return.append(stk.pct_chg.mean())
  • 计算波动率
stk_risk = []
stk_risk.append(stk.pct_chg_x.var())
stk_risk.append(stk.pct_chg_y.var())
stk_risk.append(stk.pct_chg.var())
stk_risk=list(np.sqrt(stk_risk))

  • 生成数据
rho=.4
w1=[0.01*n for n in range(101)]
portfolio_return=[w*stk_return[0]+(1-w)*stk_return[1] for w in w1]
portfolio_risk=[np.sqrt(w**2*stk_risk[0]**2
                        +2*rho*w*(1-w)*stk_risk[0]*stk_risk[1]
                        +(1-w)**2*stk_risk[1]**2)
                for w in w1]
  • 两风险资产投资组合
fig = plt.figure()
ax = plt.axes()
ax.scatter(portfolio_risk, portfolio_return)
  • 计算波动率
stk_risk = []
stk_risk.append(stk.pct_chg_x.var())
stk_risk.append(stk.pct_chg_y.var())
stk_risk.append(stk.pct_chg.var())
stk_risk=list(np.sqrt(stk_risk))

由多个风险资产组成的投资组合

假设代表平均方差和平均协方差,我们有下式

为平均相关系数, 我们有

有多于2个风险资产时,所有可行组合的风险和收益的关系不再能够由一条曲线表示。

数值实验:三风险资产投资组合

  • 生成权向量
w1=[0.001*n for n in range(1001)]
w=[[w, .001*n, 1-w-.001*n] 
   for n in range(1001) 
   for w in w1 if w+.001*n<=1]
  • 生成期望回报率向量与方差协方差矩阵
ret=stk.loc[:,['pct_chg_x','pct_chg_y',
               'pct_chg']].mean().to_numpy()
cov=stk.loc[:,['pct_chg_x','pct_chg_y',
               'pct_chg']].cov().to_numpy()
  • 计算组合回报率与波动率
portfolio_return=[]
portfolio_risk=[]
for weight in w:
    #w=np.array([w1[n],w2[n],w3[n]])
    ww=np.array(weight)
    portfolio_return.append(ww.dot(ret.T))
    portfolio_risk.append(np.sqrt(ww.dot(cov).dot(ww.T)))
  • 绘图
fig = plt.figure()
ax = plt.axes()
ax.scatter(portfolio_risk, portfolio_return)

均值-方差最优化问题

均值-方差最优化(MVO)

或者

或者

集合定义了资产权重的所有可能取值, 比如:

  • 如果不允许卖空:
  • 如果允许卖空:

均值-方差最优化(MVO)的解

  • MVO的解

    • 二次效用函数与线性约束保证了解得存在性和唯一性
    • 问题的凸性保证了有效(率)的算法
    • 最优解中有可能包括风险(方差)较大或者/而且收益较小的资产吗?
  • The model is too naive for practical purpose

    • Single-period, does not consider non-market factors
    • The inclusion of transaction costs (such as market impact costs) and tax effects
    • The addition of various types of constraints that take specific investment
      guidelines and institutional features into account
    • Modeling and quantification of the impact of estimation errors in risk and return
      forecasts on the portfolios via Bayesian techniques, stochastic optimization, or
      robust optimization approaches;

有效前沿(Efficient Frontier)与最优风险组合

投资机会集合(Investment Opportunity Set)

最小方差投资组合与有效前沿

一个无风险资产与多个风险资产

资本分配线(Capital Allocation Line)与最优风险组合(Optimal Risky Portfolio)

基金分离定理

两基金分离定理(two-fund separation theorem): 所有的投资者,不管其喜好、风险偏好、初始财富,都将只持有两个组合(或基金):一个无风险资产和一个最优的风险资产。

最优的风险资产(组合)是什么呢?

最优风险组合

最优风险组合是市场组合(market portfolio)

  • 投资者只会投资最优风险组合,不在最优风险组合中的资产价值为0,因此最优风险组合包括所有证券

  • 处于均衡状态时最优风险组合中个证券所占比例必须等于其市值占市场总市值的比例

  • 供给-需求分析

    • 供给:市场供给的所有证券即为市场组合
    • 需求:投资者只会投资最优风险组合,因此对风险证券的需求为若干份最优风险组合
    • 均衡状态下:供给=需求

数值实验:风险分散


  • 导入必要的库
import random
import pandas as pd
  • 准备数据
df=pd.read_csv('data/TRD_Dalyr.csv',sep='\t')
tic_vec=df.Stkcd.unique()
tic_vec=pd.DataFrame(tic_vec,columns=['tic'])
  • 设定实验参数
n=50
sample_size=1000
x=np.linspace(1,n,n)

Stkcd Trddt Opnprc Hiprc Loprc Clsprc Dretwd
839588 900957 2018-12-24 0.623 0.633 0.622 0.628 0.008026
839589 900957 2018-12-25 0.610 0.626 0.607 0.621 -0.011146
839590 900957 2018-12-26 0.630 0.630 0.617 0.620 -0.001610
839591 900957 2018-12-27 0.631 0.635 0.613 0.617 -0.004839
839592 900957 2018-12-28 0.612 0.623 0.612 0.616 -0.001621

数值实验:风险分散

  • 生成数据
y=[]
for i in x:
    z=[]
    #smpl=pd.DataFrame({'Trddt':[],'Dretwd':[]})
    j=0
    while j < sample_size:
        # j-th sampling
        smpl_tic=tic_vec.sample(int(i),replace=True)
        smpl=pd.DataFrame({'Trddt':[],'Dretwd':[]})
        k=0
        while k<i:
            smpl=smpl.merge(df[['Trddt','Dretwd']]
                            [df.Stkcd==smpl_tic.tic.iloc[k]],
                            how='outer',on='Trddt')
            k+=1
        smpl.fillna(0)
        z.append(np.std(smpl.mean(axis=1,numeric_only=True)))
        j+=1
        #smpl.pivot_table()
    y.append(np.mean(z))
  • 绘图
%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('seaborn-whitegrid')
fig = plt.figure()
ax = plt.axes()
ax.plot(x, y);

风险规避(Risk Aversion)与组合选择

风险规避

对风险的态度

假设投资者面对以下两种选择:

  • [A] 得到$50 (没有不确定性)
  • [B] 参加一项赌博:有50%的机会得到$100,另外50%的机会得到$0

投资者对风险的态度可分为以下几类:

  • 风险偏好(Risk Seeking): 选择B
  • 风险中性(Risk Neutral): 不区分A和B
  • 风险规避(Risk Averse): 选择A

效用理论与无差异曲线

效用与无差异曲线

  • 效用函数被用来刻画投资者对风险的态度

    • 风险规避的投资者的效用函数是凹函数
    • 例子:指数函数、对数函数等
  • 无差异曲线刻画了在维持投资者效用不变的前提下所有风险-收益的集合。投资者不会区分某一条无差异曲线上不同的点所代表的投资机会,因为它们带来的效用是相等的。

无差异曲线与效用

无差异曲线与风险偏好

组合选择

资本分配线(Capital Allocation Line,CAL)

假设投资组合由一个无风险资产和一个风险资产组成。组合的收益和方差为:

由此,我们可以得到\textbf{资本分配线(CAL)},

CAL类似消费者均衡分析中的预算线

假设投资者的效用函数为: .

最优投资者组合(Optimal Investor Portfolio)

Mathematics of MPT

Quadratic Programming (QP)

  • An example of QP

  • The Standard Form

Primal, Lagrangian Function, & Dual

  • Primal

  • The Lagrangian
    • The Lagrangian Function

  • An observatoin

  • The primal can be thought as

  • what about

  • The Dual

  • The Dual for Standard From Primal

  • Strong Duality: Assume one of the primal or dual problem is feasible. Then this problem is bounded if and only if the other one is feasible. In that case both problems have optimal solutions and their optimal values are the same.

Optimality Conditions for QP

The vectors and are optimal solutions to primal and dual problem respectively if and only if and

For a QP in standard form, the optimality conditions can be written as follows:

The Basic Mean-Variance Models

Analytical Solutions

  • Minimum Risk and Characteristic Portfolios
    • the minimum risk problem

  • the characteristic portfolios

  • the minimum-risk portfolio with unit exposure to a vector of attributes a associated with the assets.

  • solution

  • Separation Theorems
    • if no risk-free asset available

There exist two efficient portfolios (funds), namely

  • if one risk-free asset available

there exists a fully invested efficient portfolio (fund) namely

such that every efficient portfolio - that is, every solution for some - is a combination of this portfolio and the risk-free asset.

Generalizations & Implementations

Common Constraints

  • The mean-variance model

  • Other simple constraints

  • Budget constraints, such as fully invested portfolios.
  • Upper and/or lower bounds on the size of individual positions.
  • Upper and/or lower bounds on exposure to industries or sectors

Leverage constraints such as long-only, or 130/30 constraints

  • long-only constraint

  • constraints on the amount of short position (the value of the total short positions to be at most )

Discussion: Can we improve the above constraint?

Turnover constraints: a constraint on the total change in the portfolio positions.

  • initial portfolio:
  • new portfolio:
  • the total turnover (the two-sided turnover):
  • the turnover constraint

  • How to improve the above constraint?

Maximizing the Sharpe Ratio

  • The Sharp Ratio

  • Maximizing the Sharpe Ratio (Non-Convex)

  • Maximizing the Sharpe Ratio (Convex)

Putting Portfolio Theory to Work

  • Estimation of Inputs to Mean–Variance Models

  • Model the sensitivity

  • Solver

  • Performance Analysis

We will cover most of the above topics in later lectures.

CVXPY实验:组合优化

  • Generate data for long only portfolio optimization.
import numpy as np
import scipy.sparse as sp
np.random.seed(1)
n = 10
mu = np.abs(np.random.randn(n, 1))
Sigma = np.random.randn(n, n)
Sigma = Sigma.T.dot(Sigma)
  • Long only portfolio optimization.
import cvxpy as cp
w = cp.Variable(n)
gamma = cp.Parameter(nonneg=True)
ret = mu.T@w 
risk = cp.quad_form(w, Sigma)
prob = cp.Problem(cp.Maximize(ret - gamma*risk), 
               [cp.sum(w) == 1, 
                w >= 0])

  • Compute trade-off curve.
SAMPLES = 100
risk_data = np.zeros(SAMPLES)
ret_data = np.zeros(SAMPLES)
gamma_vals = np.logspace(-2, 3, num=SAMPLES)
for i in range(SAMPLES):
    gamma.value = gamma_vals[i]
    prob.solve()
    risk_data[i] = cp.sqrt(risk).value
    ret_data[i] = ret.value

CVXPY实验:组合优化

  • Plot long only trade-off curve.
import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format = 'svg'

markers_on = [29, 40]
fig = plt.figure()
ax = fig.add_subplot(111)
plt.plot(risk_data, ret_data, 'g-')
for marker in markers_on:
    plt.plot(risk_data[marker], ret_data[marker], 'bs')
    ax.annotate(r"$\gamma = %.2f$" % gamma_vals[marker], 
                xy=(risk_data[marker]+.08, ret_data[marker]-.03))
for i in range(n):
    plt.plot(cp.sqrt(Sigma[i,i]).value, mu[i], 'ro')
plt.xlabel('Standard deviation')
plt.ylabel('Return')
plt.show()

CVXPY实验:组合优化

  • plot return distributions for two points on the trade-off curve.
import scipy.stats as spstats

plt.figure()
for midx, idx in enumerate(markers_on):
    gamma.value = gamma_vals[idx]
    prob.solve()
    x = np.linspace(-2, 5, 1000)
    plt.plot(x, spstats.norm.pdf(x, ret.value, risk.value), 
             label=r"$\gamma = %.2f$" % gamma.value)

plt.xlabel('Return')
plt.ylabel('Density')
plt.legend(loc='upper right')
plt.show()

课后阅读与练习

课后阅读与练习

  • 课后阅读:教材第十三章相关内容(pp221-233)

  • 练习

    • 教材pp232:1-8
    • 在线练习

参考文献

[1] Markowitz H M. Portfolio selection[J]. The Journal of finance, 1952, 7(1): 77-91.
[2] Markowitz H M. Portfolio selection[M]//Portfolio selection. Yale university press, 1968.
[3] Markowitz H M. Mean—variance analysis[M]//Finance. Palgrave Macmillan, London, 1989: 194-198.
[4] Cornuejols G, Tütüncü R. Optimization methods in finance[M]. Cambridge University Press; 2006 Dec 21.
[5] Boyd S, Boyd SP, Vandenberghe L. Convex optimization[M]. Cambridge university press; 2004 Mar 8.

[7] Rubinstein M. Markowitz's" portfolio selection": A fifty-year retrospective[J]. The Journal of finance, 2002, 57(3): 1041-1045.
[10] Li B, Hoi S C H. Online portfolio selection: A survey[J]. ACM Computing Surveys (CSUR), 2014, 46(3): 1-36.
[11] Gomes F. Portfolio choice over the life cycle: A survey[J]. Annual Review of Financial Economics, 2020, 12: 277-304.
[12] Cochrane JH. Asset pricing: Revised edition[M]. Princeton university press; 2009 Apr 11.
[13] Markowitz HM. The early history of portfolio theory: 1600–1960[J]. Financial analysts journal. 1999 Jul 1;55(4):5-16.
[14] Roy AD. Safety first and the holding of assets. Econometrica: Journal of the econometric society. 1952 Jul 1:431-49.

[^1]

[^1]:Kolm P N, Reha Tütüncü, Fabozzi F J. 60 Years of portfolio optimization: Practical challenges and current trends[J]. European Journal of Operational Research, 2014, 234(2):356--371.