Skip to content
DeepForgeby svx
All problems
EasyProbabilitypr-294

Laplace Smoothed Log Probability

Problem

Add-k smoothing estimates a categorical probability as (count + k) / (total + k * vocab_size). Given the token count, the total token count, the vocabulary size, and the smoothing constant k, return the natural log of that estimate. Return 0.0 when the inputs are invalid.

Hint

Smoothing adds k to every count and k times the vocabulary size to the denominator.

Test cases

case 1
in: [3,10,4,1]
expected: -1.252762968495368
case 2
in: [0,10,5,1]
expected: -2.70805020110221
case 3
in: [10,10,2,1]
expected: -0.08701137698962981
case 4
in: [0,0,3,2]
expected: -1.0986122886681098
case 5
in: [-1,10,4,1]
expected: 0
Starter codePython
import math


def laplace_smoothed_log_probability(count, total, vocab_size, k):
    # Your code here
    pass

5 test cases · run your code against them in the editor