0001 function PosteriorOddsTable = model_comparison(ModelNames,ModelPriors,oo,options_,fname)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032 NumberOfModels = size(ModelNames,2);
0033
0034 disp(' ')
0035 disp(' ')
0036 if isempty(ModelPriors)
0037 prior_flag = 0;
0038 ModelPriors = ones(NumberOfModels,1)/NumberOfModels;
0039 else
0040 prior_flag = 1;
0041 improper = abs(sum(ModelPriors)-1)>1e-6;
0042 if improper
0043 disp('model_comparison:: The user supplied prior distribution over models is improper...')
0044 disp('model_comparison:: The distribution is automatically rescaled!')
0045 ModelPriors=ModelPriors/sum(ModelPriors);
0046 end
0047 end
0048
0049
0050
0051 if isfield(options_,'model_comparison_approximation')
0052 type = options_.model_comparison_approximation;
0053 if strcmp(type,'Laplace')
0054 type = 'LaplaceApproximation';
0055 end
0056 else
0057 type = 'LaplaceApproximation';
0058 end
0059
0060
0061 MarginalLogDensity = zeros(NumberOfModels,1);
0062 ShortModelNames = get_short_names(ModelNames);
0063 iname = strmatch(fname,ShortModelNames,'exact');
0064
0065 for i=1:NumberOfModels
0066 if i==iname
0067 mstruct.oo_ = oo;
0068 else
0069 if strcmpi(ModelNames{i}(end-3:end),'.mod') || strcmpi(ModelNames{i}(end-3:end),'.dyn')
0070 mstruct = load([ModelNames{i}(1:end-4) '_results.mat' ],'oo_');
0071 else
0072 mstruct = load([ModelNames{i} '_results.mat' ],'oo_');
0073 end
0074 end
0075 try
0076 eval(['MarginalLogDensity(i) = mstruct.oo_.MarginalDensity.' type ';'])
0077 catch
0078 if strcmpi(type,'LaplaceApproximation')
0079 disp(['MODEL_COMPARISON: I cant''t find the Laplace approximation associated to model ' ModelNames{i}])
0080 return
0081 elseif strcmpi(type,'ModifiedHarmonicMean')
0082 disp(['MODEL_COMPARISON: I cant''t find the modified harmonic mean estimate associated to model ' ModelNames{i}])
0083 return
0084 end
0085 end
0086 end
0087
0088
0089
0090 lmpd = log(ModelPriors)+MarginalLogDensity;
0091 [maxval,k] = max(lmpd);
0092 elmpd = exp(lmpd-maxval);
0093
0094
0095
0096 title = 'Model Comparison';
0097 headers = char('Model',ShortModelNames{:});
0098 if prior_flag
0099 labels = char('Priors','Log Marginal Density','Bayes Ratio', ...
0100 'Posterior Model Probability');
0101 values = [ModelPriors';MarginalLogDensity';exp(lmpd-lmpd(1))'; ...
0102 elmpd'/sum(elmpd)];
0103 else
0104 labels = char('Priors','Log Marginal Density','Bayes Ratio','Posterior Odds Ratio', ...
0105 'Posterior Model Probability');
0106 values = [ModelPriors';MarginalLogDensity'; exp(MarginalLogDensity-MarginalLogDensity(1))'; ...
0107 exp(lmpd-lmpd(1))'; elmpd'/sum(elmpd)];
0108 end
0109
0110 dyntable(title,headers,labels,values, 0, 15, 6);
0111
0112
0113 function name = get_model_name_without_path(modelname)
0114 idx = strfind(modelname,'\');
0115 if isempty(idx)
0116 idx = strfind(modelname,'/');
0117 end
0118 if isempty(idx)
0119 name = modelname;
0120 return
0121 end
0122 name = modelname(idx(end)+1:end);
0123
0124
0125 function name = get_model_name_without_extension(modelname)
0126 idx = strfind(modelname,'.mod');
0127 if isempty(idx)
0128 idx = strfind(modelname,'.dyn')
0129 end
0130 if isempty(idx)
0131 name = modelname;
0132 return
0133 end
0134 name = modelname(1:end-4);
0135
0136
0137 function modellist = get_short_names(modelnames)
0138 n = length(modelnames);
0139 modellist = {};
0140 for i=1:n
0141 name = get_model_name_without_extension(modelnames{i});
0142 name = get_model_name_without_path(name);
0143 modellist = {modellist{:} name};
0144 end