-
Notifications
You must be signed in to change notification settings - Fork 0
/
MCMCPONottinghamPhage.m
97 lines (75 loc) · 2.84 KB
/
MCMCPONottinghamPhage.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
function abcParams = MCMCPONottinghamPhage(protocolFile, tries, ...
acceptError, savePlot)
% MCMC ABC searching for parameters which explain the observed data well.
%
% This is a simplified prey only version
%
% function abcParams = MCMCPONottinghamPhage(protocolFile, tries, ...
% acceptError, savePlot)
%
% abcParams - The different parameter settings tried that passed the
% threshold for acceptance
%
% protocolFile - The data to plot
% tries - The number of parameter settings to try
% acceptError - Factor by which to adjust parameter acceptance threshold
% savePlot - Should the plots be saved
%
% Version Author Date Affiliation
% 1.00 J K Summers 02/05/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;
% put these values into the output matrix
abcParams(1, :) = curVals;
simTimes = params.times;
obsData(:, 1) = params.EColiOnly;
sigmaData = params.sigmaData(1);
sigmaMove = params.sigmaMove(1:numParams);
sigmaPrior = params.sigmaPrior;
prior = params.prior;
% evalute the log-likelihood at the current of the parameters
logCurLikely = ...
PONottinghamPhageLogLikelihoodGrowth(curVals, simTimes, ...
obsData, sigmaData, plotData);
for i = 2:tries
% update each parameter
for j = 1:numParams
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 = ...
PONottinghamPhageLogLikelihoodGrowth(candVals, ...
simTimes, obsData, sigmaData, plotData);
% 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
% store the output
abcParams(i, :) = curVals;
end
end