Shortcuts

# Group Fairness¶

## Module Interface¶

### BinaryFairness¶

class torchmetrics.classification.BinaryFairness(num_groups, task='all', threshold=0.5, ignore_index=None, validate_args=True, **kwargs)[source]

Computes Demographic parity and Equal opportunity ratio for binary classification problems.

Accepts the following input tensors:

• preds (int or float tensor): (N, ...). If preds is a floating point tensor with values outside [0,1] range we consider the input to be logits and will auto apply sigmoid per element. Addtionally, we convert to int tensor with thresholding using the value in threshold.

• groups (int tensor): (N, ...). The group identifiers should be 0, 1, ..., (num_groups - 1).

• target (int tensor): (N, ...).

The additional dimensions are flatted along the batch dimension.

This class computes the ratio between positivity rates and true positives rates for different groups. If more than two groups are present, the disparity between the lowest and highest group is reported. A disparity between positivity rates indicates a potential violation of demographic parity, and between true positive rates indicates a potential violation of equal opportunity.

The lowest rate is divided by the highest, so a lower value means more discrimination against the numerator. In the results this is also indicated as the key of dict is {metric}_{identifier_low_group}_{identifier_high_group}.

Parameters
Returns

The metric returns a dict where the key identifies the metric and groups with the lowest and highest true positives rates as follows: {metric}__{identifier_low_group}_{identifier_high_group}. The value is a tensor with the disparity rate.

Example (preds is int tensor):
>>> from torchmetrics.classification import BinaryFairness
>>> target = torch.tensor([0, 1, 0, 1, 0, 1])
>>> preds = torch.tensor([0, 1, 0, 1, 0, 1])
>>> groups = torch.tensor([0, 1, 0, 1, 0, 1])
>>> metric = BinaryFairness(2)
>>> metric(preds, target, groups)
{'DP_0_1': tensor(0.), 'EO_0_1': tensor(0.)}

Example (preds is float tensor):
>>> from torchmetrics.classification import BinaryFairness
>>> target = torch.tensor([0, 1, 0, 1, 0, 1])
>>> preds = torch.tensor([0.11, 0.84, 0.22, 0.73, 0.33, 0.92])
>>> groups = torch.tensor([0, 1, 0, 1, 0, 1])
>>> metric = BinaryFairness(2)
>>> metric(preds, target, groups)
{'DP_0_1': tensor(0.), 'EO_0_1': tensor(0.)}


Initializes internal Module state, shared by both nn.Module and ScriptModule.

plot(val=None, ax=None)[source]

Plot a single or multiple values from the metric.

Parameters
Return type

Tuple[Figure, Union[Axes, ndarray]]

Returns

Figure object and Axes object

Raises

ModuleNotFoundError – If matplotlib is not installed

>>> from torch import rand, randint
>>> # Example plotting a single value
>>> from torchmetrics.classification import BinaryFairness
>>> metric = BinaryFairness(2)
>>> metric.update(rand(20), randint(2,(20,)), randint(2,(20,)))
>>> fig_, ax_ = metric.plot()

>>> from torch import rand, randint, ones
>>> # Example plotting multiple values
>>> from torchmetrics.classification import BinaryFairness
>>> metric = BinaryFairness(2)
>>> values = [ ]
>>> for _ in range(10):
...     values.append(metric(rand(20), randint(2,(20,)), ones(20).long()))
>>> fig_, ax_ = metric.plot(values)


### BinaryGroupStatRates¶

class torchmetrics.classification.BinaryGroupStatRates(num_groups, threshold=0.5, ignore_index=None, validate_args=True, **kwargs)[source]

Computes the true/false positives and true/false negatives rates for binary classification by group.

Related to Type I and Type II errors.

Accepts the following input tensors:

• preds (int or float tensor): (N, ...). If preds is a floating point tensor with values outside [0,1] range we consider the input to be logits and will auto apply sigmoid per element. Addtionally, we convert to int tensor with thresholding using the value in threshold.

• target (int tensor): (N, ...).

• groups (int tensor): (N, ...). The group identifiers should be 0, 1, ..., (num_groups - 1).

The additional dimensions are flatted along the batch dimension.

Parameters
Returns

The metric returns a dict with a group identifier as key and a tensor with the tp, fp, tn and fn rates as value.

Example (preds is int tensor):
>>> from torchmetrics.classification import BinaryGroupStatRates
>>> target = torch.tensor([0, 1, 0, 1, 0, 1])
>>> preds = torch.tensor([0, 1, 0, 1, 0, 1])
>>> groups = torch.tensor([0, 1, 0, 1, 0, 1])
>>> metric = BinaryGroupStatRates(num_groups=2)
>>> metric(preds, target, groups)
{'group_0': tensor([0., 0., 1., 0.]), 'group_1': tensor([1., 0., 0., 0.])}

