-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithms.R
172 lines (144 loc) · 5.13 KB
/
algorithms.R
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
##### Code for "A Continuous-Time Mirror Descent Approach to Sparse Phase Retrieval" #####
# Define the function to generate data
# m: number of observations
# n: dimension of the signal x^*
# k: sparsity level
# sigma: measurement noise
gen_data_sparse = function(m, n, k, sigma = 0){
# Generate normalized k-sparse signal vector
x = rnorm(n)
ind = sample(1:n, k)
x[-ind] = 0
x = x / sqrt(sum(x^2))
# Generate measurement matrix
A = matrix(rnorm(m*n), nrow = m, ncol = n)
# Generate observations
y = as.vector((A%*%x)^2) + rnorm(m, sd = sigma)
return(list(A = A, x = x, y = y))
}
# Define the empirical risk, squared-magnitude-based loss
# x: signal vector
# A: measurement matrix
# y: vector of observations
wf_loss = function(x, A, y){
m = length(y)
return(sum((y - (A %*% x)^2)^2) / (4*m))
}
# Define the gradient, squared-magnitude-based loss
# x: signal vector
# A: measurement matrix
# y: vector of observations
wf_grad = function(x, A, y){
m = nrow(A)
return(t(A) %*% (((A%*%x)^2 - y) * (A%*%x)) / m)
}
# Define the Bregman divergence associated to the hyperbolic entropy mirror map
# between two vectors x and y
# beta: mirror map parameter
breg = function(x, y, beta){
beta = rep(beta, length(x))
psi1 = sum(x * asinh(x / beta) - sqrt(x^2 + beta^2))
psi2 = sum(y * asinh(y / beta) - sqrt(y^2 + beta^2))
grad = asinh(y / beta)
return(psi1 - psi2 - t(grad) %*% (x - y))
}
# Define HWF
# A: measurement matrix
# y: vector of observations
# ini: index for initialization (i_max)
# step: step size
# beta: parameter of mirror map, corresponds to initialization size
# eps: stopping criterion
# iteration: maximum number of iterations
# iterates: TRUE/FALSE, whether to save all iterates (relevant for large n)
# x_star: if iterates=FALSE, compute L2 error and Bregman divergence in each iteration
hwf = function(A, y, ini, step = 0.1, beta = 1e-6, iteration = 100, iterates = TRUE, x_star = 0){
m = nrow(A)
n = ncol(A)
# Create matrix to save iterates if iterates=TRUE
if(iterates == TRUE){
X = matrix(0, nrow = iteration, ncol = n)
}else{ # Else, create matrix to save L2 errors and Bregman divergences
X = matrix(0, nrow = iteration, ncol = 2)
}
# Initialization
# Estimate of the signal size
theta = sqrt(mean(y))
u_cur = rep(sqrt(beta/2), n)
v_cur = rep(sqrt(beta/2), n)
u_cur[ini] = sqrt(sqrt(theta^2/12) + sqrt(theta^2/12 + beta^2/4))
v_cur[ini] = sqrt(-sqrt(theta^2/12) + sqrt(theta^2/12 + beta^2/4))
x_cur = u_cur^2 - v_cur^2
# Save iterate if iterates=TRUE
if(iterates == TRUE){
X[1,] = x_cur
}else{ # Else, compute L2 error and Bregman divergence
X[1,1] = min(sqrt(sum((x_cur - x_star)^2)), sqrt(sum((x_cur + x_star)^2)))
X[1,2] = min(breg(x_star, x_cur, beta), breg(-x_star, x_cur, beta))
}
for(t in 2:iteration){
# Gradient updates
r = 2 * step * wf_grad(x_cur, A, y)
u_cur = u_cur * (1 - r)
v_cur = v_cur * (1 + r)
x_cur = u_cur^2 - v_cur^2
# Save iterate if iterates=TRUE
if(iterates == TRUE){
X[t,] = x_cur
}else{ # Else, compute L2 error and Bregman divergence
X[t,1] = min(sqrt(sum((x_cur - x_star)^2)), sqrt(sum((x_cur + x_star)^2)))
X[t,2] = min(breg(x_star, x_cur, beta), breg(-x_star, x_cur, beta))
}
}
return(X)
}
# Define Mirror descent (EG formulation)
# A: measurement matrix
# y: vector of observations
# ini: index for initialization (i_max)
# step: step size
# beta: mirror map parameter
# iteration: maximum number of iterations
# iterates: TRUE/FALSE, whether to save all iterates (relevant for large n)
# x_star: if iterates=FALSE, compute L2 error and Bregman divergence in each iteration
mirror_eg = function(A, y, ini, step = 0.4, beta = 1e-10, iteration = 100, iterates = TRUE, x_star = 0){
m = nrow(A)
n = ncol(A)
# Create matrix to save iterates if iterates=TRUE
if(iterates == TRUE){
X = matrix(0, nrow = iteration, ncol = n)
}else{ # Else, create matrix to save L2 errors and Bregman divergences
X = matrix(0, nrow = iteration, ncol = 2)
}
# Initialization
# Estimate the signal size
size_est = sqrt(mean(y))
# Initialize U and V
u_cur = rep(beta/2, n)
v_cur = rep(beta/2, n)
u_cur[ini] = size_est / (2 * sqrt(3)) + sqrt(size_est^2/12 + beta^2/4)
v_cur[ini] = -size_est / (2 * sqrt(3)) + sqrt(size_est^2/12 + beta^2/4)
x_cur = u_cur - v_cur
# Save iterate if iterates=TRUE
if(iterates == TRUE){
X[1,] = x_cur
}else{ # Else, compute L2 error and Bregman divergence
X[1,1] = min(sqrt(sum((x_cur - x_star)^2)), sqrt(sum((x_cur + x_star)^2)))
X[1,2] = min(breg(x_star, x_cur, beta), breg(-x_star, x_cur, beta))
}
for(t in 2:iteration){
# Exponentiated gradient updates
r = step * wf_grad(x_cur, A, y)
u_cur = u_cur * exp(-r)
v_cur = v_cur * exp(r)
x_cur = u_cur - v_cur
# Save iterate if iterates=TRUE
if(iterates == TRUE){
X[t,] = x_cur
}else{ # Else, compute L2 error and Bregman divergence
X[t,1] = min(sqrt(sum((x_cur - x_star)^2)), sqrt(sum((x_cur + x_star)^2)))
X[t,2] = min(breg(x_star, x_cur, beta), breg(-x_star, x_cur, beta))
}
}
return(X)
}