{"id":192189,"date":"2023-06-21T11:24:31","date_gmt":"2023-06-21T15:24:31","guid":{"rendered":"https:\/\/ibkrcampus.com\/?p=192189"},"modified":"2023-06-21T11:24:29","modified_gmt":"2023-06-21T15:24:29","slug":"python-correlation-a-practical-guide","status":"publish","type":"post","link":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/python-correlation-a-practical-guide\/","title":{"rendered":"Python Correlation &#8211; A Practical Guide"},"content":{"rendered":"\n<p><em>The post &#8220;<a href=\"https:\/\/algotrading101.com\/learn\/python-correlation-guide\/\">Python Correlation &#8211; A Practical Guide<\/a>&#8221; first appeared on AlgoTrading101 Blog.<\/em><\/p>\n\n\n\n<p><em>Excerpt<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-correlation\">What is correlation?<\/h2>\n\n\n\n<p>A correlation is a relationship between two sets of data.<\/p>\n\n\n\n<p>In the equity markets, for example, you may notice that stocks like Microsoft (MSFT) and Apple (AAPL) both tend to rise and fall at the same time. The price behavior between the two stocks is not an exact match, but there is enough similarity to say there is a relationship. In this scenario, we can say MSFT and AAPL have a positive correlation.<\/p>\n\n\n\n<p>Further, there are often relationships across markets. For example, between equities and bonds, or precious metals. We often also see a correlation between financial instruments and economic data or even sentiment indicators.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"why-do-correlations-matter\">Why do correlations matter?<\/h2>\n\n\n\n<p>There are several reasons why correlations are important, here a few benefits of tracking them in the markets \u2013<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Insights<\/strong>&nbsp;\u2013 keeping track of different relationships can provide insight into where the markets are headed. A good example is when the markets turned sharply lower in late February as a result of the Coronavirus escalation. The price of gold, which is known as an asset investors turn to when their mood for risky investments sours, rose sharply the trading day before the big initial drop in stocks. It acted as a warning signal for those equity traders mindful of the inverse correlation between the two.<\/li>\n\n\n\n<li><strong>Strength in correlated moves<\/strong>&nbsp;\u2013 It\u2019s much easier to assess trends when there is a correlated move. In other words, if a bulk of the tech stocks on your watchlist are rising, it\u2019s probably safe to say the sector is bullish, or that there is strong demand.<\/li>\n\n\n\n<li><strong>Diversification<\/strong>&nbsp;\u2013 To make sure you have some diversification in your portfolio, it\u2019s a good idea to make sure the assets within it aren\u2019t all strongly correlated to each other.<\/li>\n\n\n\n<li><strong>Signal confirmation<\/strong>&nbsp;\u2013 Let\u2019s say you want to buy a stock because your analysis shows that it is bullish. You could analyze another stock with a positive correlation to make sure it provides a similar signal.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"correlation-doesn't-imply-causation\">Correlation doesn\u2019t imply causation<\/h2>\n\n\n\n<p>A popular saying among the statistics crowd is \u201ccorrelation does not imply causation\u201d. It comes up often and it\u2019s important to understand its meaning.<\/p>\n\n\n\n<p>Essentially, correlations can provide valuable insights but you\u2019re bound to come across situations that might imply a correlation where a relationship does not exist.<\/p>\n\n\n\n<p>As an example, data has shown a sharp rise in Netflix subscribers as a result of the lockdown that followed the Coronavirus escalation. The idea is that people are forced to stay at home and therefore are more likely to watch tv.<\/p>\n\n\n\n<p>The same scenario has resulted in a rise in electricity bills. People are using more electricity at home compared to when they were at work all day.<\/p>\n\n\n\n<p>If you were blindly comparing the rise in Netflix subscribers versus the rise in electricity usage during the month of lockdown, you might reasonably conclude that the two have a relationship.<\/p>\n\n\n\n<p>However, having some perspective on the manner, it is clear that the two are not related and that it is not likely that fluctuations in one will impact the other moving forward. Rather, it is the lockdown, an external variable, that is the causation for both of these trends.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-a-correlation-coefficient\">What is a correlation coefficient?<\/h2>\n\n\n\n<p>We\u2019ve discussed that fluctuations in the stock prices of Apple and Microsoft tend to have a relationship. You might then notice other tech companies also correlate well with the two.<\/p>\n\n\n\n<p>But not all relationships are equal and the correlation coefficient can help in assessing the strength of a correlation.<\/p>\n\n\n\n<p>There are a few different ways of calculating a correlation coefficient but the most popular methods result in a number between -1 and +1.<\/p>\n\n\n\n<p>The closer the number is to +1, the stronger the relationship. If the figure is close to -1, it indicates that there is a strong inverse relationship.<\/p>\n\n\n\n<p>In the finance world, an inverse relationship is where one asset rises while the other drops. As one of the previous examples suggested, stocks and the price of gold have a long-standing inverse relationship.<\/p>\n\n\n\n<p>The closer the correlation coefficient is to zero, the more likely it is that the two variables being compared don\u2019t have any relationship to each other.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Breaking down the math to calculate the correlation coefficient<\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"719\" height=\"94\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-1.png\" alt=\"\" class=\"wp-image-192243 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-1.png 719w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-1-700x92.png 700w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-1-300x39.png 300w\" data-sizes=\"(max-width: 719px) 100vw, 719px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 719px; aspect-ratio: 719\/94;\" \/><\/figure>\n\n\n\n<p>The above formula is what\u2019s used to calculate a correlation coefficient using the Pearson method.<\/p>\n\n\n\n<p>It might look a bit intimidating the first time you look at it (unless you\u2019re a math pro ofcourse). But we will break down this formula and at the end of it you will see that it is just basic mathematics.<\/p>\n\n\n\n<p>There are libraries available that can do this automatically, but the following example will show how we can make the calculation manually.<\/p>\n\n\n\n<p>We will start by creating a dataset. We can use the Numpy library to create some random data for us. Here is the code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import numpy as np\nimport pandas as pd\ndf = pd.DataFrame(np.random.randint(0,10,size=(5, 2)), columns=list('xy'))<\/pre>\n\n\n\n<p>The image below shows what my DataFrame looks like. If you\u2019re following along, the data will look different for you as Numpy is filling in random numbers. But the format should look the same.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"704\" height=\"309\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-14.png\" alt=\"\" class=\"wp-image-192262 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-14.png 704w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-14-700x307.png 700w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-14-300x132.png 300w\" data-sizes=\"(max-width: 704px) 100vw, 704px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 704px; aspect-ratio: 704\/309;\" \/><\/figure>\n\n\n\n<p>Now that we have a dataset, let\u2019s move on to the formula. We will start by separating out the first part of the formula.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"717\" height=\"81\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-2.png\" alt=\"\" class=\"wp-image-192245 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-2.png 717w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-2-700x79.png 700w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-2-300x34.png 300w\" data-sizes=\"(max-width: 717px) 100vw, 717px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 717px; aspect-ratio: 717\/81;\" \/><\/figure>\n\n\n\n<p>We can break this down further. Remember BODMAS? it states that we must perform what is in the brackets first.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"723\" height=\"67\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-3.png\" alt=\"\" class=\"wp-image-192246 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-3.png 723w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-3-700x65.png 700w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-3-300x28.png 300w\" data-sizes=\"(max-width: 723px) 100vw, 723px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 723px; aspect-ratio: 723\/67;\" \/><\/figure>\n\n\n\n<p>For the formula above, we need to take each value of x and subtract it by the mean of x.<\/p>\n\n\n\n<p>We can use the&nbsp;<code>mean()<\/code>&nbsp;function in Pandas to create the mean for us. Like this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">df.x.mean()<\/pre>\n\n\n\n<p>But we still need to subtract the mean from x. And we also need to temporarily store this information somewhere. Let\u2019s create a new column for that and call it step1.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">df['step1'] = df.x - df.x.mean()<\/pre>\n\n\n\n<p>This is what our DataFrame looks like at this point.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"735\" height=\"282\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-15.png\" alt=\"\" class=\"wp-image-192263 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-15.png 735w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-15-700x269.png 700w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-15-300x115.png 300w\" data-sizes=\"(max-width: 735px) 100vw, 735px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 735px; aspect-ratio: 735\/282;\" \/><\/figure>\n\n\n\n<p>Now that we have the calculations needed for the first step. Let\u2019s keep going.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"721\" height=\"71\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-4.png\" alt=\"\" class=\"wp-image-192249 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-4.png 721w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-4-700x69.png 700w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-4-300x30.png 300w\" data-sizes=\"(max-width: 721px) 100vw, 721px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 721px; aspect-ratio: 721\/71;\" \/><\/figure>\n\n\n\n<p>The second step involves doing the same thing for the y column.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">df['step2'] = df.y - df.y.mean()<\/pre>\n\n\n\n<p>That\u2019s easy enough, what\u2019s next?<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"717\" height=\"69\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-5.png\" alt=\"\" class=\"wp-image-192251 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-5.png 717w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-5-700x67.png 700w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-5-300x29.png 300w\" data-sizes=\"(max-width: 717px) 100vw, 717px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 717px; aspect-ratio: 717\/69;\" \/><\/figure>\n\n\n\n<p>The formula is telling us that we need to take all the values we gathered in step 1 and multiply them by the values in step 2. We will store this in a new column labeled step3.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">df['step3'] = df.step1 * df.step2<\/pre>\n\n\n\n<p>This is what the DataFrame looks like at this point:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"755\" height=\"283\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-16.png\" alt=\"\" class=\"wp-image-192265 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-16.png 755w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-16-700x262.png 700w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-16-300x112.png 300w\" data-sizes=\"(max-width: 755px) 100vw, 755px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 755px; aspect-ratio: 755\/283;\" \/><\/figure>\n\n\n\n<p>We can now move on to the last operation in this part of the formula.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"717\" height=\"71\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-6.png\" alt=\"\" class=\"wp-image-192253 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-6.png 717w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-6-700x69.png 700w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-6-300x30.png 300w\" data-sizes=\"(max-width: 717px) 100vw, 717px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 717px; aspect-ratio: 717\/71;\" \/><\/figure>\n\n\n\n<p>if you\u2019re not familiar with this symbol:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"747\" height=\"66\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-7.png\" alt=\"\" class=\"wp-image-192254 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-7.png 747w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-7-700x62.png 700w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-7-300x27.png 300w\" data-sizes=\"(max-width: 747px) 100vw, 747px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 747px; aspect-ratio: 747\/66;\" \/><\/figure>\n\n\n\n<p>It stands for sum. This means we need to add up all the values from the previous step.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">step4 = df.step3.sum()<\/pre>\n\n\n\n<p>Great, we have summed up the values and have stored it in a variable called step4. We will come back to this later. For now, we can start on the second part of the formula.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"728\" height=\"98\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-8.png\" alt=\"\" class=\"wp-image-192255 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-8.png 728w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-8-700x94.png 700w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-8-300x40.png 300w\" data-sizes=\"(max-width: 728px) 100vw, 728px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 728px; aspect-ratio: 728\/98;\" \/><\/figure>\n\n\n\n<p>Let\u2019s follow the same steps and break down the formula.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"724\" height=\"57\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-10.png\" alt=\"\" class=\"wp-image-192258 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-10.png 724w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-10-700x55.png 700w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-10-300x24.png 300w\" data-sizes=\"(max-width: 724px) 100vw, 724px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 724px; aspect-ratio: 724\/57;\" \/><\/figure>\n\n\n\n<p>Does this look familiar? we have already done this in step 1 so we can just use that data.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"722\" height=\"61\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-9.png\" alt=\"\" class=\"wp-image-192256 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-9.png 722w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-9-700x59.png 700w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-9-300x25.png 300w\" data-sizes=\"(max-width: 722px) 100vw, 722px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 722px; aspect-ratio: 722\/61;\" \/><\/figure>\n\n\n\n<p>The next part of the formula tells us we have to square the results from step 1. We will store this data in a new column labeled step5.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">df['step5'] = df.step1 ** 2<\/pre>\n\n\n\n<p>The next part of the formula tells us to do the same thing for the y values.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"715\" height=\"64\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-11.png\" alt=\"\" class=\"wp-image-192259 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-11.png 715w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-11-700x63.png 700w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-11-300x27.png 300w\" data-sizes=\"(max-width: 715px) 100vw, 715px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 715px; aspect-ratio: 715\/64;\" \/><\/figure>\n\n\n\n<p>We can take the values that we created in step 2 and square them.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">df['step6'] = df.step2 ** 2<\/pre>\n\n\n\n<p>This is what our DataFrame looks like at this point:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"754\" height=\"326\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-17.png\" alt=\"\" class=\"wp-image-192266 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-17.png 754w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-17-700x303.png 700w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-17-300x130.png 300w\" data-sizes=\"(max-width: 754px) 100vw, 754px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 754px; aspect-ratio: 754\/326;\" \/><\/figure>\n\n\n\n<p>Let\u2019s look at the next part of the formula:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"727\" height=\"69\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-12.png\" alt=\"\" class=\"wp-image-192260 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-12.png 727w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-12-700x66.png 700w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-12-300x28.png 300w\" data-sizes=\"(max-width: 727px) 100vw, 727px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 727px; aspect-ratio: 727\/69;\" \/><\/figure>\n\n\n\n<p>This tells us that we have to take the sum of what we did in step 5 and multiply it with the sum of what we did in step 6.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">step7 = df.step5.sum() * df.step6.sum()<\/pre>\n\n\n\n<p>Let\u2019s keep going, almost there!<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"731\" height=\"110\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-13.png\" alt=\"\" class=\"wp-image-192261 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-13.png 731w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-13-700x105.png 700w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-13-300x45.png 300w\" data-sizes=\"(max-width: 731px) 100vw, 731px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 731px; aspect-ratio: 731\/110;\" \/><\/figure>\n\n\n\n<p>The last portion of this part is to simply take the square root of the figure from our previous step. We can use the Numpy library to calculate the square root.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">step8 = np.sqrt(step7)<\/pre>\n\n\n\n<p>Now that we\u2019ve done that, all that is left is to take the answer from the first part of the formula and divide it by the answer in the second part.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">step4\/step8<\/pre>\n\n\n\n<p>And there you have it, we\u2019ve manually calculated a correlation coefficient. To make sure that the calculation is correct, we can will use the&nbsp;<code>corr()<\/code>&nbsp;function which is built into Pandas to calculate the coefficient.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">df.x.corr(df.y)<\/pre>\n\n\n\n<p>Here is our final result. Your correlation coefficient will be different, but it should match the output from the Pandas calculation.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"725\" height=\"176\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-18.png\" alt=\"\" class=\"wp-image-192268 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-18.png 725w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-18-700x170.png 700w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-correlation-algotrading101-18-300x73.png 300w\" data-sizes=\"(max-width: 725px) 100vw, 725px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 725px; aspect-ratio: 725\/176;\" \/><\/figure>\n\n\n\n<p><em>Visit <a href=\"https:\/\/algotrading101.com\/learn\/python-correlation-guide\/\">AlgoTrading101<\/a>&nbsp;to read how to calculate the correlation coefficients for watchlist in Python.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A correlation is a relationship between two sets of data.<\/p>\n","protected":false},"author":816,"featured_media":192271,"comment_status":"open","ping_status":"closed","sticky":true,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[339,343,349,338,341],"tags":[806,4582,1225,1224,595,15443],"contributors-categories":[13746],"class_list":{"0":"post-192189","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-science","8":"category-programing-languages","9":"category-python-development","10":"category-ibkr-quant-news","11":"category-quant-development","12":"tag-data-science","13":"tag-dataframe","14":"tag-numpy","15":"tag-pandas","16":"tag-python","17":"tag-python-correlation","18":"contributors-categories-algotrading101"},"pp_statuses_selecting_workflow":false,"pp_workflow_action":"current","pp_status_selection":"publish","acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.9 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Python Correlation &#8211; A Practical Guide | IBKR Quant<\/title>\n<meta name=\"description\" content=\"A correlation is a relationship between two sets of data.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/192189\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Correlation - A Practical Guide | IBKR Campus US\" \/>\n<meta property=\"og:description\" content=\"A correlation is a relationship between two sets of data.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/python-correlation-a-practical-guide\/\" \/>\n<meta property=\"og:site_name\" content=\"IBKR Campus US\" \/>\n<meta property=\"article:published_time\" content=\"2023-06-21T15:24:31+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-binary-text-background.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"563\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Jignesh Davda\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jignesh Davda\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\n\t    \"@context\": \"https:\\\/\\\/schema.org\",\n\t    \"@graph\": [\n\t        {\n\t            \"@type\": \"NewsArticle\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/python-correlation-a-practical-guide\\\/#article\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/python-correlation-a-practical-guide\\\/\"\n\t            },\n\t            \"author\": {\n\t                \"name\": \"Jignesh Davda\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/a671d09b2158fcd7c22596b7049c75df\"\n\t            },\n\t            \"headline\": \"Python Correlation &#8211; A Practical Guide\",\n\t            \"datePublished\": \"2023-06-21T15:24:31+00:00\",\n\t            \"mainEntityOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/python-correlation-a-practical-guide\\\/\"\n\t            },\n\t            \"wordCount\": 1426,\n\t            \"commentCount\": 0,\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/python-correlation-a-practical-guide\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2023\\\/06\\\/python-binary-text-background.jpg\",\n\t            \"keywords\": [\n\t                \"Data Science\",\n\t                \"Dataframe\",\n\t                \"NumPy\",\n\t                \"Pandas\",\n\t                \"Python\",\n\t                \"Python Correlation\"\n\t            ],\n\t            \"articleSection\": [\n\t                \"Data Science\",\n\t                \"Programming Languages\",\n\t                \"Python Development\",\n\t                \"Quant\",\n\t                \"Quant Development\"\n\t            ],\n\t            \"inLanguage\": \"en-US\",\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"CommentAction\",\n\t                    \"name\": \"Comment\",\n\t                    \"target\": [\n\t                        \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/python-correlation-a-practical-guide\\\/#respond\"\n\t                    ]\n\t                }\n\t            ]\n\t        },\n\t        {\n\t            \"@type\": \"WebPage\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/python-correlation-a-practical-guide\\\/\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/python-correlation-a-practical-guide\\\/\",\n\t            \"name\": \"Python Correlation - A Practical Guide | IBKR Campus US\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\"\n\t            },\n\t            \"primaryImageOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/python-correlation-a-practical-guide\\\/#primaryimage\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/python-correlation-a-practical-guide\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2023\\\/06\\\/python-binary-text-background.jpg\",\n\t            \"datePublished\": \"2023-06-21T15:24:31+00:00\",\n\t            \"description\": \"A correlation is a relationship between two sets of data.\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"ReadAction\",\n\t                    \"target\": [\n\t                        \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/python-correlation-a-practical-guide\\\/\"\n\t                    ]\n\t                }\n\t            ]\n\t        },\n\t        {\n\t            \"@type\": \"ImageObject\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/python-correlation-a-practical-guide\\\/#primaryimage\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2023\\\/06\\\/python-binary-text-background.jpg\",\n\t            \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2023\\\/06\\\/python-binary-text-background.jpg\",\n\t            \"width\": 1000,\n\t            \"height\": 563,\n\t            \"caption\": \"Python\"\n\t        },\n\t        {\n\t            \"@type\": \"WebSite\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"name\": \"IBKR Campus US\",\n\t            \"description\": \"Financial Education from Interactive Brokers\",\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"SearchAction\",\n\t                    \"target\": {\n\t                        \"@type\": \"EntryPoint\",\n\t                        \"urlTemplate\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/?s={search_term_string}\"\n\t                    },\n\t                    \"query-input\": {\n\t                        \"@type\": \"PropertyValueSpecification\",\n\t                        \"valueRequired\": true,\n\t                        \"valueName\": \"search_term_string\"\n\t                    }\n\t                }\n\t            ],\n\t            \"inLanguage\": \"en-US\"\n\t        },\n\t        {\n\t            \"@type\": \"Organization\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\",\n\t            \"name\": \"Interactive Brokers\",\n\t            \"alternateName\": \"IBKR\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"logo\": {\n\t                \"@type\": \"ImageObject\",\n\t                \"inLanguage\": \"en-US\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\",\n\t                \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"width\": 669,\n\t                \"height\": 669,\n\t                \"caption\": \"Interactive Brokers\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\"\n\t            },\n\t            \"publishingPrinciples\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/about-ibkr-campus\\\/\",\n\t            \"ethicsPolicy\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/cyber-security-notice\\\/\"\n\t        },\n\t        {\n\t            \"@type\": \"Person\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/a671d09b2158fcd7c22596b7049c75df\",\n\t            \"name\": \"Jignesh Davda\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/author\\\/jignesh-davda\\\/\"\n\t        }\n\t    ]\n\t}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Python Correlation &#8211; A Practical Guide | IBKR Quant","description":"A correlation is a relationship between two sets of data.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/192189\/","og_locale":"en_US","og_type":"article","og_title":"Python Correlation - A Practical Guide | IBKR Campus US","og_description":"A correlation is a relationship between two sets of data.","og_url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/python-correlation-a-practical-guide\/","og_site_name":"IBKR Campus US","article_published_time":"2023-06-21T15:24:31+00:00","og_image":[{"width":1000,"height":563,"url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-binary-text-background.jpg","type":"image\/jpeg"}],"author":"Jignesh Davda","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Jignesh Davda","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"NewsArticle","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/python-correlation-a-practical-guide\/#article","isPartOf":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/python-correlation-a-practical-guide\/"},"author":{"name":"Jignesh Davda","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/a671d09b2158fcd7c22596b7049c75df"},"headline":"Python Correlation &#8211; A Practical Guide","datePublished":"2023-06-21T15:24:31+00:00","mainEntityOfPage":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/python-correlation-a-practical-guide\/"},"wordCount":1426,"commentCount":0,"publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"image":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/python-correlation-a-practical-guide\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-binary-text-background.jpg","keywords":["Data Science","Dataframe","NumPy","Pandas","Python","Python Correlation"],"articleSection":["Data Science","Programming Languages","Python Development","Quant","Quant Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/python-correlation-a-practical-guide\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/python-correlation-a-practical-guide\/","url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/python-correlation-a-practical-guide\/","name":"Python Correlation - A Practical Guide | IBKR Campus US","isPartOf":{"@id":"https:\/\/ibkrcampus.com\/campus\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/python-correlation-a-practical-guide\/#primaryimage"},"image":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/python-correlation-a-practical-guide\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-binary-text-background.jpg","datePublished":"2023-06-21T15:24:31+00:00","description":"A correlation is a relationship between two sets of data.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/python-correlation-a-practical-guide\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/python-correlation-a-practical-guide\/#primaryimage","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-binary-text-background.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-binary-text-background.jpg","width":1000,"height":563,"caption":"Python"},{"@type":"WebSite","@id":"https:\/\/ibkrcampus.com\/campus\/#website","url":"https:\/\/ibkrcampus.com\/campus\/","name":"IBKR Campus US","description":"Financial Education from Interactive Brokers","publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ibkrcampus.com\/campus\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/ibkrcampus.com\/campus\/#organization","name":"Interactive Brokers","alternateName":"IBKR","url":"https:\/\/ibkrcampus.com\/campus\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","width":669,"height":669,"caption":"Interactive Brokers"},"image":{"@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/"},"publishingPrinciples":"https:\/\/www.interactivebrokers.com\/campus\/about-ibkr-campus\/","ethicsPolicy":"https:\/\/www.interactivebrokers.com\/campus\/cyber-security-notice\/"},{"@type":"Person","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/a671d09b2158fcd7c22596b7049c75df","name":"Jignesh Davda","url":"https:\/\/www.interactivebrokers.com\/campus\/author\/jignesh-davda\/"}]}},"jetpack_featured_media_url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-binary-text-background.jpg","_links":{"self":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/192189","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/users\/816"}],"replies":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/comments?post=192189"}],"version-history":[{"count":0,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/192189\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media\/192271"}],"wp:attachment":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media?parent=192189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/categories?post=192189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/tags?post=192189"},{"taxonomy":"contributors-categories","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/contributors-categories?post=192189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}