how to calculate correlation
there are several methods of calculating correlation the most common method the pearson product moment correlation is discussed further in this article the pearson product moment correlation measures the linear relationship between two variables it can be used for any data set that has a finite covariance matrix here are the steps to calculate correlation to avoid the complex manual calculation consider using the correl function in excel using the pearson product moment correlation method the following formula can be used to find the correlation coefficient r r n x y x y n x 2 x 2 n y 2 y 2 where r correlation coefficient n number of observations begin aligned r frac n times sum x y sum x times sum y sqrt n times sum x 2 sum x 2 times n times sum y 2 sum y 2 textbf where r text correlation coefficient n text number of observations end aligned r n x2 x 2 n y2 y 2 n x y x y where r correlation coefficientn number of observations example of correlationinvestment managers traders and analysts find it very important to calculate correlation because the risk reduction benefits of diversification rely on this statistic financial spreadsheets and software can calculate the value of correlation quickly as a hypothetical example assume that an analyst needs to calculate the correlation for the following two data sets x 41 19 23 40 55 57 33 y 94 60 74 71 82 76 61 there are three steps involved in finding the correlation the first is to add up all the x values to find sum x add up all the y values to fund sum y and multiply each x value with its corresponding y value and sum them to find sum x y sum x 41 19 23 40 55 57 33 268sum y 94 60 74 71 82 76 61 518sum x y 41 x 94 19 x 60 23 x 74 33 x 61 20 391the next step is to take each x value square it and sum up all these values to find sum x 2 the same must be done for the y values sum x 2 41 2 19 2 23 2 33 2 11 534sum y 2 94 2 60 2 74 2 61 2 39 174noting that there are seven observations n the following formula can be used to find the correlation coefficient r r n x y x y n x 2 x 2 n y 2 y 2 where r correlation coefficient n number of observations begin aligned r frac n times sum x y sum x times sum y sqrt n times sum x 2 sum x 2 times n times sum y 2 sum y 2 textbf where r text correlation coefficient n text number of observations end aligned r n x2 x 2 n y2 y 2 n x y x y where r correlation coefficientn number of observations in this example the correlation would be r 7 x 20 391 268 x 518 squareroot 7 x 11 534 268 2 x 7 x 39 174 518 2 3 913 7 248 4 0 54correlation and portfolio diversificationin investing correlation is most important in relation to a diversified portfolio investors who wish to mitigate risk can do so by investing in non correlated assets for example consider an investor who owns airline stock if the airline industry is found to have a low correlation to the social media industry the investor may choose to invest in a social media stock understanding that an negative impact to one industry may not impact the other this is often the approach when considering investing across asset classes stocks bonds precious metals real estate cryptocurrency commodities and other types of investments each have different relationships to each other while some may be heavily correlated others may act as a hedge to diversify risk if they are not correlated risk that can be diversified away is called unsystematic risk this type of risk is specific to a company industry or asset class investing in different assets can reduce your portfolio s correlation and reduce your exposure to unsystematic risk special considerationscorrelation is often dictated and related to other statistical considerations it is common to see correlation cited when statistics is used to analyze variables in statistics a p value is used to indicate whether the findings are statistically significant it is possible to determine that two variables are correlated but there may not be enough supporting evidence to state this as a strong claim a high p value indicates there is enough evidence to meaningfully conclude that the population correlation coefficient is different from zero the easiest way to visualize whether two variables are correlated is to graphically depict them using a scatterplot each point on a scatterplot represents one sample item the x axis of the scatterplot represents one of the variables being tested while the y axis of the scatter plot represents the other the correlation coefficient of the two variables is depicted graphically often as a linear line mapped to show the relationship of the two variables if the two variables are positively correlated an increasing linear line may be drawn on the scatterplot if two variables are negatively correlated a decreasing linear line may be draw the stronger the relationship of the data points the closer each data point will be to this line scatterplots may be more useful when analyzing more complex data that might have changing relationships for example two variables may be positively correlated to a certain point then their relationship becomes negatively correlated this non linear relationship may be more difficult to identify using formulas but can be easier to spot when graphed on a scatterplot last scatterplots can easily depict correlation when they incorporate density shading a density shade or density ellipse is a shaded area on a scatterplot that visually shows the densest region of data points on a scatterplot the density ellipses will often mirror the direction of a linear correlation line if variables are related otherwise density ellipses that are more circular with no defined direction indicate lower correlation another inherent difficulty in statistics is determining whether relationships between two variables are caused by those variables consider the following statement most basketball players are tall therefore if you play basketball you will become tall it s clear that the statement above is not true individuals who are tall and understand this advantage may gravitate to basketball because their natural physical abilities best suit them for the sport however because height and activity in basketball may be positively correlated statisticians and data scientists must be aware that a strong relationship between two variables may or may be caused due to any one of the variables limitations of correlationlike other aspects of statistical analysis correlation can be misinterpreted small sample sizes may yield unreliable results even if it appears as though correlation between two variables is strong alternatively a small sample size may yield uncorrelated findings when the two variables are in fact linked correlation is often skewed when an outlier is present correlation only shows how one variable is connected to another and may not clearly identify how a single instance or outcome can impact the correlation coefficient correlation may also be misinterpreted if the relationship between two variables is nonlinear it is much easier to identify two variables with a positive or negative correlation however two variables may still be correlated with a more complex relationship 