# Easy way to calculate d-prime with Python.

In signal detection theory d-prime index is used as sensitivity index, a higher index indicates that the signal can be more readily detected.

d-prime=0 is considered as pure guessing.

d-prime=1 is considered as good measure of signal sensitivity/detectability.

d-prime=2 is considered as awesome.

Higher sensitivity rates are possible, but really rare in real life.

hit rate H: proportion of YES trials to which subject responded YES = P("yes" | YES)

false alarm rate F: proportion of NO trials to which subject responded YES = P("yes" | NO)

The formula for d’ is as follows:

d’ = z(H) – z(FA)

where z(H) is z-score for hits and z(FA) is z-score for false alarms.

## Simple example of d-prime calculation:

```
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as stats
# hit rates and false alarm rates
hitP = 23/30
faP = 4/30
# z-scores
hitZ = stats.norm.ppf(hitP)
faZ = stats.norm.ppf(faP)
# d-prime
dPrime = hitZ-faZ
print(dPrime)
```

OUT: 1.8386849075184297

## Extreme case with hit rate =0:

IMPORTANT: hit rates and false alarm rates equal to 0 or 100 will give misleading values of d-prime and calculation should be adjusted by substructing extremely low values from these rates. This little trick will have almost no effect in normal cases.

```
hitZ = stats.norm.ppf(0/30)
faZ = stats.norm.ppf(22/30)
print(hitZ-faZ)
```

OUT: -inf