In [1]:
function setrand()
    s = rand(1:1000)
    println("s = ", s)
    srand(s)
end

function loaddata(n)
    d = 2   # dimension of u space
    s = 1.4  # mean separation factor

    fracs = [0.5, 0.5]     # fraction in the different classes
    K = length(fracs)      # number of classes

    # square roots of covariances
    covs = Any[]
    for i=1:K
        A = randn(2,2)
        D,V = eig(A*A')
        D2 = [randn()+0.7  0; 0 randn()+0.7]
        push!(covs, V*D2*V')
    end

    # assign categories according to fractions
    cfracs = cumsum(fracs)
    cats = zeros(Int64,n)
    c = 1
    for i=1:n
        if i/n > cfracs[c]
            c += 1
        end
        cats[i] = c
    end
    
    # choose the means of each cluster
    means = zeros(d,K)
    for i=1:K
        means[:,i] = s*randn(d)
    end
    
    # assign U and V
    U = zeros(n,d)
    V = zeros(n)
    for i=1:n
        c = cats[i]
        V[i] = c 
        x = (means[:,c] + covs[c]*randn(d))'
        U[i,:] = x
    end

    Unew, Vnew = normalizedata(U,V)
    return Unew, Vnew, K
end

function normalizedata(U,V)
    n = size(U,1)
    xmin = minimum(U[:,1]) - 0.1
    xmax = maximum(U[:,1]) + 0.1
    ymin = minimum(U[:,2]) - 0.1
    ymax = maximum(U[:,2]) + 0.1
    m = 0.5*[ xmax+xmin ymax+ymin]
    Unew = U - repmat(m, n, 1)
    Unew[:,1] = Unew[:,1] * 8/(xmax-xmin)
    Unew[:,2] = Unew[:,2] * 8/(ymax-ymin)
    return Unew, V
end
Out[1]:
normalizedata (generic function with 1 method)
In [2]:
using PyPlot, Convex, ECOS

srand(323)
#srand(999)
U,V,K = loaddata(100)
n = length(V)
X = [ones(n) U]
y = V
y[V.==2] = -1 

U_p = U[y.==1,:]
U_n = U[y.==-1,:]

## least squares classifier

theta = Variable(3);
obj = sumsquares(1-y.*(X*theta)) + 0.1*sumsquares(theta[2:end])
#obj = sumsquares(X*theta-y) + 0.1*sumsquares(theta[2:end])
problem = minimize(obj)
solve!(problem, ECOSSolver())
theta_opt = evaluate(theta)

# separating plane
s1 = -5:0.1:5
s2 = - ( theta_opt[1] + s1*theta_opt[2] ) / theta_opt[3]
s2p = - (  1 + theta_opt[1] + s1*theta_opt[2] ) / theta_opt[3]
s2n = - ( -1 + theta_opt[1] + s1*theta_opt[2] ) / theta_opt[3]
s2_ls = s2

figure()
plot(U_p[:,1], U_p[:,2], "o", alpha=0.5)
plot(U_n[:,1], U_n[:,2], "v", alpha=0.5)
plot(s1,s2)
plot(s1,s2p, ":")
plot(s1,s2n, ":")
grid("on")
axis("square")
xlim(-5, 5)
ylim(-5, 5)
title("Least squares classifier")

## logistic regression

theta = Variable(3);
obj = logisticloss(-y.*(X*theta)) + 0.1*sumsquares(theta[2:end])
problem = minimize(obj)
solve!(problem, ECOSSolver())
theta_opt = evaluate(theta)

# separating plane
s1 = -5:0.1:5
s2 = - ( theta_opt[1] + s1*theta_opt[2] ) / theta_opt[3]
s2_lr = s2

figure()
plot(U_p[:,1], U_p[:,2], "o", alpha=0.5)
plot(U_n[:,1], U_n[:,2], "v", alpha=0.5)
plot(s1,s2)
grid("on")
axis("square")
xlim(-5, 5)
ylim(-5, 5)
title("Logistic regression")

## support vector machine

theta = Variable(3);
obj = sum(max(0,1-y.*(X*theta))) + 0.1*sumsquares(theta[2:end])
problem = minimize(obj)
solve!(problem, ECOSSolver())
theta_opt = evaluate(theta)

# separating plane
s1 = -5:0.1:5
s2 = - ( theta_opt[1] + s1*theta_opt[2] ) / theta_opt[3]
s2p = - (  1 + theta_opt[1] + s1*theta_opt[2] ) / theta_opt[3]
s2n = - ( -1 + theta_opt[1] + s1*theta_opt[2] ) / theta_opt[3]
s2_svm = s2

figure()
plot(U_p[:,1], U_p[:,2], "o", alpha=0.5, label="Positive data")
plot(U_n[:,1], U_n[:,2], "v", alpha=0.5, label="Negative data")
plot(s1,s2, label=L"$\theta^Tx=0$")
plot(s1,s2p, ":", label=L"$\theta^Tx=-1$")
plot(s1,s2n, ":", label=L"$\theta^Tx=1$")
grid("on")
axis("square")
legend()
xlim(-5, 5)
ylim(-5, 5)
title("Support vector machine")

figure()
plot(U_p[:,1], U_p[:,2], "o", alpha=0.5, label="Positive data")
plot(U_n[:,1], U_n[:,2], "v", alpha=0.5, label="Negative data")
plot(s1,s2_ls, label="Least squares classifier")
plot(s1,s2_lr, label="Logistic regression")
plot(s1,s2_svm, label="Support vector machine")
grid("on")
axis("square")
legend()
xlim(-5, 5)
ylim(-5, 5)
Out[2]:
(-5, 5)
ECOS 2.0.5 - (C) embotech GmbH, Zurich Switzerland, 2012-15. Web: www.embotech.com/ECOS

It     pcost       dcost      gap   pres   dres    k/t    mu     step   sigma     IR    |   BT
 0  -2.285e-18  -2.614e+00  +1e+01  5e-01  1e-01  1e+00  2e+00    ---    ---    1  1  - |  -  - 
 1  -7.846e-02  -1.996e-01  +1e+00  4e-02  1e-02  2e-01  2e-01  0.9056  5e-02   1  1  1 |  0  0
 2  -1.130e-02  -4.688e-02  +4e-01  1e-02  3e-03  6e-02  7e-02  0.7768  1e-01   1  1  2 |  0  0
 3  +2.707e-01  +8.523e-01  +4e-01  1e+00  4e-02  3e+00  6e-02  0.6679  6e-01   2  2  2 |  0  0
 4  +2.370e+00  +2.425e+00  +2e-02  5e-02  2e-03  2e-01  3e-03  0.9890  4e-02   2  2  2 |  0  0
 5  +5.513e+00  +6.267e+00  +3e-03  3e-02  1e-03  8e-01  5e-04  0.9540  1e-01   3  2  2 |  0  0
 6  +9.960e+00  +1.039e+01  +1e-03  8e-03  3e-04  4e-01  2e-04  0.9890  3e-01   3  3  2 |  0  0
 7  +1.315e+01  +1.352e+01  +2e-04  3e-03  1e-04  4e-01  4e-05  0.8603  1e-01   4  3  3 |  0  0
 8  +1.438e+01  +1.442e+01  +3e-05  5e-04  2e-05  5e-02  5e-06  0.8675  1e-02   4  2  3 |  0  0
 9  +1.454e+01  +1.455e+01  +6e-06  8e-05  3e-06  9e-03  1e-06  0.9766  2e-01   4  2  3 |  0  0
10  +1.459e+01  +1.459e+01  +3e-07  4e-06  1e-07  4e-04  5e-08  0.9559  4e-03   4  2  3 |  0  0
11  +1.459e+01  +1.459e+01  +6e-09  8e-08  3e-09  7e-06  1e-09  0.9797  2e-04   4  2  2 |  0  0
12  +1.459e+01  +1.459e+01  +1e-10  2e-09  7e-11  2e-07  2e-11  0.9791  1e-04   3  2  2 |  0  0

OPTIMAL (within feastol=1.7e-09, reltol=9.0e-12, abstol=1.3e-10).
Runtime: 0.000745 seconds.


ECOS 2.0.5 - (C) embotech GmbH, Zurich Switzerland, 2012-15. Web: www.embotech.com/ECOS

It     pcost       dcost      gap   pres   dres    k/t    mu     step   sigma     IR    |   BT
 0  +0.000e+00  -1.564e+02  +7e+02  6e-01  2e+00  1e+00  1e+00    ---    ---    0  0  - |  -  - 
 1  -1.691e+01  -9.713e+01  +3e+02  3e-01  1e+00  6e-01  4e-01  0.6433  8e-02   1  1  1 |  1  1
 2  -5.065e+00  -3.872e+01  +1e+02  1e-01  6e-01  3e-01  1e-01  0.6910  6e-02   2  1  1 |  0  1
 3  +3.357e+00  -8.584e+00  +3e+01  5e-02  2e-01  1e-01  4e-02  0.7377  6e-02   1  1  1 |  0  1
 4  +3.912e+00  -1.460e+00  +1e+01  2e-02  1e-01  5e-02  2e-02  0.6266  8e-02   1  1  1 |  0  2
 5  +3.358e+00  +2.733e-01  +7e+00  1e-02  6e-02  3e-02  9e-03  0.5013  1e-01   2  1  1 |  1  3
 6  +2.842e+00  +1.074e+00  +4e+00  6e-03  3e-02  2e-02  5e-03  0.7833  4e-01   2  1  1 |  5  1
 7  +2.554e+00  +1.589e+00  +2e+00  3e-03  2e-02  1e-02  3e-03  0.9791  5e-01   2  1  1 |  7  0
 8  +2.345e+00  +1.856e+00  +1e+00  2e-03  9e-03  5e-03  1e-03  0.6266  2e-01   2  1  1 |  4  2
 9  +2.186e+00  +1.972e+00  +4e-01  8e-04  4e-03  2e-03  6e-04  0.9791  4e-01   2  1  1 |  6  0
10  +2.083e+00  +2.035e+00  +1e-01  2e-04  9e-04  5e-04  1e-04  0.7833  1e-02   2  1  1 |  0  1
11  +2.051e+00  +2.040e+00  +2e-02  4e-05  2e-04  1e-04  3e-05  0.7833  2e-02   1  1  1 |  0  1
12  +2.047e+00  +2.043e+00  +9e-03  2e-05  9e-05  5e-05  1e-05  0.6266  6e-02   2  1  1 |  1  2
13  +2.045e+00  +2.044e+00  +2e-03  4e-06  2e-05  1e-05  3e-06  0.7833  2e-02   1  1  1 |  1  1
14  +2.044e+00  +2.044e+00  +9e-04  2e-06  9e-06  5e-06  1e-06  0.6266  7e-02   1  1  1 |  2  2
15  +2.044e+00  +2.044e+00  +2e-04  4e-07  2e-06  1e-06  3e-07  0.7833  1e-02   2  1  1 |  1  1
16  +2.044e+00  +2.044e+00  +8e-05  1e-07  8e-07  5e-07  1e-07  0.6266  5e-02   2  2  2 |  2  2
17  +2.044e+00  +2.044e+00  +2e-05  3e-08  2e-07  1e-07  3e-08  0.7833  9e-03   2  2  2 |  1  1
18  +2.044e+00  +2.044e+00  +7e-06  1e-08  7e-08  4e-08  1e-08  0.6266  5e-02   1  3  2 |  2  2
19  +2.044e+00  +2.044e+00  +2e-06  3e-09  2e-08  9e-09  2e-09  0.7833  9e-03   1  2  2 |  1  1
20  +2.044e+00  +2.044e+00  +7e-07  1e-09  7e-09  4e-09  1e-09  0.6266  1e-01   1  2  2 |  3  2
21  +2.044e+00  +2.044e+00  +2e-07  3e-10  2e-09  1e-09  2e-10  0.7833  5e-02   1  3  2 |  2  1
22  +2.044e+00  +2.044e+00  +7e-08  1e-10  8e-10  4e-10  1e-10  0.6266  5e-02   2  2  3 |  2  2
23  +2.044e+00  +2.044e+00  +2e-08  3e-11  2e-10  1e-10  3e-11  0.7833  5e-02   2  2  2 |  2  1

OPTIMAL (within feastol=1.9e-10, reltol=8.7e-09, abstol=1.8e-08).
Runtime: 0.022126 seconds.


ECOS 2.0.5 - (C) embotech GmbH, Zurich Switzerland, 2012-15. Web: www.embotech.com/ECOS

It     pcost       dcost      gap   pres   dres    k/t    mu     step   sigma     IR    |   BT
 0  +5.818e+00  +1.076e+02  +6e+02  6e-01  4e+00  1e+00  3e+00    ---    ---    1  1  - |  -  - 
 1  +4.875e+00  +2.629e+01  +2e+02  1e-01  9e-01  1e+00  1e+00  0.8035  2e-01   2  1  1 |  0  0
 2  +3.444e+00  +1.204e+01  +8e+01  4e-02  4e-01  6e-01  4e-01  0.6115  5e-02   1  1  1 |  0  0
 3  +2.042e+00  +6.195e+00  +4e+01  2e-02  2e-01  3e-01  2e-01  0.5966  1e-01   1  1  1 |  0  0
 4  +1.784e+00  +4.812e+00  +3e+01  1e-02  2e-01  2e-01  1e-01  0.4289  2e-01   1  1  1 |  0  0
 5  +1.200e+00  +2.504e+00  +1e+01  6e-03  8e-02  6e-02  6e-02  0.7570  3e-01   1  1  1 |  0  0
 6  +1.142e+00  +2.403e+00  +9e+00  5e-03  1e-01  6e-02  4e-02  0.4594  5e-01   1  1  1 |  0  0
 7  +7.775e-01  +1.534e+00  +6e+00  3e-03  5e-02  2e-02  3e-02  0.8097  7e-01   1  1  1 |  0  0
 8  +5.558e-01  +1.060e+00  +4e+00  2e-03  4e-02  1e-02  2e-02  0.9890  6e-01   1  1  1 |  0  0
 9  +2.497e-01  +3.504e-01  +8e-01  4e-04  7e-03  2e-03  4e-03  0.8086  3e-02   1  1  1 |  0  0
10  +2.238e-01  +2.409e-01  +1e-01  7e-05  1e-03  3e-04  7e-04  0.9155  1e-01   1  1  1 |  0  0
11  +2.147e-01  +2.171e-01  +2e-02  1e-05  2e-04  5e-05  1e-04  0.9555  1e-01   1  1  1 |  0  0
12  +2.131e-01  +2.132e-01  +2e-04  1e-07  2e-06  6e-07  1e-06  0.9888  1e-04   1  1  1 |  0  0
13  +2.131e-01  +2.131e-01  +3e-06  2e-09  2e-08  7e-09  1e-08  0.9876  1e-04   2  1  1 |  0  0
14  +2.131e-01  +2.131e-01  +6e-08  3e-11  5e-10  1e-10  3e-10  0.9804  1e-04   2  1  1 |  0  0
15  +2.131e-01  +2.131e-01  +2e-09  8e-12  1e-11  4e-12  7e-12  0.9736  2e-04   2  1  1 |  0  0

OPTIMAL (within feastol=1.3e-11, reltol=7.1e-09, abstol=1.5e-09).
Runtime: 0.001260 seconds.

/usr/local/lib/python2.7/dist-packages/matplotlib/cbook/deprecation.py:107: MatplotlibDeprecationWarning: Passing one of 'on', 'true', 'off', 'false' as a boolean is deprecated; use an actual boolean (True/False) instead.
  warnings.warn(message, mplDeprecation, stacklevel=1)