Example (preds is float tensor):
>>> from torchmetrics.classification import BinaryGroupStatRates
>>> target = torch.tensor([0, 1, 0, 1, 0, 1])
>>> preds = torch.tensor([0.11, 0.84, 0.22, 0.73, 0.33, 0.92])
>>> groups = torch.tensor([0, 1, 0, 1, 0, 1])
>>> metric = BinaryGroupStatRates(num_groups=2)
>>> metric(preds, target, groups)
{'group_0': tensor([0., 0., 1., 0.]), 'group_1': tensor([1., 0., 0., 0.])}


Initializes internal Module state, shared by both nn.Module and ScriptModule.

## Functional Interface¶

### binary_fairness¶

torchmetrics.functional.classification.binary_fairness(preds, target, groups, task='all', threshold=0.5, ignore_index=None, validate_args=True)[source]

Compute either Demographic parity and Equal opportunity ratio for binary classification problems.

This is done by setting the task argument to either 'demographic_parity', 'equal_opportunity' or all. See the documentation of _compute_binary_demographic_parity() and _compute_binary_equal_opportunity() for the specific details of each argument influence and examples.

Parameters
• preds (Tensor) – Tensor with predictions.

• target (Tensor) – Tensor with true labels (not required for demographic_parity).

• groups (Tensor) – Tensor with group identifiers. The group identifiers should be 0, 1, ..., (num_groups - 1).

• task (Literal[‘demographic_parity’, ‘equal_opportunity’, ‘all’]) – The task to compute. Can be either demographic_parity or equal_oppotunity or all.

• threshold (float) – Threshold for transforming probability to binary {0,1} predictions.

• ignore_index (Optional[int]) – Specifies a target value that is ignored and does not contribute to the metric calculation

• validate_args (bool) – bool indicating if input arguments and tensors should be validated for correctness. Set to False for faster computations.

Return type

### demographic_parity¶

torchmetrics.functional.classification.demographic_parity(preds, groups, threshold=0.5, ignore_index=None, validate_args=True)[source]

Demographic parity compares the positivity rates between all groups.

If more than two groups are present, the disparity between the lowest and highest group is reported. The lowest positivity rate is divided by the highest, so a lower value means more discrimination against the numerator. In the results this is also indicated as the key of dict is DP_{identifier_low_group}_{identifier_high_group}. where represents the positivity rate for group .

Accepts the following input tensors:

• preds (int or float tensor): (N, ...). If preds is a floating point tensor with values outside [0,1] range we consider the input to be logits and will auto apply sigmoid per element. Addtionally, we convert to int tensor with thresholding using the value in threshold.

• groups (int tensor): (N, ...). The group identifiers should be 0, 1, ..., (num_groups - 1).

• target (int tensor): (N, ...).

The additional dimensions are flatted along the batch dimension.

Parameters
• preds (Tensor) – Tensor with predictions.

• groups (Tensor) – Tensor with group identifiers. The group identifiers should be 0, 1, ..., (num_groups - 1).

• threshold (float) – Threshold for transforming probability to binary {0,1} predictions.

• ignore_index (Optional[int]) – Specifies a target value that is ignored and does not contribute to the metric calculation

• validate_args (bool) – bool indicating if input arguments and tensors should be validated for correctness. Set to False for faster computations.

Return type
Returns

The metric returns a dict where the key identifies the group with the lowest and highest positivity rates as follows: DP_{identifier_low_group}_{identifier_high_group}. The value is a tensor with the DP rate.

Example (preds is int tensor):
>>> from torchmetrics.functional.classification import demographic_parity
>>> preds = torch.tensor([0, 1, 0, 1, 0, 1])
>>> groups = torch.tensor([0, 1, 0, 1, 0, 1])
>>> demographic_parity(preds, groups)
{'DP_0_1': tensor(0.)}

Example (preds is float tensor):
>>> from torchmetrics.functional.classification import demographic_parity
>>> preds = torch.tensor([0.11, 0.84, 0.22, 0.73, 0.33, 0.92])
>>> groups = torch.tensor([0, 1, 0, 1, 0, 1])
>>> demographic_parity(preds, groups)
{'DP_0_1': tensor(0.)}


### equal_opportunity¶

torchmetrics.functional.classification.equal_opportunity(preds, target, groups, threshold=0.5, ignore_index=None, validate_args=True)[source]

