[ create a new paste ] login | about

Link: http://codepad.org/pRH25OxC    [ raw code | output | fork ]

Python, pasted on Dec 12:
import random
import time

def get_random():
    return random.random()
seed = time.time()
random.seed(seed)

population_size = 1000000

#create list of population
population = []
for i in range(population_size):
    if get_random() < 0.01:
        population.append(1)
    else:
        population.append(0)

#create test results
test_results_for_population = []
for i in range(population_size):
    if population[i]==0:
	if get_random() < 0.05:
	    test_results_for_population.append(1)
        else: 
	    test_results_for_population.append(0)
    else:
	if get_random() < 0.95:
	    test_results_for_population.append(1)
        else: 
	    test_results_for_population.append(0)

#count people with disease
people_with_disease = 0
for person in population:
    if person ==1:
        people_with_disease += 1
print "Number of people in population is " + str(population_size)
print "Number of people in population who actually have disease is " + str(people_with_disease)
print "Percentage of people in population who actually have disease is " + str(people_with_disease*100.0/population_size) + "%"

#count correct number of positive test results
correct_positives = 0
for i, person in enumerate(population):
    if person ==1 and test_results_for_population[i]==1:
        correct_positives += 1
print "Number of correct positives is " + str(correct_positives)

#count number of false positive test results
false_positives = 0
for i, person in enumerate(population):
    if person ==0 and test_results_for_population[i]==1:
        false_positives += 1
print "Number of false positives is " + str(false_positives)
print "Ratio of correct positives to total positives is " + str(correct_positives*1.0/(false_positives+correct_positives ))
print "Percentage chance a positive result is correct " + str(correct_positives*100.0/(false_positives+correct_positives )) + "%"


Output:
1
2
3
4
5
6
7
Number of people in population is 1000000
Number of people in population who actually have disease is 9929
Percentage of people in population who actually have disease is 0.9929%
Number of correct positives is 9434
Number of false positives is 49739
Ratio of correct positives to total positives is 0.15943082149
Percentage chance a positive result is correct 15.943082149%


Create a new paste based on this one


Comments: