Python Basics and Biostatistics

Sensitivity and Specificity

Back to index
In this lesson the student will learn how to:
  1. calculate sensitivity and specificity
  2. create functions in Python
By the end of this lesson the student will be able to:

   Write a script which calculates sensitivity
   and specificity using functions.

Sensitivity - the fraction of all those with the disease who get a positive test result.

Specificity - the fraction of those without the disease who get a negative test result.

These concepts can be represented as:


                    TP                             TN
   Sensitivity = -------          Specificity = -------
                 TP + FN                        TN + FP

TP - true positive
FP - false positive
TN - true negative
FN - false negative

These categories can be represented like this in a simple table:
Disease
Present
Disease
Absent
Test PositiveTPFP
Test NegativeFNTN
In general a test with high sensitivity can rule out a disorder with a negative result since relatively few negative results will be false negatives. A test with high specificity will rule in the disorder with a positive result since relatively few positive tests are false positive.

A Perfect Test

In a perfect test there is a perfect correlation between those with the disease and a positive test result and those who do not have the disease and a negative test result. In a typical test there is a good deal of overlap between these groups. A perfect test can be represented like this:

The dashed line represents the demarcation between positive and negative test results. The experimenter sets this line and in the case of the perfect test it is easy to decide where to place it. In the case of the typical test, this decision is not so easy. Due to the overlap between the groups, the placement of the line of demarcation has an impact on the diagnosis rendered. Setting it further to the right increases the number of false negatives (which means that more people with the disease may be considered not to have the disease - this is not a good thing). Setting the line of demarcation further to the left increase the number of false positives (which means that more people without the disease will be told that they do have the disease which could be psychologically traumatic for them, but further testing should resolve this situation).

Functions in Python

A functions (aka, method or subroutine) is a separate body of code designed to perform a particular task. Functions are called or invoked from the main body of your script. The act of invoking a functions is sometimes called calling the function or a function call. Here's a quick example:

#!/usr/bin/python nums = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24] def s2(ar): odd = [] even = [] for s in ar: if s%2==0: even.append(s) else: odd.append(s) return odd, evenp = s2(nums) print "ODD: " print p[0] print "EVEN: " print p[1]

This program could have been written slightly differently like this:

#!/usr/bin/python nums = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24] def even(ar): even = [] for s in ar: if s%2==0: even.append(s) return even def odd(ar): odd = [] for s in ar: if s%2==1: odd.append(s) return odd no = odd(nums) print "ODD: " print no ne = even(nums) print "EVEN:" print ne
Here's another example for you:
#!/usr/bin/python nums = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24] def sort_them_out(ar): odd = "ODD NUMBERS: " even = "EVEN NUMBERS: " for a in ar: if a%2 == 0 : even = even + str(a) + ", " else: odd = odd + str(a) + ", " print odd print even sort_them_out(nums)

ASSIGNMENT:

Write a script with the following functions:

Some of these functions will be very short, but you should organize your code to utilize these functions anyways for the sake of this assignment in order to demonstrate that you understand how to define and call functions.