Skip to content
DeepForgeby svx
All problems
MediumML Fundamentalsml-037

Gaussian Naive Bayes Likelihood

Problem

Compute the Gaussian (normal) probability density at x for the given mean and variance: (1 / sqrt(2*pi*variance)) * exp(-(x - mean)^2 / (2*variance)).

If variance <= 0, return 1.0 when x == mean and 0.0 otherwise.

Hint

Normalization constant times the exponential of the negative squared z-score over 2.

Test cases

case 1
in: [0,0,1]
expected: 0.3989422804014327
case 2
in: [2,2,4]
expected: 0.19947114020071635
case 3
in: [1,0,0]
expected: 0
case 4
in: [0.5,0,0]
expected: 0
Starter codePython
import math
def gaussian_likelihood(x, mean, variance):
    # Your code here
    pass

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