<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def perplexity(test_data, predictions, topics, vocabulary):
 """ Compute the perplexity of a set of test documents given a set of predicted topics. Let theta be the matrix of document-topic probabilities, where theta_ik = p(topic k | document i). Let Phi be the matrix of term-topic probabilities, where phi_jk = p(word j | topic k). Then for each word in each document, we compute for a given word w and document d .. math:: p(word | \theta[doc_id,:], \phi[word_id,:]) = \sum_k \theta[doc_id, k] * \phi[word_id, k] We compute loglikelihood to be: .. math:: l(D) = \sum_{i \in D} \sum_{j in D_i} count_{i,j} * log Pr(word_{i,j} | \theta, \phi) and perplexity to be .. math:: \exp \{ - l(D) / \sum_i \sum_j count_{i,j} \} Parameters test_data : SArray of type dict or SFrame with a single column of type dict Documents in bag-of-words format. predictions : SArray An SArray of vector type, where each vector contains estimates of the probability that this document belongs to each of the topics. This must have the same size as test_data; otherwise an exception occurs. This can be the output of :py:func:`~turicreate.topic_model.TopicModel.predict`, for example. topics : SFrame An SFrame containing two columns: 'vocabulary' and 'topic_probabilities'. The value returned by m['topics'] is a valid input for this argument, where m is a trained :py:class:`~turicreate.topic_model.TopicModel`. vocabulary : SArray An SArray of words to use. All words in test_data that are not in this vocabulary will be ignored. Notes ----- For more details, see equations 13-16 of [PattersonTeh2013]. References .. [PERP] `Wikipedia - perplexity <http://en.wikipedia.org/wiki/Perplexity>`_ .. [PattersonTeh2013] Patterson, Teh. `"Stochastic Gradient Riemannian Langevin Dynamics on the Probability Simplex" <http://www.stats.ox.ac.uk/~teh/research/compstats/PatTeh2013a.pdf>`_ NIPS, 2013. Examples -------- topics['topic_probabilities'], topics['vocabulary']) 1720.7 # lower values are better """

    test_data = _check_input(test_data)
    assert isinstance(predictions, _SArray), \
        "Predictions must be an SArray of vector type."
    assert predictions.dtype == _array.array, \
        "Predictions must be probabilities. Try using m.predict() with " + \
        "output_type='probability'."

    opts = {'test_data': test_data,
            'predictions': predictions,
            'topics': topics,
            'vocabulary': vocabulary}
    response = _turicreate.extensions._text.topicmodel_get_perplexity(opts)
    return response['perplexity']