← Back to Projects

Diversified Stock Portfolio Using Clustering Analysis

S&P 500 portfolio construction using K-means clustering on risk/return features.

Overview

This project constructs diversified stock portfolios from the S&P 500 using unsupervised learning. Historical price data is used to compute risk and return features for each stock; K-means clustering groups stocks with similar behavior. Portfolio construction then selects top stocks by Sharpe ratio from each cluster to achieve diversification across clusters and validate performance via backtesting against the S&P 500 index.

Data & Features

Data sources: S&P 500 constituent list and historical stock prices (US equity data fetched via the pipeline). The first 70% of the history is used for model building; the remaining 30% is reserved for validation.

Features used for clustering (all derived from historical data):

Clustering Features

Correlation with S&P 500 index (price correlation).

Beta: sensitivity of stock returns to index returns.

Annualized return (from daily returns, 252-day year).

Annualized volatility (standard deviation of daily returns, annualized).

Sharpe ratio (annualized return / annualized volatility).

Daily change in price (open-to-close) and daily variation (high-to-low), annualized.

Features are scaled (z-score) before clustering. The optimal number of clusters is chosen using the within-cluster sum of squares (elbow method); we use K = 4.

Clustering

K-means is run on the normalized feature matrix with multiple random starts. Cluster membership ensures the portfolio spans different behavior groups rather than concentrating in one segment of the risk/return space.

Portfolio Construction

Two portfolio variants are built:

(1) Diversified by cluster: Within each cluster, stocks are ranked by Sharpe ratio; the top 5 from each of the 4 clusters form a 20-stock portfolio (equal weight per stock).

(2) Top-20 by Sharpe: The top 20 stocks by Sharpe ratio across the full universe, without cluster constraints.

Both are equal-weighted. The diversified-by-cluster portfolio reduces concentration in a single risk/return profile.

Validation & Backtesting

Validation uses the holdout period (last 30% of the data). Daily returns are computed for the portfolio (equal-weighted average of constituent returns) and for the S&P 500 index.

Cumulative returns are plotted for the cluster-based portfolio, the top-20 Sharpe portfolio, and the S&P 500 to compare risk-adjusted performance.

Results

Summary

Stocks used: 199
Clusters: 4
Train period: 2021-03-222024-09-17
Validation period: 2024-09-182026-03-20
Computed: 3/23/2026, 3:00:27 AM

Cluster statistics

ClusterCountMean returnMean volMean SharpeMean beta
146-1.67%29.60%-0.0450.749
24628.84%31.20%0.9461.040
321-6.51%52.69%-0.1211.684
4868.88%25.38%0.3750.795

Elbow plot: within-cluster sum of squares

Used to choose the number of clusters K; the “elbow” suggests a good K (here we use K=4).

Return vs volatility by cluster

Each point is a stock (train-period annualized return vs annualized volatility), colored by cluster.

Cluster-wise metrics

Mean annualized return, volatility, and Sharpe ratio by cluster.

Mean return

Mean volatility

Mean Sharpe ratio

Feature correlation matrix

Correlation between clustering features (used for K-means). Helps check redundancy.

ann_returnann_volann_sharpe…ann_daily_…ann_daily_…betacor
ann_return1.00-0.170.93-0.67-0.15-0.120.61
ann_vol-0.171.00-0.310.320.970.82-0.12
ann_sharpe…0.93-0.311.00-0.65-0.27-0.220.64
ann_daily_…-0.670.32-0.651.000.350.16-0.56
ann_daily_…-0.150.97-0.270.351.000.79-0.12
beta-0.120.82-0.220.160.791.000.17
cor0.61-0.120.64-0.56-0.120.171.00

Portfolios

By cluster: top 5 by Sharpe in each of 4 clusters (equal weight).
Top 20: top 20 stocks by Sharpe ratio overall (equal weight).

Portfolio by cluster (symbols)

DVN, CF, CTRA, CPT, EVRG, CEG, ACGL, COST, AJG, ANET, EQT, AMD, APA, CRWD, DDOG, BRK-B, CB, KO, ED, ATO

Top 20 by Sharpe (symbols)

CEG, ACGL, COST, AJG, ANET, CTAS, FICO, AFL, COR, AVGO, CBOE, ABBV, BLDR, BRO, AZO, ETN, BRK-B, BSX, ERIE, CB

Validation: cumulative returns

Equal-weight portfolio vs S&P 500 index over the validation (holdout) period.