You will test convexity with numbers and measure the gap in Jensen's inequality.
The idea. For a small step , the second difference is about . A function is convex on a grid when every second difference is at least (we allow for rounding). The Jensen gap of a list is the mean of minus of the mean.
Your tasks.
second_diff(f, x, h).convex_on(f, lo, hi, n). Use the points lo + (hi - lo) * i / n for i = 1 to n - 1, with h = (hi - lo) / n, and return True when no second difference is below -1e-12.jensen_gap(f, xs).Run your code once to unlock the worked solution.
Have you tried it?
Do the tasks in the editor first, and run the checker after each one. Its hints help when a check fails. When you are ready, compare your code with this solution.
Log in to join the discussion.
Loading…