Skip to content
DeepForgeby svx
All problems
MediumProbabilitypr-297

Mixture Average Log-Likelihood

Problem

Compute the average log density of a data set under a one-dimensional Gaussian mixture. For each point evaluate the log of the sum over components of weights[k] * N(x; means[k], sigmas[k]^2) stably with log-sum-exp, then average over the points. Return 0.0 when the inputs are empty, mismatched, or any standard deviation is nonpositive.

Hint

Evaluate every component log density, take a log-sum-exp, then average over the points.

Test cases

case 1
in: [[0.5,0.5],[0,0],[1,1],[0]]
expected: -0.9189385332046728
case 2
in: [[0.5,0.5],[0,0],[1,1],[-1,0,1]]
expected: -1.252271866538006
case 3
in: [[0.3,0.7],[-1,2],[1,1],[0,1]]
expected: -1.9440342334791008
case 4
in: [[1],[3],[2],[3]]
expected: -1.612085713764618
case 5
in: [[0.5,0.5],[0,0],[1,0],[0,1]]
expected: 0
Starter codePython
import math


def mixture_average_log_likelihood(weights, means, sigmas, data):
    # Your code here
    pass

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