Skip to content

About

Team May the Code Be With You presents a Bayesian machine learning portfolio optimization project combining Black-Litterman, Hidden Markov Models, FinBERT-based NLP sentiment beliefs, and out-of-sample backtesting. Contributors: Jenny Lee, Jack Morgan, Noah Tan, Ryusei Leon Nakano, and Nomunbileg Sukhbold.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Priors Over Portfolios: Machine-Learning-Enhanced Black-Litterman Portfolio Optimization

Course: Bayesian Machine Learning
Team: May the Code Be With You
Application Area: Finance / Portfolio Optimization

Project Summary

This project builds a Bayesian portfolio optimization pipeline that combines market-implied prior returns with machine-learned investor views. The core model is the Black-Litterman framework, which updates equilibrium market expectations using uncertain beliefs generated by two models:

  1. Hidden Markov Model (HMM) - learns market regimes from historical asset returns.
  2. NLP sentiment model - fine-tunes FinBERT on financial tweet sentiment and converts ticker-level sentiment into return beliefs.

The final posterior expected returns are used to optimize a long-only portfolio and are compared against three baselines:

  • Equal-weight portfolio
  • Market-cap-weighted prior portfolio
  • Markowitz mean-variance portfolio

The main question is:

Can ML-enhanced Bayesian portfolio optimization improve allocation decisions by reconciling market priors with HMM and NLP-based investor views?


Motivation

Traditional mean-variance optimization is sensitive to noisy expected return estimates. The Black-Litterman model addresses this by starting from market-implied equilibrium returns, then updating those priors with investor views and view uncertainty.

Instead of manually specifying views, this project generates them from machine learning:

  • The HMM captures latent market regimes such as growth, defensive, or high-volatility periods.
  • The NLP model captures retail and news sentiment from financial tweets.
  • The Black-Litterman layer acts as the Bayesian reconciliation mechanism when the two signals disagree.

This makes the project both a portfolio optimization problem and a Bayesian machine learning application.


Asset Universe

The final implementation uses five equities:

Ticker Company
AAPL Apple
MSFT Microsoft
TSLA Tesla
TSM Taiwan Semiconductor Manufacturing
PG Procter & Gamble

These assets provide a mix of growth/technology exposure and a defensive consumer staples asset.


Repository Structure

.
├── NLP Beliefs/
│   ├── nlp_absolute_beliefs.csv
│   └── nlp_relative_beliefs.csv
├── assets/
│   ├── bl_allocation_performance.png
│   ├── finbert_confusion_matrix.png
│   ├── hmm_market_regimes.png
│   ├── hmm_monte_carlo_simulations.png
│   ├── nlp_beliefs.png
│   ├── nlp_beliefs_heatmap.png
│   ├── nlp_finbert_confusion_matrix.png
│   ├── nlp_sentiment_distribution.png
│   ├── nlp_sentiment_timeseries.png
│   ├── sentiment_score.png
│   └── sentiment_score_time_series.png
├── Bayesian_final_HMM_beliefs.ipynb
├── Bayesian_final_NLP_beliefs.ipynb
├── Black_Litterman.ipynb
├── README.md
├── hmm_absolute_beliefs.csv
├── hmm_relative_beliefs.csv
└── stock_tweets.csv

Data Sources

Market Data

Daily adjusted closing prices were downloaded using yfinance for the selected ticker universe. The price data was transformed into:

  • Daily returns
  • Log returns for HMM training
  • Annualized covariance matrix
  • Market-capitalization benchmark weights
  • In-sample and out-of-sample return windows

Text Data

The NLP model uses financial tweet data for sentiment inference.

  • Fine-tuning dataset: Twitter Financial News Sentiment (HuggingFace)
  • Inference dataset: web-scraped stock tweets from 2021-2022 (Kaggle)
  • Final ticker-matched tweet rows: 63,423

Tweet sentiment is converted into a scalar score in the range [-1, 1], where positive values indicate bullish sentiment and negative values indicate bearish sentiment.


Methodology

1. Market Prior Construction

The Black-Litterman prior is based on market-implied equilibrium returns:

$$ \Pi = \delta * \Sigma * w_{mkt} $$

Where:

  • $\Pi$ = market-implied expected returns
  • $\delta$ = risk-aversion coefficient
  • $\Sigma$ = annualized covariance matrix
  • $w_{mkt}$ = market-capitalization weights

Market-Implied Weights

