function Phi = nwk2phi(Nwk,beta0)
% Convert word counts per topic into word probabilities per topic

% Inputs:
%   Nwk     - W-by-K matrix of word counts per topic for W words and K
%             topics, double
%   beta0   - Concentration parameter of the Dirichlet prior, double scalar
%
% Output:
%   Phi     - W-by-K matrix of word probabilities per topic for W words and
%             K topics, double

%   Copyright 2017 The MathWorks, Inc.

T = size(Nwk,1);
nk = sum(Nwk,1);

Phi = (Nwk + beta0/T)./(nk + beta0);
if beta0==0
    bad = nk==0;
    Phi(:,bad) = 0;
end

end
