comparison m-toolbox/classes/+utils/@math/loglikelihood_matrix.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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2 %
3 % Compute log-likelihood
4 %
5 % M Nofrarias 15-06-09
6 %
7 % $Id: loglikelihood_matrix.m,v 1.5 2011/11/16 08:52:50 nikos Exp $
8 %
9 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10 function [loglk snr]= loglikelihood_matrix(xn,in,out,noise,model,params,inModel,outModel)
11 % parameters
12 fs = in(1).objs(1).fs;
13
14 % num. experiments
15 nexp = numel(in);
16 % num. transfer functions
17 nmod = numel(model(1).objs(:));
18 noutChannels = sqrt(numel(noise(1).objs));
19
20 % loop over experiments
21 loglk = 0;
22 snr = 0;
23 for i = 1:nexp
24 if ((numel(in(1).objs) == 1) && numel(out(1).objs) == 1)
25
26 freqs = in(i).objs(1).data.getX;
27 % evaluate models
28 if(isempty(outModel))
29 h11 = model(1).objs(1).setParams(params,xn).double;
30 elseif (~isempty(outModel))
31 h11 = outModel(1,1).y * model(1).getObjectAtIndex(1,1).setParams(params,xn).double';
32 end
33
34 % spectra to variance
35 % (N*fs/2)* this multiplication is done now in mcmc
36 C11 = noise(1).objs(1).data.getY;
37
38 % compute elements of inverse cross-spectrum matrix
39 InvS11 = 1./C11;
40
41 % compute log-likelihood terms first, all at once does not cancel the
42 % imag part when multiplying x.*conj(x)
43 in1 = in(i).objs(1).data.getY;
44 out1 = out(i).objs(1).data.getY;
45
46 tmplt1 = h11.*in1;
47
48 v1v1 = conj(out1 - tmplt1).*(out1 - tmplt1);
49
50 %computing SNR
51 snrexp = utils.math.stnr(tmplt1,0,out1,0,InvS11,0,0,0);
52
53 snr = snr + 20*log10(snrexp);
54
55 log1exp = sum(InvS11.*v1v1);
56
57 loglk = loglk + log1exp;
58
59 elseif ((numel(in(1).objs) == 2) && numel(out(1).objs) == 2)
60 freqs = in(i).objs(1).data.getX;
61 % loop over models
62
63 if(isempty(outModel))
64 for j = 1:nmod
65 % evaluate models
66 h(:,j) = model(i).objs(j).setParams(params,xn).double;
67 end
68 elseif (~isempty(outModel))
69 h(:,1) = outModel(1,1).y * model(i).getObjectAtIndex(1,1).setParams(params,xn).double';
70 h(:,2) = outModel(2,1).y * model(i).getObjectAtIndex(1,1).setParams(params,xn).double';
71 h(:,3) = outModel(1,2).y * model(i).getObjectAtIndex(2,2).setParams(params,xn).double';
72 h(:,4) = outModel(2,2).y * model(i).getObjectAtIndex(2,2).setParams(params,xn).double';
73 end
74
75 for j = 1:noutChannels^2
76 % spectra to variance
77 % (N*fs/2)* this multiplication is done now in mcmc
78 C(:,j) = noise(i).objs(j).data.getY;
79 end
80
81 % compute elements of inverse cross-spectrum matrix
82 detm = (C(:,1).*C(:,4) - C(:,2).*C(:,3));
83 InvS11 = C(:,4)./detm; %1 4
84 InvS22 = C(:,1)./detm; %4 1
85 InvS12 = C(:,2)./detm; %2 2
86 InvS21 = C(:,3)./detm; %3 3
87
88 % compute log-likelihood terms first, all at once does not cancel the
89 % imag part when multiplying x.*conj(x)
90 in1 = in(i).objs(1).data.getY;
91 in2 = in(i).objs(2).data.getY;
92 out1 = out(i).objs(1).data.getY;
93 out2 = out(i).objs(2).data.getY;
94
95 tmplt1 = h(:,1).*in1 + h(:,3).*in2;
96 tmplt2 = h(:,2).*in1 + h(:,4).*in2;
97
98 % matrix index convention: H(1,1)->h(1) H(2,1)->h(2) H(1,2)->h(3) H(2,2)->h(4)
99 v1v1 = conj(out1 - tmplt1).*(out1 - tmplt1);
100 v2v2 = conj(out2 - tmplt2).*(out2 - tmplt2);
101 v1v2 = conj(out1 - tmplt1).*(out2 - tmplt2);
102 v2v1 = conj(out2 - tmplt2).*(out1 - tmplt1);
103
104 %computing SNR
105 snrexp = utils.math.stnr(tmplt1,tmplt2,out1,out2,InvS11,InvS22,InvS12,InvS21);
106
107 snr = snr + 20*log10(snrexp);
108
109 log1exp = sum(InvS11.*v1v1 + InvS22.*v2v2 - InvS12.*v1v2 - InvS21.*v2v1);
110
111 loglk = loglk + log1exp;
112
113 elseif ((numel(in(1).objs) == 4) && numel(out(1).objs) == 3)
114 % here we are implementing only the magnetic case
115 % We have 4 inputs (the 4 conformator waveforms of the magnetic
116 % analysis and
117 % 3 outputs (that correspond to the IFO.x12 and IFO.ETA1 and
118 % IFO.PHI1
119
120
121 for j = 1:noutChannels^2
122 % spectra to variance
123
124 % (N*fs/2)* this factor multiplication is done now in mcmc,
125 % before splitting
126 C(:,j) = noise(i).objs(j).data.getY;
127 end
128 if( isempty(inModel) && ~isempty(outModel))
129
130 freqs = in(i).objs(1).data.getX;
131
132 % faster this way
133 h(:,1) = outModel(1,1).y * model(i).getObjectAtIndex(1,1).setParams(params,xn).double;
134 h(:,2) = outModel(2,1).y * model(i).getObjectAtIndex(1,1).setParams(params,xn).double;
135 h(:,3) = outModel(3,1).y * model(i).getObjectAtIndex(1,1).setParams(params,xn).double;
136 h(:,4) = outModel(1,1).y * model(i).getObjectAtIndex(1,2).setParams(params,xn).double;
137 h(:,5) = outModel(2,1).y * model(i).getObjectAtIndex(1,2).setParams(params,xn).double;
138 h(:,6) = outModel(3,1).y * model(i).getObjectAtIndex(1,2).setParams(params,xn).double;
139 h(:,7) = outModel(1,2).y * model(i).getObjectAtIndex(2,3).setParams(params,xn).double;
140 h(:,8) = outModel(2,2).y * model(i).getObjectAtIndex(2,3).setParams(params,xn).double;
141 h(:,9) = outModel(3,2).y * model(i).getObjectAtIndex(2,3).setParams(params,xn).double;
142 h(:,10) = outModel(1,3).y * model(i).getObjectAtIndex(3,4).setParams(params,xn).double;
143 h(:,11) = outModel(2,3).y * model(i).getObjectAtIndex(3,4).setParams(params,xn).double;
144 h(:,12) = outModel(3,3).y * model(i).getObjectAtIndex(3,4).setParams(params,xn).double;
145
146
147 % compute elements of inverse cross-spectrum matrix
148 detm = (C(:,1).*C(:,5).*C(:,9) + ...
149 C(:,2).*C(:,6).*C(:,7) + ...
150 C(:,3).*C(:,4).*C(:,8) -...
151 C(:,7).*C(:,5).*C(:,3) -...
152 C(:,8).*C(:,6).*C(:,1) -...
153 C(:,9).*C(:,4).*C(:,2));
154
155
156 InvS11 = (C(:,5).*C(:,9) - C(:,8).*C(:,6))./detm;
157 InvS12 = -(C(:,4).*C(:,9) - C(:,7).*C(:,6))./detm;
158 InvS13 = (C(:,4).*C(:,8) - C(:,7).*C(:,5))./detm;
159 InvS21 = -(C(:,2).*C(:,9) - C(:,8).*C(:,3))./detm;
160 InvS22 = (C(:,1).*C(:,9) - C(:,7).*C(:,3))./detm;
161 InvS23 = -(C(:,1).*C(:,8) - C(:,7).*C(:,2))./detm;
162 InvS31 = (C(:,2).*C(:,6) - C(:,5).*C(:,3))./detm;
163 InvS32 = -(C(:,1).*C(:,6) - C(:,4).*C(:,3))./detm;
164 InvS33 = (C(:,1).*C(:,5) - C(:,4).*C(:,2))./detm;
165
166 % compute log-likelihood terms first, all at once does not cancel the
167 % imag part when multiplying x.*conj(x)
168 for ll = 1:noutChannels
169 outV(:,ll) = out(i).objs(ll).data.getY;
170 end
171 for kk = 1:model(i).ncols
172 inV(:,kk) = in(i).objs(kk).data.getY;
173 end
174
175 % faster this way
176 v(:,1) = outV(:,1) - h(:,1).*inV(:,1) - h(:,4).*inV(:,2) - h(:,7).*inV(:,3) - h(:,10).*inV(:,4);
177 v(:,2) = outV(:,2) - h(:,2).*inV(:,1) - h(:,5).*inV(:,2) - h(:,8).*inV(:,3) - h(:,11).*inV(:,4);
178 v(:,3) = outV(:,3) - h(:,3).*inV(:,1) - h(:,6).*inV(:,2) - h(:,9).*inV(:,3) - h(:,12).*inV(:,4);
179
180 v1v1 = conj(v(:,1)).*v(:,1);
181 v1v2 = conj(v(:,1)).*v(:,2);
182 v1v3 = conj(v(:,1)).*v(:,3);
183 v2v1 = conj(v(:,2)).*v(:,1);
184 v2v2 = conj(v(:,2)).*v(:,2);
185 v2v3 = conj(v(:,2)).*v(:,3);
186 v3v1 = conj(v(:,3)).*v(:,1);
187 v3v2 = conj(v(:,3)).*v(:,2);
188 v3v3 = conj(v(:,3)).*v(:,3);
189
190 log1exp = sum(InvS11.*v1v1 +...
191 InvS12.*v1v2 +...
192 InvS13.*v1v3 +...
193 InvS21.*v2v1 +...
194 InvS22.*v2v2 +...
195 InvS23.*v2v3 +...
196 InvS31.*v3v1 +...
197 InvS32.*v3v2 +...
198 InvS33.*v3v3);
199
200 loglk = loglk + log1exp;
201
202
203 else
204 error('For the magnetic case, implement an outModel and leave your inModel blank')
205 end
206
207 else
208 error('Implemented cases: 1 input / 1output, 2 inputs / 2outputs (TN3045 analysis), and 4 inputs / 3 outpus (magnetic complete analysis model. Other cases have not been implemented yet. Sorry for the inconvenience)');
209 end
210
211 end
212 end
213