Need help debugging undergrad bioinformatics problem- binomial probability
1
0
Entering edit mode
9.2 years ago
m.saola ▴ 20

My program is supposed to calculate the probability of a single observation (e.g. 8 successes{heads in my case}; 10 tosses/trials; probability of a head = 0.5) plus all the extreme possibilities (e.g. 9 heads out of 10 tosses; 10 heads of 10 tosses) also known as the p-value. But my professor said that the extreme is relative depending on whether or not the number of heads is greater than or equal to 1/2 * the number of trials. So I know that if there's 2 success out of 10 trials I will count down (1 head out of 10 tosses; 0 heads out of 10 tosses) when finding the probabilities to find the p-value.

I wrote the code for the program and it is running smoothly but I am not getting the correct p-value that I should be getting because for example I tested it out with success=8, trials=10 and p=0.5. I calculated by using a calculator and then testing out my program but I'm getting two different answers. I think I've made an error in my program. Can anyone look over my code to see where I've made a mistake?

CODE:

'''This program will calculate the p-value'''

import math

trials=input('Number of trials? ')
assert type(trials)==int, 'Number of trials must be an integer'
success=input('Number of successes? ')
assert type(success)==int, 'Number of successes must be an integer'
p_success=input('Probability of success? ')
assert type(p_success)==float, 'Probability of success must be a decimal number'

success_start = success
if success_start >= (0.5*trials):
    success_stop = trials + 1
    success_increment = 1
elif success_start < (0.5*trials):
    success_stop = -1
    success_increment = -1
total_prob= 0    
for n in range(success_start, success_stop, success_increment):
    combs=math.factorial(trials)/(math.factorial(success)*math.factorial(trials-success))
    prob=(p_success**success)*((1-p_success)**(trials-success))
    binom_prob=combs*prob
    total_prob=total_prob + binom_prob


print 'For', trials, 'trials and', success, 'successes, the p-value is', total_prob
Python debugging • 1.3k views
ADD COMMENT
1
Entering edit mode

This is not a bioinfomatics problem in the strict sense.

ADD REPLY
0
Entering edit mode

Oops I mean to say I'm taking a undergraduate quantitative biology/bioinformatics class and this is my homework assignment. anyway, Python programming is quite new to me and I'm unsure why I'm getting a wrong p value for my code.

ADD REPLY
3
Entering edit mode
9.2 years ago

I won't tell you the answer, but if you add:

print 'n ', n, ' has prob ',binom_prob

at the end of your for loop I expect you'll see the problem.

ADD COMMENT
0
Entering edit mode

I made such a dumb mistake. Thank you I was able to correct it with your help!

ADD REPLY

Login before adding your answer.

Traffic: 2346 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6