-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtests.py
More file actions
62 lines (54 loc) · 2.23 KB
/
tests.py
File metadata and controls
62 lines (54 loc) · 2.23 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
import numpy as np
from unittest import TestCase
from ya_glm.opt.constraint import convex
class TestProjectionsOnConstraints(TestCase):
def setUp(self):
pass
def assert_arrays_close(self, test_array, ref_array):
"Custom assertion for arrays being almost equal"
try:
np.testing.assert_allclose(test_array, ref_array)
except AssertionError:
self.fail()
def test_Positive(self):
cons = convex.Positive()
v = np.array([-1, 0, 2, 3, -2])
self.assert_arrays_close(cons.prox(v), [0, 0, 2, 3, 0])
self.assertEqual(cons.prox(-2), 0)
def test_LinearEquality(self):
A = np.identity(2)
b = np.array([1,1])
cons = convex.LinearEquality(A, b)
proj = cons.prox(b) # the proj of b should just be b
self.assert_arrays_close(proj, b)
def test_L2Ball(self):
cons1 = convex.L2Ball(1)
self.assert_arrays_close(cons1.prox([0,0,0]), [0,0,0])
self.assert_arrays_close(cons1.prox([1,0,0]), [1,0,0])
self.assert_arrays_close(cons1.prox([0.5,0,0]), [0.5,0,0])
self.assert_arrays_close(cons1.prox([1,1,1]), np.array([1,1,1])/np.sqrt(3))
self.assert_arrays_close(cons1.prox([1,-1,1]), np.array([1,-1,1])/np.sqrt(3))
cons4 = convex.L2Ball(4)
self.assert_arrays_close(cons4.prox([0,0,0]), [0,0,0])
self.assert_arrays_close(cons4.prox([1,0,0]), [1,0,0])
self.assert_arrays_close(cons4.prox([0.5,0,0]), [0.5,0,0])
self.assert_arrays_close(cons4.prox([-4,3,0]), np.array([-4,3,0])/(5/4))
def test_Isotonic(self):
cons = convex.Isotonic(increasing=True)
for v in [
np.arange(5),
np.array([-1, 0, 2, 3, -2]),
np.array([-1, 3, 0, 3, 2])
]:
result = cons.prox(v)
lags = result[1:] - result[:-1]
self.assertTrue((lags >= 0).all())
cons = convex.Isotonic(increasing=False)
for v in [
np.arange(5),
np.array([-1, 0, 2, 3, -2]),
np.array([-1, 3, 0, 3, 2])
]:
result = cons.prox(v)
lags = result[1:] - result[:-1]
self.assertTrue((lags <= 0).all())