-
Notifications
You must be signed in to change notification settings - Fork 0
/
MCMCNottinghamPhageNoLToy.m
113 lines (87 loc) · 3.39 KB
/
MCMCNottinghamPhageNoLToy.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
function [abcParams, accepts] = MCMCNottinghamPhageNoLToy(protocolFile, ...
dataFile, tries, reportWindow, acceptError, compMode, fitAll, savePlot)
% Run the MCMC progress to attempt to fit paramters for toy data.
%
% Data and information on priors are read in from the protocol file
% Toy data is generated from "True" values stored in the protocol file
%
% function abcParams = MCMCNottinghamPhageToy(protocolFile, tries, ...
% acceptError, fitAll, savePlot, plotTitle)
%
% abcParams - The different parameter settings tried that passed the
% threshold for acceptance
% accepts - The acceptance scores
%
% protocolFile - The data to plot and other parameters
% tries - The number of parameter settings to try
% 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 09/06/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;
trueVals = params.trueVals;
% put these values into the output matrix
abcParams(1, :) = 10.^curVals;
data = readtable(dataFile);
simTimes = data.times;
simMode = params.mode(1);
dataNoise = params.dataNoise(1);
plotTitle = ['Toy ' char(params.plotName(1)) char(params.trueTitles(1)) ...
' AR' num2str(acceptError) ' I' num2str(tries) ' FA' num2str(fitAll)];
obsData = getAndPlotSpecies(trueVals, fixedVals, dataNoise, ...
simTimes, simMode, savePlot, plotTitle);
sigmaMove = params.sigmaMove(1:numParams);
minPrior = log10(params.minPrior);
maxPrior = log10(params.maxPrior);
accepts = zeros(tries, 1);
invalidParams = zeros(numParams, 1);
for i = 2:tries
if mod(i, 10) == 0
i
if i > reportWindow
(sum(accepts((i-reportWindow):i)) / reportWindow) * 100
end
end
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
% evaluate if data from the candidate values gives data within the
% acceptable error range from the observed data.
tolerable = NottinghamPhageSimGrowth(10.^candVals, ...
fixedVals, simTimes, obsData, dataNoise, simMode, compMode, ...
fitAll, acceptError);
if tolerable
curVals = candVals;
% store the acceptable values
abcParams = [abcParams; 10.^curVals'];
accepts(i) = 1;
end
end
end
plotGraphs(10.^curVals, simMode, fixedVals, simTimes, savePlot, plotTitle);
plotHistograms(log10(abcParams), paramNames, savePlot, plotTitle);
% plotAbcData(abcParams, paramNames, savePlot, plotTitle);
invalidParams * 100 / tries
sum(accepts) * 100 / tries
toc
end