18

Machine Learning is Stocks. Pre-Processing for Unsupervised Company Classificati...

 3 years ago
source link: https://towardsdatascience.com/machine-learning-is-stocks-pre-processing-for-unsupervised-company-classification-8f35ce8343f?gi=56a3186a4186
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

This article the idea of building a new, data driven classification of companies based on their financials, instead of the type of business they do.

The premise of this study is based on the idea that great stocks to buy would be superior among their peers. In this context, I define a peer group as a group of companies that have similar financial structure (e.g. small cap, lots of debt, profitable).

If you haven’t read an introduction, I suggest you read my preface to this study here:

Bringing Up To Speed

In theprevious article, I’ve selected 5 dimensions to define financial structure of a company:

  • Profitability — to capture whether the company makes money or not
  • Total Assets — to capture company size
  • Debt to Equity Ratio — to capture financing structure
  • Operating Margin — to capture the difficulty of making profits
  • Earnings per Share Growth (EPS) — to capture year-to-year growth

I’ve also made sure that these dimensions are independent. This is an important consideration since I want to measure how a company is doing at each independent front regardless of its performance elsewhere. Here is a Spearman Correlation plot I’ve shown earlier:

YFJraaR.png!web

Financial Data from Vhinny

Data Exploration

Before I go into clustering, let’s evaluate distributions for these 5 features. Understanding the distributions is crucial to select an appropriate clustering algorithm. It will also surface potential flows in the data which I will handle during pre-processing.

Below are the distributions of all five (5) features presented on a single plot:

MRbQjyZ.png!web

Distributions for Select Features

The X axis shows the value of each feature. The Y axis shows the number of companies falling into each corresponding bin. For this analysis, each company is presented once with its financials reported in 2019. The total number of companies used in this study is ~4,000.

There are few observations that may attract one’s attention:

  • Negative Debt to Equity
  • Surprisingly high proportion Debt to Equity below 0.5
  • Skewed distributions of Assets and Debt to Equity

Negative Debt to Equity is uncommon, but possible. While debt cannot be negative, equity can be negative if total liabilities outgrow company’s total assets.

Skewed Distributionsare also okay, except we have to remember this when we choose a clustering algorithm. Some algorithms rely on the assumption of normal distribution, which would not hold here.

Most of the Debt to Equity ratios below 0.5 are indeed concerning. Lots of public companies rely heavily on debt financing which often outweighs stockholders’ equity. Upon further investigation, I found a lot of 0’s in this feature, suggesting potential flows in the data extraction process. Looking at all the features I’ve chosen, I would expect them all, except profitability, to be non-zero.

Having replaced all 0’s with nulls, we see significant change in the Debt to Equity ratios. The other features were not significantly affected:

vUfaQjA.png!web

Distributions for Select Features

This diagram is the same as the one before, except dark blue charts were constructed using only non-zero values . Having placed the blue charts on top of the red charts, the only tangible difference is in the Debt to Equity feature. I will trust the blue going forward, relying on my business sense.

Choosing the Clustering Algorithm

Clustering exercise generally is half art, half science. Since there is no consistent metric to evaluate performance of the algorithm, many algorithms are often tried before a working solution is found. While some distance measure may be used for comparison, it is still fairly abstract. A good overview of popular clustering methods may be foundhere.

The method I’ve chosen for this study is K-means clustering due to its computational efficiency, O(n) , and relatively trivial configuration. The only decision I have to make is for the number of clusters , which can be determined experimentally in a quite a streamlined way.

Final Steps Before I Cluster

During the Data Exploration phase, we’ve seen the skewed distributions for Assets and Debt to Equity features. Since K-means algorithm is based on finding optimal means in the data, skewed distributions are dangerous because outliers may pull these means uncontrollably in their direction.

One way to deal with this is to remove outliers. Since 4,000 examples is not an excessive amount of data, removing data points is not desirable. Instead, I can enforce max and min values in the distribution to reduce their distance to the mean.

One way to do this is to use quantiles and interquartile distance to cap the min and max values in the distribution. In this case, I’ve used

  • Q1–3*IQR to cap min
  • Q3+3*IQR to cap max

where Q1 and Q3 and the first (25%) and the third (75%) quantiles and IQR is the interquartile range. I’ve arbitrarily chosen a multiple of 3 for the IQR too keep the caps relatively far away from the median to not significantly disturb the original distribution.

Below is a sample code for this operation to better illustrate this approach.

This capping procedure was applied to all the features in the dataset. The resulting distributions for Assets and Debt to Equity ratio are shown below:

jauq63m.png!web

ai6Nja3.png!web

The Effect of Capping Tails of the Original Distribution

Next Steps

This concludes data exploration and pre-processing phases of this analysis. In the next article, I will determine the optimal number of clusters based on the data and identify characteristic profiles for the companies.

By the way …

Let’s Connect!

I’m happy to connect with people who share my path towards financial independence . If you also search for financial independence or if you’d like to collaborate , bounce ideas or exchange thoughts, please reach out! Here are some places to find me:

Cheers!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK