({{, (RefLink[Correlation, paclet:ref/Correlation])[(Subscript[v, 1]),(Subscript[v, 2])] 
gives the correlation between the vectors (Subscript[v, 1]) and (Subscript[v, 2]).}, {, (RefLink[Correlation, paclet:ref/Correlation])[m]
gives the correlation matrix for the matrix m.}, {, (RefLink[Correlation, paclet:ref/Correlation])[(Subscript[m, 1]),(Subscript[m, 2])]
gives the correlation matrix for the matrices (Subscript[m, 1]) and (Subscript[m, 2]).}, {, (RefLink[Correlation, paclet:ref/Correlation])[dist]
gives the correlation matrix for the multivariate symbolic distribution dist.}, {, (RefLink[Correlation, paclet:ref/Correlation])[dist,i,j]
gives the (i,j)(^th) correlation for the multivariate symbolic distribution dist. }})

OpenersNotesSection(Spacer1)Details

(RefLink[Correlation, paclet:ref/Correlation])[(Subscript[v, 1]),(Subscript[v, 2])] gives Pearson's correlation coefficient between (Subscript[v, 1]) and (Subscript[v, 2]).

The lists (Subscript[v, 1]) and (Subscript[v, 2]) must be the same length.

(RefLink[Correlation, paclet:ref/Correlation])[(Subscript[v, 1]),(Subscript[v, 2])] is equivalent to (RefLink[Covariance, paclet:ref/Covariance])[(Subscript[v, 1]),(Subscript[v, 2])]/((RefLink[StandardDeviation, paclet:ref/StandardDeviation])[(Subscript[v, 1])](RefLink[StandardDeviation, paclet:ref/StandardDeviation])[(Subscript[v, 2])]).

For a matrix m with p columns, (RefLink[Correlation, paclet:ref/Correlation])[m] is a p×p matrix of the correlations between columns of m.

For an n×p matrix (Subscript[m, 1]) and an n×q matrix (Subscript[m, 2]), (RefLink[Correlation, paclet:ref/Correlation])[(Subscript[m, 1]),(Subscript[m, 2])] is a p×q matrix of the correlations between columns of (Subscript[m, 1]) and columns of (Subscript[m, 2]).

(RefLink[Correlation, paclet:ref/Correlation]) works with (RefLink[SparseArray, paclet:ref/SparseArray]) objects.

(RefLink[Correlation, paclet:ref/Correlation])[dist,i,j] gives (RefLink[Covariance, paclet:ref/Covariance])[dist,i,j]/((Subscript[σ, i]) (Subscript[σ, j])), where (Subscript[σ, i]) is the i(^th) component of the standard deviation of dist.

(RefLink[Correlation, paclet:ref/Correlation])[dist] gives a correlation matrix with the (i,j)(^th) entry given by (RefLink[Correlation, paclet:ref/Correlation])[dist,i,j].

OpenersPrimaryExamplesSection(Spacer1)Examples  (25)

OpenersExampleSection0(Spacer1)Basic Examples  (3)

Correlation between two vectors:

Correlation[{a,b},{x,y}]

Real values:

Refine[%,{a,b,x,y}∈Reals]

Correlation matrix for a matrix:

Correlation[{{a,b},{c,d}}]

Real values:

Refine[%,{a,b,c,d}∈Reals]

Correlation matrix for two matrices:

Correlation[{{a,b},{c,d}},{{x},{y}}]//MatrixForm

Real values:

Refine[%,{a,b,c,d,x,y}∈Reals]

OpenersExampleSection1(Spacer1)Scope  (12)

OpenersExampleSubsection0(Spacer1)Data  (7)

Exact input yields exact output:

Correlation[{5,3/4,1},{2,1/2,1}]

Correlation[{1,π,2},{2,2,1}]//Simplify

Approximate input yields approximate output:

Correlation[{1.5,3,5,10},{2,1.25,15,8}]

Correlation[N[{1,2,5,6},20],N[{2,3,6,8},20]]

Correlation between vectors of complexes:

Correlation[{2+I,3-2I,5+4I},{I,1+2I,10-5I}]

Works with large arrays:

Correlation[RandomReal[1,10^7],RandomReal[1,10^7]]

(RefLink[SparseArray, paclet:ref/SparseArray]) data can be used:

Correlation[SparseArray[{{2,2}->1,{5,3}->2,{10,1}->3}]]//MatrixForm

Find the correlation of (RefLink[WeightedData, paclet:ref/WeightedData]):

Correlation[WeightedData[{{1,0},{0,1},{1,1}},{(Subscript[w, 1]),(Subscript[w, 2]),(Subscript[w, 3])}]]//Simplify//MatrixForm

data={{8,2},{3,7},{5,9},{4,1},{9,3},{0,12},{4,8},{2,11},{3,2},{11,1}};
weights={0.15,0.09,0.12,0.10,0.16,0.,0.11,0.08,0.08,0.09};

Correlation[WeightedData[data,weights]]//MatrixForm

Find the correlation for data involving quantities:

{v,w}=Quantity[{{2.5,3,5,10},{2,1.25,15,8}},"Meters"];

Correlation[v,w]

OpenersExampleSubsection1(Spacer1)Distributions and Processes  (5)

Correlation for a continuous multivariate distribution:

Correlation[BinormalDistribution[ρ]]//MatrixForm

Correlation[BinormalDistribution[ρ],2,1]

Correlation for a discrete multivariate distribution:

Correlation[MultivariatePoissonDistribution[μ,{2,3}]]//MatrixForm

Correlation[MultivariatePoissonDistribution[μ,{2,3}],2,1]

Correlation for derived distributions:

Correlation[ProductDistribution[ExponentialDistribution[1],NormalDistribution[3,5]]]//MatrixForm

𝒟=CopulaDistribution[{"Frank",2}, {UniformDistribution[{0,1}], UniformDistribution[{0,1}]}];

Correlation[𝒟]//MatrixForm

Data distribution:

𝒟=HistogramDistribution[RandomVariate[BinormalDistribution[.75],10^4]];

Correlation[𝒟]//MatrixForm

Correlation[BinormalDistribution[.75]]//MatrixForm

Correlation matrix for a random process at times s and t:

Correlation[WienerProcess[][{s,t}]]//MatrixForm

Correlation matrix for (RefLink[TemporalData, paclet:ref/TemporalData]) at times 0.2` and 0.3`:

td=RandomFunction[WienerProcess[1,1],{0,10,0.05},100]

Correlation[td[{0.2,0.3}]]//MatrixForm

OpenersExampleSection2(Spacer1)Applications  (3)

Compute the correlation of two financial time series:

gspc=TimeSeries[];

ndx=TimeSeries[];

Correlation[gspc["Values"],ndx["Values"]]

(RefLink[Correlation, paclet:ref/Correlation]) can be used to measure linear association:

data=BlockRandom[SeedRandom[1];Table[RandomVariate[BinormalDistribution[i],3000],{i,{-.99,-.75,-.25,-.5,0.,.25,.5,.75,.99}}]];

Grid[Partition[Table[ListPlot[i, PlotStyle -> Directive[PointSize[Tiny]], 
     FrameTicks -> None, Frame -> True, Axes -> None,PlotLabel -> Row[{"ρ : ", Correlation[i][[1,2]]}]], {i, data}], 
   3]]

(RefLink[Correlation, paclet:ref/Correlation]) can only detect monotonic relationships:

uni=RandomReal[{-3,3},3000];

f[x_]:={{x,-Sqrt[Abs[x]]+RandomReal[.5]},{x,.25(x^2)+RandomReal[.5]},{x,-Sinc[x]+RandomReal[.5]},{Cos[x],Sin[x]+RandomReal[.5]}}

data=f/@uni;

Table[ListPlot[data[[All,i]],Frame->True,Axes->None,PlotLabel->Row[{"ρ : ",Correlation[data[[All,i]]][[1,2]]}],PlotStyle->Directive[PointSize[Tiny]],FrameTicks->None],{i,4}]

(RefLink[HoeffdingD, paclet:ref/HoeffdingD]) can be used to detect a variety of dependence structures:

Table[HoeffdingD[data[[All,i]]][[1,2]],{i,4}]

OpenersExampleSection3(Spacer1)Properties & Relations  (7)

The correlation matrix is symmetric and positive semidefinite:

corr=Correlation[RandomVariate[BinormalDistribution[1/3],10^3]];

SymmetricMatrixQ[corr]

PositiveSemidefiniteMatrixQ[corr]

A correlation matrix is a covariance matrix scaled by standard deviations:

data=RandomReal[5,{20,5}];

s=DiagonalMatrix[1/StandardDeviation[data]];

Correlation[data]==s.Covariance[data].s

(RefLink[Correlation, paclet:ref/Correlation]) and (RefLink[AbsoluteCorrelation, paclet:ref/AbsoluteCorrelation]) agree for zero mean and unit marginal variances:

𝒟=BinormalDistribution[ρ];

Mean[𝒟]

Variance[𝒟]

Correlation[𝒟]

AbsoluteCorrelation[𝒟]

(RefLink[SpearmanRho, paclet:ref/SpearmanRho]) is (RefLink[Correlation, paclet:ref/Correlation]) applied to ranks:

data=Transpose@RandomVariate[BinormalDistribution[.8],100];

SpearmanRho[data[[1]],data[[2]]]

rnks=Ordering[Ordering[#]]&/@data;

Correlation[rnks[[1]],rnks[[2]]]//N

(RefLink[CorrelationFunction, paclet:ref/CorrelationFunction]) for a process is the off-diagonal entry in the correlation matrix:

𝒫=WienerProcess[μ,σ];

Correlation[𝒫[{s,t}],1,2]

CorrelationFunction[𝒫,s,t]

Simplify[%%-%,0<s<t]

(RefLink[Correlation, paclet:ref/Correlation]) and (RefLink[Covariance, paclet:ref/Covariance]) are the same for standardized vectors:

sample=RandomVariate[DiscreteUniformDistribution[{{2,3},{4,5}}],200];

Covariance[sample]===Correlation[sample]

Covariance[Standardize[sample]]===Correlation[sample]

The diagonal elements of a correlation matrix are equal to 1:

Diagonal[Correlation[RandomReal[10,{100,4}]]]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[Covariance, paclet:ref/Covariance])  ▪  (RefLink[AbsoluteCorrelation, paclet:ref/AbsoluteCorrelation])  ▪  (RefLink[CorrelationFunction, paclet:ref/CorrelationFunction])  ▪  (RefLink[Variance, paclet:ref/Variance])  ▪  (RefLink[CentralMoment, paclet:ref/CentralMoment])  ▪  (RefLink[Expectation, paclet:ref/Expectation])  ▪  (RefLink[CorrelationTest, paclet:ref/CorrelationTest])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Descriptive Statistics, paclet:guide/DescriptiveStatistics])}, {▪(RefLinkPlain[Statistical Moments and Generating Functions, paclet:guide/StatisticalMomentsAndGeneratingFunctions])}})}})