avatarB/O Trading Blog

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3325

Abstract

ium.com/v2/resize:fit:800/0*SjKPVAj2IwQ9hWLw.png"><figcaption></figcaption></figure><p id="29e6">In contrast, the Close price of INDEXX Laboratories doesn’t seem to match NASDAQ 100 at all.</p><figure id="e393"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*cmaFUaNuyWKcp0Aw.png"><figcaption></figcaption></figure><h1 id="5458">Scatterplots</h1><p id="6f80">Another way to analyze linear correlation between two values is to plot them out in a scatter plot.</p><p id="e710">When plotting the close prices of Fastenal and the NADAQ 100, we see a clear linear relationship and can imagine a that the dots align with a diagonal line drawn through the center of the dot distribution.</p><figure id="b5a1"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*fb8KJ6YaDgJycuzM.png"><figcaption></figcaption></figure><p id="54d1">When plotting the close prices of INDEXX and the NASDAQ 100, it seems that the dots are randomly scattered around the graph.</p><figure id="a320"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*w1nXPXN76PyHNQOJ.png"><figcaption></figcaption></figure><h1 id="ffce">Pearson Correlation</h1><p id="64b0">So far we have used visualization to assess a relationship between variables but what if we actually want to quantity the relationship in a precise value?</p><p id="35ed">The <a href="https://en.wikipedia.org/wiki/Pearson_correlation_coefficient">Pearson correlation</a> — invented by Karl Pearson — is used to assess the quality of a linear relationship between two sets of data. It is calculated as the covariance of the two variables divided by the product of the standard deviation of each data set.</p><p id="fdca"><i>Pearson’s correlation = covariance(X, Y) / (std(X) * std(Y))</i></p><p id="e9bb">The coefficient that is calculated by this formula returns values between -1 and 1. A value of -1 represents a complete negative correlation, whereas a value of 0 represents no correlation at all and 1 represents full positive correlation.</p><p id="29a4">The Pearson value I calculated for Fastenal is 0.783, so a very close positive correlation. This is also apparent in the heatmap below. The map has a light orange color where Close_1 (Fastenal Close) and Close_2 (NADAQ 100) intersect, which indicates a close positive relation.</p><figure id="fc93"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*oaDlArdgI9KOekYG.png"><figcaption></figcaption></figure><p id="a389">The Pearson coefficient for INDEXX is -0.63 so a slightly negative correlation. This is also indicated in the heatmap by the dark purple color, where Close_1/Close_2 intersect.</p><figure id="f84a"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*B77DZ7T0lz2m_7HL.png"><figcaption></figcaption></figure><h1 id="f1b2">Spearman Correlation</h1><p id="5175">The <a href="https://en.wikipedia.org/wiki/Spearman%27s_rank_correlation_coefficient">Spearman Correlation</a> is another measure of the relationship between two variables or data sets invented by Charles Spearman. It assesses how well the relationship between two variables can be described using a <a href="https://en.wikipedia.org/wiki/Monotonic_function">monotonic function</a>. A monotonic function is a function between dataset that preserves or reverses the given order.</p><p i

Options

d="593f">As with the Pearson correlation coefficient, the scores range between -1 and 1. The meaning of the range is the same as for the Pearson Correlation.</p><p id="fb72">The Spearman Correlation Coefficient is calculated from the relative rank of values on each sample. This is a common approach used in non-parametric statistics, e.g. statistical methods where we do not assume a distribution of the data such as Gaussian.</p><p id="0d9d"><i>Spearman’s correlation coefficient = covariance(rank(X), rank(Y)) / (std(rank(X)) * std(rank(Y)))</i></p><p id="f691">For the Spearman coefficient I calculated for Fastenal was 0.73 so again a high positive correlation.</p><figure id="ddf5"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*n2D4rBtQy4KteJmS.png"><figcaption></figcaption></figure><p id="3c5d">For the Spearman coefficient for INDEXX was -0.122, which represents a slightly negative correlation.</p><figure id="f976"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*Oe6aGdIu7tR2xI2j.png"><figcaption></figcaption></figure><h1 id="5712">Kendall Correlation</h1><p id="6f63">A third method for assessing the relationship between variables is the<a href="https://en.wikipedia.org/wiki/Kendall_rank_correlation_coefficient"> Kendall Correlation rank coefficient</a>, named after Maurice Kendall. It is used to measure the ordinal association between two measured quantities.</p><p id="a526">Here the formula for the Kendall coefficient:</p><figure id="ab90"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*vlfp9IWpNPd3sgVF.png"><figcaption></figcaption></figure><p id="2d67"><i>Source: <a href="https://en.wikipedia.org/wiki/Kendall_rank_correlation_coefficient">Wikipedia</a>, Creative Commons License 3.0</i></p><p id="49e8">The Kendall coefficient for Fastenal is 0.54, which represents a positive correlation.</p><figure id="1934"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*CDkW7ZLUgkKyenm2.png"><figcaption></figcaption></figure><p id="e751">The Kendall coefficient for INDEXX is -0.078, which represents a slightly negative correlation.</p><figure id="f049"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*JQ4YIZfcPWRszx9I.png"><figcaption></figcaption></figure><h1 id="437e">Python Implementation</h1><p id="7976">The analysis above can be implemented using the following Python libraries:</p><ul><li>pandas</li><li>numpy</li><li>sklearn</li><li>yfinance</li><li>pandas_ta.</li></ul><p id="ba2a">If you would like to learn how to code this analysis in Python, look out for my next post.</p><h1 id="85df">Wrapping Up</h1><p id="a992">In this post we looked at the different ways to assess correlation between a market index and different stocks.</p><p id="f750">I hope you found this post worth your time. Thanks for reading.</p><p id="1953">You can support my writing for free <a href="https://medium.com/@chris_42047/membership">using this link</a>. Don’t miss another story — <a href="https://medium.com/subscribe/@chris_42047">subscribe to my stories by email</a>. For more premium content, check out my ‘<a href="https://algorithmictrading.substack.com/">B/O Trading Blog</a>’ on Substack.</p><p id="2799"><i>This post contains affiliate marketing links.</i></p><p id="7e09">Have a great day!</p></article></body>

