-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogistic_Lasso.m
131 lines (113 loc) · 3.17 KB
/
Logistic_Lasso.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
clear;
clc;
%rng(1);
%% Generating simulation data %%
beta=zeros(1,2000);
beta(1)=1;
beta(2)=-1;
beta(3)=1;
beta(4)=-1;
beta(5)=1;
beta(6)=-1;
beta(7)=1;
beta(8)=-1;
beta(9)=1;
beta(10)=-1;
beta_t=beta';
train_size=500;
test_size=200;
sample_size=train_size+test_size;
intercept=0.0;
x = normrnd(0, 1, sample_size, size(beta,2));
[n,p]=size(x);
% Setting corrlation %
% cor=0; % correlation %
% for i=1:n
% for j=1:p-1
% x(i,j)=X(i,j+1)*sqrt(1-cor)+X(i,1)*sqrt(cor);
% end
% end
%l = intercept + (x * beta' + 0.2 * normrnd(0, 1, n, 1));
l = intercept + x * beta';
prob=exp(l)./(1 + exp(l));
for i=1:sample_size
if prob(i)>0.5
y(i)=1;
else
y(i)=0;
end
end
y=y';
x_train=x(1:train_size,:);
x_test=x(train_size+1:sample_size,:);
y_train=y(1:train_size,:);
y_test=y(train_size+1:sample_size,:);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Logistic + Lasso %%
col=size(x_train,2);
row=size(x_train,1);
beta=zeros(col,1);
% calculating the beta_zero %
temp=sum(y_train)/row;
beta_zero=log(temp/(1-temp));
% Inputting X, Y, beta_int and lambda %
beta_int=[beta_zero;beta];
beta_true=[beta_zero;beta_t];
x0=ones(row,1);
X=[x0,x_train];
Y=y_train;
% Setting lambda
lambda_max =norm(X'*Y,'inf'); % according to the https://github.com/yangziyi1990/SparseGDLibrary.git
lambda_min = lambda_max * 0.001;
m=10;
for i=1:m
Lambda1(i)=lambda_max*(lambda_min/lambda_max)^(i/m);
lambda=Lambda1(i);
beta=Logistic_Lasso_func(X,Y,beta_int,lambda);
beta_path(:,i)=beta;
fprintf('iteration times:%d\n',i);
end
[Opt,Mse]=CV_Lasso_logistic(X,Y,Lambda1);
beta_opt=beta_path(:,Opt);
beta_zero=beta_opt(1);
beta=beta_opt(2:end);
l = beta_zero + x_test * beta;
prob=exp(l)./(1 + exp(l));
for i=1:test_size
if prob(i)>0.5
test_y(i)=1;
else
test_y(i)=0;
end
end
error=test_y'-y_test;
error_number_testing=length(nonzeros(error))
beta_non_zero=length(nonzeros(beta_opt))
%% Performance
[accurancy,sensitivity,specificity]=performance(y_test,test_y');
fprintf('The accurancy of testing data (Lasso): %f\n' ,accurancy);
fprintf('The sensitivity of testing data (Lasso): %f\n' ,sensitivity);
fprintf('The specificity of testing data (Lasso): %f\n' ,specificity);
%% performance for training data
beta_zero=beta_opt(1);
beta=beta_opt(2:end);
l1 = beta_zero + x_train * beta;
prob1=exp(l1)./(1 + exp(l1));
for i=1:train_size
if prob1(i)>0.5
train_y(i)=1;
else
train_y(i)=0;
end
end
error_train=train_y'-y_train;
error_number_train=length(nonzeros(error_train))
[accurancy_train,sensitivity_train,specificity_train]=performance(y_train,train_y');
fprintf('The accurancy of training data(Lasso): %f\n' ,accurancy_train);
fprintf('The sensitivity of training data (Lasso): %f\n' ,sensitivity_train);
fprintf('The specificity of training data (Lasso): %f\n' ,specificity_train);
%% performance for beta
[accurancy_beta,sensitivity_beta,specificity_beta]=performance_beta(beta_true,beta_opt);
fprintf('The accurancy of beta (Lasso): %f\n' ,accurancy_beta);
fprintf('The sensitivity of beta (Lasso): %f\n' ,sensitivity_beta);
fprintf('The specificity of beta (Lasso): %f\n' ,specificity_beta);