function Z = pvi_ising(K,z,W,y,N) % Particle variational inference for the Ising model. Note: particles % are initialized randomly, so even though the algorithm is % deterministic, it won't give you the same answer every time. % % USAGE: Z = pvi_ising(K,z,W,[y],[N]) % % INPUTS: % K - number of particles % z - [1 x D] data vector, where D is the number of nodes. Missing % values are indicated by nans. % W - [D x D] weight matrix % y (optional) - [1 x D] biases for each node (default: all zeros) % N (optional) - number of iterations (default: 50) % % OUTPUTS: % Z - [N x K x D] array of particles for each iteration % % Sam Gershman, Feb 2014 % defaults if nargin < 4 || isempty(y); y = zeros(size(z)); end if nargin < 5 || isempty(N); N = 50; end % initialization D = length(z); h = find(isnan(z)); x = repmat(z,K,1); x(:,h) = sign(randn(K,length(h))); % pre-allocate ix = ~eye(size(W)); v = [ones(K,1); -ones(K,1)]; Z = zeros(N,K,D); Z(1,:,:) = x; % run PVI for i = 2:N for d = h a = x(:,ix(d,:))*W(ix(d,:),d) + y(d); [~,ii] = sort([a; -a],'descend'); x(:,d) = v(ii(1:K)); end Z(i,:,:) = x; end