# Fairness Tensor

## Introduction

Fairness.jl uses the concept of Fairness Tensor to compute metrics and speed up the computation. In Fairness.jl, FairTensor is a struct with a 3D matrix and an array of strings for the class names in protected attribute. For a FairTensor `ft`

, the 3D matrix can be accessed using `ft.mat`

and the array of strings can be accessed using `ft.labels`

.

`ft.mat`

is a 3-dimensional Array. For a dataset with C number of classes in the sensitive attribute, a fairness tensor with matrix of size size C x 2 x 2 is constructed.

It is a stack of C 2-dimensional arrays of size 2 x 2 arrays. Each 2 x 2 array represents [[TP, FP], [FN, TN]]. Here TP corresponds to True Positives, FP to False Positives, FN to False Negatives and TN to True Negatives for each class in the protected attribute.

## Using Fairness Tensor

`Fairness.FairTensor`

— Type`FairTensor{C}`

Fairness Tensor with C classes. It consists of C 2 x 2 matrices stacked up to form a Matrix of size C x 2 x 2. Each 2 x 2 matrix contains values [[TP, FP], [FN, TN]].

`Fairness.fair_tensor`

— Function`fair_tensor(ŷ, y, grp)`

Computes the fairness tensor, where ŷ are the predicted classes, y are the ground truth values, grp are the group values. The ordering follows that of `levels(y)`

.

Note that ŷ, y and grp are all categorical arrays

### Example

```
julia> using Fairness
julia> ŷ = categorical([1, 0, 1, 1, 0]);
julia> y = categorical([0, 0, 1, 1, 1]);
julia> grp = categorical(["Asian", "African", "Asian", "American", "African"]);
julia> ft = fair_tensor(ŷ, y, grp);
julia> ft.mat
3×2×2 Array{Int64, 3}:
[:, :, 1] =
0 1
1 0
1 0
[:, :, 2] =
0 1
0 0
1 0
julia> ft.labels
3-element Vector{String}:
"African"
"American"
"Asian"
```