Stock price information can be gathered from market-tracking websites, such as Bloomberg and Yahoo! Finance. Organize your returns as a sequence when you have your data, recording the two stocks in question as stock X and stock Y to simplify your calculations. For example, your data for stock X might be 0. 9, 1. 3, 1. 7, 0. 4, 0. 7 over five days, while the data for Y is 2. 5, 3. 5, 3. 6, 3. 1, 2. 3. Correlation coefficients can vary or even switch signs over time (from positive to negative), so the period of time you choose is important. Short-term traders may be fine using 20 or 50 days’ worth of data, but longer-term investors will want to use 150 or 250. [2] X Research source

Continuing with the previous example, the number of days, n, would be 5. This means that the mean of X’s returns would be μx=0. 9+1. 3+1. 7+0. 4+0. 75{\displaystyle \mu _{x}={\frac {0. 9+1. 3+1. 7+0. 4+0. 7}{5}}}, or 1. 0. Similarly, Y’s returns would averageμy=2. 5+3. 5+3. 6+3. 1+2. 35{\displaystyle \mu _{y}={\frac {2. 5+3. 5+3. 6+3. 1+2. 3}{5}}}, or 3. 0.

In the formula, Xn{\displaystyle X_{n}} and Yn{\displaystyle Y_{n}} represent the stock’s return on each day in the period. The idea is to sum up the product of the differences between the stock return and mean return for each day. For example, the part of the covariance formula for first day would be calculated as: (0. 9−1. 0)×(2. 5−3. 0){\displaystyle (0. 9-1. 0)\times (2. 5-3. 0)}. This would then be added to the result for the other four days then divided by 4 (5-1). This solves to 0. 774{\displaystyle {\frac {0. 77}{4}}}, which is 0. 1925. The covariance between returns on stock X and Y is 0. 1925.

Specifically, the equation is: ∑n=1n(Vn−μV)2n−1{\displaystyle {\frac {\sum {n=1}^{n}(V{n}-\mu _{V})^{2}}{n-1}}} where V represents the variable in question (either X or Y). This means that the part of the variance equation for first day of returns for stock X would be calculated as (0. 9−1. 0)2{\displaystyle (0. 9-1. 0)^{2}}, which would solve to 0. 01. Continue this for each day of X, adding them up as you go along. Then, divide by n−1{\displaystyle n-1} to get your answer. For the example, the top calculation would be 0. 832, so the variable is that divided by 4, or 0. 208. This means that the variance of X’s returns, σx2{\displaystyle \sigma _{x}^{2}}, is 0. 208. Following the same process with Y yields σy2=0. 272{\displaystyle \sigma _{y}^{2}=0. 272}.

After calculations, the results are σx=0. 456{\displaystyle \sigma _{x}=0. 456} σy=0. 522{\displaystyle \sigma _{y}=0. 522}. Note that these calculations have been rounded to three decimal places to ease later calculations. Keeping more decimal places in your calculations will make them more accurate.

For the example stocks, your equation would be set up as ρxy=0. 19250. 456×0. 522{\displaystyle \rho _{xy}={\frac {0. 1925}{0. 456\times 0. 522}}}

Continuing with the example, the equation solves to ρxy=0. 809{\displaystyle \rho _{xy}=0. 809}. So, the correlation coefficient between returns on stocks X and Y is 0. 809. Note that this result has been rounded to three decimal places.

For example, the R-squared value for the example correlation coefficient would be ρxy2=0. 8092=0. 654. {\displaystyle \rho _{xy}^{2}=0. 809^{2}=0. 654. }

Correlation coefficients always vary between 1 and -1. A result of 0 indicates that there is no correlation. [7] X Research source So, for example, the example result of 0. 809 from the other part of this article would mean that stocks X and Y are highly correlated. The two securities experience price movements in the same direction and usually in roughly the same magnitude.

This practice reduces “unsystematic risk,” which is risk inherent to individual securities. [8] X Research source

For example, the stock price of a gold mining company might be positively related to the price of gold (with a high, positive correlation coefficient). If the price of gold is expected to increase, an investor would have reason to believe that the price of the company’s stock will as well.

On Excel, you can add this line by clicking “Chart” and then “Add Trendline. " The program will then calculate a trend line based on your data. [10] X Research source The correlation coefficient is a measure of how closely the two stock returns fit the regression line. That is, how closely the return values satisfy a linear relation such as Y = βX + α for some constants α and β.