Correlation between Market Index and Stocks

Photo by Pietro Jeng on Unsplash.com

The other day I wrote a post about analyzing the correlation between two crypto coin prices to see if their price movement is related. In this article I would like to take a slightly different direction and discuss the correlation between a market index and different stocks.

Every trader has experienced the effect of the value of his positions plummeting when there is a sudden market downturn. If we could quantify the correlation between the market and our holdings, it would allow us to diversify our portfolio by picking stocks that have a positive and negative relationship to the market.

Taking things a step further, if we find assets that very closely follow the market trends, we may be able to get signals of trend reversals from the market index and make buy/sell decisions based on those signals.

This story is solely for general information purposes, and should not be relied upon for trading recommendations or financial advice. Source code and information is provided for educational purposes only, and should not be relied upon to make an investment decision. Please review my full cautionary guidance before continuing.

What is Correlation?

Correlation is statistical relation ship of dependence between two variables or data sets. In statistics, correlation often refers to the degree in which two variables are linearly related.

In this post we are going to examine the correlation of prices, in particular the relation between the prices of a market index and different stocks.

Trade Ideas provides AI stock suggestions, AI alerts, scanning, automated trading, real-time stock market data, charting, educational resources, and more. Get a 15% discount with promo code ‘BOTRADING15’.

Visual Correlation

The easiest way to check the correlation of an investment asset to a market index is by plotting them out in a chart. By plotting them together, we can immediately tell if their price movement are closely related, somewhat or not at all.

In the graph below we plotted 1-minute prices of two stocks, INDEXX Laboratories, Inc (IDXX) and Fastenal Corporation (FAST) against the NASDAQ Composite Index. The two stocks are both part of that index.

The price data has been normalized to remove the difference in scale between the prices to be compared.

Here you see that the Close price of Fastenal seems to match the Close price of the NASDAQ 100 index very closely.

In contrast, the Close price of INDEXX Laboratories doesn’t seem to match NASDAQ 100 at all.

Scatterplots

Another way to analyze linear correlation between two values is to plot them out in a scatter plot.

When plotting the close prices of Fastenal and the NADAQ 100, we see a clear linear relationship and can imagine a that the dots align with a diagonal line drawn through the center of the dot distribution.

When plotting the close prices of INDEXX and the NASDAQ 100, it seems that the dots are randomly scattered around the graph.

Pearson Correlation

So far we have used visualization to assess a relationship between variables but what if we actually want to quantity the relationship in a precise value?

The Pearson correlation — invented by Karl Pearson — is used to assess the quality of a linear relationship between two sets of data. It is calculated as the covariance of the two variables divided by the product of the standard deviation of each data set.

Pearson’s correlation = covariance(X, Y) / (std(X) * std(Y))

The coefficient that is calculated by this formula returns values between -1 and 1. A value of -1 represents a complete negative correlation, whereas a value of 0 represents no correlation at all and 1 represents full positive correlation.

The Pearson value I calculated for Fastenal is 0.783, so a very close positive correlation. This is also apparent in the heatmap below. The map has a light orange color where Close_1 (Fastenal Close) and Close_2 (NADAQ 100) intersect, which indicates a close positive relation.

The Pearson coefficient for INDEXX is -0.63 so a slightly negative correlation. This is also indicated in the heatmap by the dark purple color, where Close_1/Close_2 intersect.

Spearman Correlation

The Spearman Correlation is another measure of the relationship between two variables or data sets invented by Charles Spearman. It assesses how well the relationship between two variables can be described using a monotonic function. A monotonic function is a function between dataset that preserves or reverses the given order.

As with the Pearson correlation coefficient, the scores range between -1 and 1. The meaning of the range is the same as for the Pearson Correlation.

The Spearman Correlation Coefficient is calculated from the relative rank of values on each sample. This is a common approach used in non-parametric statistics, e.g. statistical methods where we do not assume a distribution of the data such as Gaussian.

Spearman’s correlation coefficient = covariance(rank(X), rank(Y)) / (std(rank(X)) * std(rank(Y)))

For the Spearman coefficient I calculated for Fastenal was 0.73 so again a high positive correlation.

For the Spearman coefficient for INDEXX was -0.122, which represents a slightly negative correlation.

Kendall Correlation

A third method for assessing the relationship between variables is the Kendall Correlation rank coefficient, named after Maurice Kendall. It is used to measure the ordinal association between two measured quantities.

Here the formula for the Kendall coefficient:

Source: Wikipedia, Creative Commons License 3.0

The Kendall coefficient for Fastenal is 0.54, which represents a positive correlation.

The Kendall coefficient for INDEXX is -0.078, which represents a slightly negative correlation.

Python Implementation

The analysis above can be implemented using the following Python libraries:

  • pandas
  • numpy
  • sklearn
  • yfinance
  • pandas_ta.

If you would like to learn how to code this analysis in Python, look out for my next post.

Wrapping Up

In this post we looked at the different ways to assess correlation between a market index and different stocks.

I hope you found this post worth your time. Thanks for reading.

You can support my writing for free using this link. Don’t miss another story — subscribe to my stories by email. For more premium content, check out my ‘B/O Trading Blog’ on Substack.

This post contains affiliate marketing links.

Have a great day!

Python Trading
Trading
Automatic Trading
Algorithmic Trading
Recommended from ReadMedium