Skip to content
DeepForgeby svx
All problems
MediumProbabilitypr-258

Jensen Gap for Log

Problem

For a two-point random variable equal to x1 with probability p and x2 otherwise, return the Jensen gap for the concave function ln: E[ln X] - ln E[X], which is at most 0. Return 0.0 when x1 <= 0, x2 <= 0 or p is outside [0, 1].

Hint

For a concave function the expectation of the function lies below the function of the expectation.

Test cases

case 1
in: [1,2,0.5]
expected: -0.05889151782819174
case 2
in: [2,8,0.5]
expected: -0.2231435513142097
case 3
in: [1,1,0.5]
expected: 0
case 4
in: [1,10,0.9]
expected: -0.4115953768729902
case 5
in: [1,2,-0.1]
expected: 0
Starter codePython
import math


def jensen_gap_log(x1, x2, p):
    # Your code here
    pass

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