Skip to content
DeepForgeby svx
All problems
MediumProbabilitypr-293

Gaussian Naive Bayes Log-Likelihood

Problem

For each class, compute the log-likelihood of a point under independent Gaussians: the sum over features of -0.5 * log(2 * pi * variance) - (x - mean)^2 / (2 * variance). means[c] and variances[c] hold the per-feature parameters of class c. Return the list of per-class log-likelihoods, or an empty list for mismatched shapes or a nonpositive variance.

Hint

The Gaussian log density is a normalization term minus the squared z-score over two.

Test cases

case 1
in: [[0,1],[[0,1],[1,0]],[[1,1],[1,1]]]
expected: [-1.8378770664093453,-2.8378770664093453]
case 2
in: [[1,1],[[0,0],[1,1]],[[1,1],[1,1]]]
expected: [-2.8378770664093453,-1.8378770664093453]
case 3
in: [[2],[[0]],[[4]]]
expected: [-2.112085713764618]
case 4
in: [[0,0],[[0]],[[1,1]]]
expected: []
case 5
in: [[1],[[0]],[[0]]]
expected: []
Starter codePython
import math


def gaussian_naive_bayes_log_likelihood(x, means, variances):
    # Your code here
    pass

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