UK-based online statistics and data analysis support for USA, UK, and international clients. No exams, no impersonation, no fabricated data.
Correlation Tests

Distance Correlation: Formula, Interpretation, SPSS, Python, R and Excel Guide

Distance Covariance, Nonlinear Dependence, Permutation Testing and Excel Matrix Calculations Distance Correlation: Formula, Interpretation, SPSS, Python, R and Excel Guide Distance Correlation is a dependence measure...

Statistics guide Ethical learning support SPSS/R/Python/Excel friendly
Distance Correlation: Formula, Interpretation, SPSS, Python, R and Excel Guide

Distance Covariance, Nonlinear Dependence, Permutation Testing and Excel Matrix Calculations

Distance Correlation: Formula, Interpretation, SPSS, Python, R and Excel Guide

Distance Correlation is a dependence measure that can detect both linear and nonlinear relationships between variables. Pearson correlation is excellent for straight-line association, but it can miss curved or more complex patterns. Distance Correlation uses pairwise distances, double-centering and distance covariance to test whether two variables are statistically dependent. This guide explains the formula, interpretation, permutation test, SPSS output, Python charts, R validation charts and a fully worked Excel matrix method.

AdvertisementGoogle AdSense top placement reserved here

Quick Answer: Distance Correlation Result

The Excel worked file uses 20 complete paired observations with an example X variable and Y variable, described in the workbook as G1 and G3 when available. The quick check gives Pearson r = 0.9925, Spearman rho = 0.9940 and sample covariance = 46.2895. The fully worked distance-correlation matrix calculation gives dCov² = 22.8643, dVarX² = 17.8885, dVarY² = 30.2526 and Distance Correlation = 0.9914.

Using a 999-shuffle permutation check on the same worked X and Y values, the observed distance correlation sits far beyond the random permutation distribution, producing an approximate permutation p-value = 0.001. This supports the conclusion that the variables are dependent. Because dCor = 0.9914 is extremely close to 1, the strength label is Very strong dependence.

Complete pairs20
Pearson r0.9925
Spearman rho0.9940
Distance Correlation0.9914

dCov²22.8643
dVarX²17.8885
dVarY²30.2526
Permutation p0.001

Final interpretation: The worked example shows very strong dependence between X and Y. Pearson, Spearman and Distance Correlation all agree because the relationship is strong, ordered and nearly linear. The main advantage of Distance Correlation is that it would still be useful if the relationship were curved or nonlinear, where Pearson might understate the dependence.

Important reporting note: The Excel workbook shows the deterministic matrix calculation for Distance Correlation. The p-value should be reported from the Python, R or SPSS permutation procedure because statistical significance is based on comparing the observed dCor with shuffled versions of the data.

Table of Contents

  1. What Is Distance Correlation?
  2. Why Use Distance Correlation?
  3. Distance Correlation Formula
  4. Null and Alternative Hypotheses
  5. Dataset and Variables Used
  6. SPSS Output Interpretation
  7. Python Chart-by-Chart Interpretation
  8. R Chart-by-Chart Validation
  9. Excel Results Explained
  10. SPSS, R, Python and Excel Workflows
  11. Code Blocks
  12. APA Reporting Wording
  13. Common Mistakes
  14. When to Use Distance Correlation
  15. Downloads and Resources
  16. Related Guides
  17. FAQs

What Is Distance Correlation?

Distance Correlation, often written as dCor, measures dependence between two variables by comparing their pairwise distance structures. Instead of only asking whether the raw values move together in a straight line, it asks whether observations that are far apart in X also tend to be far apart in Y. This makes it more general than ordinary Pearson correlation.

The key idea is simple: if X and Y are related, then the distance pattern among X values should contain information about the distance pattern among Y values. If X and Y are independent, the distance structures should not match beyond random chance. Distance Correlation turns that idea into a coefficient between 0 and 1.

Distance Correlation is especially useful alongside Pearson correlation, Spearman correlation, correlation heatmaps and correlation assumptions, because it broadens the analyst’s view from linear association to general dependence.

Simple definition: Distance Correlation is a statistical measure that detects whether two variables are dependent by comparing their pairwise distance patterns.

Why Use Distance Correlation?

