forked from mwgeurts/libra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcdcov.m
executable file
·1536 lines (1418 loc) · 65 KB
/
mcdcov.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 [rew,raw]=mcdcov(x,varargin)
%MCDCOV computes the MCD estimator of a multivariate data set. This
% estimator is given by the subset of h observations with smallest covariance
% determinant. The MCD location estimate is then the mean of those h points,
% and the MCD scatter estimate is their covariance matrix. The default value
% of h is roughly 0.75n (where n is the total number of observations), but the
% user may choose each value between n/2 and n. Based on the raw estimates,
% weights are assigned to the observations such that outliers get zero weight.
% The reweighted MCD estimator is then given by the mean and covariance matrix
% of the cases with non-zero weight. To compute the MCD estimator,
% the FASTMCD algorithm is used.
%
% The MCD method is intended for continuous variables, and assumes that
% the number of observations n is at least 5 times the number of variables p.
% If p is too large relative to n, it would be better to first reduce
% p by variable selection or robust principal components (see the functions
% robpca.m and rapca.m).
%
% The MCD method was introduced in:
%
% Rousseeuw, P.J. (1984), "Least Median of Squares Regression,"
% Journal of the American Statistical Association, Vol. 79, pp. 871-881.
%
% The MCD is a robust method in the sense that the estimates are not unduly
% influenced by outliers in the data, even if there are many outliers.
% Due to the MCD's robustness, we can detect outliers by their large
% robust distances. The latter are defined like the usual Mahalanobis
% distance, but based on the MCD location estimate and scatter matrix
% (instead of the nonrobust sample mean and covariance matrix).
%
% The FASTMCD algorithm uses several time-saving techniques which
% make it available as a routine tool to analyze data sets with large n,
% and to detect deviating substructures in them. A full description of the
% algorithm can be found in:
%
% Rousseeuw, P.J. and Van Driessen, K. (1999), "A Fast Algorithm for the
% Minimum Covariance Determinant Estimator," Technometrics, 41, pp. 212-223.
%
% An important feature of the FASTMCD algorithm is that it allows for exact
% fit situations, i.e. when more than h observations lie on a (hyper)plane.
% Then the program still yields the MCD location and scatter matrix, the latter
% being singular (as it should be), as well as the equation of the hyperplane.
%
%
% Required input argument:
% x : a vector or matrix whose columns represent variables, and rows represent observations.
% Missing values (NaN's) and infinite values (Inf's) are allowed, since observations (rows)
% with missing or infinite values will automatically be excluded from the computations.
%
% Optional input arguments:
% cor : If non-zero, the robust correlation matrix will be
% returned. The default value is 0.
% h : The quantile of observations whose covariance determinant will
% be minimized. Any value between n/2 and n may be specified.
% The default value is 0.75*n.
% alpha : (1-alpha) measures the fraction of outliers the algorithm should
% resist. Any value between 0.5 and 1 may be specified. (default = 0.75)
% ntrial : The number of random trial subsamples that are drawn for
% large datasets. The default is 500.
% plots : If equal to one, a menu is shown which allows to draw several plots,
% such as a distance-distance plot. (default)
% If 'plots' is equal to zero, all plots are suppressed.
% See also makeplot.m
% classic : If equal to one, the classical mean and covariance matrix are computed as well.
% (default = 0)
% center : If equal to one the dataset is considered to be centered, i.e. the
% MCD location of the data is considered to be the origin.
%
% Input arguments for advanced users:
% Hsets : Instead of random trial h-subsets (default, Hsets = []), Hsets makes it possible to give certain
% h-subsets as input. Hsets is a matrix that contains the indices of the observations of one
% h-subset as a row.
% factor : If not equal to 0 (default), the consistency factor is adapted. Only useful in case of the
% kmax approach.
%
% I/O: result=mcdcov(x,'alpha',0.75,'h',h,'ntrial',500)
% If only one output argument is listed, only the final result ('result')
% is returned.
% The user should only give the input arguments that have to change their default value.
% The name of the input arguments needs to be followed by their value.
% The order of the input arguments is of no importance.
%
% Examples: [rew,raw]=mcdcov(x);
% result=mcdcov(x,'h',20,'plots',0);
% [rew,raw]=mcdcov(x,'alpha',0.8,'cor',0)
%
% The output structure 'raw' contains intermediate results, with the following
% fields :
%
% raw.center : The raw MCD location of the data.
% raw.cov : The raw MCD covariance matrix (multiplied by a consistency factor).
% raw.cor : The raw MCD correlation matrix, if input argument 'cor' was non-zero.
% raw.wt : Weights based on the estimated raw covariance matrix 'raw.cov' and
% the estimated raw location 'raw.center' of the data. These weights determine
% which observations are used to compute the final MCD estimates.
% raw.objective : The determinant of the raw MCD covariance matrix.
%
% The output structure 'rew' contains the final results, namely:
%
% rew.center : The robust location of the data, obtained after reweighting, if
% the raw MCD is not singular. Otherwise the raw MCD center is
% given here.
% rew.cov : The robust covariance matrix, obtained after reweighting, if the raw MCD
% is not singular. Otherwise the raw MCD covariance matrix is given here.
% rew.cor : The robust correlation matrix, obtained after reweighting, if
% options.cor was non-zero.
% rew.h : The number of observations that have determined the MCD estimator,
% i.e. the value of h.
% rew. Hsubsets : A structure that contains Hopt and Hfreq:
% Hopt : The subset of h points whose covariance matrix has minimal determinant,
% ordered following increasing robust distances.
% Hfreq : The subset of h points which are the most frequently selected during the whole
% algorithm.
% rew.alpha : (1-alpha) measures the fraction of outliers the algorithm should
% resist.
% rew.md : The distance of each observation from the classical
% center of the data, relative to the classical shape
% of the data. Often, outlying points fail to have a
% large Mahalanobis distance because of the masking
% effect.
% rew.rd : The distance of each observation to the final,
% reweighted MCD center of the data, relative to the
% reweighted MCD scatter of the data. These distances allow
% us to easily identify the outliers. If the reweighted MCD
% is singular, raw.rd is given here.
% rew.cutoff : Cutoff values for the robust and mahalanobis distances
% rew.flag : Flags based on the reweighted covariance matrix and the
% reweighted location of the data. These flags determine which
% observations can be considered as outliers. If the reweighted
% MCD is singular, raw.wt is given here.
% rew.method : A character string containing information about the method and
% about singular subsamples (if any).
% rew.plane : In case of an exact fit, rew.plane is a structure that contains
% eigvct and eigval:
% eigvct: contains the eigenvectors that define the plane. I.e.
% the eigenvectors belonging to none-zero
% eigenvalues.
% eigval: contains the corresponding eigenvalues of
% eigvct.
% rew.classic : If the input argument 'classic' is equal to one, this structure
% contains results of the classical analysis: center (sample mean),
% cov (sample covariance matrix), md (Mahalanobis distances), class ('COV').
% rew.class : 'MCDCOV'
% rew.X : If x is bivariate, same as the x in the call to mcdcov,
% without rows containing missing or infinite values.
%
% This function is part of LIBRA: the Matlab Library for Robust Analysis,
% available at:
% http://wis.kuleuven.be/stat/robust
%
% Written by Katrien Van Driessen and Bjorn Rombouts
% Revisions by Sanne Engelen, Sabine Verboven, Wannes Van den Bossche
% Last Update: 09/04/2004, 01/08/2007, 25/04/2016
% The FASTMCD algorithm works as follows:
%
% The dataset contains n cases and p variables.
% When n < 2*nmini (see below), the algorithm analyzes the dataset as a whole.
% When n >= 2*nmini (see below), the algorithm uses several subdatasets.
%
% When the dataset is analyzed as a whole, a trial subsample of p+1 cases
% is taken, of which the mean and covariance matrix are calculated.
% The h cases with smallest relative distances are used to calculate
% the next mean and covariance matrix, and this cycle is repeated csteps1
% times. For small n we consider all subsets of p+1 out of n, otherwise
% the algorithm draws 500 random subsets by default.
% Afterwards, the 10 best solutions (means and corresponding covariance
% matrices) are used as starting values for the final iterations.
% These iterations stop when two subsequent determinants become equal.
% (At most csteps3 iteration steps are taken.) The solution with smallest
% determinant is retained.
%
% When the dataset contains more than 2*nmini cases, the algorithm does part
% of the calculations on (at most) maxgroup nonoverlapping subdatasets, of
% (roughly) maxobs cases.
%
% Stage 1: For each trial subsample in each subdataset, csteps1 (see below) iterations are
% carried out in that subdataset. For each subdataset, the 10 best solutions are
% stored.
%
% Stage 2 considers the union of the subdatasets, called the merged set.
% (If n is large, the merged set is a proper subset of the entire dataset.)
% In this merged set, each of the 'best solutions' of stage 1 are used as starting
% values for csteps2 (sse below) iterations. Also here, the 10 best solutions are stored.
%
% Stage 3 depends on n, the total number of cases in the dataset.
% If n <= 5000, all 10 preliminary solutions are iterated.
% If n > 5000, only the best preliminary solution is iterated.
% The number of iterations decreases to 1 according to n*p (If n*p <= 100,000 we
% iterate csteps3 (sse below) times, whereas for n*p > 1,000,000 we take only one iteration step).
%
if rem(nargin-1,2)~=0
error('The number of input arguments should be odd!');
end
% Assigning some input parameters
data = x;
raw.cor = [];
rew.cor = [];
rew.plane = [];
% The maximum value for n (= number of observations) is:
nmax=50000;
% The maximum value for p (= number of variables) is:
pmax=50;
% To change the number of subdatasets and their size, the values of
% maxgroup and nmini can be changed.
maxgroup=5;
nmini=300;
% The number of iteration steps in stages 1,2 and 3 can be changed
% by adapting the parameters csteps1, csteps2, and csteps3.
csteps1=2;
csteps2=2;
csteps3=100;
% dtrial : number of subsamples if not all (p+1)-subsets will be considered.
dtrial=500;
if size(data,1)==1
data=data';
end
% Observations with missing or infinite values are ommitted.
ok=all(isfinite(data),2);
data=data(ok,:);
xx=data;
[n,p]=size(data);
% Some checks are now performed.
if n==0
error('All observations have missing or infinite values.')
end
if n > nmax
error(['The program allows for at most ' int2str(nmax) ' observations.'])
end
if p > pmax
error(['The program allows for at most ' int2str(pmax) ' variables.'])
end
if n < p
error('Need at least (number of variables) observations.')
end
%internal variables
hmin=quanf(0.5,n,p);
%Assiging default values
h=quanf(0.75,n,p);
default=struct('alpha',0.75,'h',h,'plots',1,'ntrial',dtrial,'cor',0,'seed',0,'classic',0,'center',0,'Hsets',[],'factor',0);
list=fieldnames(default);
options=default;
IN=length(list);
i=1;
counter=1;
%Reading optional inputarguments
if nargin>2
%
% placing inputfields in array of strings
%
chklist=cell(1,floor((nargin-1)/2));
for j=1:nargin-1
if rem(j,2)~=0
varargin{j}=strtrim(varargin{j}); %remove white space
chklist{i}=varargin{j};
i=i+1;
end
end
%
% Checking which default parameters have to be changed
% and keep them in the structure 'options'.
%
while counter<=IN
index=find(strcmp(list(counter,:),chklist));
if ~isempty(index) % in case of similarity
for j=1:nargin-2 % searching the index of the accompanying field
if rem(j,2)~=0 % fieldnames are placed on odd index
if strcmp(chklist{index},varargin{j})
I=j; %index of located similarity
end
end
end
options.(chklist{index})=varargin{I+1};
% options=setfield(options,chklist{index},varargin{I+1});
end
counter=counter+1;
end
dummy=sum(strcmp(chklist,'h')+2*strcmp(chklist,'alpha'));
switch dummy
case 1% checking inputvariable h
% hmin is the minimum number of observations whose covariance determinant
% will be minimized.
if isempty(options.Hsets)
if options.h < hmin
disp(['Warning: The MCD must cover at least ' int2str(hmin) ' observations.'])
disp(['The value of h is set equal to ' int2str(hmin)])
options.h = hmin;
elseif options.h > n
error('h is greater than the number of non-missings and non-infinites.')
elseif options.h < p
error(['h should be larger than the dimension ' int2str(p) '.'])
end
end
options.alpha=options.h/n;
case 2
if options.alpha < 0.5
options.alpha=0.5;
mess=sprintf(['Attention (mcdcov.m): Alpha should be larger than 0.5. \n',...
'It is set to 0.5.']);
disp(mess)
end
if options.alpha > 1
options.alpha=0.75;
mess=sprintf(['Attention (mcdcov.m): Alpha should be smaller than 1.\n',...
'It is set to 0.75.']);
disp(mess)
end
options.h=quanf(options.alpha,n,p);
case 3
error('Both input arguments alpha and h are provided. Only one is required.')
end
end
centered=options.center; %data is considered centered if one
h=options.h; %number of regular datapoints on which estimates are based. h=[alpha*n]
plots=options.plots; %relevant plots are plotted
alfa=options.alpha; %percentage of regular observations
ntrial=options.ntrial; %number of subsets to be taken in the first step
cor=options.cor; %correlation matrix
seed=options.seed; %seed of the random generator
cutoff.rd=sqrt(chi2inv(0.975,p)); %cutoff value for the robust distance
cutoff.md=cutoff.rd; %cutoff value for the mahalanobis distance
Hsets = options.Hsets;
if ~isempty(Hsets)
Hsets_ind = 1;
else
Hsets_ind = 0;
end
factor = options.factor;
if factor == 0
factor_ind = 0;
else
factor_ind = 1;
end
% Some initializations.
rew.flag=NaN(1,length(ok));
raw.wt=NaN(1,length(ok));
raw.rd=NaN(1,length(ok));
rew.rd=NaN(1,length(ok));
rew.mahalanobis=NaN(1,length(ok));
rew.method=sprintf('\nMinimum Covariance Determinant Estimator.');
correl=NaN;
% weights : weights of the observations that are not excluded from the computations.
% These are the observations that don't contain missing or infinite values.
% bestobj : best objective value found.
weights=zeros(1,n);
bestobj=inf;
% The classical estimates are computed
% [Loadings,Scores,Eigs,#nonzero eigs,centeredData,meanData]
[Pcl,~,Lcl,rcl,~,cXcl] = classSVD(data,0,centered);
clasmean=cXcl;
clascov=Pcl*diag(Lcl)*Pcl';
if p < 5
eps=1e-12;
elseif p <= 8
eps=1e-14;
else
eps=1e-16;
end
%h-th order zero statistic
if ~centered
med=median(data);
else
med=zeros(1,p);
end
mad=sort(abs(bsxfun(@minus,data,med)));
mad=mad(h,:);
iis=find( (mad < eps)); %all vars with h-th order zero statistic
if ~isempty(iis)
ii=iis(1); %smallest variable
% The h-th order statistic is zero for the ii-th variable. The array plane contains
% all the observations which have the same value for the ii-th variable.
plane=find(abs(data(:,ii)-med(ii)) < eps)';
weights(plane)=1;
if p==1
if ~centered
meanplane=mean(data(plane,:));
else
meanplane=zeros(1,p);
end
rew.flag=weights;
raw.wt=weights;
[raw.center,rew.center]=deal(meanplane);
[raw.cov,rew.cov,raw.objective]=deal(0);
if plots
rew.method=sprintf('\nUnivariate location and scale estimation.');
rew.method=char(rew.method,sprintf('%g of the %g observations are identical.',length(plane),n));
end
else
[P,~,L,~,~,meanplane] = classSVD(data(plane,:),0,centered);
rew.plane.eigvct=P;
rew.plane.eigval=L;
covplane=P*diag(L)*P';
[raw.center,raw.cov,rew.center,rew.cov,raw.objective,raw.wt,rew.flag,...
rew.method]=displ(3,length(plane),weights,n,p,meanplane,covplane,rew.method,rew.plane,h,iis);
end
rew.Hsubsets.Hopt = plane;
rew.Hsubsets.Hfreq = plane;
%classical analysis?
if options.classic==1
classic.cov=clascov;
classic.center=clasmean;
classic.class='COV';
else
classic=0;
end
%assigning the output
rewo=rew;rawo=raw;
rew=struct('center',{rewo.center},'cov',{rewo.cov},'cor',{cor},'h',{h},'Hsubsets',{rewo.Hsubsets},'alpha',{alfa},...
'flag',{rewo.flag},'plane', {rewo.plane},'method',{rewo.method},'class',{'MCDCOV'},'classic',{classic},'X',{xx});
raw=struct('center',{rawo.center},'cov',{rawo.cov},'cor',{rawo.cor},'objective',{rawo.objective},...
'wt',{rawo.wt},'class',{'MCDCOV'},'classic',{classic},'X',{x});
if size(data,2)~=2
rew=rmfield(rew,'X');
raw=rmfield(raw,'X');
end
return
end
%exact fit situation
if rcl < p
% all observations lie on a hyperplane.
rew.plane.eigvct=Pcl;%./mad';
rew.plane.eigval=Lcl;
weights(1:n)=1;
if cor
correl=clascov./(sqrt(diag(clascov))*sqrt(diag(clascov))');
[rew.cor,raw.cor]=deal(correl);
end
[raw.center,raw.cov,rew.center,rew.cov,raw.objective,raw.wt,rew.flag,...
rew.method]=displ(1,n,weights,n,p,clasmean,clascov,rew.method,rew.plane);
%classical analysis?
if options.classic==1
classic.cov=clascov;
classic.center=clasmean;
classic.class='COV';
else
classic=0;
end
rew.Hsubsets.Hopt=1:n;
rew.Hsubsets.Hfreq=1:n;
%assigning the output
rewo=rew;rawo=raw;
rew=struct('center',{rewo.center},'cov',{rewo.cov},'cor',{rewo.cor},'h',{h},'Hsubsets',{rewo.Hsubsets},'alpha',{alfa},...
'rd',{rewo.rd},'cutoff',{cutoff},'flag',{rewo.flag},'plane',{rewo.plane},'method',{rewo.method},...
'class',{'MCDCOV'},'classic',{classic},'X',{xx});
raw=struct('center',{rawo.center},'cov',{rawo.cov},'cor',{rawo.cor},'objective',{rawo.objective},...
'cutoff',{cutoff},'wt',{rawo.wt}, 'class',{'MCDCOV'},'classic',{classic},'X',{x});
if size(data,2)~=2
rew=rmfield(rew,'X');
raw=rmfield(raw,'X');
end
return
end
% The standardization of the data will now be performed.
data=bsxfun(@rdivide,bsxfun(@minus,data,med),mad);
% The standardized classical estimates are now computed.
clmean=(clasmean-med)./mad;%mean(data);
clcov=bsxfun(@rdivide,bsxfun(@rdivide,clascov,mad),mad');%cov(data);
% The univariate non-classical case is now handled.
if p==1 && h~=n
[rew.center,rewsca,weights,raw.center,raw.cov,raw.rd,Hopt]=unimcd(data,h,centered);
rew.Hsubsets.Hopt = Hopt';
rew.Hsubsets.Hfreq = Hopt';
[raw.cov,raw.center]=trafo(raw.cov,raw.center,med,mad);
raw.objective=raw.cov;
raw.cutoff=cutoff.rd;
raw.wt=weights;
rew.cov=rewsca^2;
mah=(data-rew.center).^2/rew.cov;
rew.rd=sqrt(mah');
rew.flag=(rew.rd<=cutoff.rd);
rew.cutoff=cutoff.rd;
[rew.cov,rew.center]=trafo(rew.cov,rew.center,med,mad);
rew.mahalanobis=abs(data'-clmean)/sqrt(clcov);
%classical analysis
if options.classic==1
classic.cov=clascov;
classic.center=clasmean;
classic.md=rew.mahalanobis;
classic.class='COV';
else
classic=0;
end
%assigning the output
rewo=rew;rawo=raw;
rew=struct('center',{rewo.center},'cov',{rewo.cov},'cor',{rewo.cor},'h',{h},'Hsubsets',{rewo.Hsubsets},...
'alpha',{alfa},'rd',{rewo.rd},'cutoff',{cutoff},'flag',{rewo.flag}, 'plane',{[]},'method',{rewo.method},...
'class',{'MCDCOV'},'md',{rewo.mahalanobis},'classic',{classic},'X',{xx});
raw=struct('center',{rawo.center},'cov',{rawo.cov},'cor',{rawo.cor},'objective',{rawo.objective},...
'rd',{rawo.rd},'cutoff',{cutoff},'wt',{rawo.wt}, 'class',{'MCDCOV'},'classic',{classic},'X',{x});
if size(data,2)~=2
rew=rmfield(rew,'X');
raw=rmfield(raw,'X');
end
try
if plots && options.classic
makeplot(rew,'classic',1)
elseif plots
makeplot(rew)
end
catch %output must be given even if plots are interrupted
%> delete(gcf) to get rid of the menu
return
end
return
end
% The classical case is now handled.
if h==n
if plots
msg=sprintf('The MCD estimates based on %g observations are equal to the classical estimates.\n',h);
rew.method=char(rew.method,msg);
end
raw.center=clmean;
raw.cov=clcov;
raw.objective=det(clcov);
mah=mahalanobis(data,clmean,'cov',clcov);
rew.mahalanobis=sqrt(mah);
raw.rd=rew.mahalanobis;
weights= mah <= cutoff.rd^2;
raw.wt=weights;
[rew.center,rew.cov]=weightmecov(data,weights,centered);
if cor
raw.cor=raw.cov./(sqrt(diag(raw.cov))*sqrt(diag(raw.cov))');
rew.cor=rew.cov./(sqrt(diag(rew.cov))*sqrt(diag(rew.cov))');
else
raw.cor=0;
rew.cor=0;
end
[Pfull,~,r] = covsvd(rew.cov,1);
Pzero=Pfull(:,r+1:p);
if r<p
[obsinplaneInd,count,~]=obsinplane(data,rew.center,Pzero,centered);
if ~centered
covar = cov(data(obsinplaneInd,:));
meanvct=mean(data(obsinplaneInd,:));
else
covar=(data(obsinplaneInd,:)'*data(obsinplaneInd,:))/(length(obsinplaneInd)-1);
meanvct=zeros(1,p);
end
[covar,center]=trafo(covar,meanvct,med,mad);
[rew.plane.eigvct,rew.plane.eigval,~] = covsvd(covar,0);
if cor
correl=covar./(sqrt(diag(covar))*sqrt(diag(covar))');
end
rew.method=displrw(count,n,p,center,covar,rew.method,rew.plane,cor,correl);
rew.rd=raw.rd;
else
mah=mahalanobis(data,rew.center,'cov',rew.cov);
weights = mah <= cutoff.md^2;
rew.rd=sqrt(mah);
end
[raw.cov,raw.center]=trafo(raw.cov,raw.center,med,mad);
[rew.cov,rew.center]=trafo(rew.cov,rew.center,med,mad);
raw.objective=raw.objective*prod(mad)^2;
rew.flag=weights;
%classical analysis?
if options.classic==1
classic.cov=clascov;
classic.center=clasmean;
classic.md=rew.mahalanobis;
classic.class='COV';
else
classic=0;
end
%assigning Hsubsets:
rew.Hsubsets.Hopt = 1:n;
rew.Hsubsets.Hfreq = 1:n;
%assigning the output
rewo=rew;rawo=raw;
rew=struct('center',{rewo.center},'cov',{rewo.cov},'cor',{rewo.cor},'h',{h},'Hsubsets',{rewo.Hsubsets},'alpha',{alfa},...
'rd',{rewo.rd},'cutoff',{cutoff},'flag',{rewo.flag},'plane',{rewo.plane},...
'method',{rewo.method},'class',{'MCDCOV'},'md',{rewo.mahalanobis},'classic',{classic},'X',{xx});
raw=struct('center',{rawo.center},'cov',{rawo.cov},'cor',{rawo.cor},'objective',{rawo.objective},...
'rd',{rawo.rd},'cutoff',{cutoff},'wt',{rawo.wt}, 'class',{'MCDCOV'},'classic',{classic},'X',{x});
if size(data,2)~=2
rew=rmfield(rew,'X');
raw=rmfield(raw,'X');
end
try
if plots && options.classic
makeplot(rew,'classic',1)
elseif plots
makeplot(rew)
end
catch %output must be given even if plots are interrupted
%> delete(gcf) to get rid of the menu
return
end
return
end
percent=h/n;
teller = zeros(1,n+1);
if Hsets_ind
csteps = csteps1;
fine = 0;
part = 0;
final = 1;
tottimes = 0;
nsamp = size(Hsets,1);
obsingroup = n;
else
% If n >= 2*nmini the dataset will be divided into subdatasets. For n < 2*nmini the set
% will be treated as a whole.
if n >= 2*nmini
maxobs=maxgroup*nmini;
if n >= maxobs
ngroup=maxgroup;
group(1:maxgroup)=nmini;
else
ngroup=floor(n/nmini);
minquan=floor(n/ngroup);
group = zeros(1,ngroup);
group(1)=minquan;
for s=2:ngroup
group(s)=minquan+double(rem(n,ngroup)>=s-1);
end
end
part=1;
adjh=floor(group(1)*percent);
nsamp=floor(ntrial/ngroup);
minigr=sum(group);
obsingroup=fillgroup(n,group,ngroup,seed);
% obsingroup : i-th row contains the observations of the i-th group.
% The last row (ngroup+1-th) contains the observations for the 2nd stage
% of the algorithm.
else
[part,group,ngroup,adjh,minigr,obsingroup]=deal(0,n,1,h,n,n);
replow=[50,22,17,15,14,zeros(1,45)];
if n < replow(p)
% All (p+1)-subsets will be considered.
allSubsets=1;
if ~centered
perm=[1:p,p];
nsamp=nchoosek(n,p+1);
else
perm=[1:p-1,p-1];
nsamp=nchoosek(n,p);
end
else
allSubsets=0;
nsamp=ntrial;
end
end
% some further initialisations.
csteps=csteps1;
% tottimes : the total number of iteration steps.
% fine : becomes 1 when the subdatasets are merged.
% final : becomes 1 for the final stage of the algorithm.
[tottimes,fine,final,prevObj]=deal(0);
if part
% bmean1 : contains, for the first stage of the algorithm, the means of the ngroup*10
% best estimates.
% bcov1 : analogous to bmean1, but now for the covariance matrices.
% bobj1 : analogous to bmean1, but now for the objective values.
% coeff1 : if in the k-th subdataset there are at least adjh observations that lie on
% a hyperplane then the coefficients of this plane will be stored in the
% k-th column of coeff1.
Pzeros1=cell(ngroup,1);
bobj1=inf(ngroup,10);
bmean1=cell(ngroup,10);
bP1 = cell(ngroup,10);
bL1 = cell(ngroup,10);
[Pzeros1{:}]=deal(NaN);
[bmean1{:}]=deal(NaN);
[bL1{:}]=deal(NaN);
[bP1{:}]=deal(NaN);
end
% bmean : contains the means of the ten best estimates obtained in the second stage of the
% algorithm.
% bcov : analogous to bmean, but now for the covariance matrices.
% bobj : analogous to bmean, but now for the objective values.
% coeff : analogous to coeff1, but now for the merged subdataset.
% If the data is not split up, the 10 best estimates obtained after csteps1 iterations
% will be stored in bmean, bcov and bobj.
bobj=inf(1,10);
bmean=cell(1,10);
% Pzeros=NaN(p,1); %does not require initialization
bP = cell(1,10);
bL = cell(1,10);
[bmean{:}]=deal(NaN);
[bP{:}]=deal(NaN);
[bL{:}] = deal(NaN);
end
seed=0;
while final~=2
if fine || (~part && final)
if ~Hsets_ind
nsamp=10;
end
if final
adjh=h;
ngroup=1;
if n*p <= 1e+5
csteps=csteps3;
elseif n*p <=1e+6
csteps=10-(ceil(n*p/1e+5)-2);
else
csteps=1;
end
if n > 5000
nsamp=1;
end
else
adjh=floor(minigr*percent);
csteps=csteps2;
end
end
% found : becomes 1 if we have a singular intermediate MCD estimate.
% is set to 0 for every new subgroup
found=0;
for k=1:ngroup
if ~fine
found=0;
end
for i=1:nsamp
tottimes=tottimes+1;
% ns becomes 1 if we have a singular trial subsample and if there are at
% least adjh observations in the subdataset that lie on the concerning hyperplane.
% In that case we don't have to take C-steps. The determinant is zero which is
% already the lowest possible value. If ns=1, no C-steps will be taken and we
% start with the next sample. If we, for the considered subdataset, haven't
% already found a singular MCD estimate, then the results must be first stored in
% bmean, bcov, bobj or in bmean1, bcov1 and bobj1. If we, however, already found
% a singular result for that subdataset, then the results won't be stored
% (the hyperplane we just found is probably the same as the one we found earlier.
% We then let adj be zero. This will guarantee us that the results won't be
% stored) and we start immediately with the next sample.
adj=1;
ns=0;
% For the second and final stage of the algorithm the array sortdist(1:adjh)
% contains the indices of the observations corresponding to the adjh observations
% with minimal relative distances with respect to the best estimates of the
% previous stage. An exception to this, is when the estimate of the previous
% stage is singular. For the second stage we then distinguish two cases :
%
% 1. There aren't adjh observations in the merged set that lie on the hyperplane.
% The observations on the hyperplane are then extended to adjh observations by
% adding the observations of the merged set with smallest orthogonal distances
% to that hyperplane.
% 2. There are adjh or more observations in the merged set that lie on the
% hyperplane. We distinguish two cases. We haven't or have already found such
% a hyperplane. In the first case we start with a new sample. But first, we
% store the results in bmean1, bcov1 and bobj1. In the second case we
% immediately start with a new sample.
%
% For the final stage we do the same as 1. above (if we had h or more observations
% on the hyperplane we would already have found it).
if ~Hsets_ind
if final
if ~isinf(bobj(i))
meanvct=bmean{i};
P=bP{i};
L = bL{i};
if bobj(i)==0
[~,~,distToPlane]=obsinplane(data,meanvct,Pzeros,centered);
[~,sortdist]=sort(distToPlane);
else
[~,sortdist]=mahal2(bsxfun(@minus,data,meanvct)*P,sqrt(L),part,fine,final,k,obsingroup);
end
else
break
end
elseif fine
if ~isinf(bobj1(k,i))
meanvct=bmean1{k,i};
P=bP1{k,i};
L=bL1{k,i};
if bobj1(k,i)==0
[~,~,distToPlane]=obsinplane(data(obsingroup{end},:),meanvct,Pzeros1{k},centered);
[dis,ind]=sort(distToPlane);
sortdist=obsingroup{end}(ind);
if dis(adjh) < 1e-8
if found==0
obj=0;
Pzeros=Pzeros1{k};
found=1;
else
adj=0;
end
ns=1;
end
else
[~,sortdist]=mahal2(bsxfun(@minus,data(obsingroup{end},:),meanvct)*P,sqrt(L),part,fine,final,k,obsingroup);
end
else
break;
end
else %first stage: subgroups
if ~centered
psamp = p+1;
else
psamp = p;
end
if ~part %there are no subsets
if allSubsets %all p+1 subsets out of n are considered
l=psamp;
perm(l)=perm(l)+1;
while ~(l==1 || perm(l) <=(n-(psamp-l)))
l=l-1;
perm(l)=perm(l)+1;
for j=(l+1):psamp
perm(j)=perm(j-1)+1;
end
end
index=perm;
else %take a random p+1 subset out of n
[index,seed]=randomset(n,psamp,seed);
end
else %there are k=1:ngroup subsets
[index,seed]=randomset(group(k),psamp,seed);
index=obsingroup{k}(index);
end
[Pfull,~,Lfull,r,~,meanvct] = classSVD(data(index,:),1,centered);
Pzero=Pfull(:,r+1:p);
P=Pfull(:,1:r);
L=Lfull(1:r);
if r==0 %all points collapse into single point
ns = 1;
rew.center=meanvct;
rew.cov=zeros(p,p);
rew.flag = zeros(1,n);
rew.flag(index) = 1;
rew.Hsubsets.Hopt=1:n;
rew.Hsubsets.Hfreq=1:n;
elseif r < p
% The trial subsample is singular.
% We distinguish two cases :
%
% 1. There are adjh or more observations in the subdataset that lie
% on the hyperplane. If the data is not split up, we have adjh=h and thus
% an exact fit. If the data is split up we distinguish two cases.
% We haven't or have already found such a hyperplane. In the first case
% we check if there are more than h observations in the entire set
% that lie on the hyperplane. If so, we have an exact fit situation.
% If not, we start with a new trial subsample. But first, the
% results must be stored bmean1, bcov1 and bobj1. In the second case
% we immediately start with a new trial subsample.
%
% 2. There aren't adjh observations in the subdataset that lie on the
% hyperplane. We then extend the trial subsample until it isn't singular
% anymore.
% The smallest eigenvector belonging to a non-zero eigenvalue contains
%the coefficients of the hyperplane.
if ~part
[obsinplaneInd,count,~]=obsinplane(data,meanvct,Pzero,centered);
else
[obsinplaneInd,count,~]=obsinplane(data(obsingroup{k},:),meanvct,Pzero,centered);
end
if count >= adjh
if ~part %full dataset, so more than h samples on plane
if ~centered
covar = cov(data(obsinplaneInd,:));
meanvct=mean(data(obsinplaneInd,:));
else
covar=(data(obsinplaneInd,:)'*data(obsinplaneInd,:))/(length(obsinplaneInd)-1);
meanvct=zeros(1,p);
end
[covar,center]=trafo(covar,meanvct,med,mad);
[rew.plane.eigvct,rew.plane.eigval,~] = covsvd(covar,0);
weights(obsinplaneInd)=1;
[raw.center,raw.cov,rew.center,rew.cov,raw.objective,...
raw.wt,rew.flag,rew.method]=displ(2,count,weights,n,p,center,covar,...
rew.method,rew.plane);
if cor
correl=covar./(sqrt(diag(covar))*sqrt(diag(covar))');
[rew.cor,raw.cor]=deal(correl);
end
rew.Hsubsets.Hopt=obsinplaneInd;
rew.Hsubsets.Hfreq=obsinplaneInd;
return
elseif found==0 %no singular sample found in this subgroup so far
[obsinplaneInd,count2,~]=obsinplane(data,meanvct,Pzero,centered);
if count2>=h
if ~centered
covar = cov(data(obsinplaneInd,:));
meanvct=mean(data(obsinplaneInd,:));
else
covar=(data(obsinplaneInd,:)'*data(obsinplaneInd,:))/(length(obsinplaneInd)-1);
meanvct=zeros(1,p);
end
[covar,center]=trafo(covar,meanvct,med,mad);
[rew.plane.eigvct,rew.plane.eigval,~] = covsvd(covar,0);
weights(obsinplaneInd)=1;
[raw.center,raw.cov,rew.center,rew.cov,raw.objective,...
raw.wt,rew.flag,rew.method]=displ(2,count2,weights,n,p,center,covar,...
rew.method,rew.plane);
if cor
correl=covar./(sqrt(diag(covar))*sqrt(diag(covar))');
[rew.cor,raw.cor]=deal(correl);
end
rew.Hsubsets.Hopt=obsinplaneInd;
rew.Hsubsets.Hfreq=obsinplaneInd;
return
end
obj=0;
Pzeros1{k}=Pzero;
found=1;
ns=1;
else %a singular sample is already found in this subgroup
ns=1;
adj=0;
end
else %count < adjh
covmat = P*diag(L)*P';
while det(covmat) < exp(-50*p) %add samples until no longer singular
[index1,seed]=addobs(index,n,seed);
if ~centered
[covmat,meanvct] = updatecov(data(index,:),covmat,meanvct,data(setdiff(index1,index),:),[],1);
else
[covmat,~] = updatecov(data(index,:),covmat,meanvct,data(setdiff(index1,index),:),[],1);
end
index = index1;
end
end %count >= adjh
end
if ~ns %no subspace plane with count >= adjh has been found: find sorted robust distances
if ~part
[~,sortdist] = mahal2(bsxfun(@minus,data,meanvct)*P,sqrt(L),part,fine,final,k,obsingroup);
else
[~,sortdist] = mahal2(bsxfun(@minus,data(obsingroup{k},:),meanvct)*P,sqrt(L),part,fine,final,k,obsingroup);
end
end
end
end
if ~ns
%none singular sample: do C-steps
for j=1:csteps
tottimes=tottimes+1;
if j == 1
if Hsets_ind
obs_in_set = Hsets(i,:);
else
obs_in_set = sort(sortdist(1:adjh));
teller(obs_in_set) = teller(obs_in_set) + 1;
teller(end) = teller(end) + 1;
end
else
% The observations correponding to the adjh smallest mahalanobis
% distances determine the subset for the next iteration.
if ~part
[~,sortdist] = mahal2(bsxfun(@minus,data,meanvct)*P,sqrt(L),part,fine,final,k,obsingroup);
else
if final
[~,sortdist] = mahal2(bsxfun(@minus,data,meanvct)*P,sqrt(L),part,fine,final,k,obsingroup);
elseif fine
[~,sortdist] = mahal2(bsxfun(@minus,data(obsingroup{end},:),meanvct)*P,sqrt(L),part,fine,final,k,obsingroup);
else
[~,sortdist] = mahal2(bsxfun(@minus,data(obsingroup{k},:),meanvct)*P,sqrt(L),part,fine,final,k,obsingroup);
end
end
% Creation of a H-subset.
obs_in_set=sort(sortdist(1:adjh));
teller(obs_in_set) = teller(obs_in_set) + 1;
teller(end) = teller(end) + 1;
end
[Pfull,~,Lfull,r,~,meanvct] = classSVD(data(obs_in_set,:),1,centered);
Pzero=Pfull(:,r+1:p);
P=Pfull(:,1:r);