forked from JuliaFirstOrder/ProximalOperators.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_regularize.jl
47 lines (30 loc) · 863 Bytes
/
test_regularize.jl
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
using ProximalOperators
using LinearAlgebra
# Nonsmooth + regularization
mu, lam = 2.0, 3.0
f = NormL1(mu)
g = Regularize(f, lam)
h = ElasticNet(mu, lam)
x = randn(10)
gx = call_test(g, x)
hx = call_test(h, x)
@test abs(gx - hx)/(1+abs(gx)) <= 1e-12
yg, gy = prox_test(g, x, 0.5)
yh, hy = prox_test(h, x, 0.5)
@test abs(gy - hy)/(1+abs(gy)) <= 1e-12
@test norm(yg - yh, Inf)/(1+norm(yg, Inf)) <= 1e-12
# Smooth + regularization (test also gradient)
m, n = 10, 20
lam = 1.5
A = randn(m, n)
b = randn(m)
f = LeastSquares(A, b)
g = Regularize(f, lam)
x = randn(n)
res = A*x-b
gx = call_test(g, x)
@test abs(0.5*norm(res)^2 + (0.5*lam)*norm(x)^2 - gx)/(1+abs(gx)) <= 1e-12
prox_test(g, x, 0.7)
grad_gx, gx1 = gradient_test(g, x)
@test abs(gx - gx1)/(1+abs(gx)) <= 1e-12
@test norm(grad_gx - A'*(A*x - b) - lam*x, Inf)/(1+norm(grad_gx, Inf)) <= 1e-12