All problems
Jensen Gap for the Exponential
Problem
A random variable equals x1 with probability p and x2 otherwise. Return the Jensen gap for the convex function f(x) = e^x:
gap = E[e^X] - e^(E[X]) >= 0.
The gap is 0 when the two outcomes coincide.
Hint
Compute the expectation of f first, then subtract f of the expectation.
Test cases
case 1
in: [0,1,0.5]
expected: 0.21041964352939435
case 2
in: [0,0,0.5]
expected: 0
case 3
in: [1,2,0.25]
expected: 0.4667598553070178
case 4
in: [-1,1,0.5]
expected: 0.5430806348152437
Starter codePython
import math
def jensen_gap_exponential(x1, x2, p):
# Your code here
pass4 test cases · run your code against them in the editor