# The must-have machine learning technique for financial market data analysis

# The must-have machine learning technique for financial market data analysis

Machine learning, although powerful, isn't a crystal ball for financial markets. The biggest mistake people make is believing that inputting price data into an algorithm will generate foolproof predictions.

While machine learning is not always great for predicting prices, it is still useful. Quants use regression for factor analysis, natural language processing for sentiment analysis, deep neural networks for image analysis, and reinforcement learning for synthetic time series analysis.

Today, you’ll use KMeans to cluster stocks together for trading and risk management.

## The must-have machine learning technique for financial market data analysis

KMeans clustering is an unsupervised machine learning algorithm that works by creating clusters from a dataset and assigning each data point to its closest cluster. It was developed in the 1950s by Stuart Lloyd and later refined by J. MacQueen in 1967.

KMeans clustering can be used to identify stocks that are similar in terms of their performance and risk profile. By clustering stocks, investors can create more diversified portfolios, remove correlated assets, and identify candidates for pairs trading strategies. KMeans clustering can also be used to identify stocks that are undervalued or overvalued relative to their peers.

By understanding KMeans clustering, investors can make more informed decisions about which stocks to invest in and how to allocate their portfolios.

And the best part?

You can use KMeans clustering like the professionals with Python.

Here's how.

## Imports and set up

First, start with the imports. You need pandas for manipulating data, scikit-learn to fit the KMeans model, Matplotlib for plotting, and OpenBB to get market data.

```
1from math import sqrt
2
3import pandas as pd
4from sklearn.cluster import KMeans
5import matplotlib.pyplot as plt
6plt.rc("font", size=10)
7
8from openbb_terminal.sdk import openbb
```

Now, use pandas to read an HTML table from Wikipedia. The table has a list of the Dow Jones stocks which we’ll use for the analysis.

```
1dji = (
2 pd.read_html('https://en.wikipedia.org/wiki/Dow_Jones_Industrial_Average')[1]
3)
4symbols = dji.Symbol.tolist()
5data = openbb.stocks.ca.hist(
6 symbols,
7 start_date="2020-01-01",
8 end_date="2022-12-31"
9)
```

This code parses out the symbols and downloads price data using the OpenBB SDK.

## Data preprocessing

We will use KMeans to cluster stocks together based on their returns and volatility.

```
1moments = (
2 data
3 .pct_change()
4 .describe()
5 .T[["mean", "std"]]
6 .rename(columns={"mean": "returns", "std": "vol"})
7) * [252, sqrt(252)]
```

This is a compact pandas statement that uses chaining. First, compute the percent change to get the daily returns. Then use the pandas describe method to get a DataFrame of summary statistics. Transpose it and select the mean and standard deviation. Then rename the columns and annualize then. You end up with a list of Dow Jones stocks, their annualized mean and standard deviation.

## Do KMeans clustering

The first step is to measure inertia. Inertia measures how well a dataset was clustered by KMeans. It’s calculated by measuring the distance between each data point and its centroid, squaring this distance, and summing these squares across one cluster. Using inertia, you can determine the optimal number of clusters, where adding another cluster doesn't significantly reduce the inertia.

```
1sse = []
2for k in range(2, 15):
3 kmeans = KMeans(n_clusters=k, n_init=10)
4 kmeans.fit(moments)
5 sse.append(kmeans.inertia_)
6
7plt.plot(range(2, 15), sse)
8plt.title("Elbow Curve");
```

The result is a smooth, downward sloping chart. You can estimate where adding another cluster doesn’t significantly reduce the inertia. It looks like it’s around five or six.

Next, build and plot the clusters.

```
1kmeans = KMeans(n_clusters=5, n_init=10).fit(moments)
2plt.scatter(
3 moments.returns,
4 moments.vol,
5 c=kmeans.labels_,
6 cmap="rainbow",
7);
8
9plt.title("Dow Jones stocks by return and volatility (K=5)")
10for i in range(len(moments.index)):
11 txt = f"{moments.index[i]} ({kmeans.labels_[i]})"
12 xy = tuple(moments.iloc[i, :] + [0, 0.01])
13 plt.annotate(txt, xy)
```

First, fit the model to the data using five clusters. Then plot the points and annotate each one with the ticker symbol and its cluster.

It’s clear to see how stocks are grouped together. You can use this analysis to diversify stock portfolios by reducing exposure to stocks in similar clusters. KMeans is also a great way to select potential pairs trading candidates by identifying which stocks are economically linked.

Machine learning is not a black box that will print you money in the market. But used correctly, it’s a useful tool to add to your toolkit.