using Convex using SCS ## Generate data. n = 2; # dimensionality of data C = 10; # inverse regularization parameter in the objective N = 10; # number of positive examples M = 10; # number of negative examples using Distributions # positive data points pos = rand(MvNormal([1.0, 2.0], 1.0), N); # negative data points neg = rand(MvNormal([-1.0, 2.0], 1.0), M); function svm(pos, neg, solver=SCSSolver(verbose=0)) # Create variables for the separating hyperplane w'*x = b. w = Variable(n) b = Variable() # Form the objective. obj = sumsquares(w) + C*sum(max(1+b-w'*pos, 0)) + C*sum(max(1-b+w'*neg, 0)) # Form and solve problem. problem = minimize(obj) solve!(problem, solver) return evaluate(w), evaluate(b) end; w, b = svm(pos, neg); ## Plot our results. using Gadfly # Generate the separating hyperplane line_x = -2:0.1:2; line_y = (-w[1] * line_x + b)/w[2]; # Plot the positive points, negative points, and separating hyperplane. plot(Scale.y_continuous(minvalue=-1, maxvalue=4), layer(x=pos[1,:], y=pos[2,:], Geom.point, Theme(default_color=color("green"))), layer(x=neg[1,:], y=neg[2,:], Geom.point, Theme(default_color=color("red"))), layer(x=line_x, y=line_y, Geom.line, Theme(default_color=color("blue"))))