daily update
This commit is contained in:
208
to_explore/pyquantnews/54_BlackLitterman.ipynb
Normal file
208
to_explore/pyquantnews/54_BlackLitterman.ipynb
Normal file
@ -0,0 +1,208 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "63831470",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"<div style=\"background-color:#000;\"><img src=\"pqn.png\"></img></div>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "535538d4",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"This code utilizes the Black-Litterman model to incorporate subjective views into the portfolio optimization process. It fetches price data for selected assets, constructs a covariance matrix, and sets up absolute views on asset returns. The Black-Litterman model is then used to compute new expected returns, followed by the construction of an efficient frontier. This approach helps in creating a more informed and optimized portfolio by blending market equilibrium with investor views."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "30dc6f4f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "6c1d4af5",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from pypfopt.black_litterman import BlackLittermanModel\n",
|
||||
"from pypfopt.efficient_frontier import EfficientFrontier\n",
|
||||
"from pypfopt import risk_models, plotting"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "293a45be",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from openbb_terminal.sdk import openbb\n",
|
||||
"import seaborn as sns\n",
|
||||
"sns.set_theme()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "60664575",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Fetch price data for selected assets from the OpenBB terminal"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "02c6c09c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"prices = openbb.economy.index([\"AAPL\", \"BBY\", \"BAC\", \"SBUX\", \"T\"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "9081783b",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Define absolute views on the expected returns for specific assets"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "5d9cb1ca",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"viewdict = {\n",
|
||||
" \"AAPL\": 0.20, \n",
|
||||
" \"BBY\": 0.30,\n",
|
||||
" \"BAC\": 0.10,\n",
|
||||
" \"SBUX\": 0.2,\n",
|
||||
" \"T\": 0.15\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "a3a6f94a",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Construct the sample covariance matrix using historical price data"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "2d0e2e01",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"cov_matrix = risk_models.sample_cov(prices)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "500bebeb",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Initialize the Black-Litterman model with equal weight priors and absolute views"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "6db6f3b2",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"bl = BlackLittermanModel(\n",
|
||||
" cov_matrix, \n",
|
||||
" absolute_views=viewdict,\n",
|
||||
" pi=\"equal\"\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "b3ac9e80",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Compute the implied expected returns using the Black-Litterman model and initialize Efficient Frontier"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "024947c8",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"rets = bl.bl_returns()\n",
|
||||
"ef = EfficientFrontier(rets, cov_matrix)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "644d3dec",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Plot the efficient frontier showing the possible portfolios"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "86ea1335",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"plotting.plot_efficient_frontier(ef, show_tickers=True)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "aba68864",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Calculate and display the optimal weights for the portfolio"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "ef1c2d68",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"bl.bl_weights()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "8bbae896",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"<a href=\"https://pyquantnews.com/\">PyQuant News</a> is where finance practitioners level up with Python for quant finance, algorithmic trading, and market data analysis. Looking to get started? Check out the fastest growing, top-selling course to <a href=\"https://gettingstartedwithpythonforquantfinance.com/\">get started with Python for quant finance</a>. For educational purposes. Not investment advise. Use at your own risk."
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"jupytext": {
|
||||
"cell_metadata_filter": "-all",
|
||||
"main_language": "python",
|
||||
"notebook_metadata_filter": "-all"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
Reference in New Issue
Block a user