Pearson correlation is excellent when the relationship is linear. Spearman correlation is useful when the relationship is monotonic. But real data can have curves, U-shapes, clusters or other nonlinear patterns. Distance Correlation is designed to detect broader dependence, not only straight-line association.

MeasureBest DetectsRangeWorked Example
Pearson correlationLinear association-1 to +10.9925
Spearman correlationMonotonic rank association-1 to +10.9940
Distance CorrelationGeneral dependence, including nonlinear patterns0 to 10.9914
Permutation testStatistical evidence against independencep-valueabout 0.001 with 999 shuffles

In this worked example, Pearson, Spearman and Distance Correlation are all very high. That agreement tells us the relationship is strong, ordered and close to linear. In other datasets, Distance Correlation may reveal dependence even when Pearson correlation is weak, especially when the association is curved rather than linear.

Distance Correlation Formula

The calculation begins with pairwise distance matrices. For one-dimensional X and Y, the basic distances are:

aij = |xi − xj|    and    bij = |yi − yj|

Each distance matrix is then double-centered by subtracting row means, column means and adding the grand mean:

Aij = aij − āi. − ā.j + ā..
Bij = bij − b̄i. − b̄.j + b̄..

After double-centering, distance covariance and distance variances are calculated from the matrices:

dCov²(X,Y) = mean(AijBij)
dVar²(X) = mean(Aij²),    dVar²(Y) = mean(Bij²)

The final normalized Distance Correlation is:

dCor(X,Y) = √[ dCov²(X,Y) / √(dVar²(X) × dVar²(Y)) ]
QuantityWorked ValueMeaning
dCov²22.8643Average product of centered X and Y distance matrices.
dVarX²17.8885Average squared centered X distance matrix.
dVarY²30.2526Average squared centered Y distance matrix.
dCor0.9914Final normalized dependence coefficient.
dCor²0.9829Squared dependence strength.

Null and Alternative Hypotheses

Distance Correlation is usually tested with a permutation procedure. The test randomizes the Y values many times, recomputes dCor for each shuffle and then compares the observed dCor with the randomized distribution.

HypothesisStatementMeaning
Null hypothesisH0: X and Y are independent.The observed distance pattern is no stronger than random pairing.
Alternative hypothesisH1: X and Y are dependent.The observed distance pattern is stronger than expected by random pairing.
Decision rulep < .05Reject the null hypothesis of independence.

Decision for the worked example: The observed dCor is approximately 0.9914, and the 999-shuffle permutation check gives p ≈ 0.001. This supports rejecting independence and reporting very strong dependence.

Dataset and Variables Used

The Excel workbook uses a transparent 20-row worked example. The X values run from 3 to 22, and the Y values rise from 8 to 34 with small local deviations. This produces a very strong positive relationship that is easy to verify across Pearson, Spearman and Distance Correlation.

Workbook ItemValueInterpretation
Complete paired observations20Twenty numeric X–Y pairs are used in the matrix calculation.
Mean X12.5000Average of the X variable in the worked example.
Mean Y20.0500Average of the Y variable in the worked example.
Pearson r0.9925Very strong straight-line association.
Spearman rho0.9940Very strong monotonic rank association.
Distance Correlation0.9914Very strong dependence.

The workbook note says the default variables in scripts are G1 and G3 when available; otherwise, the first two numeric variables are used. That makes the Excel file flexible for replacing the demo values with another dataset.

AdvertisementGoogle AdSense middle placement reserved here

SPSS Output Interpretation for Distance Correlation

The SPSS output PDF provides the software-based confirmation for the Distance Correlation workflow. In an SPSS-style interpretation, focus on the observed distance correlation, the permutation p-value and the decision about independence. Because Distance Correlation is designed to detect broad dependence, the wording should emphasize dependence rather than only linear association.

Open the SPSS Distance Correlation output PDF

SPSS Output ItemWhat It MeansHow to Interpret It
Distance correlation statisticObserved dependence coefficient.A value near 1 indicates very strong dependence.
Permutation p-valueRandomization-based significance test.Small p-values reject independence.
Number of permutationsUsually 999 or another selected count.More permutations give finer p-value resolution.
Comparison with Pearson/SpearmanLinear and rank-based context.If all measures are high, the relationship is strong and likely monotonic/linear.

For this worked example, the SPSS interpretation should agree with the Excel, Python and R results: the variables show very strong dependence, and the permutation test supports rejecting independence.

