-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_logit2.m
More file actions
187 lines (166 loc) · 3.62 KB
/
test_logit2.m
File metadata and controls
187 lines (166 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
% two-dimensional case
n = 10;
if 1
if 0
if 0
% separable data
data = 2*rand(2, 2*n)-1;
i = find(data(2, :) < -data(1, :));
train1 = data(:, i);
i = find(data(2, :) > -data(1, :));
train2 = data(:, i);
axis([0 1 0 1]);
r = -20:1:0;
else
obj = logit_density([-1; -1]*30);
data = sample(obj, 2*n);
train1 = data(:, 1:n);
train2 = -data(:, (n+1):(2*n));
end
else
% non-separable data
v = eye(2);
train1 = randnorm(n, -ones(2, 1)/2, [], v);
train2 = randnorm(n, ones(2, 1)/2, [], v);
% the optimal logit
obj = logit_density([1; 1]);
r = -5:0.5:5;
end
if 0
% introduce an error
train1 = [train1 train2(:,1)];
train2(:,1) = [];
end
if 0
% project onto unit circle
train1 = train1./repmat(sqrt(sum(train1.^2)),2,1);
train2 = train2./repmat(sqrt(sum(train2.^2)),2,1);
end
figure(1);
plot(train1(1, :), train1(2, :), 'o', train2(1, :), train2(2, :), 'x');
% black is the truth
draw(obj, 'k');
% note that we do not augment the data, so the line must go through zero.
data = [train1 -train2];
else
clf
figure(1);
plot(train1(1, :), train1(2, :), 'o', train2(1, :), train2(2, :), 'x');
end
obj = logit_density(ones(rows(data), 1)/10, 0);
obj.e_type = 'fixed';
%exp(evidence(obj, data))
%return
obj = train(obj, data);
figure(1)
draw(obj,'g')
% number of errors
sum(exp(logProb(obj, data)) <= 0.5)
if 0
% now with raised logistic
obj = logit_density(randn(rows(data), 1), 0.4);
obj.e_type = 'fixed';
obj = train(obj, data);
figure(1)
h = draw(obj,'m')
set(h, 'LineStyle', '--')
% number of errors
sum(exp(logProb(obj, data)) <= 0.5)
end
if 0
% plot the regression solution
data2 = [train1 train2];
y = [ones(1,cols(train1)) -ones(1,cols(train2))];
m = y*pinv(data2);
b = mean(y - m*data2);
figure(1)
draw_line_clip(m(1),b,-m(2), 'Color', 'green')
end
%return
if 1
% plot the posterior over theta
v = obj.theta;
r1 = min(v) - 2*abs(min(v));
r2 = max(v) + 2*abs(max(v));
r = linspace(r1,r2,50);
figure(2);
plot_posterior_theta(obj, data, r);
%exp(evidence_theta(obj, data))/inc/inc
if 0
figure(5);
post = get_posterior_theta(obj);
plot(post, 'mesh', r);
end
end
if 0
% plot the posterior over angle
figure(2)
plot_posterior_angle(obj, data);
hold on
plot_margin_angle(obj,data);
hold off
axis([-3 -1 -1 1])
end
if 0
r1 = -10;
r2 = 10;
inc = (r2-r1)/50;
r = r1:inc:r2;
% plot the prior
x = ndgridmat(r,r)';
p = prior_theta_logProb(obj, x);
p = exp(p);
p = reshape(p, length(r), length(r));
figure(6);
mesh(r, r, p);
view(0, 85);
rotate3d on;
colormap('hsv');
axis tight;
end
if 0
% plot a slice of the prior
r1 = -10;
r2 = 10;
inc = (r2-r1)/50;
r = r1:inc:r2;
x = [r; zeros(size(r))];
p = prior_theta_logProb(obj, x);
figure(6);
plot(r, exp(p));
end
if 0
% this posterior is a mixture of Beta densities
figure(4);
r = 0:0.01:1;
plot_posterior_e(obj, data, r);
if 0
figure(6);
post = get_posterior_e(obj);
plot(post);
end
if 0
% plot the prior
p = prior_e_logProb(obj, r);
figure(6);
plot(r, exp(p));
end
%exp(evidence(obj, data))
end
if 0
% probability of the classifier
p1 = sum(logProb(obj, data))
% fit Gaussians
norm1 = normal_density(2);
norm1 = train(norm1, train1);
norm2 = normal_density(2);
norm2 = train(norm2, train2);
figure(1);
draw(norm1);
draw(norm2);
p2 = sum(logProb(norm1, train1)) + sum(logProb(norm2, train2))
p3 = sum(logProb(norm1, train1) - ...
logSum(logProb(norm1, train1), logProb(norm2, train1))) + ...
sum(logProb(norm2, train2) - ...
logSum(logProb(norm1, train2), logProb(norm2, train2)))
end