-
Notifications
You must be signed in to change notification settings - Fork 11
/
test_chance_constr.py
152 lines (120 loc) · 4.46 KB
/
test_chance_constr.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
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
import unittest
import time
import math
from matplotlib import pyplot
from matplotlib.backends.backend_pdf import PdfPages
import pymc
from scipy.linalg import sqrtm
import scipy.stats
from cvxpy import CVXOPT
from cvxstoc import NormalRandomVariable, prob
from cvxpy.expressions.variables import Variable
from cvxpy.atoms import *
from cvxpy import Minimize, Maximize, Problem
import numpy
from cvxpy.expressions.variables import NonNegative
class TestChanceConstr(unittest.TestCase):
def setUp(self):
numpy.random.seed(1)
def assert_feas(self, prob):
if prob.status is not "infeasible":
self.assertAlmostEqual(1,1)
else:
self.assertAlmostEqual(1,0)
def test_simple_problem(self):
# Create problem data.
n = numpy.random.randint(1,10)
eta = 0.95
num_samples = 10
c = numpy.random.rand(n,1)
mu = numpy.zeros(n)
Sigma = numpy.eye(n)
a = NormalRandomVariable(mu, Sigma)
b = numpy.random.randn()
# Create and solve optimization problem.
x = Variable(n)
p = Problem(Maximize(x.T*c), [prob(max_entries(x.T*a-b) >= 0, num_samples) <= 1-eta])
p.solve()
self.assert_feas(p)
def test_compute_conserv_approx_to_prob(self):
omega = NormalRandomVariable(0,1)
event = exp(omega) <= 0.5
num_samples = 50
self.assertAlmostEqual(1,1)
def test_robust_svm(self):
# Create problem data.
m = 100 # num train points
m_pos = math.floor(m/2)
m_neg = m - m_pos
n = 2 # num dimensions
mu_pos = 2*numpy.ones(n)
mu_neg = -2*numpy.ones(n)
sigma = 1
X = numpy.matrix(numpy.vstack((mu_pos + sigma*numpy.random.randn(m_pos,n),
mu_neg + sigma*numpy.random.randn(m_neg,n))))
y = numpy.hstack((numpy.ones(m_pos), -1*numpy.ones(m_neg)))
C = 1 # regularization trade-off parameter
ns = 50
eta = 0.1
# Create and solve optimization problem.
w, b, xi = Variable(n), Variable(), NonNegative(m)
constr = []
Sigma = 0.1*numpy.eye(n)
for i in range(m):
mu = numpy.array(X[i])[0]
x = NormalRandomVariable(mu, Sigma)
chance = prob(-y[i]*(w.T*x+b) >= (xi[i]-1), ns)
constr += [chance <= eta]
p = Problem(Minimize(norm(w,2) + C*sum_entries(xi)),
constr)
p.solve(verbose=True)
w_new = w.value
b_new = b.value
# Create and solve the canonical SVM problem.
constr = []
for i in range(m):
constr += [y[i]*(X[i]*w+b) >= (1-xi[i])]
p2 = Problem(Minimize(norm(w,2) + C*sum_entries(xi)), constr)
p2.solve()
w_old = w.value
b_old = b.value
self.assert_feas(p)
def test_value_at_risk(self):
# Create problem data.
n = numpy.random.randint(1,10)
pbar = numpy.random.randn(n)
Sigma = numpy.eye(n)
p = NormalRandomVariable(pbar,Sigma)
o = numpy.ones((n,1))
beta = 0.05
num_samples = 50
# Create and solve optimization problem.
x = Variable(n)
p1 = Problem(Minimize(-x.T*pbar), [prob(-x.T*p >= 0, num_samples) <= beta, x.T*o == 1, x >= -0.1])
p1.solve()
# Create and solve analytic form of optimization problem (as a check).
p2 = Problem(Minimize(-x.T*pbar),
[x.T*pbar >= scipy.stats.norm.ppf(1-beta) * norm2(sqrtm(Sigma) * x), x.T*o == 1, x >= -0.1])
p2.solve()
tol = 0.1
if numpy.abs(p1.value - p2.value) < tol:
self.assertAlmostEqual(1,1)
else:
self.assertAlmostEqual(1,0)
def test_yield_constr_cost_min(self):
# Create problem data.
n = 10
c = numpy.random.randn(n)
P, q, r = numpy.eye(n), numpy.random.randn(n), numpy.random.randn()
mu, Sigma = numpy.zeros(n), 0.1*numpy.eye(n)
omega = NormalRandomVariable(mu, Sigma)
m, eta = 100, 0.95
# Create and solve optimization problem.
x = Variable(n)
yield_constr = prob(quad_form(x+omega,P)
+ (x+omega).T*q + r >= 0, m) <= 1-eta
p = Problem(Minimize(x.T*c), [yield_constr])
p.solve()
self.assert_feas(p)
if __name__ == "__main__":
unittest.main()