|
| 1 | +function pStruct = getConfidenceIntervals(pStruct, alpha, type, varargin) |
| 2 | + % getConfidenceIntervals() calculates the confidence intervals for the |
| 3 | + % model parameters or properties. This is done by four approaches: |
| 4 | + % The values of CI.local_PL and CI.PL are determined by the point on which |
| 5 | + % a threshold according to the confidence level alpha (calculated by a |
| 6 | + % chi2-distribution) is reached. local_PL computes this point by a local |
| 7 | + % approximation around the MAP estimate using the Hessian matrix, PL uses |
| 8 | + % the profile likelihoods instead. |
| 9 | + % The value of CI.local_B is computed by using the cummulative distribution |
| 10 | + % function of a local approximation of the profile based on the Hessian |
| 11 | + % matrix at the MAP estimate. |
| 12 | + % The value of CI.S is calculated using samples for the model parameters |
| 13 | + % and the according percentiles based on the confidence levels alpha. |
| 14 | + % |
| 15 | + % USAGE: |
| 16 | + % * pStruct = getConfidenceIntervals(pStruct, alpha) |
| 17 | + % |
| 18 | + % Parameters: |
| 19 | + % pStruct: parameter or properties struct |
| 20 | + % alpha: vector with desired confidence levels for the intervals |
| 21 | + % varargin: |
| 22 | + % options: A PestoOptions instance |
| 23 | + % |
| 24 | + % Return values: |
| 25 | + % pStruct: updated parameter or properties struct |
| 26 | + % |
| 27 | + % Generated fields of parameters: |
| 28 | + % CI: Information about confidence levels |
| 29 | + % * local_PL: Threshold based approach, uses a local approximation by |
| 30 | + % the Hessian matrix at the MAP estimate |
| 31 | + % (requires parameters.MS, e.g. from getMultiStarts) |
| 32 | + % * PL: Threshold based approach, uses profile likelihoods |
| 33 | + % (requires parameters.P, e.g. from getParameterProfiles) |
| 34 | + % * local_B: Mass based approach, uses a local approximation by |
| 35 | + % the Hessian matrix at the MAP estimate |
| 36 | + % (requires parameters.MS, e.g. from getMultiStarts) |
| 37 | + % * S: Bayesian approach, uses percentiles based on samples |
| 38 | + % (requires parameters.S, e.g. from getParameterSamples) |
| 39 | + |
| 40 | + |
| 41 | + %% Checking and assigning inputs |
| 42 | + if length(varargin) >= 1 |
| 43 | + options = handleOptionArgument(varargin{1}); |
| 44 | + else |
| 45 | + options = PestoOptions(); |
| 46 | + end |
| 47 | + |
| 48 | + % Maximum posterior index |
| 49 | + iMAP = options.MAP_index; |
| 50 | + if (isempty(iMAP)) |
| 51 | + iMAP = 1; |
| 52 | + end |
| 53 | + |
| 54 | + % set names for fields in pStruct |
| 55 | + if strcmp(type, 'par') |
| 56 | + p_index = 'parameter_index'; |
| 57 | + else |
| 58 | + p_index = 'property_index'; |
| 59 | + end |
| 60 | + |
| 61 | + % parameter index |
| 62 | + if isempty(options.(p_index)) |
| 63 | + options.(p_index) = 1 : pStruct.number; |
| 64 | + end |
| 65 | + |
| 66 | + % Initialization |
| 67 | + pStruct.CI.alpha_levels = alpha; |
| 68 | + |
| 69 | + % Loop: alpha levels |
| 70 | + for iConfLevel = 1:length(alpha) |
| 71 | + % Loop: Parameters |
| 72 | + for iP = options.(p_index) |
| 73 | + if isfield(pStruct,'MS') |
| 74 | + pStruct = getCIfromOptimization(pStruct, alpha(iConfLevel), type, iMAP, iP, iConfLevel, options); |
| 75 | + end |
| 76 | + |
| 77 | + % Confidence intervals computed using profile likelihood |
| 78 | + if isfield(pStruct,'P') |
| 79 | + pStruct = getCIfromProfiles(pStruct, alpha(iConfLevel), type, iMAP, iP, iConfLevel); |
| 80 | + end |
| 81 | + |
| 82 | + % Confidence intervals computed using sample |
| 83 | + if isfield(pStruct,'S') |
| 84 | + pStruct.CI.S(iP,:,iConfLevel) = prctile(pStruct.S.(type)(iP,:,1),50 + 100*[-alpha(iConfLevel)/2, alpha(iConfLevel)/2]); |
| 85 | + end |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | + %% Output |
| 90 | + switch options.mode |
| 91 | + case 'visual' |
| 92 | + plotConfidenceIntervals(pStruct, alpha, [], options); |
| 93 | + disp('-> Calculation of confidence intervals for parameters FINISHED.'); |
| 94 | + case 'text' |
| 95 | + disp('-> Calculation of confidence intervals for parameters FINISHED.'); |
| 96 | + case 'silent' % no output |
| 97 | + end |
| 98 | + |
| 99 | +end |
| 100 | + |
| 101 | + |
| 102 | +function pStruct = getCIfromOptimization(pStruct, alpha, type, iMAP, iP, iConfLevel, options) |
| 103 | + |
| 104 | + if strcmp(type, 'par') |
| 105 | + % Inversion of Hessian |
| 106 | + if isempty(options.fixedParameters) |
| 107 | + Sigma = pinv(pStruct.MS.hessian(:,:,iMAP)); |
| 108 | + else |
| 109 | + Sigma = nan(pStruct.number); |
| 110 | + ind = setdiff(1:pStruct.number,options.fixedParameters); |
| 111 | + Sigma(ind,ind) = pinv(pStruct.MS.hessian(ind,ind,iMAP)); |
| 112 | + end |
| 113 | + else |
| 114 | + Sigma = pStruct.MS.prop_Sigma(:,:,iMAP); |
| 115 | + end |
| 116 | + |
| 117 | + % Confidence intervals computed using local approximation and a |
| 118 | + % threshold (-> similar to PL-based confidence intervals) |
| 119 | + pStruct.CI.local_PL(iP,1,iConfLevel) = pStruct.MS.(type)(iP,iMAP) - sqrt(icdf('chi2',alpha,1)*Sigma(iP,iP)); |
| 120 | + pStruct.CI.local_PL(iP,2,iConfLevel) = pStruct.MS.(type)(iP,iMAP) + sqrt(icdf('chi2',alpha,1)*Sigma(iP,iP)); |
| 121 | + |
| 122 | + % Confidence intervals computed using local approximation and the |
| 123 | + % probability mass (-> similar to Bayesian confidence intervals) |
| 124 | + pStruct.CI.local_B(iP,1,iConfLevel) = icdf('norm', (1-alpha)/2,pStruct.MS.(type)(iP,iMAP),sqrt(Sigma(iP,iP))); |
| 125 | + pStruct.CI.local_B(iP,2,iConfLevel) = icdf('norm',1-(1-alpha)/2,pStruct.MS.(type)(iP,iMAP),sqrt(Sigma(iP,iP))); |
| 126 | + |
| 127 | +end |
| 128 | + |
| 129 | + |
| 130 | +function pStruct = getCIfromProfiles(pStruct, alpha, type, iMAP, iP, iConfLevel) |
| 131 | + |
| 132 | + if ~isempty(pStruct.P(iP).(type)) |
| 133 | + % left bound |
| 134 | + if strcmp(type, 'par') |
| 135 | + ind = find(pStruct.P(iP).(type)(iP,:) <= pStruct.MS.(type)(iP,iMAP)); |
| 136 | + j = find(pStruct.P(iP).R(ind) <= exp(-icdf('chi2',alpha,1)/2),1,'last'); |
| 137 | + if ~isempty(j) |
| 138 | + pStruct.CI.PL(iP,1,iConfLevel) = interp1(pStruct.P(iP).R(ind([j,j+1])),... |
| 139 | + pStruct.P(iP).(type)(iP,ind([j,j+1])),exp(-icdf('chi2',alpha,1)/2)); |
| 140 | + else |
| 141 | + pStruct.CI.PL(iP,1,iConfLevel) = -inf; |
| 142 | + end |
| 143 | + else |
| 144 | + ind = find(pStruct.P(iP).(type) <= pStruct.MS.(type)(iP,1)); |
| 145 | + j = find(pStruct.P(iP).R(ind) <= exp(-icdf('chi2',alpha,1)/2),1,'last'); |
| 146 | + if ~isempty(j) |
| 147 | + pStruct.CI.PL(iP,1,iConfLevel) = interp1(pStruct.P(iP).R(ind([j,j+1])),... |
| 148 | + pStruct.P(iP).(type)(ind([j,j+1])),exp(-icdf('chi2',alpha,1)/2)); |
| 149 | + else |
| 150 | + pStruct.CI.PL(iP,1,iConfLevel) = -inf; |
| 151 | + end |
| 152 | + end |
| 153 | + |
| 154 | + % right bound |
| 155 | + if strcmp(type, 'par') |
| 156 | + ind = find(pStruct.P(iP).(type)(iP,:) >= pStruct.MS.(type)(iP,iMAP)); |
| 157 | + j = find(pStruct.P(iP).R(ind) <= exp(-icdf('chi2',alpha,1)/2),1,'first'); |
| 158 | + if ~isempty(j) |
| 159 | + pStruct.CI.PL(iP,2,iConfLevel) = interp1(pStruct.P(iP).R(ind([j-1,j])),... |
| 160 | + pStruct.P(iP).(type)(iP,ind([j-1,j])),exp(-icdf('chi2',alpha,1)/2)); |
| 161 | + else |
| 162 | + pStruct.CI.PL(iP,2,iConfLevel) = inf; |
| 163 | + end |
| 164 | + else |
| 165 | + ind = find(pStruct.P(iP).(type) >= pStruct.MS.(type)(iP,1)); |
| 166 | + j = find(pStruct.P(iP).R(ind) <= exp(-icdf('chi2',alpha,1)/2),1,'first'); |
| 167 | + if ~isempty(j) |
| 168 | + pStruct.CI.PL(iP,2,iConfLevel) = interp1(pStruct.P(iP).R(ind([j-1,j])),... |
| 169 | + pStruct.P(iP).(type)(ind([j-1,j])),exp(-icdf('chi2',alpha,1)/2)); |
| 170 | + else |
| 171 | + pStruct.CI.PL(iP,2,iConfLevel) = inf; |
| 172 | + end |
| 173 | + end |
| 174 | + |
| 175 | + else |
| 176 | + pStruct.CI.PL(iP,[1,2],iConfLevel) = nan(1,2); |
| 177 | + end |
| 178 | + |
| 179 | +end |
0 commit comments