-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWSSR_QP_cos.m
More file actions
132 lines (100 loc) · 2.97 KB
/
Copy pathWSSR_QP_cos.m
File metadata and controls
132 lines (100 loc) · 2.97 KB
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
% This function constructs an N by N similarity matrix based on an input
% data matrix, which is done by solving N weighted sparse simplex
% representation (WSSR) problems.
%%%% Inputs:
% X: N by P data matrix.
% k: the number of nearest neighbours to consider.
% rho: the l1-penalty parameter.
% normalize: 1 or 0 depending on whether to normalise the data or not.
% stretch: 1 or 0 depending on whether to stretch X_{-i} to touch the
% perpendicular hyperplane of x_{i} or not.
% weight: 1 or 0 depending on whether to apply a weight matrix within the
% l1 and l2 norm penalty of the objective.
%%% Outputs:
% W0: the N by N coefficient matrix that consists of all the solution
% vectors.
% objs: a vector of length N that stores all the objective function values
% for all points given their solution vectors.
% Last updated: 8 Jul. 2020
function [W0, objs] = WSSR_QP_cos(X, k, rho, normalize, stretch, weight)
N = size(X, 1);
objs = zeros(N, 1);
W0 = zeros(N);
epsilon = 1e-4;
if nargin < 4
normalize = 1;
end
if normalize == 1
X0 = X;
X = norml2(X0, 1);
end
if nargin < 5
stretch = 1;
end
if nargin < 6
weight = 1;
end
%%
for i = 1:N
%% We remove any zero cosine similarities
idx = 1:N;
idx(i) = [];
Xopt = X(idx,:)';
yopt = X(i,:)';
% calculate the cosine similarities
sims = abs(yopt'*Xopt);
if sum(sims <= 1e-4) ~= 0
ind = find(sims >= 1e-4);
if length(ind) > 0
sims = sims(ind);
idx = idx(ind);
else
sims = 1e-4;
idx = randsample(idx, 1);
end
end
%% sort the similarity values in descending order
[vals, inds]= sort(abs(sims), 'descend');
if k == 0 % consider only the positive similarity values
dk = vals(vals>0);
nn = inds(vals>0);
k = length(dk);
else
if k > length(vals) % if some zero entries have been removed from sims
dk = vals;
nn = inds;
k = length(dk);
else
dk = vals(1:k);
nn = inds(1:k);
end
end
%% calculate the weight matrix
if weight == 1
D = diag(1./dk);
else
D = eye(length(dk));
end
%% stretch the data points that will be considered in the program
Y = X(idx(nn),:)';
if stretch
Xst = Y;
Ts = 1./(yopt'*Xst);
Xst = Xst*diag(Ts);
Y = Xst;
end
%% QP for Constrained LASSO
H = Y'*Y+epsilon.*D*D;
f = rho.*diag(D)-Y'*yopt;
%% solve the QP
options = optimoptions(@quadprog,'Display','off');
[beta,fopt,flag,out,lambda] = quadprog(H, f, -eye(k), zeros(k,1), ones(1,k), 1, ...
zeros(k,1), [], [], options);
W0(i,nn) = beta;
%% calculate objective function value for point i
partA = sum((yopt-Y*beta).^2);
partB = sum(D*beta);
partC = sum((D*beta).^2);
objs(i) = partA/2 + partB*rho + partC*epsilon/2;
end
end