%% Compare Goodness of Fit
% Compare the goodness of fit for two LDA models by calculating the perplexity of a held-out test set of documents.
% To reproduce the results, set rng to 'default'.

rng('default')

% Load the example data. The file sonnetsPreprocessed.txt contains preprocessed versions of Shakespeare's sonnets. The file contains one sonnet per line, with words separated by a space. Extract the text from sonnetsPreprocessed.txt, split the text into documents at newline characters, and then tokenize the documents.

filename = "sonnetsPreprocessed.txt";
str = extractFileText(filename);
textData = split(str,newline);
documents = tokenizedDocument(textData);

% Set aside 10% of the documents at random for testing.

numDocuments = numel(documents);
cvp = cvpartition(numDocuments,'HoldOut',0.1);
documentsTrain = documents(cvp.training);
documentsTest = documents(cvp.test);

% Create a bag-of-words model from the training documents.

bag = bagOfWords(documentsTrain)

% Fit an LDA model with 20 topics to the bag-of-words model. To suppress verbose output, set 'Verbose' to 0.

numTopics = 20;
mdl1 = fitlda(bag,numTopics,'Verbose',0);

% View information about the model fit.

mdl1.FitInfo

% Compute the perplexity of the held-out test set.

[~,ppl1] = logp(mdl1,documentsTest)

% Fit an LDA model with 40 topics to the bag-of-words model.

numTopics = 40;
mdl2 = fitlda(bag,numTopics,'Verbose',0);

% View information about the model fit.

mdl2.FitInfo

% Compute the perplexity of the held-out test set.

[~,ppl2] = logp(mdl2,documentsTest)

% A lower perplexity suggests that the model may be better fit to the held-out test data.
% Copyright 2012 The MathWorks, Inc.