Skip to content
DeepForgeby svx
All problems
HardProbabilitypr-298

EM Q-Function Value

Problem

The EM Q-function is the expected complete-data log-likelihood under the current responsibilities: the sum over points n and components k of responsibilities[n][k] * (log(weights[k]) + log N(data[n]; means[k], sigmas[k]^2)). Return that value, or 0.0 when the inputs are empty, mismatched, or a used component has a nonpositive weight or standard deviation.

Hint

Weight each component log density by its responsibility, then sum over points and components.

Test cases

case 1
in: [[1],[0],[1],[0],[[1]]]
expected: -0.9189385332046727
case 2
in: [[0.5,0.5],[0,1],[1,1],[0,1],[[0.8,0.2],[0.3,0.7]]]
expected: -3.474171427529236
case 3
in: [[0.3,0.7],[-1,1],[1,2],[0,2],[[0.5,0.5],[0.25,0.75]]]
expected: -5.584384325277145
case 4
in: [[0.5,0.5],[0,0],[1,1],[],[]]
expected: 0
case 5
in: [[0.5,0.5],[0,0],[1,-1],[0],[[1,0]]]
expected: 0
Starter codePython
import math


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

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