Skip to content

Instantly share code, notes, and snippets.

@fmm
Last active January 22, 2016 20:41
Show Gist options
  • Save fmm/e737b7afdc61fb2e1c25 to your computer and use it in GitHub Desktop.
Save fmm/e737b7afdc61fb2e1c25 to your computer and use it in GitHub Desktop.

Revisions

  1. Filipe Martins revised this gist Jan 22, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions hw1q1.m
    Original file line number Diff line number Diff line change
    @@ -32,9 +32,9 @@
    load('q1x.dat'); % m x 2
    load('q1y.dat'); % m x 1
    [m n] = size(q1x);
    X = [ones(m,1) q1x]; % m x (n+1)
    X = [ones(m,1) q1x]; % m x 3
    Y = q1y; % m x 1
    itheta = zeros(n+1,1); % (n+1) x 1
    itheta = zeros(n+1,1); % 3 x 1
    [theta, ll] = newton(itheta,X,Y);
    figure; hold on;
    x = min(X(:,2)):.01:max(X(:,2));
  2. Filipe Martins revised this gist Jan 22, 2016. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion hw1q1.m
    Original file line number Diff line number Diff line change
    @@ -36,7 +36,6 @@
    Y = q1y; % m x 1
    itheta = zeros(n+1,1); % (n+1) x 1
    [theta, ll] = newton(itheta,X,Y);
    % y = x * (-theta(2)/theta(3) + (-theta(1)/theta(3))
    figure; hold on;
    x = min(X(:,2)):.01:max(X(:,2));
    y = -theta(2)/theta(3)*x-theta(1)/theta(3);
  3. Filipe Martins created this gist Jan 22, 2016.
    53 changes: 53 additions & 0 deletions hw1q1.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    %%%%% sigmoid.m %%%%%
    function f = sigmoid(z)
    f = 1 ./ (1 + exp(-z));
    end
    %%%%%%%%%%%%%%%%%%%%%%%%%

    %%%%% likelihood.m %%%%%
    function L = likelihood(theta,X,Y)
    h = sigmoid(X * theta); % m x 1
    L = (Y' * log(h)) + ((1 - Y)' * log(1 - h));
    end
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    %%%%% newton.m %%%%%
    function [theta,ll] = newton(itheta,X,Y)
    theta = itheta;
    ll(1) = likelihood(itheta,X,Y);
    max_iter = 50;
    for i=1:max_iter
    h = sigmoid(X * theta); % m x 1
    grad = (Y - h)' * X; % 1 x n
    hh = h .* (1 - h); % m x 1
    H = (-1 .* (hh .* X))' * X; % n x n
    theta = theta - inv(H) * grad'; % n x 1
    ll(i+1) = likelihood(theta,X,Y);
    end
    res = theta;
    end
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    %%%%% solve.m %%%%%
    load('q1x.dat'); % m x 2
    load('q1y.dat'); % m x 1
    [m n] = size(q1x);
    X = [ones(m,1) q1x]; % m x (n+1)
    Y = q1y; % m x 1
    itheta = zeros(n+1,1); % (n+1) x 1
    [theta, ll] = newton(itheta,X,Y);
    % y = x * (-theta(2)/theta(3) + (-theta(1)/theta(3))
    figure; hold on;
    x = min(X(:,2)):.01:max(X(:,2));
    y = -theta(2)/theta(3)*x-theta(1)/theta(3);
    plot(x,y,'m');
    x1y0 = X(find(Y == 0),2);
    x2y0 = X(find(Y == 0),3);
    x1y1 = X(find(Y == 1),2);
    x2y1 = X(find(Y == 1),3);
    plot(x1y0,x2y0,'rx');
    plot(x1y1,x2y1,'go');
    xlabel('x1');
    ylabel('x2');
    legend('decision boundary');
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%