Skip to content
DeepForgeby svx
All problems
MediumNLPnlp-028

Add-k Smoothing

Problem

Return the add-k smoothed conditional probability (count + k) / (context_count + k * vocab_size).

count is the observed count of the event, context_count is the total count of its context, vocab_size is the number of possible outcomes, and k is the smoothing constant.

Hint

Add k to the numerator and k times the vocabulary size to the denominator.

Test cases

case 1
in: [0,10,5,1]
expected: 0.06666666666666667
case 2
in: [3,10,5,1]
expected: 0.26666666666666666
case 3
in: [0,0,4,0.5]
expected: 0.25
case 4
in: [5,5,1,0.1]
expected: 1
Starter codePython
def add_k_smoothing(count, context_count, vocab_size, k):
    # Your code here
    pass

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