Python Chart-by-Chart Interpretation

The Python charts explain the full Distance Correlation workflow visually: the raw relationship, comparison with Pearson and Spearman, the permutation test, distance matrices, rank behavior and final p-value summary.

Python Chart 1: Distance Correlation Scatter

Python Distance Correlation scatterplot showing X and Y relationship
Python scatterplot showing the relationship between X and Y for Distance Correlation.

The scatterplot shows the raw association before any matrix calculations are applied. In this worked example, the points follow a clear upward pattern, which explains why Pearson, Spearman and Distance Correlation are all extremely high. The plot indicates that as X increases, Y also increases, with only minor local deviations.

This chart is important because Distance Correlation should never be interpreted without looking at the data shape. Here, the shape is close to linear and monotonic, so the high dCor agrees naturally with the high Pearson and Spearman coefficients.

Python Chart 2: Association Measure Comparison

Python association measure comparison for distance correlation Pearson and Spearman
Python comparison of Distance Correlation, Pearson correlation and Spearman correlation.

This chart compares the major association measures directly. The workbook values show Distance Correlation = 0.9914, Pearson r = 0.9925 and Spearman rho = 0.9940. Because all three measures are very close to 1, the relationship is strong under linear, rank-based and distance-based definitions.

The comparison also teaches the purpose of Distance Correlation. When Pearson is weak but dCor is high, the relationship may be nonlinear. In this example, all three are high, so the result is simpler: the variables are strongly related in nearly every reasonable sense.

Python Chart 3: Permutation Distribution

Python permutation distribution for Distance Correlation
Python permutation distribution showing random-shuffle dCor values compared with the observed statistic.

The permutation distribution is the key inferential chart. It shows what distance correlation values look like when Y is randomly shuffled and the relationship with X is destroyed. In a 999-shuffle check for the worked values, the observed dCor = 0.9914 is far to the right of the random distribution, giving p ≈ 0.001.

This means the observed dependence is not plausible under random pairing. The permutation chart is more informative than a formula alone because it shows how extreme the observed statistic is relative to the null distribution.

Python Chart 4: Distance Matrix X

Python distance matrix for X variable in Distance Correlation
Python heatmap of pairwise distances among X values.

This matrix shows the pairwise absolute distances between X observations. Values near the diagonal are small because adjacent observations are close, while distances grow as observations are farther apart. This matrix is the first raw ingredient in the Distance Correlation calculation.

The important interpretation is not one single cell, but the overall distance structure. If this X-distance structure matches the Y-distance structure after centering, the variables are dependent. In this example, that match is very strong.

Python Chart 5: Distance Matrix Y

Python distance matrix for Y variable in Distance Correlation
Python heatmap of pairwise distances among Y values.

This matrix shows the pairwise absolute distances for Y. Because Y rises with X in the worked example, the visual structure of the Y-distance matrix should resemble the X-distance matrix. That resemblance is the visual reason why the distance covariance is large.

If X and Y were independent, the X-distance and Y-distance heatmaps would not align meaningfully. Here they align strongly, supporting the very high final dCor value.

Python Chart 6: Rank Scatter

Python rank scatterplot for Distance Correlation
Python rank scatterplot showing monotonic ordering between X and Y.

The rank scatter confirms that the relationship is also strongly monotonic. The workbook Spearman value is 0.9940, meaning that the ordering of X and Y is almost the same. That is why the rank plot should show points lying close to an increasing diagonal pattern.

This chart is useful because it explains why Spearman agrees with Distance Correlation in this example. The relationship is not just dependent; it is also ordered.

Python Chart 7: P-Value Summary

Python p value summary for Distance Correlation
Python p-value summary chart for the Distance Correlation permutation test.

The p-value summary chart gives the decision-ready result. It should be used to write the final inferential sentence: the observed distance correlation is very large, and the permutation test supports rejecting the null hypothesis of independence.

In the worked-value permutation check, p ≈ 0.001. With 999 permutations, this is the strongest commonly reported resolution because the minimum p-value is 1/(999+1). The practical conclusion is that the dependence is very strong and statistically supported.

R Chart-by-Chart Validation

The R charts validate the same Distance Correlation story in a separate software workflow. They repeat the scatter, measure comparison, permutation distribution, distance matrices, rank scatter and p-value summary using R output.

R Chart 1: Distance Correlation Scatter

R Distance Correlation scatterplot
R scatterplot showing the same strong positive relationship between X and Y.

The R scatterplot should match the Python scatterplot by showing a very clear upward pattern. This visual agreement confirms that the strong dependence is not a software-specific artifact.

Because the data pattern is simple and strong, the R chart supports the same conclusion: X and Y have very strong dependence, and the relationship is also nearly linear and monotonic.

R Chart 2: Association Measure Comparison

R association measure comparison for distance correlation
R comparison of dCor, Pearson r and Spearman rho.

This chart validates the high agreement among association measures. When Distance Correlation, Pearson and Spearman all sit close to 1, the relationship is strong under different definitions of association.

For reporting, this chart supports wording such as “Distance Correlation confirmed the strong dependence already suggested by Pearson and Spearman.”

R Chart 3: Permutation Distribution

R permutation distribution for distance correlation
R permutation distribution comparing the observed dCor with shuffled null values.

The R permutation chart is the inferential validation. It should show the observed statistic far beyond the typical shuffled dCor values, confirming a very small p-value.

This is the chart to cite when explaining why the null hypothesis of independence is rejected. The data preserve their dependence under the observed pairing but lose it when Y is randomly shuffled.

R Chart 4: Distance Matrix X

R distance matrix X for Distance Correlation
R heatmap of the X distance matrix.

The R distance-matrix chart provides another view of the pairwise X-distance structure. Its gradient should reflect the ordered nature of the X values, with larger distances between observations farther apart in the sequence.

This matrix becomes meaningful when compared with the Y-distance matrix. The stronger their centered structures match, the stronger the distance covariance becomes.

R Chart 5: Distance Matrix Y

R distance matrix Y for Distance Correlation
R heatmap of the Y distance matrix.

The Y-distance matrix should visually resemble the X-distance matrix because the worked example has a strong positive relationship. The similarity between these matrices is exactly what Distance Correlation is designed to capture.

This chart makes the method less abstract. Distance Correlation is not magic; it is comparing pairwise distance patterns after centering.

R Chart 6: Rank Scatter

R rank scatterplot for Distance Correlation
R rank scatterplot showing strong monotonic association.

The rank scatter confirms that the relationship is strongly monotonic. This agrees with the workbook’s Spearman rho = 0.9940.

When the rank scatter is almost perfectly increasing, it explains why the Spearman result is so close to the Pearson and Distance Correlation results.

R Chart 7: P-Value Summary

R p value summary for Distance Correlation
R p-value summary chart for the Distance Correlation permutation test.

The R p-value summary gives the final statistical decision in a compact form. It should confirm that the observed dCor is significant under the permutation test.

When Python, R, SPSS and Excel all tell the same story, the conclusion is strong: the two variables are dependent, and the observed relationship is very large.

Excel Results Explained

The fully worked Excel file is designed as a transparent teaching file. It contains separate sheets for Data_Input, Distance_X, Distance_Y, Centered_X, Centered_Y, Product_AB, A2_Matrix, B2_Matrix, Results and Formula_Guide. This lets the reader trace every step from raw values to final dCor.

Excel SheetPurposeMain Formula Idea
Data_InputStores X and Y values, centered values and quick Pearson/Spearman checks.Use two numeric variables with complete paired rows.
Distance_X / Distance_YCalculates pairwise absolute distances.|xi − xj| and |yi − yj|.
Centered_X / Centered_YDouble-centers each distance matrix.Distance − row mean − column mean + grand mean.
Product_ABMultiplies centered X and Y matrices.Aij × Bij.
A2_Matrix / B2_MatrixSquares each centered distance matrix.Aij² and Bij².
ResultsComputes final distance covariance, variance and correlation.sqrt(dCov² / sqrt(dVarX² × dVarY²)).

Excel Final Results

MetricValueInterpretation
X variableX variable (example G1)Use the X column from Data_Input.
Y variableY variable (example G3)Use the Y column from Data_Input.
Complete paired observations20Number of rows used.
Distance covariance squared22.8643Average of Aij × Bij.
Distance variance X squared17.8885Average of Aij².
Distance variance Y squared30.2526Average of Bij².
Distance correlation0.9914Final dependence coefficient.
Pearson r0.9925Straight-line association context.
Spearman rho0.9940Rank-based monotonic context.
Distance correlation squared0.9829Squared dependence strength.
Strength labelVery strong dependenceRule-of-thumb interpretation.

