使用 Monte Carlo 进行投资组合优化,但必须满足约束条件
Using Monte Carlo for portfolio optimisation but must satisfy constraints
我在尝试找到投资组合权重并在满足约束的同时优化它们时遇到问题。
我需要一些帮助来设计一个代码,使我能够在满足一系列约束的同时模拟多个权重数组,请参阅下面的示例以获取解释:
问题 - 在满足约束条件下模拟各种权重:
仪器 = ['Fixed Income', 'Equity 1', 'Equity 2', 'Multi-Asset', 'Cash']
限制条件:
- 每个权重在0.1到0.4之间
- 现金 = 0.05
- 权益 1 小于 0.3
目前我有代码:
import numpy as np:
instruments = ['Fixed Income', 'Equity 1', 'Equity 2', 'Multi-Asset', 'Cash']
weights = np.random.uniform(0.1, 0.4, len(instruments)) # random number generation
weights = weights / weights.sum() # normalise weights
# I have done test runs, and normalised weights always fit the constraints
if weights[-1] > 0.03:
excess = weights[-1] - 0.03
# distribute excess weights equally
weights[:-1] = weights[:-1] + excess / (len(weights) - 1)
我被卡住了,我也意识到当我分配多余的重量时,我实际上已经打破了我的约束。
有办法吗?我必须通过 monte-carlo
感谢大家的帮助。
这是一种解决方案:
import numpy as np
N = 1000000
instruments = ['Fixed Income', 'Equity 1', 'Equity 2', 'Multi-Asset', 'Cash']
# each row is set of weights in order of instruments above
weights = np.zeros(shape=(N, len(instruments)))
weights[:, -1] = 0.05
weights[:, 1] = np.random.uniform(0, 0.3, N)
cols = (0, 2, 3)
# fill columns with random numbers
for col in cols[0:-1]:
w_remaining = 1 - np.sum(weights, axis=1)
weights[:, col] = np.random.uniform(0.1, 0.4, N)
# the last column is constrained for normalization
weights[:, cols[-1]] = 1 - np.sum(weights, axis=1)
# select only rows that meet condition:
cond1 = np.all(0.1 <= weights[:, cols], axis=1)
cond2 = np.all(weights[:, cols] <= 0.4, axis=1)
valid_rows = cond1*cond2
weights = weights[valid_rows, :]
# verify sum of weights == 1:
np.testing.assert_allclose(np.sum(weights, axis=1), 1)
此解决方案是高效的,但会丢弃不满足约束条件的生成示例。
在我的 PoV 中,使用 montecarlo 模拟进行投资组合优化的效率非常低,因为即使使用 sobol 序列,当你增加资产数量时问题也很难解决。
另一方面,大多数投资组合优化问题都是二次或线性规划问题,因此您可以使用 cvxpy 来解决它,但对每个问题建模都需要时间。最后,您可以尝试 Riskfolio-Lib 一个基于 cvxpy 的库,它简化了投资组合优化模型的实施,甚至有一种格式来实施资产 类 约束,就像您正在尝试实施的那样。
您可以查看此 link 中的第一个示例:https://riskfolio-lib.readthedocs.io/en/latest/examples.html
我在尝试找到投资组合权重并在满足约束的同时优化它们时遇到问题。 我需要一些帮助来设计一个代码,使我能够在满足一系列约束的同时模拟多个权重数组,请参阅下面的示例以获取解释:
问题 - 在满足约束条件下模拟各种权重:
仪器 = ['Fixed Income', 'Equity 1', 'Equity 2', 'Multi-Asset', 'Cash']
限制条件:
- 每个权重在0.1到0.4之间
- 现金 = 0.05
- 权益 1 小于 0.3
目前我有代码:
import numpy as np:
instruments = ['Fixed Income', 'Equity 1', 'Equity 2', 'Multi-Asset', 'Cash']
weights = np.random.uniform(0.1, 0.4, len(instruments)) # random number generation
weights = weights / weights.sum() # normalise weights
# I have done test runs, and normalised weights always fit the constraints
if weights[-1] > 0.03:
excess = weights[-1] - 0.03
# distribute excess weights equally
weights[:-1] = weights[:-1] + excess / (len(weights) - 1)
我被卡住了,我也意识到当我分配多余的重量时,我实际上已经打破了我的约束。
有办法吗?我必须通过 monte-carlo
感谢大家的帮助。
这是一种解决方案:
import numpy as np
N = 1000000
instruments = ['Fixed Income', 'Equity 1', 'Equity 2', 'Multi-Asset', 'Cash']
# each row is set of weights in order of instruments above
weights = np.zeros(shape=(N, len(instruments)))
weights[:, -1] = 0.05
weights[:, 1] = np.random.uniform(0, 0.3, N)
cols = (0, 2, 3)
# fill columns with random numbers
for col in cols[0:-1]:
w_remaining = 1 - np.sum(weights, axis=1)
weights[:, col] = np.random.uniform(0.1, 0.4, N)
# the last column is constrained for normalization
weights[:, cols[-1]] = 1 - np.sum(weights, axis=1)
# select only rows that meet condition:
cond1 = np.all(0.1 <= weights[:, cols], axis=1)
cond2 = np.all(weights[:, cols] <= 0.4, axis=1)
valid_rows = cond1*cond2
weights = weights[valid_rows, :]
# verify sum of weights == 1:
np.testing.assert_allclose(np.sum(weights, axis=1), 1)
此解决方案是高效的,但会丢弃不满足约束条件的生成示例。
在我的 PoV 中,使用 montecarlo 模拟进行投资组合优化的效率非常低,因为即使使用 sobol 序列,当你增加资产数量时问题也很难解决。
另一方面,大多数投资组合优化问题都是二次或线性规划问题,因此您可以使用 cvxpy 来解决它,但对每个问题建模都需要时间。最后,您可以尝试 Riskfolio-Lib 一个基于 cvxpy 的库,它简化了投资组合优化模型的实施,甚至有一种格式来实施资产 类 约束,就像您正在尝试实施的那样。
您可以查看此 link 中的第一个示例:https://riskfolio-lib.readthedocs.io/en/latest/examples.html