-
Notifications
You must be signed in to change notification settings - Fork 0
/
ps4.py
135 lines (108 loc) · 3.97 KB
/
ps4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 6.00.2x Problem Set 4
import numpy
import random
import pylab
from ps3b import *
#
# PROBLEM 1
#
def simulationDelayedTreatment(numTrials):
"""
Runs simulations and make histograms for problem 1.
Runs numTrials simulations to show the relationship between delayed
treatment and patient outcome using a histogram.
Histograms of final total virus populations are displayed for delays of 300,
150, 75, 0 timesteps (followed by an additional 150 timesteps of
simulation).
numTrials: number of simulation runs to execute (an integer)
"""
# parameters
numViruses = 100
maxPop = 1000
maxBirthProb = 0.1
clearProb = 0.05
resistances = {'guttagonol': False}
mutProb = 0.005
conditions = [300, 150, 75, 0]
# trials
for m in conditions:
Pop = []
Pop_eachTrial = []
for n in range(numTrials):
virusList = []
for i in range(numViruses):
virusList.append(ResistantVirus(maxBirthProb, clearProb, resistances, mutProb))
trePatient = TreatedPatient(virusList, maxPop)
for j in range(m):
Pop.append(trePatient.update())
trePatient.addPrescription('guttagonol')
for j in range(m, m + 150):
Pop.append(trePatient.update())
Pop_eachTrial.append(Pop[-1])
# graphing
pylab.xlabel('Final Total Virus Population')
pylab.ylabel('Number of Trials')
pylab.title('The Effect of Delaying ' + str(m) + ' Times Treatment On Patinet Disease')
pylab.hist(Pop_eachTrial, bins = 10)
pylab.xlim(0, 200)
pylab.figure()
# end of trials
#
# PROBLEM 2
#
def simulationTwoDrugsDelayedTreatment(numTrials):
"""
Runs simulations and make histograms for problem 2.
Runs numTrials simulations to show the relationship between administration
of multiple drugs and patient outcome.
Histograms of final total virus populations are displayed for lag times of
300, 150, 75, 0 timesteps between adding drugs (followed by an additional
150 timesteps of simulation).
numTrials: number of simulation runs to execute (an integer)
"""
# TODO
# parameters
numViruses = 100
maxPop = 1000
maxBirthProb = 0.1
clearProb = 0.05
resistances = {'guttagonol': False, 'grimpex': False}
mutProb = 0.005
conditions = [300, 150, 75, 0]
# trials
for m in conditions:
Pop = []
Pop_eachTrial = []
for n in range(numTrials):
virusList = []
for i in range(numViruses):
virusList.append(ResistantVirus(maxBirthProb, clearProb, resistances, mutProb))
trePatient = TreatedPatient(virusList, maxPop)
for j in range(150):
Pop.append(trePatient.update())
trePatient.addPrescription('guttagonol')
for j in range(150, 150 + m):
Pop.append(trePatient.update())
trePatient.addPrescription('grimpex')
for j in range(150 + m, 300 + m):
Pop.append(trePatient.update())
Pop_eachTrial.append(Pop[-1])
mean = sum(Pop_eachTrial)/float(numTrials)
sd = stdDev(Pop_eachTrial)
# graphing
pylab.xlabel('Final Total Virus Population')
pylab.ylabel('Number of Trials')
pylab.title('The Effect of Delaying ' + str(m) + ' Times Treatment On Patinet Disease')
pylab.hist(Pop_eachTrial, bins = 20)
pylab.xlim(0, 600)
xmin, xmax = pylab.xlim()
ymin, ymax = pylab.ylim()
pylab.text((xmax-xmin)*0.8, (ymax-ymin)/2,
'Mean = ' + str(round(mean, 4)) + '\nSD = ' + str(round(sd, 4)))
pylab.figure()
def stdDev(X):
mean = sum(X)/float(len(X))
tot = 0.0
for x in X:
tot += (x - mean)**2
return (tot/len(X))**0.5