Ticker Market Weight
AAPL 38.64%
MSFT 26.37%
PG 2.84%
TSLA 13.90%
TSM 18.25%

Market-Implied Prior Returns

Ticker Prior Expected Return
AAPL 22.81%
MSFT 20.53%
PG 6.27%
TSLA 37.93%
TSM 21.24%

2. NLP Sentiment Beliefs

The NLP component fine-tunes FinBERT on labeled financial tweet data, then applies the fine-tuned model to ticker-matched stock tweets.

NLP Pipeline

  1. Load Twitter Financial News Sentiment dataset (for fine-tuning).
  2. Remap labels into FinBERT convention: positive, negative, neutral.
  3. Fine-tune FinBERT: adapt to tweet-domain financial language.
  4. Evaluate fine-tuned FinBERT against base FinBERT.
  5. Run inference on 2021-2022 stock tweets --> per-tweet sentiment score.
  6. Aggregate sentiment by ticker and month: mean sentiment, std, mention volume
  7. Convert sentiment into absolute and relative Black-Litterman views.

FinBERT Evaluation

We fine-tuned FinBERT's classification head on the financial tweet-domain labeled data. We compared the fine-tuned model with the base model on the test set. Metrics: per-class F1, macro-F1, accuracy, and confusion matrix.

The fine-tuned model improved accuracy by about 14.82 percentage points and macro F1 by about 17.05 percentage points.

Model Accuracy Macro F1
Base FinBERT 0.7303 0.6690
Fine-tuned FinBERT 0.8786 0.8395

Base vs Fine-tuned FinBERT Confusion Matrices

Sentiment Inference

We first score every tweet with the fine-tuned FinBERT. Each tweet is scored as a sentiment scalar:

score = P(positive) - P(negative) ∈ [-1, +1]

We aggregated sentiment by ticker and calendar period (monthly).

For each bucket we compute:

  • mean_sentiment: mean sentiment score across all tweets in the period
  • sentiment_std: standard deviation (uncertainty / Omega)
  • n_mentions: number of tweets (low volume = higher uncertainty)

NLP Sentiment Distribution

Monthly Sentiment by Ticker

Final NLP Absolute Beliefs

We collapse the time-series aggregation into a single set of beliefs over the full dataset window.

  • ProbabilityPositive: Volume-weighted soft probability of positive returns
  • Confidence: Directional conviction derived from the distance of ProbabilityPositive from neutrality
  • Expected Return: sentiment score scaled to rough return units using a calibration factor SENTIMENT_RETURN_SCALE (default 0.15 = ±15% annual)
Ticker Expected Return Probability Positive Confidence Total Mentions
AAPL 1.16% 72.55% 45.10% 12,107
MSFT 1.63% 78.26% 56.51% 5,984
TSLA 1.34% 77.23% 54.47% 44,211
TSM 3.11% 87.11% 74.21% 601
PG 0.36% 56.78% 13.55% 517

Final NLP Relative Views

Next, we generate relative beliefs. For every pair (A, B) we compute:

  • MeanRelativeReturn: scaled sentiment differential between A and B
  • StdRelativeReturn: propagated uncertainty from both assets
  • ProbabilityOutperform: P(sentiment_A > sentiment_B) across time periods

Only pairs where |MeanRelativeReturn| > VIEW_THRESHOLD become active Black-Litterman views.

View Mean Relative Return Probability Outperform
TSM over PG 2.64% 84.64%
MSFT over PG 1.40% 72.81%
TSLA over PG 1.06% 70.21%
AAPL over PG 0.93% 67.44%
TSM over AAPL 1.71% 75.69%
TSM over TSLA 1.57% 68.98%

NLP Absolute and Relative Beliefs

NLP Interpretation

The NLP signal is useful but imperfect. In hindsight, we observe that Tesla had extremely high tweet volume and remained strongly positive despite a large decline during the 2022 bear market, suggesting retail-investor optimism bias.

This is exactly where Black-Litterman is useful: it can include the sentiment view without allowing it to completely dominate the portfolio, because every view is assigned uncertainty.


3. HMM Market Regime Beliefs

The HMM component uses historical log returns from 2018-2025 to learn latent market states.

HMM Setup

Parameter Value
Number of hidden states 4
Training period 2018-01-01 to 2025-01-01
Simulations 1,000
Forecast horizon 252 trading days
Random seed 42

The model converged after 47 iterations.

Hidden State Counts

State Count
0 234
1 86
2 1,259
3 181