Equal opportunity compares the true positive rates between all groups.

If more than two groups are present, the disparity between the lowest and highest group is reported. The lowest true positive rate is divided by the highest, so a lower value means more discrimination against the numerator. In the results this is also indicated as the key of dict is EO_{identifier_low_group}_{identifier_high_group}. where represents the true positives rate for group .

Accepts the following input tensors:

• preds (int or float tensor): (N, ...). If preds is a floating point tensor with values outside [0,1] range we consider the input to be logits and will auto apply sigmoid per element. Addtionally, we convert to int tensor with thresholding using the value in threshold.

• target (int tensor): (N, ...).

• groups (int tensor): (N, ...). The group identifiers should be 0, 1, ..., (num_groups - 1).

The additional dimensions are flatted along the batch dimension.

Parameters
Return type
Returns

The metric returns a dict where the key identifies the group with the lowest and highest true positives rates as follows: EO_{identifier_low_group}_{identifier_high_group}. The value is a tensor with the EO rate.

Example (preds is int tensor):
>>> from torchmetrics.functional.classification import equal_opportunity
>>> target = torch.tensor([0, 1, 0, 1, 0, 1])
>>> preds = torch.tensor([0, 1, 0, 1, 0, 1])
>>> groups = torch.tensor([0, 1, 0, 1, 0, 1])
>>> equal_opportunity(preds, target, groups)
{'EO_0_1': tensor(0.)}

Example (preds is float tensor):
>>> from torchmetrics.functional.classification import equal_opportunity
>>> target = torch.tensor([0, 1, 0, 1, 0, 1])
>>> preds = torch.tensor([0.11, 0.84, 0.22, 0.73, 0.33, 0.92])
>>> groups = torch.tensor([0, 1, 0, 1, 0, 1])
>>> equal_opportunity(preds, target, groups)
{'EO_0_1': tensor(0.)}


### binary_groups_stat_rates¶

torchmetrics.functional.classification.binary_groups_stat_rates(preds, target, groups, num_groups, threshold=0.5, ignore_index=None, validate_args=True)[source]

Compute the true/false positives and true/false negatives rates for binary classification by group.

Related to Type I and Type II errors.

Accepts the following input tensors:

• preds (int or float tensor): (N, ...). If preds is a floating point tensor with values outside [0,1] range we consider the input to be logits and will auto apply sigmoid per element. Addtionally, we convert to int tensor with thresholding using the value in threshold.

• target (int tensor): (N, ...).

• groups (int tensor): (N, ...). The group identifiers should be 0, 1, ..., (num_groups - 1).

The additional dimensions are flatted along the batch dimension.

Parameters
Return type
Returns

The metric returns a dict with a group identifier as key and a tensor with the tp, fp, tn and fn rates as value.

Example (preds is int tensor):
>>> from torchmetrics.functional.classification import binary_groups_stat_rates
>>> target = torch.tensor([0, 1, 0, 1, 0, 1])
>>> preds = torch.tensor([0, 1, 0, 1, 0, 1])
>>> groups = torch.tensor([0, 1, 0, 1, 0, 1])
>>> binary_groups_stat_rates(preds, target, groups, 2)
{'group_0': tensor([0., 0., 1., 0.]), 'group_1': tensor([1., 0., 0., 0.])}

Example (preds is float tensor):
>>> from torchmetrics.functional.classification import binary_groups_stat_rates
>>> target = torch.tensor([0, 1, 0, 1, 0, 1])
>>> preds = torch.tensor([0.11, 0.84, 0.22, 0.73, 0.33, 0.92])
>>> groups = torch.tensor([0, 1, 0, 1, 0, 1])
>>> binary_groups_stat_rates(preds, target, groups, 2)
{'group_0': tensor([0., 0., 1., 0.]), 'group_1': tensor([1., 0., 0., 0.])}


© Copyright Copyright (c) 2020-2023, Lightning-AI et al... Revision 1edf6a11.

Built with Sphinx using a theme provided by Read the Docs.
Versions
latest
stable
v0.11.4
v0.11.3
v0.11.2
v0.11.1
v0.11.0
v0.10.3
v0.10.2
v0.10.1
v0.10.0
v0.9.3
v0.9.2
v0.9.1
v0.9.0
v0.8.2
v0.8.1
v0.8.0
v0.7.3
v0.7.2
v0.7.1
v0.7.0
v0.6.2
v0.6.1
v0.6.0
v0.5.1
v0.5.0
v0.4.1
v0.4.0
v0.3.2
v0.3.1
v0.3.0
v0.2.0
v0.1.0