Kendall Rank Corr. Coef.¶
Module Interface¶
- class torchmetrics.KendallRankCorrCoef(variant='b', t_test=False, alternative='two-sided', num_outputs=1, **kwargs)[source]
Computes Kendall Rank Correlation Coefficient:
where
represents concordant pairs,
stands for discordant pairs.
where
represents concordant pairs,
stands for discordant pairs and
represents a total number of ties.
where
represents concordant pairs,
stands for discordant pairs,
is a total number of observations and
is a
min
of unique values inpreds
andtarget
sequence.Definitions according to Definition according to The Treatment of Ties in Ranking Problems.
As input to
forward
andupdate
the metric accepts the following input:preds
(Tensor
): Sequence of data in float tensor of either shape(N,)
or(N,d)
target
(Tensor
): Sequence of data in float tensor of either shape(N,)
or(N,d)
As output of
forward
andcompute
the metric returns the following output:kendall
(Tensor
): A tensor with the correlation tau statistic, and if it is not None, the p-value of corresponding statistical test.
- Parameters
variant¶ (
Literal
[‘a’, ‘b’, ‘c’]) – Indication of which variant of Kendall’s tau to be usedalternative¶ (
Optional
[Literal
[‘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
) – Number of outputs in multioutput settingkwargs¶ (
Any
) – Additional keyword arguments, see Advanced metric settings for more info.
- Raises
ValueError – If
t_test
is not of a type boolValueError – If
t_test=True
andalternative=None
- Example (single output regression):
>>> import torch >>> from torchmetrics.regression import KendallRankCorrCoef >>> preds = torch.tensor([2.5, 0.0, 2, 8]) >>> target = torch.tensor([3, -0.5, 2, 1]) >>> kendall = KendallRankCorrCoef() >>> kendall(preds, target) tensor(0.3333)
- Example (multi output regression):
>>> import torch >>> from torchmetrics.regression import KendallRankCorrCoef >>> preds = torch.tensor([[2.5, 0.0], [2, 8]]) >>> target = torch.tensor([[3, -0.5], [2, 1]]) >>> kendall = KendallRankCorrCoef(num_outputs=2) >>> kendall(preds, target) tensor([1., 1.])
- Example (single output regression with t-test):
>>> import torch >>> from torchmetrics.regression import KendallRankCorrCoef >>> preds = torch.tensor([2.5, 0.0, 2, 8]) >>> target = torch.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):
>>> import torch >>> from torchmetrics.regression import KendallRankCorrCoef >>> preds = torch.tensor([[2.5, 0.0], [2, 8]]) >>> target = torch.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]))
Initializes internal Module state, shared by both nn.Module and ScriptModule.
Functional Interface¶
- torchmetrics.functional.kendall_rank_corrcoef(preds, target, variant='b', t_test=False, alternative='two-sided')[source]
Computes Kendall Rank Correlation Coefficient.
where
represents concordant pairs,
stands for discordant pairs.
where
represents concordant pairs,
stands for discordant pairs and
represents a total number of ties.
where
represents concordant pairs,
stands for discordant pairs,
is a total number of observations and
is a
min
of unique values inpreds
andtarget
sequence.Definitions according to Definition according to The Treatment of Ties in Ranking Problems.
- Parameters
preds¶ (
Tensor
) – Sequence of data of either shape(N,)
or(N,d)
target¶ (
Tensor
) – Sequence of data of either shape(N,)
or(N,d)
variant¶ (
Literal
[‘a’, ‘b’, ‘c’]) – Indication of which variant of Kendall’s tau to be usedalternative¶ (
Optional
[Literal
[‘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
- Returns
Correlation tau statistic (Optional) p-value of corresponding statistical test (asymptotic)
- Raises
ValueError – If
t_test
is not of a type boolValueError – If
t_test=True
andalternative=None
- Example (single output regression):
>>> 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):
>>> 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)
>>> 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):
>>> 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]))