Mercurial > hg > ltpda
comparison m-toolbox/classes/@pest/combineExps.m @ 0:f0afece42f48
Import.
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Wed, 23 Nov 2011 19:22:13 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f0afece42f48 |
---|---|
1 % combineExps combine the results of different parameter estimation | |
2 % experimets and give the final joint estimate | |
3 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
4 % | |
5 % DESCRIPTION: combineExps combine different parameter estimates into a | |
6 % single joint estimate. Joint covariance is also computed. | |
7 % | |
8 % CALL: obj = combineExps(objs); | |
9 % obj = combineExps(objs); | |
10 % | |
11 % INPUTS: obj - can be a vector | |
12 % | |
13 % <a href="matlab:utils.helper.displayMethodInfo('pest', 'combineExps')">Parameters Description</a> | |
14 % | |
15 % VERSION: $Id: combineExps.m,v 1.9 2011/04/08 08:56:25 hewitson Exp $ | |
16 % | |
17 % HISTORY: 10-12-2010 G. Congedo | |
18 % | |
19 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
20 | |
21 function varargout = combineExps(varargin) | |
22 | |
23 %%% Check if this is a call for parameters | |
24 if utils.helper.isinfocall(varargin{:}) | |
25 varargout{1} = getInfo(varargin{3}); | |
26 return | |
27 end | |
28 | |
29 import utils.const.* | |
30 utils.helper.msg(msg.PROC3, 'running %s/%s', mfilename('class'), mfilename); | |
31 | |
32 % Collect input variable names | |
33 in_names = cell(size(varargin)); | |
34 for ii = 1:nargin,in_names{ii} = inputname(ii);end | |
35 | |
36 % Collect all AOs and plists | |
37 [pests, pest_invars] = utils.helper.collect_objects(varargin(:), 'pest', in_names); | |
38 pl = utils.helper.collect_objects(varargin(:), 'plist', in_names); | |
39 | |
40 | |
41 % combine plists | |
42 % pl = parse(pl, getDefaultPlist()); | |
43 | |
44 if nargout == 0 | |
45 error('### combineExps cannot be used as a modifier. Please give an output variable.'); | |
46 end | |
47 | |
48 if ~all(isa(pests, 'pest')) | |
49 error('### combineExps must be only applied to pest objects.'); | |
50 end | |
51 | |
52 N = numel(pests); | |
53 | |
54 I = cell(N,1); | |
55 C = cell(N,1); | |
56 p = cell(N,1); | |
57 pnames = cell(N,1); | |
58 chi2 = zeros(N,1); | |
59 dof = zeros(N,1); | |
60 | |
61 % Iterate over all input pest | |
62 for ii=1:N | |
63 | |
64 counter = sprintf('%u',ii); | |
65 | |
66 % Extract values | |
67 pnames{ii} = pests(ii).names; | |
68 p{ii} = pests(ii).y; | |
69 chi2(ii) = pests(ii).chi2; | |
70 if isempty(chi2(ii)) | |
71 error(['### Could not find chi2 value for input pest #' counter]); | |
72 end | |
73 dof(ii) = pests(ii).dof; | |
74 if isempty(dof(ii)) | |
75 error(['### Could not find dof value for input pest #' counter]); | |
76 end | |
77 I{ii} = pests(ii).procinfo.find('infomatrix'); | |
78 C{ii} = pests(ii).cov; | |
79 if isempty(C{ii}) | |
80 C{ii} = inv(I{ii}); | |
81 elseif isempty(I{ii}) | |
82 I{ii} = inv(C{ii}); | |
83 elseif isempty(I{ii}) && isempty(C{ii}); | |
84 error(['### Could not find covariance matrix for input pest #' counter]); | |
85 end | |
86 | |
87 end | |
88 | |
89 % Now redefine all quantities to handle the general case when we have | |
90 % different sizes | |
91 Inew = cell(N,1); | |
92 pnew = cell(N,1); | |
93 pnamesAll = pnames{1}; | |
94 dofnew = zeros(N,1); | |
95 chi2new = zeros(N,1); | |
96 for ii=1:N | |
97 pnamesAll = union(pnamesAll,pnames{ii}); | |
98 end | |
99 Np = numel(pnamesAll); | |
100 for ii=1:N | |
101 Inew{ii} = zeros(Np); | |
102 pnew{ii} = zeros(Np,1); | |
103 % dofnew{ii} = zeros(Np,1); | |
104 % information matrix | |
105 for kk=1:Np | |
106 for ll=1:Np | |
107 ixk = strcmp(pnamesAll{kk},pnames{ii}); | |
108 ixl = strcmp(pnamesAll{ll},pnames{ii}); | |
109 ixk = find(ixk); | |
110 ixl = find(ixl); | |
111 if ixk~=0 & ixl~=0 | |
112 Inew{ii}(kk,ll) = I{ii}(ixk,ixl); | |
113 end | |
114 end | |
115 end | |
116 % param vector | |
117 for kk=1:Np | |
118 ix = strcmp(pnamesAll{kk},pnames{ii}); | |
119 ix = find(ix); | |
120 if ix~=0 | |
121 pnew{ii}(kk) = p{ii}(ix); | |
122 end | |
123 end | |
124 % dof & chi2 | |
125 dofnew(ii) = dof(ii) + numel(p{ii}) - Np; | |
126 chi2new(ii) = chi2(ii) * dof(ii) / dofnew(ii); | |
127 | |
128 % for kk=1:Np | |
129 % ix = strcmp(pnamesAll,pnames{ii}{kk}); | |
130 % ix = find(ix); | |
131 % arr = I{ii}(kk,:); | |
132 % % (1:kk) = arr(1:ix) | |
133 % % Inew{ii}(ix,:) = [0 arr(kk:end)]; | |
134 % % pnew{ii}(kk) = p{ii}(ix); | |
135 % end | |
136 end | |
137 I = Inew; | |
138 p = pnew; | |
139 dof = dofnew; | |
140 chi2 = chi2new; | |
141 | |
142 % Compute joint information matrix | |
143 II = zeros(size(I{1})); | |
144 for ii=1:N | |
145 II = II+I{ii}; | |
146 end | |
147 % CC = inv(II); | |
148 | |
149 % Compute joint parameter estimate | |
150 pp = zeros(size(p{1})); | |
151 for ii=1:N | |
152 pp = pp+I{ii}*p{ii}; | |
153 end | |
154 pp = II\pp; | |
155 | |
156 % Compute joint errors and correlation matrix | |
157 CC = inv(II); | |
158 dp = sqrt(diag(CC)); | |
159 corr = zeros(size(CC)); | |
160 for ll=1:size(CC,1) | |
161 for mm=1:size(CC,2); | |
162 corr(ll,mm) = CC(ll,mm)/dp(ll)/dp(mm); | |
163 end | |
164 end | |
165 | |
166 % Compute joint chi2 and dof | |
167 DOF = sum(dof); | |
168 CHI2 = chi2'*dof/DOF; | |
169 | |
170 % Output pest | |
171 out = pest(); | |
172 out = out.setNames(pnamesAll); | |
173 % out = out.setYunits(pests(1).yunits); | |
174 out = out.setY(pp); | |
175 out = out.setCov(CC); | |
176 out = out.setDy(dp); | |
177 out = out.setCorr(corr); | |
178 out = out.setChi2(CHI2); | |
179 out = out.setDof(DOF); | |
180 name = pests(1).name; | |
181 if N>1 | |
182 for ii=2:N | |
183 name = [name ',' pests(ii).name]; | |
184 end | |
185 end | |
186 out = out.setName(['combineExps(' name ')']); | |
187 | |
188 out.procinfo = plist('infomatrix',II); | |
189 out.addHistory(getInfo('None'), pl, pest_invars(:), [pests(:).hist]); | |
190 | |
191 % Set outputs | |
192 if nargout > 0 | |
193 varargout{1} = out; | |
194 end | |
195 | |
196 end | |
197 | |
198 | |
199 %-------------------------------------------------------------------------- | |
200 % Get Info Object | |
201 %-------------------------------------------------------------------------- | |
202 function ii = getInfo(varargin) | |
203 if nargin == 1 && strcmpi(varargin{1}, 'None') | |
204 sets = {}; | |
205 pl = []; | |
206 else | |
207 sets = {'Default'}; | |
208 pl = getDefaultPlist; | |
209 end | |
210 % Build info object | |
211 ii = minfo(mfilename, 'pest', 'ltpda', utils.const.categories.helper, '$Id: combineExps.m,v 1.9 2011/04/08 08:56:25 hewitson Exp $', sets, pl); | |
212 end | |
213 | |
214 %-------------------------------------------------------------------------- | |
215 % Get Default Plist | |
216 %-------------------------------------------------------------------------- | |
217 function plout = getDefaultPlist() | |
218 persistent pl; | |
219 if exist('pl', 'var')==0 || isempty(pl) | |
220 pl = buildplist(); | |
221 end | |
222 plout = pl; | |
223 end | |
224 | |
225 function plo = buildplist() | |
226 plo = plist.EMPTY_PLIST; | |
227 end | |
228 |