Section 3: Correlation in Stocks

In this lesson, the correlation between data of different companies is discussed.

The stock behavior of companies dealing in similar services is usually related, and this relation can be measured using correlation.

Correlation

Correlation is a statistical technique that determines how strongly two variables are related to each other and how a change in one would affect the other. It can also be defined as a measure of dependence between two or more quantities.

The two types of correlation, in terms of stock behavior, can be described as follows:

  • Positive correlation: The stock value of one company goes up, and in correlation with it, the stock values of other companies also go up.

  • Negative correlation: The stock value of one company goes up, and in correlation with it, the stock values of other companies go down.

Correlation score

For positive correlation, this score is between 0 and 1, and for negative correlation, this score is between -1 and 0 (inclusive). A strong positive correlation has a score above 0.4. It is the same for the negative correlation; it’s strong below a -0.4 value. A score of 1 represents a perfect positive relationship and usually occurs when the correlation is taken with itself.

Correlation for all companies will be calculated with each other to observe what kind of relationship exists between the data of different companies and to see if there is a strong or weak correlation. The closing price and daily return variables will be used as parameters to get the correlation score for every company.

Before starting, all the company files need to be read in as variables, and their Time column needs to be set as the index. The following piece of code does the preprocessing before we actually observe the correlation.

Get hands-on with 1200+ tech skills courses.