Hidden Market Regimes

Monte Carlo Simulation

After fitting the HMM, the model simulated 1,000 future price paths over 252 trading days. These simulations were used to estimate expected returns, probability of positive return, and confidence for each asset.

AAPL Monte Carlo Simulations

Final HMM Absolute Beliefs

Ticker Expected Return Probability Positive Confidence
AAPL 36.88% 80.20% 60.40%
MSFT 32.17% 79.20% 58.40%
TSLA 15.60% 72.50% 45.00%
TSM 91.29% 76.10% 52.20%
PG 37.56% 77.20% 54.40%

Strongest HMM Relative Views

View Mean Relative Return Probability Outperform
TSM over PG 53.72% 64.10%
AAPL over TSLA 21.27% 68.10%
MSFT over TSLA 16.57% 65.10%
AAPL over MSFT 4.70% 53.00%
TSM over TSLA 75.68% 69.40%

4. Black-Litterman View Integration

The Black-Litterman model combines:

  • Market-implied prior returns
  • HMM absolute and relative beliefs
  • NLP absolute and relative beliefs
  • View uncertainty matrix $\Omega$

The model uses the standard posterior update:

$$ E[R] = \Pi + \tau * \Sigma * P.T * inv(P * \tau * \Sigma * P.T + \Omega) * (Q - P * \Pi) $$

Where:

  • P = picking matrix that identifies assets in each view
  • Q = view return vector
  • $\Omega$ = diagonal covariance matrix of view uncertainty
  • $\tau$ = prior uncertainty scaling parameter

A total of 30 views were integrated:

  • 10 HMM relative views
  • 5 HMM absolute views
  • 10 NLP relative views
  • 5 NLP absolute views
+------------------+      +--------------------+
|  yfinance Data   |      | ML Belief CSVs     |
| (Prices, Mcaps)  |      | (HMM & NLP Models) |
+--------+---------+      +---------+----------+
         |                          |
         v                          v
+--------+---------+      +---------+----------+
|  Market Prior    |      | Views & Confidence |
|    (Pi, Sigma)   |      |    (P, Q, Omega)   |
+--------+---------+      +---------+----------+
         |                          |
         +------------+-------------+
                      |
                      v
          +-----------+-----------+
          |  Black-Litterman Core |
          |   (Posterior Returns) |
          +-----------+-----------+
                      |
                      v
          +-----------+-----------+
          |  Markowitz Optimizer  |
          |   (Portfolio Weights) |
          +-----------+-----------+
                      |
                      v
          +-----------+-----------+
          | OOS Performance Metrics|
          |(Sharpe, Drawdown, etc.)|
          +-----------------------+

5. Posterior Expected Returns

The Black-Litterman posterior compressed aggressive growth expectations and increased the defensive expected return for PG.

Ticker Market Prior Return BL Posterior Return
AAPL 27.58% 14.35%
MSFT 24.64% 14.75%
PG 8.70% 13.16%
TSLA 42.11% 14.30%
TSM 23.61% 15.93%

This shift shows the Bayesian update moving the portfolio away from high-volatility growth exposure and toward a more defensive allocation.


Portfolio Optimization

After computing posterior returns, we used a long-only maximum Sharpe ratio optimizer with the constraint:

sum(weights) = 1, weights >= 0

Allocation Comparison

Ticker Equal Weight Market Prior Markowitz MV ML-Enhanced BL
AAPL 20.00% 37.98% 18.63% 0.00%
MSFT 20.00% 27.71% 13.64% 7.69%
PG 20.00% 2.77% 45.90% 68.13%
TSLA 20.00% 13.56% 21.84% 0.00%
TSM 20.00% 17.98% 0.00% 24.18%

The ML-enhanced BL strategy heavily tilted toward PG because PG had lower volatility while its posterior return became comparable to the growth assets.


Backtesting Results

Strategy Annual Return Annual Volatility Sharpe Ratio Max Drawdown Turnover Allocation Stability
Equal Weight 31.89% 22.19% 1.437 -29.11% 0.5137 inf
Market Prior 33.04% 23.21% 1.423 -30.83% 0.5137 68.97
Markowitz MV 20.21% 19.07% 1.060 -24.96% 1.0281 44.78
ML-Enhanced BL 18.06% 14.46% 1.249 -14.86% 1.4311 15.22

Allocation and Performance Comparison


Key Findings

1. Black-Litterman acted as a risk-control engine, delivering strong downside protection

The ML-enhanced BL portfolio achieved the lowest volatility (14.46%) and max drawdown (-14.86%) of all four strategies, which is less than half the drawdown of Equal-Weight (-29.11%) and Market-Prior (-30.83%). Note that a risk-adjusted framework is designed to survive downside, not chase upside.

2. The ML signals identified a defensive environment

The HMM and NLP models flagged elevated uncertainty across the asset universe, and the BL framework translated those signals into a more conservative allocation by accepting lower returns (18.06%) in exchange for substantially reduced drawdown relative to all three baselines.

3. Sharpe ratios were competitive, but not dominant.

ML-Enhanced-BL (1.249) outperformed Markowitz-MV (1.060) on a risk-adjusted basis but trailed Equal-Weight (1.437) and Market-Prior (1.423). The gap relative to the simpler benchmarks is worth acknowledging; the ML framework reduced risk but did not generate sufficient alpha to fully compensate on a Sharpe basis over this particular out-of-sample window.

4. Black-Litterman successfully reconciled conflicting signals

With views from two very different model types (regime-based HMM and sentiment-based NLP), the BL framework produced a coherent, investable allocation. The high turnover (1.43) suggests the posterior was sensitive to shifting signals month-to-month, which addressable with view damping or weight caps in future iterations.

5. FinBERT fine-tuning demonstrated meaningful predictive improvement

The fine-tuned model improved classification accuracy by 14.8 percentage points and macro F1 by 17 points over base FinBERT. The fact that the resulting sentiment signal reflected retail optimism bias in TSLA is an interesting finding, as it confirms that tweet-based sentiment captures investor sentiment, not just noise. Thus, calibrating view confidence is a promising direction for future work.


Limitations

  • The asset universe is small, with only five equities.
  • The NLP signal is sensitive to tweet volume and retail-investor bias.
  • HMM simulation results can be sensitive to the number of hidden states and training window.
  • The optimized ML-BL portfolio became highly concentrated in PG.
  • Transaction costs were not explicitly deducted from returns.
  • The out-of-sample period was relatively short.

Future Work

  • Expand the asset universe to 20-30 equities.
  • Add sector ETFs and macro variables such as VIX, Treasury yields, and inflation data.
  • Use rolling-window backtesting instead of a single train/test split.
  • Add transaction costs and rebalance frequency constraints.
  • Calibrate view confidence using historical predictive accuracy.
  • Test alternative NLP models such as FinBERT variants, DeBERTa, or LLM-based sentiment scoring.
  • Limit portfolio concentration using maximum asset-weight constraints.
  • Compare against a standard Black-Litterman model with manually specified views.

Team Contributions

Team Member Main Responsibility Contribution
Jenny Lee NLP beliefs Built the NLP sentiment pipeline, fine-tuned FinBERT, generated ticker-level sentiment scores, and exported NLP absolute/relative Black-Litterman views.
Jack Morgan HMM beliefs Built the Hidden Markov Model pipeline, trained market regime detection, ran Monte Carlo simulations, and exported HMM absolute/relative views.
Noah Tan Black-Litterman Implemented the Black-Litterman integration layer, constructed prior/posterior expected returns, parsed view matrices, and built the optimization engine.
Ryusei Leon Nakano Backtesting, GitHub Helped evaluate strategies using out-of-sample performance metrics, organized project materials, and prepared the repository/presentation structure.
Nomunbileg Sukhbold Backtesting, GitHub Helped evaluate strategy performance, organize project outputs, write documentation, and prepare the final GitHub repository/presentation.

Conclusion

This project shows how Bayesian machine learning can be used to combine market priors with uncertain model-generated views. The HMM and NLP models produced different types of signals: one based on market regimes and one based on sentiment. The Black-Litterman framework provided a principled way to combine those signals into posterior expected returns.

The final ML-enhanced BL portfolio did not produce the highest return or Sharpe ratio, but it substantially reduced volatility and maximum drawdown. This suggests that the framework was most effective as a risk-management and capital-preservation system, rather than an aggressive return-maximization strategy.

About

Team May the Code Be With You presents a Bayesian machine learning portfolio optimization project combining Black-Litterman, Hidden Markov Models, FinBERT-based NLP sentiment beliefs, and out-of-sample backtesting. Contributors: Jenny Lee, Jack Morgan, Noah Tan, Ryusei Leon Nakano, and Nomunbileg Sukhbold.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages