-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZeroFPR_SVDD.m
95 lines (61 loc) · 2.26 KB
/
ZeroFPR_SVDD.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
function [X_star, Y_star, alpha_star, Rsquared_star, ...
a_star, SV_star, YSV_star, param_star] = ...
ZeroFPR_SVDD(X, Y, alpha, Rsquared, kernel, param, C1, C2, treshold, param_opt)
% ZeroFPR_SVDD
% Usage:[X_star, Y_star, alpha_star, Rsquared_star, ...
% a_star, SV_star, YSV_star, param_star] = ...
% ZeroFPR_SVDD(X, Y, alpha, Rsquared, kernel, param, C1, C2, treshold, param_opt)
% X: training set
% Y: labels of training set
% alpha: lagrange multipliers of SVDD
% Rsquared: squared radius of the SVDD
% kernel: 'linear, 'gaussian', 'polynomial'
% param: kernel parameter
% C1, C2: SVDD weights
% treshold: percentage of FP to be achieved
% param_opt: 'Y' if a parameter optimization is necessary
maxiter=1000;
i=0;
m = size(X,2);
y_i= SVDD_N1C_TEST(X, Y, alpha, X, kernel, param, Rsquared);
%FPR_old=0;
while(i<maxiter)
i=i+1;
X_pred_i=[X,Y,y_i];
XP_i = X_pred_i(X_pred_i(:,m+2)==1,(1:m));
XN_i = X_pred_i(X_pred_i(:,m+2)==-1,(1:m));
X_i = [XP_i;XN_i];
YP_i=X_pred_i(X_pred_i(:,m+2)==1,m+1);
YN_i=X_pred_i(X_pred_i(:,m+2)==-1,m+1);
Y_i = [YP_i;YN_i];
if(isequal(param_opt,'Y'))
disp('Parametr optimization started')
%optimization on 1000 points of the training set
X_opt=X_i(1:1000,:);
Y_opt=Y_i(1:1000,:);
intKerPar = linspace(0.1,5,10);
[param_star, ~, ~, ~, ~] = ...
OptimiseParam_NSVDD(X_opt, Y_opt, kernel, 0.5, 3, intKerPar, C1, C2);
else
param_star = param;
end
[alpha, Rsquared_i, a_i, SV_i, YSV_i] = ...
SVDD_N1C_TRAINING(X_i, Y_i, kernel, param_star, C1, C2,'off');
y_i = SVDD_N1C_TEST(X_i, Y_i, alpha, X, kernel, param_star, Rsquared_i);
M_i = [y_i Y];
N = nnz(Y_i(:,1)==-1);
FP = sum(M_i(:,1)==+1 & M_i(:,2)==-1);
FPR_i = FP/N;
if(FPR_i<treshold)% || (abs(FPR_i-FPR_old)<0.01*FPR_old))
disp('Treshold reached')
break;
end
disp(['Iteration ', num2str(i), '--> FPR = ', num2str(FPR_i)])
end
X_star = X_i;
Y_star = Y_i;
alpha_star = alpha;
Rsquared_star = Rsquared_i;
a_star = a_i;
SV_star = SV_i;
YSV_star = YSV_i;