---
title: Kendall Rank Corr. Coef.
description: Compute Kendall Rank Correlation Coefficient.
canonical: >-
  https://lightning.ai/docs/torchmetrics/stable/regression/kendall_rank_corr_coef
---
# Kendall Rank Corr. Coef. {#kendall-rank-corr-coef}

## Module Interface {#module-interface}

### `class torchmetrics.KendallRankCorrCoef(variant='b', t_test=False, alternative='two-sided', num_outputs=1, **kwargs)` {#torchmetrics.KendallRankCorrCoef}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/regression/kendall.py#L36-L212)

Compute [Kendall Rank Correlation Coefficient](https://en.wikipedia.org/wiki/Kendall_rank_correlation_coefficient).

```math
tau_a = \frac{C - D}{C + D}
```

where $`C`$ represents concordant pairs, $`D`$ stands for discordant pairs.

```math
tau_b = \frac{C - D}{\sqrt{(C + D + T_{preds}) * (C + D + T_{target})}}
```

where $`C`$ represents concordant pairs, $`D`$ stands for discordant pairs and $`T`$ represents a total number of ties.

```math
tau_c = 2 * \frac{C - D}{n^2 * \frac{m - 1}{m}}
```

where $`C`$ represents concordant pairs, $`D`$ stands for discordant pairs, $`n`$ is a total number of observations and $`m`$ is a `min` of unique values in `preds` and `target` sequence.

Definitions according to Definition according to [The Treatment of Ties in Ranking Problems](https://www.jstor.org/stable/2332303).

As input to `forward` and `update` the metric accepts the following input:

*   `preds` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): Sequence of data in float tensor of either shape `(N,)` or `(N,d)`
    
*   `target` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): Sequence of data in float tensor of either shape `(N,)` or `(N,d)`
    

As output of `forward` and `compute` the metric returns the following output:

*   `kendall` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): A tensor with the correlation tau statistic, and if it is not None, the p-value of corresponding statistical test.
    

Parameters:

*   **variant** ([`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal "(in Python v3.15)")\[`'a'`, `'b'`, `'c'`\]) – Indication of which variant of Kendall’s tau to be used
    
*   **t\_test** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool "(in Python v3.15)")) – Indication whether to run t-test
    
*   **alternative** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal "(in Python v3.15)")\[`'two-sided'`, `'less'`, `'greater'`\]\]) – Alternative hypothesis for t-test. Possible values: - ‘two-sided’: the rank correlation is nonzero - ‘less’: the rank correlation is negative (less than zero) - ‘greater’: the rank correlation is positive (greater than zero)
    
*   **num\_outputs** ([`int`](https://docs.python.org/3/builtins/functions.html#int "(in Python v3.15)")) – Number of outputs in multioutput setting
    
*   **kwargs** ([`Any`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.15)")) – Additional keyword arguments, see [Advanced metric settings](/docs/torchmetrics/1.9.0/user-guide/overview.md#metric-kwargs) for more info.
    

Raises:

*   [**ValueError**](https://docs.python.org/3/builtins/exceptions.html#ValueError "(in Python v3.15)") – If `t_test` is not of a type bool
    
*   [**ValueError**](https://docs.python.org/3/builtins/exceptions.html#ValueError "(in Python v3.15)") – If `t_test=True` and `alternative=None`
    

Example (single output regression):

```python
>>> from torch import tensor
>>> from torchmetrics.regression import KendallRankCorrCoef
>>> preds = tensor([2.5, 0.0, 2, 8])
>>> target = tensor([3, -0.5, 2, 1])
>>> kendall = KendallRankCorrCoef()
>>> kendall(preds, target)
tensor(0.3333)
```

Example (multi output regression):

```python
>>> from torchmetrics.regression import KendallRankCorrCoef
>>> preds = tensor([[2.5, 0.0], [2, 8]])
>>> target = tensor([[3, -0.5], [2, 1]])
>>> kendall = KendallRankCorrCoef(num_outputs=2)
>>> kendall(preds, target)
tensor([1., 1.])
```

Example (single output regression with t-test):

```python
>>> from torchmetrics.regression import KendallRankCorrCoef
>>> preds = tensor([2.5, 0.0, 2, 8])
>>> target = tensor([3, -0.5, 2, 1])
>>> kendall = KendallRankCorrCoef(t_test=True, alternative='two-sided')
>>> kendall(preds, target)
(tensor(0.3333), tensor(0.4969))
```

Example (multi output regression with t-test):

```python
>>> from torchmetrics.regression import KendallRankCorrCoef
>>> preds = tensor([[2.5, 0.0], [2, 8]])
>>> target = tensor([[3, -0.5], [2, 1]])
>>> kendall = KendallRankCorrCoef(t_test=True, alternative='two-sided', num_outputs=2)
>>> kendall(preds, target)
(tensor([1., 1.]), tensor([nan, nan]))
```

#### `plot(val=None, ax=None)` {#torchmetrics.KendallRankCorrCoef.plot}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/regression/kendall.py#L172-L212)

Plot a single or multiple values from the metric.

Parameters:

*   **val** (`Union`\[[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)"), [`Sequence`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Sequence "(in Python v3.15)")\[[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")\], [`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")\]) – Either a single result from calling metric.forward or metric.compute or a list of these results. If no value is provided, will automatically call metric.compute and plot that result.
    
*   **ax** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`Axes`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.html#matplotlib.axes.Axes "(in Matplotlib v3.11.2)")\]) – An matplotlib axis object. If provided will add plot to that axis
    

Return type:

[`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple "(in Python v3.15)")\[[`Figure`](https://matplotlib.org/stable/api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure "(in Matplotlib v3.11.2)"), `Union`\[[`Axes`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.html#matplotlib.axes.Axes "(in Matplotlib v3.11.2)"), [`ndarray`](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray "(in NumPy v2.5)")\]\]

Returns:

Figure and Axes object

Raises:

[**ModuleNotFoundError**](https://docs.python.org/3/builtins/exceptions.html#ModuleNotFoundError "(in Python v3.15)") – If matplotlib is not installed

```python
>>> from torch import randn
>>> # Example plotting a single value
>>> from torchmetrics.regression import KendallRankCorrCoef
>>> metric = KendallRankCorrCoef()
>>> metric.update(randn(10,), randn(10,))
>>> fig_, ax_ = metric.plot()
```

[![../../\_images/kendall\_rank\_corr\_coef-1.png](/docs/torchmetrics/1.9.0/static/_images/kendall_rank_corr_coef-1.png)](/docs/torchmetrics/1.9.0/static/_images/kendall_rank_corr_coef-1.png)

```python
>>> from torch import randn
>>> # Example plotting multiple values
>>> from torchmetrics.regression import KendallRankCorrCoef
>>> metric = KendallRankCorrCoef()
>>> values = []
>>> for _ in range(10):
...     values.append(metric(randn(10,), randn(10,)))
>>> fig, ax = metric.plot(values)
```

[![../../\_images/kendall\_rank\_corr\_coef-2.png](/docs/torchmetrics/1.9.0/static/_images/kendall_rank_corr_coef-2.png)](/docs/torchmetrics/1.9.0/static/_images/kendall_rank_corr_coef-2.png)

## Functional Interface {#functional-interface}

### `torchmetrics.functional.kendall_rank_corrcoef(preds, target, variant='b', t_test=False, alternative='two-sided')` {#torchmetrics.functional.kendall_rank_corrcoef}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/functional/regression/kendall.py#L335-L430)

Compute [Kendall Rank Correlation Coefficient](https://en.wikipedia.org/wiki/Kendall_rank_correlation_coefficient).

```math
tau_a = \frac{C - D}{C + D}
```

where $`C`$ represents concordant pairs, $`D`$ stands for discordant pairs.

```math
tau_b = \frac{C - D}{\sqrt{(C + D + T_{preds}) * (C + D + T_{target})}}
```

where $`C`$ represents concordant pairs, $`D`$ stands for discordant pairs and $`T`$ represents a total number of ties.

```math
tau_c = 2 * \frac{C - D}{n^2 * \frac{m - 1}{m}}
```

where $`C`$ represents concordant pairs, $`D`$ stands for discordant pairs, $`n`$ is a total number of observations and $`m`$ is a `min` of unique values in `preds` and `target` sequence.

Definitions according to Definition according to [The Treatment of Ties in Ranking Problems](https://www.jstor.org/stable/2332303).

Parameters:

*   **preds** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")) – Sequence of data of either shape `(N,)` or `(N,d)`
    
*   **target** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")) – Sequence of data of either shape `(N,)` or `(N,d)`
    
*   **variant** ([`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal "(in Python v3.15)")\[`'a'`, `'b'`, `'c'`\]) – Indication of which variant of Kendall’s tau to be used
    
*   **t\_test** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool "(in Python v3.15)")) – Indication whether to run t-test
    
*   **alternative** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal "(in Python v3.15)")\[`'two-sided'`, `'less'`, `'greater'`\]\]) – Alternative hypothesis for t-test. Possible values: - ‘two-sided’: the rank correlation is nonzero - ‘less’: the rank correlation is negative (less than zero) - ‘greater’: the rank correlation is positive (greater than zero)
    

Return type:

`Union`\[[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)"), [`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple "(in Python v3.15)")\[[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)"), [`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")\]\]

Returns:

Correlation tau statistic (Optional) p-value of corresponding statistical test (asymptotic)

Raises:

*   [**ValueError**](https://docs.python.org/3/builtins/exceptions.html#ValueError "(in Python v3.15)") – If `t_test` is not of a type bool
    
*   [**ValueError**](https://docs.python.org/3/builtins/exceptions.html#ValueError "(in Python v3.15)") – If `t_test=True` and `alternative=None`
    

Example (single output regression):

```python
>>> from torchmetrics.functional.regression import kendall_rank_corrcoef
>>> preds = torch.tensor([2.5, 0.0, 2, 8])
>>> target = torch.tensor([3, -0.5, 2, 1])
>>> kendall_rank_corrcoef(preds, target)
tensor(0.3333)
```

Example (multi output regression):

```python
>>> from torchmetrics.functional.regression import kendall_rank_corrcoef
>>> preds = torch.tensor([[2.5, 0.0], [2, 8]])
>>> target = torch.tensor([[3, -0.5], [2, 1]])
>>> kendall_rank_corrcoef(preds, target)
tensor([1., 1.])
```

Example (single output regression with t-test)

```python
>>> from torchmetrics.functional.regression import kendall_rank_corrcoef
>>> preds = torch.tensor([2.5, 0.0, 2, 8])
>>> target = torch.tensor([3, -0.5, 2, 1])
>>> kendall_rank_corrcoef(preds, target, t_test=True, alternative='two-sided')
(tensor(0.3333), tensor(0.4969))
```

Example (multi output regression with t-test):

```python
>>> from torchmetrics.functional.regression import kendall_rank_corrcoef
>>> preds = torch.tensor([[2.5, 0.0], [2, 8]])
>>> target = torch.tensor([[3, -0.5], [2, 1]])
>>> kendall_rank_corrcoef(preds, target, t_test=True, alternative='two-sided')
    (tensor([1., 1.]), tensor([nan, nan]))
```

---

**Navigation**

- Parent: [Regression](/docs/torchmetrics/1.9.0/regression.md)
