%% Calculate Document Log-Probabilities from Word Count Matrix
% Load the example data. sonnetsCounts.mat contains a matrix of word counts and a corresponding vocabulary of preprocessed versions of Shakespeare's sonnets.

load sonnetsCounts.mat
size(counts)

% Fit an LDA model with 20 topics.

numTopics = 20;
mdl = fitlda(counts,numTopics)

% Compute the document log-probabilities of the training documents. Specify to draw 500 samples for each document.

numSamples = 500;
logProbabilities = logp(mdl,counts, ...
    'NumSamples',numSamples);

% Show the document log-probabilities in a histogram.

figure
histogram(logProbabilities)
xlabel("Log Probability")
ylabel("Frequency")
title("Document Log-Probabilities")

% Identify the indices of the three documents with the lowest log-probability.

[~,idx] = sort(logProbabilities);
idx(1:3)

% Copyright 2012 The MathWorks, Inc.