-
Notifications
You must be signed in to change notification settings - Fork 0
/
MCMCNottinghamPhageNoLEven.m
150 lines (120 loc) · 4.39 KB
/
MCMCNottinghamPhageNoLEven.m
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
function [abcParams, accepts] = ...
MCMCNottinghamPhageNoLEven(protocolFile, dataFile, sucesses, ...
reportWindow, acceptError, compMode, fitAll, savePlot)
% Run the MCMC progress to attempt to fit paramters for observed data.
%
% Data and information on priors are read in from the data and protocol
% files
%
% function abcParams = MCMCNottinghamPhageNoL(protocolFile, dataFile,
% tries, reportWindow, acceptError, compMode, fitAll, savePlot)
%
% abcParams - The different parameter settings tried that passed the
% threshold for acceptance
% accepts - The acceptance scores
%
% protocolFile - The parameters for the fitting process
% dataFile - Data to fit to
% tries - The number of parameter settings to try
% reportWindow - Show acceptance percentage over the last this many tries
% acceptError - Factor by which to adjust parameter acceptance threshold
% compMode - How should simulated and observed data be compared?
% fitAll - If False do not fit 2 predator data set.
% savePlot - Should the plots be saved
% Version Author Date Affiliation
% 1.00 J K Summers 10/09/17 Kreft Lab - School of Biosciences -
% University of Birmingham
%
tic
params = readtable(protocolFile);
numParams = params.numParams(1);
% initial values
paramNames = params.paramNames(1: numParams);
curVals = log10(params.initVals(1: numParams));
fixedVals = params.fixedVals;
abcParams = [];
data = readtable(dataFile);
simTimes = data.times;
simMode = params.mode(1);
dataNoise = params.dataNoise(1);
plotTitle = [char(params.plotName(1)) char(params.trueTitles(1)) ...
' AR' num2str(acceptError) ' I' num2str(sucesses) ...
' FA' num2str(fitAll) ' '];
obsData(:, 1) = data.EColiOnly;
obsData(:, 2) = data.EColiWithBd;
obsData(:, 3) = data.BdWithEColiOnly;
obsData(:, 4) = data.EColiWithPhage;
obsData(:, 5) = data.PhageWithEColiOnly;
obsData(:, 6) = data.EColiAll;
obsData(:, 7) = data.BdAll;
obsData(:, 8) = data.PhageAll;
sigmaMove = params.sigmaMove(1:numParams);
minPrior = log10(params.minPrior);
maxPrior = log10(params.maxPrior);
accepts = [];
dataGaps = [];
invalidParams = zeros(numParams, 1);
i = 0;
bestVal = acceptError + 1;
inbounds = 0;
setTries = 0;
while i < sucesses
setTries = setTries + 1;
validParams = true;
candVals = curVals;
% update each parameter
for j = 1:numParams
% propose a new value from a normal range
candVals(j) = normrnd(curVals(j), sigmaMove(j));
if (candVals(j) < minPrior(j)) || (candVals(j) > maxPrior(j))
validParams = false;
invalidParams(j) = invalidParams(j) + 1;
break;
end
end
if validParams
if mod(inbounds, 100) == 0
inbounds
if inbounds > reportWindow
currPer = (sum(accepts((inbounds - reportWindow):inbounds)) / ...
reportWindow) * 100
tolPer = sum(accepts(1:inbounds) / inbounds) * 100
end
end
% evaluate if data from the candidate values gives data within the
% acceptable error range from the observed data.
[tolerable, dataGap] = ...
NottinghamPhageSimGrowth(10.^candVals, ...
fixedVals, simTimes, obsData, dataNoise, simMode, compMode, ...
fitAll, acceptError);
inbounds = inbounds + 1;
dataGaps(inbounds) = dataGap;
if tolerable
curVals = candVals;
% store the acceptable values
abcParams = [abcParams; 10.^curVals'];
accepts(inbounds) = 1;
if dataGap < bestVal
bestVal = dataGap
bestParams = 10.^curVals;
end
i = i + 1;
else
accepts(inbounds) = 0;
end
end
end
if sum(accepts) > 0
plotGraphs(abcParams(size(abcParams, 1), :), simMode, fixedVals, ...
simTimes, savePlot, plotTitle);
plotHistograms(log10(abcParams), paramNames, true, savePlot, ...
plotTitle);
plotAcceptances(accepts, savePlot, plotTitle);
plotParamsOutOfBounds(invalidParams, setTries, savePlot, plotTitle);
plotGaps(dataGaps, savePlot, plotTitle);
% plotAbcData(abcParams, paramNames, savePlot, plotTitle);
end
% invalidParams * 100 / setTries
sum(accepts) * 100 / sucesses
toc
end