Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
vgupta1 committed Apr 14, 2014
0 parents commit 73ed45c
Show file tree
Hide file tree
Showing 30 changed files with 1,325 additions and 0 deletions.
31 changes: 31 additions & 0 deletions CVXOPT Scripts/cvxOptTiming.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from cvxopt import *
from l1regls import *

#Gen some random data
m, n = 100, 1000
A = normal(m, n)
b = normal(m, 1)
lam = 1

###
#Convert problem to standard form
###
P = spdiag( [2 * A.T * A] + [0] * n)

ones = matrix([1.] * n)
c = matrix([-2 * A.T * b, lam * ones])

eye_n = spdiag([1.0] * n)
G = matrix([ [eye_n, -1 * eye_n], [-1 * eye_n, -1 * eye_n] ])

h = matrix(0., (2*n, 1) )

def run():
for i in range(10):
sol=solvers.qp(P, c, G, h)
# l1regls(A, b)

run()



15 changes: 15 additions & 0 deletions CVXOPT Scripts/cvxOptTiming_specialized.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import cvxopt
import l1regls as l1

def run():
for i in range(100):
l1.l1regls(A, b)

m, n = 300, 30
A = cvxopt.normal(300, 30)
b = cvxopt.normal(300, 1)

run()



25 changes: 25 additions & 0 deletions CVXOPT Scripts/cvxopt_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from cvxopt import *

#Gen some random data
m, n = 100, 1000
A = normal(m, n)
b = normal(m, 1)
lam = 1

###
#Convert problem to standard form
###





#####
def run():
for i in range(10):
sol=solvers.qp(P, c, G, h)

run()



24 changes: 24 additions & 0 deletions CVXOPT Scripts/cvxpy_timing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from cvxpy import *
import numpy

#gen some data
m, n = 100, 20
A, y = randn(m, n), randn(m, 1)

#Build the lasso program
#Note the similarity to a CVX style program
lam = parameter(attribute='nonnegative')
x = variable(n, 1, name="x")
obj = minimize( norm2( y - A * x) + norm1(x))
cnsts = []
p = program(obj, cnsts)
p.solve()

####
# Timing test.
# Only run once... bc it's dense
for i in range(10):
lam.value = 1
p.solve()


25 changes: 25 additions & 0 deletions CVX_Scripts/LoopedLeastSquares.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
%%%%%%%
% Regularized Least Squares in 'For' Loop
%%%%%%%
% Created by V. Gupta 13 Jan 2013


%% Load test data from a file
y = csvread('testResponse.csv');
A = csvread('testSignals.csv');
m = size(A, 1);
n = size(A, 2);

%% Loop version of Least Squares
cvx_begin
variable x(n)
variable r(m)

for i = 1:m
r(i) == y(i) - A(i, :) * x
end

minimize norm(r, 2) + norm(x, 1)
cvx_end

%Question: how do we incorporate dual variables
23 changes: 23 additions & 0 deletions CVX_Scripts/LoopedLeastSquares_dual.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
%%%%%%%
% Regularized Least Squares in 'For' Loop
%%%%%%%
% Created by V. Gupta 13 Jan 2013


%% Load test data from a file
y = csvread('testResponse.csv');
A = csvread('testSignals.csv');
m = size(A, 1);
n = size(A, 2);

%% Loop version of Least Squares
cvx_begin
variables x(n) r(m)
dual variable p{m}

for i = 1:m
p{i} : r(i) == y(i) - A(i, :) * x
end

minimize norm(r, 2) + norm(x, 1)
cvx_end
45 changes: 45 additions & 0 deletions CVX_Scripts/MomentProblems.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
%%%%%%%
% Moment Problems
% Finds a max entropy distribution on given support
% that approximately satisfies the given moment conditions
%%%%%%%
% Created by V. Gupta 13 Jan 2013

%% Read in the required moments
moments = csvread('Moments.csv');
numMoments = size(moments, 1);

%% Form required optimization problem
supp = (1:100)./10;
cvx_begin
variable p(length(supp))
maximize sum(entr(p));

%must be a probability
p >= 0;
sum(p) == 1;

for i = 1:numMoments
supp.^i * p <= 1.05 * moments(i);
supp.^i * p >= .95 * moments(i);
end

cvx_end

%% Plot some output
figure(1)
bar(p)
ylabel('Prob', 'FontSize', 15)
title('Distribution', 'FontSize', 15)

%Plot the dual variables
figure(2)


%% Tasks:
% Add dual variable for the normalization constraint
% Add dual variables for the moment constraints
% Add a plot for of the dual variables in figure(2)

%% Challenge questions:
% Could you have written this optimization without the loop?
8 changes: 8 additions & 0 deletions CVX_Scripts/Moments.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
5.9484
38.877
271.62
1997.6
15303
1.2119e+05
9.8648e+05
8.2157e+06
13 changes: 13 additions & 0 deletions CVX_Scripts/SimpleSDP.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
%%% Simple example demonstrating SDP functionality for CVX
% Created by V. Gupta 24 Jan 2013
cvx_begin
variable X(3, 3)
X == semidefinite(3)

5 * X(1,1) + 2 * X(2, 3) - X(3, 3) == 1
2 * X(2, 2) + 3 * X(3, 3) <= 2

minimize 3 * X(1, 1) - 2 * X(2, 3)


cvx_end
29 changes: 29 additions & 0 deletions CVX_Scripts/Simpledual.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
%%%%%%%
% Max Entropy Problem with Dual Vars
% Solves a simple max-entropy problem on given
% support
%%%%%%%
% Created by V. Gupta 1 Jan 2013

supp = 1:10;

cvx_begin
variable p(length(supp))
maximize sum(entr(p))

%must be a probability
sum(p) == 1; p >= 0;

%Constrain mean
%But incorporate a dual variable
%notice we DO NOT write dual variable lambda(1)
dual variable lambda
lambda : supp * p == 4;

cvx_end

%Plot some output
p
bar(p)
ylabel('Prob', 'FontSize', 15)
title('Max Entropy Distribution', 'FontSize', 15)
36 changes: 36 additions & 0 deletions CVX_Scripts/cvxTimingComparisons.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
%%%%%%%
% Timing Comparisons for CVX on regularized least squares
%%%%%%%
% Created by V. Gupta 13 Jan 2013


%Gen some random data
m = 100;
n = 20;
A = randn(m, n);
y = randn(m, 1);
lambda = 2.^(.1 * (0:99) - 5)
tot_cputime = 0;
tot_time = 0;
tic;
for i = 10:10:100
cvx_begin
variable x(n)
minimize norm(y - A*x, 2) + lambda(i) * norm(x, 1)

%uncomment this tic to geta n approx solver time
%tic;
cvx_end
tot_time = tot_time + toc;

tot_cputime = tot_cputime + cvx_cputime;
end

tot_cputime
tot_time
tot_time / tot_cputime

% Gurobi
% 53.2 s cputime
% 22.9 s tot_time

Loading

0 comments on commit 73ed45c

Please sign in to comment.