%sigmoid
xs = -4:.02:4; 
plot(xs, 1./(1+exp(-xs)), 'LineWidth', 3); 
set(gca, 'FontSize', 18); 
xlabel z; ylabel P;

% log sigmoids
xs = -6:.02:6; plot(xs, log(1+exp(-xs)), xs, log(1+exp(xs)), 'LineWidth', 3); set(gca, 'FontSize', 18); xlabel Z(X); ylabel('-log P(Y | X)'); axis([-6 6 -.5 6])


% 3 data points: (-1,1) (1.2, 0) (2, 1)
xs = -2:.02:3; 
plot(1.2,0,'o',[-1 2],[1 1],'+',xs,1./(1+exp(-(.94-.39*xs))),'k-','LineWidth',2,'MarkerSize',15); 
axis([min(xs) max(xs) -.1 1.1])
print -depsc ex.eps

% find optimum for 3 data points
wstar = logistic([1 1.2; 1 -1; 1 2], [0; 1; 1], [], 0);

% calculate likelihood terms for 3 data points
[gxs, gys] = meshgrid(-5:.33:10,-6:.33:4); 
z1 = log(1+exp(gxs+1.2*gys));
z2 = log(1+exp(-gxs+gys));
z3 = log(1+exp(-gxs-2*gys));

% total likelihood
contourf(gxs, gys, z1+z2+z3); xlabel('W_0'); ylabel('W_1'); set(gca,'FontSize',18);
line(wstar(1), wstar(2), 'Marker', 'x', 'MarkerSize', 20, 'Color', 'r', 'LineWidth', 2)
xlabel('W_0'); ylabel('W_1'); zlabel -log(P); 
set(gca,'FontSize',18); 
axis equal; axis tight;
print -depsc lik.eps

% likelihood for example 1, as surface
surf(gxs, gys, z1); 
xlabel('W_0'); ylabel('W_1'); zlabel -log(P); 
set(gca,'FontSize',18); 
axis([-5 10 -6 4 -.1 18])
print -depsc x1surf.eps

% example 1, as contour
contourf(gxs, gys, z1); 
xlabel('W_0'); ylabel('W_1'); zlabel -log(P); 
set(gca,'FontSize',18); 
axis equal; axis tight;
print -depsc x1.eps

% example 2 likelihood
contourf(gxs, gys, z2); 
xlabel('W_0'); ylabel('W_1'); zlabel -log(P); 
set(gca,'FontSize',18); 
axis equal; axis tight;
print -depsc x2.eps

% example 3 likelihood
contourf(gxs, gys, z3); 
xlabel('W_0'); ylabel('W_1'); zlabel -log(P); 
set(gca,'FontSize',18); 
axis equal; axis tight;
print -depsc x3.eps

% weight space
[w0, w1] = meshgrid(-5:5:10,4:-10/3:-6);
w0 = w0';
w1 = w1';
w0 = w0(:);
w1 = w1(:);
xs = -4:.02:4;
clf
for i = 1:length(w0)
        subplot(4, 4, i);
        ps = 1./(1+exp(-(w0(i)+w1(i)*xs)));
        plot(1.2,0,'o',[-1 2],[1 1],'+',xs,ps,'k-','LineWidth',2,'MarkerSize',8)
        axis([min(xs) max(xs) -.1 1.1])
end

