Skip to content

Instantly share code, notes, and snippets.

@samsonq
Created July 25, 2023 02:48
Show Gist options
  • Save samsonq/f98ccc92a2dba969082b33270ac4a443 to your computer and use it in GitHub Desktop.
Save samsonq/f98ccc92a2dba969082b33270ac4a443 to your computer and use it in GitHub Desktop.
Black-Litterman Model for Optimal Asset Allocation
import numpy as np
from pypfopt.black_litterman import BlackLittermanModel
from pypfopt.efficient_frontier import EfficientFrontier
from pypfopt import plotting
from openbb_terminal.sdk import openbb
import seaborn as sns
sns.set_theme()
prices = openbb.economy.index(["AAPL", "BBY", "BAC", "SBUX", "T"])
viewdict = {
"AAPL": 0.20,
"BBY": 0.25,
"BAC": 0.10,
"SBUX": 0.2,
"T": 0.15
}
cov_matrix = risk_models.sample_cov(prices)
bl = BlackLittermanModel(
cov_matrix,
absolute_views=viewdict,
pi="equal"
)
rets = bl.bl_returns()
ef = EfficientFrontier(rets, cov_matrix)
weights = bl.bl_weights()
plotting.plot_efficient_frontier(ef, show_tickers=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment