perplexity() computes the perplexity of a fitted tree: the exponential
of the negative mean log-likelihood per observed position, \(\exp(-\ell /
n)\). It reads as an effective number of equally likely next states: a
perplexity of \(k\) for \(k\) states means the tree predicts no better
than chance, and a perplexity of 1 means every next state is predicted with
certainty. Lower values indicate better prediction.
In-sample perplexity (without newdata) favors deep, lightly smoothed
trees that reproduce the fitting data; held-out perplexity, computed on
sequences not used for fitting, measures how well the tree generalizes. It
returns a single number. tune_tree() uses cross-validated
perplexity to choose a tree's settings.
Arguments
- tree
A
transitiontreesobject.- newdata
Sequences to score, in any form accepted by
context_tree().NULL(default) gives in-sample perplexity.
Conditions
transitiontrees_bad_input when tree is not a fitted tree or
newdata has no usable sequence.
Examples
tree <- context_tree(trajectories, max_depth = 2)
perplexity(tree)
#> [1] 2.310147
perplexity(tree, newdata = engagement)
#> [1] 2.137931