codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
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 )) + "%"
Private
[
?
]
Run code
Submit