The workbook’s report sentence is: “Distance correlation between X variable (example G1) and Y variable (example G3) was 0.9914, indicating Very strong dependence.” Add the permutation p-value from Python, R or SPSS when writing the final statistical report.

Excel Formula Steps

StepExcel Formula PatternPurpose
Pairwise X distance=ABS(x_i-x_j)Builds the X distance matrix.
Pairwise Y distance=ABS(y_i-y_j)Builds the Y distance matrix.
Double centering=distance-row_mean-column_mean+grand_meanRemoves row, column and grand distance effects.
Distance covariance squared=AVERAGE(Product_AB_matrix)Calculates mean Aij × Bij.
Distance variance X squared=AVERAGE(A2_matrix)Calculates mean Aij².
Distance variance Y squared=AVERAGE(B2_matrix)Calculates mean Bij².
Distance correlation=SQRT(dCov2/SQRT(dVarX2*dVarY2))Final normalized dependence coefficient.
AdvertisementGoogle AdSense in-content placement reserved here

SPSS, R, Python and Excel Workflows

SoftwareMain StepsBest Use
SPSSRun the Distance Correlation procedure or script, request permutation testing and export the output PDF.Formal output and report verification.
PythonCompute pairwise distances, double-center matrices, calculate dCor and run permutation testing; generate charts.Best for custom visuals and reproducibility.
RUse distance-correlation functions, permutation tests and validation charts.Best for statistical validation and publication-style output.
ExcelBuild matrices manually using formulas and compute deterministic dCor step by step.Best for teaching the internal calculation logic.

Code Blocks for Distance Correlation

SPSS Workflow Note

* Distance Correlation in SPSS depends on the available extension/procedure.
* General workflow:
* 1. Select two numeric variables such as G1 and G3.
* 2. Request Distance Correlation / dependence test.
* 3. Use permutation p-value for inference.
* 4. Export the output to PDF.

* If using an SPSS extension command, run the installed command
* with X variable, Y variable, and number of permutations specified.

Python Code

import numpy as np

def distance_correlation(x, y):
    x = np.asarray(x, dtype=float).reshape(-1, 1)
    y = np.asarray(y, dtype=float).reshape(-1, 1)

    a = np.abs(x - x.T)
    b = np.abs(y - y.T)

    A = a - a.mean(axis=0)[None, :] - a.mean(axis=1)[:, None] + a.mean()
    B = b - b.mean(axis=0)[None, :] - b.mean(axis=1)[:, None] + b.mean()

    dcov2 = np.mean(A * B)
    dvarx2 = np.mean(A * A)
    dvary2 = np.mean(B * B)

    dcor = np.sqrt(dcov2 / np.sqrt(dvarx2 * dvary2))
    return dcor, dcov2, dvarx2, dvary2

# Example values from the Excel worked file
x = np.arange(3, 23)
y = np.array([8, 9, 11, 10, 13, 15, 16, 16, 18, 20,
              21, 20, 23, 25, 24, 27, 28, 31, 32, 34])

observed, dcov2, dvarx2, dvary2 = distance_correlation(x, y)
print(observed, dcov2, dvarx2, dvary2)

# Permutation test
rng = np.random.default_rng(12345)
B = 999
perm = np.array([distance_correlation(x, rng.permutation(y))[0] for _ in range(B)])
p_value = (np.sum(perm >= observed) + 1) / (B + 1)
print("Permutation p-value:", p_value)

R Code

# Distance Correlation in R
# install.packages("energy")
library(energy)

x <- 3:22
y <- c(8, 9, 11, 10, 13, 15, 16, 16, 18, 20,
       21, 20, 23, 25, 24, 27, 28, 31, 32, 34)

# Distance correlation
dcor_value <- dcor(x, y)
dcor_value

# Permutation test for independence
dcov_test <- dcov.test(x, y, R = 999)
dcov_test

Excel Formula Patterns

# Distance matrix cell
=ABS(x_i - x_j)

# Double-centered distance
=distance_cell - row_mean - column_mean + grand_mean

# Distance covariance squared
=AVERAGE(Product_AB_range)

# Distance variance X squared
=AVERAGE(A2_Matrix_range)

# Distance variance Y squared
=AVERAGE(B2_Matrix_range)

# Distance correlation
=SQRT(dCov2 / SQRT(dVarX2 * dVarY2))

APA Reporting Wording

When reporting Distance Correlation, state the variables, sample size, observed dCor, permutation p-value and interpretation. Also mention Pearson or Spearman if you are comparing methods.

APA-Style Full Report

A distance correlation analysis was conducted to examine dependence between the X variable and Y variable in the worked example. The analysis included 20 complete paired observations. The observed distance correlation was very strong, dCor = .991, with dCor² = .983. A 999-shuffle permutation test indicated that the observed dependence was statistically significant, p = .001. Pearson correlation, r = .992, and Spearman correlation, ρ = .994, also indicated a very strong positive linear and monotonic relationship. These results support rejecting the null hypothesis of independence.

Short APA-Style Version

Distance Correlation indicated very strong dependence between X and Y, dCor = .991, p = .001 based on a 999-shuffle permutation test. Pearson and Spearman results were similarly high, r = .992 and ρ = .994.

Practical Report Wording

The variables show very strong dependence. In this example, the dependence is also visible in ordinary Pearson and Spearman correlations, which means the relationship is strong, ordered and close to linear. Distance Correlation confirms this relationship using a broader dependence-based method.

Common Mistakes in Distance Correlation Interpretation

MistakeWhy It Is a ProblemBetter Practice
Using only Pearson for nonlinear dataPearson can miss curved dependence.Use Distance Correlation when nonlinear dependence is possible.
Reporting dCor without a p-valueThe coefficient gives strength, not the inferential test by itself.Use a permutation test for the p-value.
Confusing distance correlation with correlation distanceThey are related terms but used differently in statistics and clustering.Define the method clearly as a dependence measure.
Ignoring the scatterplotA single coefficient cannot show the shape of the relationship.Always inspect the scatterplot and rank scatter.
Treating dCor as signedDistance Correlation ranges from 0 to 1 and does not show positive or negative direction.Use Pearson/Spearman or scatterplots to describe direction.
Assuming large dCor proves causationDependence is not causal proof.Use causal language only with a proper causal design.

When to Use Distance Correlation

Use Distance Correlation when you want to test whether two variables are dependent but do not want to rely only on linear association. It is especially useful when scatterplots suggest nonlinear patterns, curved relationships, clusters or other complex dependence structures.

It is also useful as a robustness check. If Pearson, Spearman and Distance Correlation all agree, your relationship is likely strong and stable. If Pearson is low but Distance Correlation is high, investigate nonlinear dependence before concluding that there is no relationship.

Downloads and Resources

FAQs About Distance Correlation

What is Distance Correlation in simple words?

Distance Correlation is a dependence measure that checks whether the pairwise distance pattern in one variable matches the pairwise distance pattern in another variable.

How is Distance Correlation different from Pearson correlation?

Pearson correlation measures linear association, while Distance Correlation can detect broader dependence, including nonlinear relationships.

What range does Distance Correlation have?

Distance Correlation ranges from 0 to 1. Values near 0 suggest weak or no dependence, while values near 1 suggest strong dependence.

What was the Distance Correlation in the worked example?

The worked Excel example gave Distance Correlation = 0.9914, indicating very strong dependence.

What was the permutation p-value?

Using a 999-shuffle permutation check on the worked values, the p-value was approximately 0.001.

Does Distance Correlation show direction?

No. Distance Correlation is not signed. Use the scatterplot, Pearson correlation or Spearman correlation to describe positive or negative direction.

Can I calculate Distance Correlation in Excel?

Yes. You can calculate it by building pairwise distance matrices, double-centering them, computing distance covariance and distance variances, and then applying the final dCor formula.

Does high Distance Correlation prove causation?

No. High Distance Correlation shows dependence, not causation. Causal claims require a proper causal research design.

Need help applying this to your own data?

Salar Cafe can help interpret output, clean datasets, review assumptions, build dashboards and explain statistical results ethically.

Need help interpreting your data analysis results?

Contact Salar Cafe
Engr. Muhammad Yar Saqib author profile photo

Engr. Muhammad Yar Saqib

WhatsApp Get Data Analysis Help