-
Notifications
You must be signed in to change notification settings - Fork 23
/
SparseCoefRecovery.py
95 lines (87 loc) · 3.57 KB
/
SparseCoefRecovery.py
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
# This function takes the D x N matrix of N data points and write every
# point as a sparse linear combination of other points.
# Xp: D x N matrix of N data points
# cst: 1 if using the affine constraint sum(c)=1, else 0
# Opt: type of optimization, {'L1Perfect','L1Noisy','Lasso','L1ED'}
# lambda: regularizartion parameter of LASSO, typically between 0.001 and
# 0.1 or the noise level for 'L1Noise'
# CMat: N x N matrix of coefficients, column i correspond to the sparse
# coefficients of data point in column i of Xp
# For this to work install cvxpy from:
# https://cvxgrp.github.io/cvxpy/install/index.html
import numpy as np
import cvxpy as cvx
def SparseCoefRecovery(Xp, cst=0, Opt='Lasso', lmbda=0.001):
D, N = Xp.shape
CMat = np.zeros([N, N])
for i in range(0, N):
y = Xp[:, i]
if i == 0:
Y = Xp[:, i + 1:]
elif i > 0 and i < N - 1:
Y = np.concatenate((Xp[:, 0:i], Xp[:, i + 1:N]), axis=1)
else:
Y = Xp[:, 0:N - 1]
if cst == 1:
if Opt == 'Lasso':
c = cvx.Variable(N - 1, 1)
obj = cvx.Minimize(cvx.norm(c, 1) + lmbda * cvx.norm(Y * c - y))
constraint = [cvx.sum(c) == 1]
prob = cvx.Problem(obj, constraint)
prob.solve()
elif Opt == 'L1Perfect':
c = cvx.Variable(N - 1, 1)
obj = cvx.Minimize(cvx.norm(c, 1))
constraint = [Y * c == y, cvx.sum(c) == 1]
prob = cvx.Problem(obj, constraint)
prob.solve()
elif Opt == 'L1Noise':
c = cvx.Variable(N - 1, 1)
obj = cvx.Minimize(cvx.norm(c, 1))
constraint = [(Y * c - y) <= lmbda, cvx.sum(c) == 1]
prob = cvx.Problem(obj, constraint)
prob.solve()
elif Opt == 'L1ED':
c = cvx.Variable(N - 1 + D, 1)
obj = cvx.Minimize(cvx.norm(c, 1))
constraint = [np.concatenate((Y, np.identity(D)), axis=1)
* c == y, cvx.sum(c[0:N - 1]) == 1]
prob = cvx.Problem(obj, constraint)
prob.solve()
else:
if Opt == 'Lasso':
c = cvx.Variable(N - 1, 1)
obj = cvx.Minimize(cvx.norm(c, 1) + lmbda * cvx.norm(Y * c - y))
prob = cvx.Problem(obj)
prob.solve()
elif Opt == 'L1Perfect':
c = cvx.Variable(N - 1, 1)
obj = cvx.Minimize(cvx.norm(c, 1))
constraint = [Y * c == y]
prob = cvx.Problem(obj, constraint)
prob.solve()
elif Opt == 'L1Noise':
c = cvx.Variable(N - 1, 1)
obj = cvx.Minimize(cvx.norm(c, 1))
constraint = [(Y * c - y) <= lmbda]
prob = cvx.Problem(obj, constraint)
prob.solve()
elif Opt == 'L1ED':
c = cvx.Variable(N - 1 + D, 1)
obj = cvx.Minimize(cvx.norm(c, 1))
constraint = [np.concatenate((Y, np.identity(D)), axis=1) * c == y]
prob = cvx.Problem(obj, constraint)
prob.solve()
if i == 0:
CMat[0, 0] = 0
CMat[1:N, 0] = c.value[0: N - 1]
elif i > 0 and i < N - 1:
CMat[0:i, i] = c.value[0:i]
CMat[i, i] = 0
CMat[i + 1:N, i] = c.value[i:N - 1]
else:
CMat[0:N - 1, N - 1] = c.value[0:N - 1]
CMat[N - 1, N - 1] = 0
return CMat
if __name__ == "__main__":
pass