马科维茨均值方差模型

马科维茨均值方差模型

  1. 马科维茨均值-方差模型为多目标优化问题,有效前沿即多目标优化问题的pareto解(风险一定,收益最大;收益一定,风险最小)
  2. 马科维茨模型以预期收益率期望度量收益,以收益率方差度量风险
  3. 收益与风险计算函数portstats
  4. 函数语法:PortRisk
  5. PortReturn=portstats(ExpReturn,ExpCovariance,PortWts)
  6. 输入:ExpReturn,ExpCovariance,PortWts:资产预期收益率、资产的协方差矩阵、资产权重
  7. 输出:PortRisk,PortReturn:资产组合风险(标准差)、资产组合预期收益(期望)
  8. 例如:计算三家企业股票组合的风险与收益
  9. 代码如下:
import numpy as np


def portstats(ExpReturn, ExpCovariance, PortWts):
    PortRisk = 0
    for i in range(len(PortWts)):
        for j in range(len(PortWts)):
            PortRisk += PortWts[i] * PortWts[j] * ExpCovariance[i * 3 + j]
    PortReturn = np.sum(ExpReturn * np.array(PortWts))
    return np.sqrt(PortRisk), PortReturn

# 组合中每个证券的预期收益率
ExpReturn = [0.000540, 0.000275, 0.000236]
# 组合中证券的协方差矩阵
ExpCovariance = 0.0001 * np.array([5.27, 2.80, 1.74,
                                   2.80, 4.26, 1.67,
                                   1.74, 1.67, 2.90])
ExpCovariance = ExpCovariance.tolist()
# 组合中每个证券的初始权重(初始投资金额)/初始总金额

PortWts = 1.0 / 3 * np.array([1, 1, 1])
PortWts = PortWts.tolist()
# 调用portstats函数
[PortRisk, PortReturn] = portstats(ExpReturn, ExpCovariance, PortWts)
print(PortRisk, PortReturn)
# 计算结果

  1. 结果如下:

风险(标准差) PortRisk= 0.016617
组合收益率 PortReturn= 3.5033e-004

  1. 接下来进行有效前沿的计算
  2. 有效前沿计算函数PortRisk, PortReturn,PortWts=frontcon(ExpReturn,ExpCovariance,NumPorts,PortReturn,AssetBounds, Groups,GroupBounds,varargin)
  3. ExpReturn,ExpCovariance 资产预期收益率、资产的协方差矩阵
  4. NumPorts, 有效前沿上输出点的小数,默认为10
  5. AssetBounds 每种资产权重的上下限
  6. Groups,GroupBounds,varargin 资产分组、每个资产群约束
  7. 代码如下:
import numpy as np
import matplotlib.pyplot as plt  # 绘图
import scipy.optimize as sco


def frontcon(ExpReturn, ExpCovariance, NumPorts):
    noa = len(ExpReturn)

    def statistics(weights):
        weights = np.array(weights)
        z = np.dot(ExpCovariance, weights)
        x = np.dot(weights, z)
        port_returns = (np.sum(ExpReturn * weights.T))
        port_variance = np.sqrt(x)
        num1 = port_returns / port_variance
        return np.array([port_returns, port_variance, num1])

    # 定义一个函数对 方差进行最小化
    def min_variance(weights):
        return statistics(weights)[1]

    bnds = tuple((0, 1) for x in range(noa))
    # 在不同目标收益率水平(target_returns)循环时,最小化的一个约束条件会变化。
    target_returns = np.linspace(min(ExpReturn), max(ExpReturn), NumPorts)
    target_variance = []
    PortWts = []
    for tar in target_returns:
        # 在最优化时采用两个约束,1.给定目标收益率,2.投资组合权重和为1。
        cons = ({
     'type': 'eq', 'fun': lambda x: statistics(x)[0] - tar}, {
     'type': 'eq', 'fun': lambda x: np.sum(x) - 1})
        res = sco.minimize(min_variance, noa * [1. / noa, ], method='SLSQP', bounds=bnds, constraints=cons)
        target_variance.append(res['fun'])

        PortWts.append(res["x"])
    target_variance = np.array(target_variance)
    return [target_variance, target_returns, PortWts]


ExpReturn = np.array([0.00540, 0.00275, 0.00236])
ExpCovariance = 0.0001 * np.array([[5.27, 2.80, 1.74],
                                   [2.80, 4.26, 1.67],
                                   [1.74, 1.67, 2.90]])
NumPorts = 10

[target_variance, target_returns, PortWts] = frontcon(ExpReturn, ExpCovariance, NumPorts)

plt.plot(target_variance, target_returns)
plt.title("Mean-Variance-Efficient Frontier")
plt.xlabel("Risk(Standard Deviation)")
plt.ylabel("Expected Return")
plt.show()


  1. 画图如下:
    马科维茨均值方差模型_第1张图片
  2. 今天分享就到这,多学习,勤分享。

你可能感兴趣的:(python,人工智能,动态规划,矩阵,机器学习)