-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathosl_inverse_mne_weights.m
1243 lines (985 loc) · 46 KB
/
osl_inverse_mne_weights.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function [W, W_nonorm,lf] = osl_inverse_mne_weights(SensorData, LeadFields, Noise, Options)
%OSL_INVERSE_MNE_WEIGHTS creates weights for source reconstruction using MNE
%
% W = OSL_INVERSE_MNE_WEIGHTS(SensorData, LeadFields, NOISE, OPTIONS)
% calculates cell array of weights, W, for source reconstruction. Sources
% at dipole i are estimated with W{i}*B.
%
% [W, W_NONORM, LF] = OSL_MNE_WEIGHTS(...) also returns the un-normalised
% weights W_NONORM and a cell array of lead fields for each dipole, LF.
%
% The inputs should be formatted as a set of data structures:
%
% SensorData.cov - covariance of the sensor data (nChans x nChans)
% .nSamples - number of time samples used to compute covariance
% matrix.
%
% LeadFields.nSources - number of sources to estimate
% .nDims - dimensionality of each source (expected to be 3 at
% the moment)
% .lf - matrix of lead fields, nChans x (nDims x
% nSources). Fields for x, y and z components of the
% same dipole should appear in consecutive columns.
%
% Noise.model - determines specification of the noise covariance.
% Choices are {'empirical', 'white',
% 'diag_datacov'}. For the first option, a noise
% covariance matrix must be passed in. For the
% second two, a scaling factor lambda should be
% provided, or is assumed unity.
% .lambda - scaling factor on the noise covariance
% .cov - empirical noise covariance (nChans x nChans)
%
% Further algorithm choices are set in the subfields of the Options
% structure.
%
% Options.ReduceRank.weights - Boolean [true] converts to scalar weights
% .leadFields - Integer [3] dimensionality of lead fields
% (ignored at the moment)
%
% .sourceModel - Choose the estimation method using a string, from:
% 'Wens' (based on paper referenced below),
% 'MNE' (which uses gamma-MAP estimation of a white covariance matrix),
% 'MNE-INA' (integrated nested approximation for
% averaging over the prior on gamma)
% 'MNE-scaled-noise' (also estimates a parameter
% rho for the scaling between sensor data and
% noise)
% 'sparse-Bayes' (gamma-MAP estimation of a
% sparse Bayes solution)
% 'rvm-beamformer' - beamformer solution using
% the regularised data covariance estimated using
% the sparse Bayes model.
%
% .Normalise.weights - sLoreta, norm, noise-proj, none: normalises
% reconstruction weights using a variety of
% methods. sLoreta uses normalisation from the
% Wens paper. 'norm' normalised the 2-norm of
% the weights. 'noise-proj' normalises by the
% projection of the noise:
% sqrt(tr[W*noiseCov*W]), e.g. for constructing
% pseudo-z-stats (see Vrba and Robinson 2001).
% 'none' applies no normalisation
% .Normalise.leadFields - [true] or false normalises lead fields as
% a compensation for depth bias.
%
% .gammaPrior - form of prior on source variance parameters. Can
% be 'uniform-sd - flat on sqrt(gamma) [Default].
% 'uniform-variance' - flat on gamma.
% 'log-uniform' - flat on log-gamma (this is the
% Jeffrey's prior but produces an
% improper posterior).
% 'cauchy' - weakly informative prior with a
% peak at zero, flat centre and
% tail-off.
% 'normal' - on gamma
% 'log-normal' - on gamma
% or a handle to a function
% lp(gamma) = sum_i (log p(gamma_i))
% which returns the log of a normalised prior density
% for any scalar or vector gamma >= 0.
%
% References:
% Wipf and Nagarajan. A unified Bayesian framework for MEG/EEG source imaging. Neuroimage (2009) vol. 44 (3) pp. 947-66
%
% Dale, A.M. & Sereno, M.I.(1993) "Improved localization of cortical
% activity by combining EEG and MEG with MRI cortical surface
% reconstruction: a linear approach," J. Cogn. Neurosci 5, pp. 162--176.
%
% Pascual-Marqui, R.D. (2002) "Standardized low-resolution brain
% electromagnetic tomography (sLORETA): technical details," Methods Find.
% Exp. Clin. Pharmacol. 24 (Suppl D) pp. 5--12.
%
% Hamalainen, M.S., Lin, F. & Mosher, J.C. (2010) "Anatomically and
% functionally constrained minimum-norm estimates." In Hansen, P.C.,
% Kringelbach, M.L. & Salmelin, R. (ed.) "MEG: an introduction to
% methods," Oxford University Press, Oxford, UK, pp. 186--215.
%
% Wipf and Nagarajan (2007). Beamforming using the relevance vector
% machine. Proc. 24th Int. Conf. on Machine Learning
%
% Vrba and Robinson (2001). Signal processing in magnetoencephalography.
% Methods 25(2), 249--271.
%
% Wens, V. et al. (2015) "A geometric correction scheme for spatial leakage effects in MEG/EEG seed-based functional connectivity mapping," Hum. Brain. Mapp. (In review)
% Copyright 2015 OHBA
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program. If not, see <http://www.gnu.org/licenses/>.
% $LastChangedBy$
% $Revision$
% $LastChangedDate$
% Contact: [email protected]
% Originally written on: GLNXA64 by Giles Colclough, 19-Jan-2015 14:40:17
global DEBUG
DEBUG = true;
%% Input Checking
% Sensor data covariance
SensorData.nSensors = ROInets.rows(SensorData.cov);
validateattributes(SensorData.cov, {'numeric'}, ...
{'2d', 'real', 'ncols', SensorData.nSensors}, ...
mfilename, 'SensorData.cov', 1);
% Check for positive definiteness? No - sensor data may not be full rank
% assert(ROInets.isposdef(SensorData.cov), ...
% [mfilename ':SensorDataCovarianceNotPosDef'], ...
% 'Data covariance matrix SensorData.cov should be positive definite. \n');
% lead fields
D = 3; % dimensions in space
% at the moment, expecting 3D lead fields only
assert(isequal(LeadFields.nDims, D), ...
[mfilename ':LeadFieldsWrongDimensionality'], ...
'Lead field dimensionality should be for 3D space. \n');
% check matrix size matches dimension fields and correct number of sensors
validateattributes(LeadFields.lf, {'numeric'}, ...
{'2d', 'real', 'nrows', SensorData.nSensors, ...
'ncols', LeadFields.nSources * LeadFields.nDims}, ...
mfilename, 'LeadFields.lf');
% set default options
Options = parse_options(Options);
%% Parse noise matrix
% noise covariance can be specified as the identity, the diagonal of the
% data covariance, or passed in as a measured or estimated quantity.
% For the first two cases, a global scaling is possible with parameter
% lambda.
Noise = parse_noise(Noise, diag(SensorData.cov));
%% Lead field normalisation
% normalisation of the leadfields is often performed to compensate to some
% extent for the depth bias.
LeadFields = normalise_leadfields(LeadFields, Options);
%% Source model and weights
fprintf('%s: computing weights using model %s. \n', ...
mfilename, Options.sourceModel);
switch lower(Options.sourceModel)
case 'wens'
W_3d = wens_estimate(SensorData, Noise, LeadFields);
case 'mne'
W_3d = mne_estimate(SensorData, Noise, LeadFields, ...
Options.gammaPrior.fn);
case 'mne-ina'
W_3d = mne_estimate_ina(SensorData, Noise, LeadFields, ...
Options.gammaPrior.fn);
case 'mne-scaled-noise'
W_3d = mne_estimate_scale_noise(SensorData, Noise, LeadFields, ...
Options.gammaPrior.fn);
case 'sparse-bayes'
W_3d = sparse_bayes_estimate(SensorData, Noise, LeadFields, ...
Options.gammaPrior);
case 'rvm-beamformer'
W_3d = rvm_beamformer(SensorData, Noise, LeadFields, ...
Options.gammaPrior);
otherwise
error([mfilename ':InvalidSourceMethod'], ...
'The chosen source method %s is not recognised. \n', ...
Options.method);
end%switch
%% Parse into cell array and normalise weights
for iVox = LeadFields.nSources:-1:1, % initialise matrices by looping backwards
% Partition weights for this dipole
dipInd = (D*iVox - (D-1)):(D*iVox); % relevant indices for this dipole. LeadFields.nDims and D should be identical (checked above)
Ws = W_3d(dipInd,:); % single-dipole weights
if Options.ReduceRank.weights,
% project onto direction of maximum variance using PCA.
dipCov = Ws * SensorData.cov * Ws.'; % single-dipole covariance 3x3
[ns,~] = eigs(dipCov, [], 1); % ns is direction of maximum source variance. This code seems about as fast as [ns,~] = fast_svds(dipCov, 1);
W_nonorm{iVox} = ns.' * Ws; % weights for this dipole projected onto direction of maximum variance
lf{iVox} = LeadFields.lf(:,dipInd) * ns; % lead fields for this dipole projected onto direction of maximum variance
else
W_nonorm{iVox} = Ws;
lf{iVox} = LeadFields.lf(:,dipInd);
end%if
% apply weights normalisation
switch lower(Options.Normalise.weights)
case 'sloreta'
% normalising constant for depth bias using sLORETA
% Wens et al. sec 2.4
lambda_s = sqrt(trace(W_nonorm{iVox} ...
* empirical_bayes_cov(Noise.cov, ...
sourceCov, ...
LeadFields.lf) ...
* W_nonorm{iVox}.'));
case 'norm'
% Normalise by norm of weights
lambda_s = norm(W_nonorm{iVox});
case 'noise-proj'
% Normalise by projected noise power.
lambda_s = sqrt(trace(W_nonorm{iVox} * Noise.cov * W_nonorm{iVox}.'));
case 'none'
% apply no normalisation
lambda_s = 1.0;
otherwise
error([mfilename ':InvalidNormalisationMethod'], ...
'Normalisation method %s is invalid. \n', ...
Options.Normalise);
end%switch
W{iVox} = W_nonorm{iVox} ./ lambda_s; % normalised scalar weights vector for this dipole
end%loop over voxels
end%osl_inverse_mne_weights
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function sensorCov = empirical_bayes_cov(noiseCov, sourceCov, lf3d)
%EMPIRICAL_BAYES_COV regularised estimate of data covariance in sensors
%
% SENSORCOV = EMPIRICAL_BAYES_COV(NOISECOV, SOURCECOV, 3D_LEADFIELDS)
% returns the empirical bayes estimate of the sensor covariances based on
% the noise covariance matrix, the modelled source covariance matrix and
% the lead field matrix in 3D.
sensorCov = noiseCov + lf3d * sourceCov * lf3d.';
% ensure real - sometimes goes a bit off.
% sensorCov = real(sensorCov);
sensorCov = (sensorCov + sensorCov')./2.0;
end%empirical_bayes_cov
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function W_3d = estimate_weights(noiseCov, sourceCov, lf3d, rho)
%ESTIMATE_WEIGHTS for reconstruction of 3d sources
%
% W = ESTIMATE_WEIGHTS(NOISECOV, SOURCECOV, 3D_LEADFIELDS) estimates
% weights W for estimating sources S = W*B in 3d.
%
% W = ESTIMATE_WEIGHTS(NOISECOV, SOURCECOV, 3D_LEADFIELDS, RHO) estimates
% weights W for estimating sources S = W*B in 3d when there is an
% additional scaling factor rho between the measured noise and measured
% data.
%
% equation 13 from Wipf and Nagarajan (2009).
% given B = (LS + e)./rho, and S ~ N(0,sourceCov) then S is normally
% distributed with mean
% W * B = rho sourceCov L' (noiseCov + L sourceCov L')^{-1} B
% set default rho as identity
if nargin < 4 || ~exist('rho', 'var') || isempty(rho),
rho = 1;
end%if
W_3d = rho .* sourceCov * lf3d.' * ...
osl_cholinv(empirical_bayes_cov(noiseCov, sourceCov, lf3d));
end%estimate_weights
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function W_3d = beamformer_weights(noiseCov, sourceCov, lf3d, rho)
%ESTIMATE_WEIGHTS for reconstruction of 3d sources
%
% W = BEAMFORMER_WEIGHTS(NOISECOV, SOURCECOV, 3D_LEADFIELDS) estimates
% weights W for estimating sources S = W*B in 3d.
%
% W = BEAMFORMER_WEIGHTS(NOISECOV, SOURCECOV, 3D_LEADFIELDS, RHO) estimates
% weights W for estimating sources S = W*B in 3d when there is an
% additional scaling factor rho between the measured noise and measured
% data.
%
% Vrba and Robinson 2001
% set default rho as identity
if nargin < 4 || ~exist('rho', 'var') || isempty(rho),
rho = 1;
end%if
invDataCov = osl_cholinv(empirical_bayes_cov(noiseCov, sourceCov, lf3d));
W_3d = rho .* lf3d.' * invDataCov ./ trace(lf3d.' * invDataCov * lf3d);
end%estimate_weights
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function W = wens_estimate(SensorData, Noise, LeadFields)
%WENS_ESTIMATE reconstruction weights by Vincent Wens' method
% regularization parameter from Wens et al. Sec 2.4
% use property sum(eig(B, A)) = trace(inv(A) * B)
k = sum(eig(LeadFields.lf * LeadFields.lf.', Noise.cov)) ./ ...
(sum(eig(SensorData.cov, Noise.cov)) - SensorData.nSensors);
% In our formulation, gamma = 1/k and C = I
sourceCov = (1.0 ./ real(k)) * speye(LeadFields.nDims * LeadFields.nSources);
% Extract weights for 3d sources
W = estimate_weights(Noise.cov, sourceCov, LeadFields.lf);
end%wens_estimate
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [W, logGamma] = mne_estimate(Data, Noise, LeadFields, prior)
%MNE_ESIMATE regularized source reconstruction weights
%
% Estimates source covariance for MNE, using a white covariance matrix,
% with a single scaling parameter.
% Use Eq 18 from Wipf and Nagarajan (2009), optimising w.r.t scalar gamma
global DEBUG
sourceCovFn = @(logGamma) exp(real(logGamma)) .* ...
speye(LeadFields.nDims * LeadFields.nSources);
% variance of data can change by factor of e^20 relative to noise in search
% space
% note that for priors flat in log-space, which create improper posteriors,
% the inference can be quite sensitive to the lower bound used here.
noiseToLFScale = mean(log(diag(Noise.cov))) - ...
mean(log(sum(LeadFields.lf.^2)));
logGammaBound = noiseToLFScale + [-20 50];
% if the prior has a scale, set it to be about e^7 above noise
scale = exp(noiseToLFScale + 7);
optimise_target = @(logGamma) optimise_target_single_prior(Data, Noise, ...
LeadFields, logGamma, prior, scale, sourceCovFn);
% optimise using a golden section search and parabolic interpolation
logGamma = fminbnd(optimise_target, logGammaBound(1), logGammaBound(2));
sourceCov = sourceCovFn(logGamma);
% Extract weights for 3d sources
W = estimate_weights(Noise.cov, sourceCov, LeadFields.lf);
if DEBUG,
% plot
lg = log(logspace(log10(exp(logGammaBound(1))), log10(exp(logGammaBound(2))), 50));
L = arrayfun(optimise_target, lg);
figure('Color', 'w', 'Name', 'Optimisation target for log(gamma)');
semilogy(lg, L);
xlabel('Log (\gamma)');
ylabel('L');
% display result
fprintf('\nBounds on logGamma: %0.2g, %0.2g. \n', logGammaBound);
fprintf('Chosen logGamma: %0.6g. \n', logGamma);
end%if DEBUG
end%mne_Jeffreys_prior_estimate
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function W = mne_estimate_scale_noise(Data, Noise, LeadFields, priorFn)
%MNE_DOUBLE_SEARCH_ESIMATE regularized source estimates with noise scaling
%
% Estimates source covariance for MNE, using a white covariance matrix,
% with a single scaling parameter.
% Allows an additional parameter to control scaling between noise and data
% as rho*B = LS + e
global DEBUG
% It seems as though this functionality will not work well. No minimum for
% rho, unless forced by prior?
warning([mfilename ':scale_noise'], ...
['The scale-noise mne estimate is unstable. ', ...
'It may not find a minimum for rho. \n']);
sourceCovFn = @(logGamma) exp(real(logGamma)) .* ...
speye(LeadFields.nDims * LeadFields.nSources);
% put noise and sources are on same scale
noiseToLFScale = mean(log(diag(Noise.cov))) - ...
mean(log(sum(LeadFields.lf.^2)));
% put data and noise on scale set by mean eigenvalue
dataEigVals = eig(Data.cov);
noiseEigVals = eig(Noise.cov);
NoiseToDataScale = log(mean(noiseEigVals)) - log(mean(dataEigVals));
% set initial values
logGammaInit = noiseToLFScale;
logRhoInit = NoiseToDataScale./2;
paramsInit = [logGammaInit; logRhoInit];
% priors forms are the same for rho and gamma
priors = {priorFn; priorFn};
% If there is a scale on the priors, we want to constrain the space to
% moderately sensible values
% If we reckon our initial guesses are any good, let's constrain the
% variance parameters to be within a factor of exp(7).
scales = exp(paramsInit + 7);
optimise_target = @(params) optimise_target_double_prior(Data, Noise, ...
LeadFields, params, ...
priors, scales, ...
sourceCovFn);
% set optimisation params
maxIter = 4000;
% optimise using a multivariate nonlinear Nelder-Mead minimization
[params, fval, success] = fminsearch(optimise_target, paramsInit, ...
optimset('MaxFunEvals', maxIter, ...
'MaxIter', maxIter));
logGamma = params(1);
sourceCov = sourceCovFn(logGamma);
rho = exp(params(2));
% Extract weights for 3d sources
W = estimate_weights(Noise.cov, sourceCov, LeadFields.lf, rho);
if DEBUG || ~success,
% use a grid search
lrBound = real(log10(rho)) + [-7 7];
lgBound = real(log10(exp(logGamma))) + [-10 10];
nR = 20;
nG = 30;
lr = log(logspace(lrBound(1), lrBound(2), nR));
lg = log(logspace(lgBound(1), lgBound(2), nG));
for iR = length(lr):-1:1,
for iG = length(lg):-1:1,
L(iG,iR) = optimise_target([lg(iG), lr(iR)]);
end%for
end%for
figure('Color', 'w', 'Name', 'Optimisation target for log(gamma)');
surfl(real(lr), real(lg), real(L));
colormap pink
xlabel('Log (\rho)');
ylabel('Log (\gamma)');
zlabel('L');
% best gamma and rho?
[searchFval, iMin] = min(L(:));
if searchFval < fval,
fprintf('%s: Using best parameters from grid search. \n', mfilename);
[iG,iR] = ind2sub(size(L), iMin);
logGamma = lg(iG);
sourceCov = sourceCovFn(logGamma);
rho = exp(lr(iR));
W = estimate_weights(Noise.cov, sourceCov, LeadFields.lf, rho);
end%if
end%if DEBUG
end%mne_estimate_scale_noise
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function W = mne_estimate_ina(Data, Noise, LeadFields, prior)
%MNE_ESTIMATE_INA regularized source estimates
%
% Estimates source distribution using mean of marginal distribution on
% sources. Marginal distribution is approximated by averaging over several
% discrete points in gamma-space, rather than taking the MAP estimate of
% gamma. This is the integrated nested approximation.
% Returns weight W to estimate sources as S = W*B.
global DEBUG
% Find or estimate peak of marginal gamma distribution
% either:
[~, logGammaPeak] = mne_estimate(Data, Noise, LeadFields, prior);
gammaPeak = exp(logGammaPeak);
% or:
noiseToLFScale = mean(log(diag(Noise.cov))) - ...
mean(log(sum(LeadFields.lf.^2)));
% gammaPeak = exp(noiseToLFScale);
% define inverse weights function
nSourceElements = LeadFields.nDims * LeadFields.nSources;
weights = @(g) estimate_weights(Noise.cov, ...
g.*speye(nSourceElements), ...
LeadFields.lf);
% if the prior has a scale, set it to be about e^7 above noise
scale = exp(noiseToLFScale + 7);
sourceCovFn = @(logGamma) exp(real(logGamma)) .* speye(nSourceElements);
% integrate with clenshaw-curtis integration in 3 bands
nLower = 2^7 + 1;
nInner = 2^12 + 1;
nUpper = 2^6 + 1;
gammaBounds = [gammaPeak./1e40, gammaPeak./1e9, gammaPeak*1e6, gammaPeak*1e30];
[g1, w1] = clen_curt_points(nLower, gammaBounds(1), gammaBounds(2)*0.9999);
[g2, w2] = clen_curt_points(nInner, gammaBounds(2), gammaBounds(3)*0.9999);
[g3, w3] = clen_curt_points(nUpper, gammaBounds(3), gammaBounds(4));
g = cat(1, g1, g2, g3);
w = cat(1, w1, w2, w3);
[g,is] = sort(g);
w = w(is);
% we want to avoid exact zeros in x when working in log-space
exactZeros = 0 == g;
g(exactZeros) = [];
w(exactZeros) = [];
N = length(g);
% set up tracking plot
if DEBUG,
fprintf('Finding values of logp(g|B). \n');
flowBound = eps(min(g));
logPrior = arrayfun(@(lx) prior(lx, scale), log(g+flowBound));
figure('Name', 'p(g)', 'Color', 'w');
plot(log(g), exp(logPrior - log_sum_exp(logPrior)), 'r');
xlabel('Log(\gamma)');
hold on;
end%if
% declare memory
W = zeros(nSourceElements, Data.nSensors);
log_pg = zeros(N,1);
% find coefficients for expected weights at each x
for i = 1:N,
if ~mod(i, 100),
fprintf('%s: marginal contribution %d of %d. \n', ...
mfilename, i, N);
end%if
log_pg(i) = -0.5*optimise_target_single_prior(Data, Noise, ...
LeadFields, log(g(i)), prior, scale, sourceCovFn); %logp_g_on_B(x(i));
end%for
log_wpg = log_pg + log(w);
log_Exp_pg = log_sum_exp(log_wpg);
if DEBUG,
figure('Name', 'p(g|B)', 'Color', 'w');
plot(log(g), log_pg - log_Exp_pg, 'k');
xlabel('Log(\gamma)');
end%if
% loop over integration points, accumulating result
for iInt = 1:N,
W = W + exp(log_wpg(iInt) - log_Exp_pg) .* weights(g(iInt));
% write out progress
if DEBUG
if ~mod(iInt, 100),
fprintf('%s: INA contribution %d of %d. \n', ...
mfilename, iInt, N);
end%if
end%if
end%for
if DEBUG,
fprintf('%s: normalisation check: sum of weighting factors: %0.3g. \n', ...
mfilename, exp(log_sum_exp(log_wpg - log_Exp_pg)));
gammaMapW = weights(gammaPeak);
fractionalImprovement = norm(W - gammaMapW)./norm(W) * 100;
gammaMean = sum(exp(log_wpg - log_Exp_pg) .* g);
fprintf('%s: difference between INA estimate and gamma-MAP: %0.2g%%. \n', ...
mfilename, fractionalImprovement);
fprintf(['%s: gamma-MAP = %0.4g,\tE[gamma] = %0.4g,', ...
'\tdifference: %0.3g%%. \n'], ...
mfilename, gammaPeak, gammaMean, ...
(gammaMean - gammaPeak)./gammaMean * 100);
end%if
end%mne_estimate_ina
%{
% % Previously used a nested function:
% function logp = logp_g_on_B(g)
% %LOGP_G_ON_B
% weightsCov = weights(g)'*weights(g);
% IminusLW = (speye(Data.nSensors) - LeadFields.lf*weights(g));
% logp = - 0.5 * Data.nSamples * Data.nSensors * log(2*pi) ...
% - 0.5 * Data.nSamples * logDetNoise ...
% - 0.5 * trace(IminusLW * Data.cov * IminusLW.' * noiseInv) * Data.nSamples ...
% - 0.5 * Data.nSamples * nSourceElements * log(2*pi*g) ...
% - 0.5 * sum(sum(weightsCov .* Data.cov)) * Data.nSamples ./ g ...
% + prior(log(g),scale);
% end%logp_g_on_B
%}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function W = sparse_bayes_estimate(Data, Noise, LeadFields, GammaPrior)
%SPARSE_BAYES_ESIMATE weights for sparse bayes source reconstruction
%
% Estimates source covariance using sparse Bayes estimate: allow for
% source variance at each location and in each orientation in space.
%
% To use this algorithm, -log p(gamma) MUST BE CONCAVE for each gamma.
%
% Ref: Wipf and Nagarajan, 2009
% estimate regularized covariance
sourceCov = sparse_bayes_covariance(Data, Noise, LeadFields, GammaPrior);
% Extract weights for 3d sources
W = estimate_weights(Noise.cov, sourceCov, LeadFields.lf);
end%sparse_bayes_estimate
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function W = rvm_beamformer(Data, Noise, LeadFields, GammaPrior)
%RVM_BEAMFORMER weights for relevance vector machine beamforming
%
% Estimates source covariance using sparse Bayes estimate: allow for
% source variance at each location and in each orientation in space,
% according to Wipf and Nagarajan 2007, but updated to use a full noise
% covariance estimate.
%
% MAP optimisation of source variances occurs using formulae from Wipf and
% Nagarajan, 2009.
%
% To use this algorithm, -log p(gamma) MUST BE CONCAVE for each gamma.
% estimate regularized covariance
% Wipf and Nagarajan 2007 eq 10
sourceCov = sparse_bayes_covariance(Data, Noise, LeadFields, GammaPrior);
% Extract weights for 3d sources
% Wipf and Nagarajan 2007 Eq 7
W = beamformer_weights(Noise.cov, sourceCov, LeadFields.lf);
end%rvm_beamformer
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function sourceCov = sparse_bayes_covariance(Data, Noise, LeadFields, ...
GammaPrior)
%SPARSE_BAYES_COVARIANCE regularized source covariance
%
% Estimates source covariance using sparse Bayes estimate: allow for
% source variance at each location and in each orientation in space.
%
% To use this algorithm, -log p(gamma) MUST BE CONCAVE for each gamma.
%
% Ref: Wipf and Nagarajan, 2009
global DEBUG
nSourceElements = LeadFields.nDims * LeadFields.nSources;
sourceCovFn = @(gamma) spdiags(real(gamma), ...
0, nSourceElements, nSourceElements);
logGammaInit = (mean(log(diag(Noise.cov))) ...
- mean(log(sum(LeadFields.lf.^2)))) ...
.* ones(nSourceElements, 1);
% set scale for priors on gamma
scale = exp(logGammaInit + 5);
% use updates from gamma-MAP in Wipf and Nagarajan (2009)
oldGamma = exp(logGammaInit);
nIterMax = 1000;
iIter = 0;
deltaGamma = NaN(nIterMax,1);
gamma = zeros(size(oldGamma));
L = NaN(nIterMax,1);
HALT_CONDITION = 1e-4; %fractional change in norm(gamma) to class as convergence
while iIter <= nIterMax,
% some monitoring behaviour
if DEBUG,
% monitor cost function Eq 18 in Wipf and Nagarajan
if iIter > 0,
L(iIter) = optimise_target_single_prior(Data, Noise, LeadFields, ...
log(oldGamma), GammaPrior.fn, ...
scale, @(lg) sourceCovFn(exp(lg)), ...
true);
end%if
if (~mod(iIter, 5) || 1 == iIter) && 0 ~= iIter,
fprintf('%s: SB g-MAP iteration no %4d, deltaGamma %0.5g, L %0.6g.\n', mfilename, iIter, deltaGamma(iIter), L(iIter));
end%if
if 0 == iIter,
ff = figure('Color', 'w', 'Name', 'Updating gamma estimates');
rmFF = onCleanup(@() close(ff));
hist(log(gamma), 3000);
xlabel('Log(\gamma)');
elseif ~mod(iIter, 5) || (1 == iIter),
nzg = gamma~=0;
hist(log(gamma(nzg)), 3000);
title(sprintf('fraction non-zero: %0.2f%%', ...
sum(nzg)./nSourceElements * 100));
drawnow;
end%if
end%if
% start of loop proper
iIter = iIter + 1;
% use Factorize object to hold inverse of regularized covariance
% without computation
invSigmaB = osl_cholinv(empirical_bayes_cov(Noise.cov, sourceCovFn(oldGamma), LeadFields.lf));
% update rules
% Wipf and Nagarajan 2009 Eq. 31
for i = nSourceElements:-1:1,
gamma(i) = oldGamma(i) ...
* sqrt(trace(LeadFields.lf(:,i).' * invSigmaB * Data.cov * invSigmaB * LeadFields.lf(:,i))) ... % Frobenius norm term
./ sqrt(trace(LeadFields.lf(:,i).' * invSigmaB * LeadFields.lf(:,i) ...
- GammaPrior.diff_fn(oldGamma(i), scale) ./ Data.nSamples)); % Final trace term
end%for
% monitor change
deltaGamma(iIter) = norm(gamma - oldGamma) ./ norm(oldGamma);
% test for convergence
isConverged = deltaGamma(iIter) < HALT_CONDITION;
if isConverged,
break
end%if
% re-assign old gamma
oldGamma = gamma;
end%while
% tidy up
deltaGamma(isnan(deltaGamma)) = [];
% check if hit max iter
if iIter >= nIterMax,
warning([mfilename ':gMAPMaxIter'], ...
'Max number of iterations, %d, reached without convergence. \n', ...
nIterMax);
end%if
% final result
sourceCov = sourceCovFn(gamma);
if DEBUG
delete(rmFF); % delete old figure
figure('Name', 'Sparse-Bayes convergence', 'Color', 'w');
semilogy(deltaGamma, 'r', 'LineWidth', 2);
xlabel('Iteration', 'FontSize', 14);
ylabel('Fractional change in norm(\gamma)', 'FontSize', 14);
figure('Name', 'Gamma estimates', 'Color', 'w');
width = 0.8;
nzg = gamma~=0;
[n,x] = hist(log(gamma(nzg)), 3000);
hh = bar(x, n, width);
set(get(hh, 'Children'), ...
'FaceColor', [120, 120, 120]/255, ... % set to be grey
'FaceAlpha', 0.4, ...
'EdgeColor', 'none');
title(sprintf('fraction non-zero: %0.2f%%', ...
sum(nzg)./nSourceElements * 100));
xlabel('Log(\gamma)');
end%if
end%sparse_bayes_covariance
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function L = optimise_target_single_prior(Data, Noise, LeadFields, ...
logGamma, prior, scale, ...
sourceCovFn, quiet)
%OPTIMISE_TARGET_single_PRIOR
% minimise L to find gamma-MAP solution
global DEBUG
persistent iTARGETCALL
if nargin < 8 || ~exist('quiet', 'var'),
quiet = false;
end%if
Sigma_EB = empirical_bayes_cov(Noise.cov, sourceCovFn(logGamma), ...
LeadFields.lf);
% we want to maximise
% log p(g|B) = -0.5 Tr(BB' Sigma_b^{-1}) - n/2 logdet Sigma_b + log p(g).
try
logDetSigma = ROInets.logdet(Sigma_EB, 'chol');
catch ME
% some errors with non pos def matrices occuring, which is surprising.
if strcmp(ME.identifier, 'MATLAB:posdef'),
warning([mfilename ':OptimTarget:posdef'], ...
'Regularised covariance not positive defninite. \n');
logDetSigma = ROInets.logdet(Sigma_EB);
else
rethrow(ME);
end%if
end%try
% use property sum(eig(B, A)) = trace(inv(A) * B)
% or trace(AB) = sum(sum(A .* B')) (and covariance matrices are symmetric)
L = real((trace(Data.cov * osl_cholinv(Sigma_EB)) ... % faster than elementwise product or sum(eig()).
+ logDetSigma) * Data.nSamples - 2 * prior(logGamma, scale));
if DEBUG && ~quiet,
if ~exist('iTARGETCALL', 'var') || isempty(iTARGETCALL),
iTARGETCALL = 1;
else
iTARGETCALL = iTARGETCALL + 1;
end%if
fprintf(['Call to optim fn %4.0d: L = %0.8G, logGamma = %0.4G, ', ...
'logdet(Sigma_EB) = %0.6G. \n'], ...
iTARGETCALL, L, logGamma, logDetSigma);
end%if DEBUG
end%optimise_target_single_prior
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function L = optimise_target_double_prior(Data, Noise, LeadFields, params, ...
priors, priorScales, sourceCovFn)
%OPTIMISE_TARGET_DOUBLE_PRIOR
% minimise L to find gamma-MAP solution for S under B = (LS + e)/r.
global DEBUG
persistent iTARGETCALL
logGamma = real(params(1));
logRho = real(params(2));
pg = priors{1};
pr = priors{2};
Sigma_EB = empirical_bayes_cov(Noise.cov, sourceCovFn(logGamma), ...
LeadFields.lf);
try
logDetSigma = ROInets.logdet(Sigma_EB, 'chol');
catch ME
% some errors with non pos def matrices occuring, which is surprising.
if strcmp(ME.identifier, 'MATLAB:posdef'),
warning([mfilename ':OptimTarget:posdef'], ...
'Regularised covariance not positive defninite. \n');
logDetSigma = ROInets.logdet(Sigma_EB);
else
rethrow(ME);
end%if
end%try
% we want to maximise
% log p(r,g|B) = -0.5 r^2 Tr(BB' Sigma_b^{-1}) - n/2 logdet Sigma_b
% + n*log(r) + log p(r) + log p(g).
%
% use property sum(eig(B, A)) = trace(inv(A) * B)
% or trace(AB) = sum(sum(A .* B')) (and covariance matrices are symmetric)
L = (exp(2*logRho) * trace(Data.cov * osl_cholinv(Sigma_EB)) ... % faster than elementwise product or sum(eig()).
+ logDetSigma - 2*logRho) .* Data.nSamples ...
- 2*pg(logGamma, priorScales(1)) - 2*pr(logRho, priorScales(2));
if DEBUG,
if ~exist('iTARGETCALL', 'var') || isempty(iTARGETCALL),
iTARGETCALL = 1;
else
iTARGETCALL = iTARGETCALL + 1;
end%if
fprintf(['Call to optim fn %4.0d: L = %0.8G, logGamma = %0.4G, ', ...
'logRho = %0.4G, logdet(Sigma_EB) = %0.6G. \n'], ...
iTARGETCALL, L, logGamma, logRho, ...
logDetSigma);
end%if DEBUG
end%optimise_target_double_prior
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [lp, diff_lp] = parse_priors(priorChoice)
%PARSE_PRIORS creates function handle for priors on hierarchical variance
%parameters.
%
% Some priors require scales, so lp takes two arguments, lp(loggamma, scale).
% May also require d(lp)/dg. (= - df(g)/dg in Wipf and Nagarajan)
% pass on any passed-in functions
if isa(priorChoice, 'function_handle'),
lp = @(log_g,~) priorChoice(exp(log_g));
diff_lp = @(~,~) error([mfilename ':NoDiffLP'], ...
'No differential of gamma prior passed in.\n');
return
end%if
switch lower(priorChoice)
case 'uniform-variance'
% p(g) ~ 1
lp = @(log_g,~) 0; % ignore additive constant
diff_lp = @(g,~) 0;
case 'uniform-sd'
%p(sqrt(g)) ~ 1 --> p(g) ~ 0.5 / sqrt(g)
lp = @(log_g,~) - 0.5 * sum(log_g); % ignore additive constant
diff_lp = @(g,~) - 0.5 ./ sum(g);
case 'uniform-log'
% p(g) ~ 1/g
lp = @(log_g,~) - sum(log_g); % ignore additive constants as improper unless bounded
diff_lp = @(g,~) - 1.0 ./ sum(g);
case 'cauchy'
% p(sqrt(g) ~ 1/pi sqrt(A) / (g+A)
% --> p(g) ~ 1/(2 pi) sqrt(A) / ((A+g) sqrt(g))
lp = @(log_g,A) sum(-log(2*pi) + 0.5*log(A) ...
- 0.5*log_g - log(exp(log_g) + A));
diff_lp = @(g,A) sum(-0.5 ./ g - 1.0 ./ (g + A));
case 'log-normal' % scale on same space as g, so s.d. is log(A).
lp = @(log_g,A) - 0.5 * (log_g.' * log_g) ./ log(A)^2 ...
- length(log_g) * 0.5 * log(2*pi*log(A)^2);
diff_lp = @(g,A) sum( - log(g) ./ (log(A)^2 * g));
case 'normal'
lp = @(log_g,A) - 0.5 * (exp(log_g).' * exp(log_g)) ./ A^2 ...
- length(log_g) * 0.5 * log(2*pi*A^2);