# 目的関数を設定する prob += pulp.lpSum([weights[a] * returns[a] for a in assets])
# 制約条件を設定する prob += pulp.lpSum([weights[a] for a in assets]) == 1 prob += pulp.lpSum([weights[a] * risks[a] for a in assets]) <= 0.06
# 最適化問題を解く prob.solve()
# 結果を表示する print('Optimal Portfolio:') for a in assets: print('{}: {:.2%}'.format(a, weights[a].value()))
print('Expected Return: {:.2%}'.format(pulp.value(prob.objective))) print('Risk: {:.2%}'.format(pulp.lpSum([weights[a] * risks[a] for a in assets]).value()))