-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbpm_brute.m
More file actions
66 lines (62 loc) · 1.53 KB
/
Copy pathbpm_brute.m
File metadata and controls
66 lines (62 loc) · 1.53 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
function [s,m,v] = bpm_brute(task, nsamples, fun, varargin)
% BPM_BRUTE Compute moments of version space by brute force.
%
% [s,m,v] = BPM_BRUTE(task,nsamples) returns
% s volume of version space (in log domain)
% m mean vector
% v covariance matrix
% estimated by Monte Carlo on nsamples samples.
%
% Algorithm: uniformly sample the sphere and weight solutions
% by their likelihood.
% written by Tom Minka
if nargin < 3
fun = @bpm_likelihood;
end
if ~isempty(task.kernel)
error('kernel unsupported')
end
e = task.e;
x = task.data;
[n,d] = size(x);
groupsize = 1000;
total = zeros(d,1);
total2 = zeros(d);
total_ok = 0;
total_sampled = 0;
while(nsamples > 0)
howmany = min([nsamples groupsize]);
%disp(['sampling ' num2str(howmany)])
% sample from N(0,I)
ws = randn(d, howmany);
% sample from Laplace(0,I)
%ws = exprnd(1, 2*d, howmany);
%ws = ws(1:d, :) - ws(d+(1:d), :);
nsamples = nsamples - howmany;
total_sampled = total_sampled + howmany;
if 1
p = feval(fun,task,ws,varargin{:});
else
% this part is the same as bpm_likelihood
if strcmp(task.type, 'step')
if e == 0
p = all(x*ws > 0,1);
else
% each w is weighted by number of training errors
p = col_sum(x*ws > 0);
p = (1-e).^p .* e.^(n - p);
end
else
p = exp(col_sum(normcdfln(x*ws)));
end
end
total = total + ws*p';
if nargout > 1
total2 = total2 + scale_cols(ws,p)*ws';
end
total_ok = total_ok + sum(p);
end
s = log(total_ok/total_sampled);
m = total/total_ok;
v = total2/total_ok;
v = v - m*m';