Python Basics and Biostatistics

Bayes' Thereom

Back to index
In this lesson the student will learn how to:
  1. convert odds to probability and vice versa
  2. use nested and recursive subroutines
  3. calculate post-test odds
By the end of this lesson the student will be able to:

  Write scripts to convert between odds and probability 
  and to calculate post-test odds.

Quick Review of Probability and Odds

Converting Between Odds and Probability

             probability
    Odds = ---------------
           1 - probability

                     odds
    Probability =  --------
                   1 + odds

For instance, if the probability of an event occurring is 25% (or 1/4) the odds will be 1:3. Likewise, if the odds of an event occurring are 2:3 the probability will be 40% (or 2/5).

Bayes' Equation

The Bayes' equation adapted for clinical diagnosis looks like this:


                                      sensitivity
    Post-test odds = pretest odds * ---------------
                                    1 - specificity

The post-test odds are the odds that a patient has the disease, taking into account both the test results and your prior knowledge about the patient. The pretest odds are the odds that the patient has the disease determined from information you know before running the test. The ratio sensitivity / (1 - specificity) is called the likelihood ratio. It is the probability of obtaining the positive test result in a patient with the disease (sensitivity) divided by the probability of obtaining a positive test result in a patient without the disease (1 - specificity). So, Bayes' equation can be written in a simpler (and more general) form:

    Post-test odds = pretest odds * likelihood ratio

As you will recall from a prior lesson:

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

Genetic Diseases and Penetrance

For some genetic diseases you need to distinguish the sensitivity to detect the genetic defect from the sensitivity to detect clinical disease. Some genetic diseases have poor penetrance, meaning that some people with the abnormal gene do not get the disease. A test that detects the gene with few false positives would produce a lot of false positives when assessed for its ability to detect the clinical disease.

Nested Subroutines and Recursion

Take a look at this nested subroutine call. It's just an example showing a subroutine being called from within another subroutine.

#!/usr/bin/python def innercall(): print "inside inner" def outercall(): print "before inner" innercall() print "after inner" outercall()
You can make fairly long chains of subroutines by calling one subroutine from another subroutine and then another and another, etc. You can also make a subroutine which calls itself. This is called recursion. The problem with recursion is that we could wind up with an infinite loop if we aren't careful. In fact this first example shows an infinite loop. Press ^C to kill the script.
#!/usr/bin/python def rekur(c): print "COUNT: " + str(c) c+=1 rekur(c) rekur(1)
Here's one way to solve this problem:
#!/usr/bin/python def rekur(c,t): print "COUNT: " + str(c) c+=1 if(c<t): rekur(c,t) else: print "FINISHED:" + str(c) rekur(1,10)

ASSIGNMENT:

  1. Write a script to convert odds to probability and vice versa. Write your script to ask the user which type of conversion to perform and then take a decimal based value to convert. In other words, you don't want 3:1 as input.
  2. Write a script to calculate post-test odds from input for pretest odds, sensitivity and specificity.