-
Notifications
You must be signed in to change notification settings - Fork 0
/
MCMCNottinghamPhageToySmooth.m
147 lines (111 loc) · 4.57 KB
/
MCMCNottinghamPhageToySmooth.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
function abcParams = MCMCNottinghamPhageToySmooth(protocolFile, tries, ...
acceptError, compMode, fitAll, windowWidth, savePlot, plotTitle)
% 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
%
% 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
% plotTitle - Base file name for all plots
% Version Author Date Affiliation
% 1.00 J K Summers 09/06/17 Kreft Lab - School of Biosciences -
% University of Birmingham
%
params = readtable(protocolFile);
numParams = params.numParams(1);
% initial values
paramNames = params.paramNames(1: numParams);
curVals = params.initVals(1: numParams);
fixedVals = params.fixedVals;
trueVals = params.trueVals;
% put these values into the output matrix
abcParams(1, :) = curVals;
simTimes = params.times;
simMode = params.mode(1);
sigmaData = params.sigmaData(1);
dataNoise = params.dataNoise(1);
obsData = getAndPlotSpecies(trueVals, fixedVals, sigmaData, dataNoise, ...
simTimes, simMode, savePlot, plotTitle);
sigmaMove = params.sigmaMove(1:numParams);
sigmaPrior = params.sigmaPrior;
prior = params.prior;
% evalute the log-likelihood at the current of the parameters
logCurLikely = ...
NottinghamPhageLogLikelihoodGrowth(curVals, fixedVals, ...
simTimes, obsData, dataNoise, simMode, compMode, fitAll);
allConverged = false;
converged = zeros(numParams, 1);
i = 2;
while (i <= tries) && ~allConverged
if mod(i, 10) == 0
i
end
% update each parameter
for j = 1:numParams
if ~converged(j)
candVals = curVals;
% propose a new value from a normal range
candVals(j) = normrnd(curVals(j), sigmaMove(j));
if candVals(j) > 0
% evaluate the log-likelihood at the candidate value
logCandLikely = ...
NottinghamPhageLogLikelihoodGrowth(candVals, ...
fixedVals, simTimes, obsData, dataNoise, simMode, ...
compMode, fitAll);
% evaluate the log prior density, i.e. the density of a
% Normal distribution centered on prior(j), with a sigma
% of prior.sigma(j)
logPriorCand = log(normpdf(candVals(j), prior(j), ...
sigmaPrior(j)));
% do the same but for the current value
logPriorCur = log(normpdf(curVals(j), prior(j), ...
sigmaPrior(j)));
% evaluate the log numerator and log denominator of the
% M-H ratio
logPriorCand = logCandLikely + logPriorCand;
logPriorCur = logCurLikely + logPriorCur;
% accept reject mechanism
accept = rand;
if log(accept) < (logPriorCand - logPriorCur) * ...
acceptError
curVals(j) = candVals(j);
logCurLikely = logCandLikely;
end
end
end
end
% store the output
abcParams(i, :) = curVals;
if i > windowWidth
smoothParams(i - windowWidth, :) = ...
medianWindow(abcParams((i - windowWidth) : i, :));
if i > (windowWidth + 20)
for j = 1:numParams
if ~converged(j)
converged(j) = ...
SteadyState(1:(i - windowWidth), ...
smoothParams(:, j), 20);
if converged(j)
a = 1;
end
end
end
end
allConverged = ~sum(~converged);
end
i = i + 1;
end
plotGraphs(curVals, simMode, fixedVals, simTimes, savePlot, plotTitle);
plotAbcData(abcParams, paramNames, savePlot, plotTitle);
end