Skip to content
This repository has been archived by the owner on Nov 4, 2023. It is now read-only.

Commit

Permalink
Merge pull request #31 from hoechenberger/sdt
Browse files Browse the repository at this point in the history
NF: Add A' calculation
  • Loading branch information
hoechenberger committed Feb 16, 2015
2 parents d9fcb3b + be6bf48 commit 55a5005
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
46 changes: 44 additions & 2 deletions pphelper/sdt.py
Expand Up @@ -12,6 +12,7 @@

from __future__ import division, unicode_literals
from scipy.stats import norm
import numpy as np


def d_prime(hits, false_alarms, n, nafc=1):
Expand Down Expand Up @@ -51,6 +52,47 @@ def d_prime(hits, false_alarms, n, nafc=1):
return d


def a_prime(hits, false_alarms, n, nafc=1):
"""
Calculate the sensitivity index A'.
Parameters
----------
hits : float
The number of hits when detecting a signal.
false_alarms : float
The number of false alarms.
n : int
The number of trials in target and no-target trials.
nafc : int, optional
The number of alternative choices in the task. A value of ``1``
implies a Yes/No task.
Defaults to 1.
Returns
-------
A : float
The calculated A'.
Example
-------
>>> from pphelper import sdt
>>> sdt.A_prime(20, 10, 25)
0.79166666666666674
"""
if nafc != 1:
raise NotImplementedError('Only 1-AFC implemented so far.')

hit_rate, fa_rate = _calculate_hit_and_fa_rates(hits, false_alarms, n)
A = 0.5 + \
(np.sign(hit_rate - fa_rate) *
((hit_rate - fa_rate)**2 + np.abs(hit_rate - fa_rate)) /
(4 * np.max([hit_rate, fa_rate]) - 4 * hit_rate * fa_rate))

return A


def criterion(hits, false_alarms, n, nafc=1):
"""
Calculate the decision criterion C.
Expand Down Expand Up @@ -95,11 +137,11 @@ def _calculate_hit_and_fa_rates(hits, false_alarms, n):
hit_rate = hits / n
fa_rate = false_alarms / n

# Adjust for exteme cases, loglinear approach
# Adjust for extreme cases, log-linear approach
# http://stats.stackexchange.com/a/134802
if (hit_rate == 0) or (hit_rate == 1) or (fa_rate == 0) or \
(fa_rate == 1):
hit_rate = (hits + 0.5) / (n + 1)
fa_rate = (false_alarms + 0.5) / (n + 1)

return hit_rate, fa_rate
return hit_rate, fa_rate
10 changes: 10 additions & 0 deletions pphelper/tests/test_sdt.py
Expand Up @@ -55,6 +55,16 @@ def test_d_prime_fa_rate_0():
assert np.allclose(result, result_expected)


def test_a_prime():
hits = 20
fas = 10
n = 25

result_expected = 0.79166666666666674
result = d_prime(hits, fas, n)
assert np.allclose(result, result_expected)


def test_criterion_loose():
hits = 20
fas = 10
Expand Down

0 comments on commit 55a5005

Please sign in to comment.