Skip to content
DeepForgeby svx
All problems
MediumProbabilitypr-291

Bernoulli Naive Bayes Log-Posterior

Problem

For a Bernoulli naive Bayes model, the unnormalized log posterior of a class is log(prior) plus the sum over features of x[i] * log(theta[i]) + (1 - x[i]) * log(1 - theta[i]). Given a binary feature vector, the class prior, and per-feature success probabilities, return that score. Return 0.0 for invalid probabilities or mismatched lengths.

Hint

Take logarithms of the prior and the per-feature Bernoulli probabilities, then add.

Test cases

case 1
in: [[1,0],0.6,[0.9,0.3]]
expected: -0.9728610833625495
case 2
in: [[0,0],0.5,[0.2,0.4]]
expected: -1.4271163556401456
case 3
in: [[1,1],0.7,[0.95,0.8]]
expected: -0.6311117896404927
case 4
in: [[1],1,[0.5]]
expected: -0.6931471805599453
case 5
in: [[1],0,[0.5]]
expected: 0
Starter codePython
import math


def bernoulli_naive_bayes_log_posterior(x, prior, feature_probs):
    # Your code here
    pass

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