Premium problem117. Bernoulli Naive Bayes Log Scores

Medium Locked

Same setup as the Gaussian version but with binary features: likelihoods[c][j] is P(feature j=1∣c)P(\text{feature } j = 1 \mid c). Return the unnormalised log score for each class, not a probability.

scorec=log⁡πc+∑j[xjlog⁡θcj+(1−xj)log⁡(1−θcj)]\text{score}_c = \log \pi_c + \sum_j \bigl[x_j \log \theta_{cj} + (1-x_j)\log(1-\theta_{cj})\bigr]

Classifiers usually stop here: to pick a class you only need the argmax, and normalising costs an exponential you do not need.

Input

priors = [0.5, 0.5]
likelihoods = [[0.9, 0.1], [0.2, 0.8]]
features = [1, 0]

Output

[-0.9038682118755978, -3.912023005428146]

Premium problem

This one's part of Premium. Unlock the full Probability track plus every other premium problem on the site.

Implement solve(...)