Second Derivatives and Curvature

~12 mincode completion

Implement second_derivative(coeffs, x, h) using the formula above, with coeffs given low power first as in the earlier problem.

Note the in the denominator: use a larger than you would for a first derivative, because dividing noise by amplifies it.

Examples

x squared curves upward at a constant rate of 2

Input
second_derivative([0, 0, 1], 3, 0.001)
Output
2

A straight line has no curvature at all

Input
second_derivative([4, 2], 10, 0.001)
Output
0

A steeper parabola curves ten times as hard, so tolerates a fifth of the learning rate

Input
second_derivative([0, 0, 5], 1, 0.001)
Output
10

Hints

Hint 1

Convert the input with before doing elementwise work.

Hint 2

A common slip here: divides by h instead of h squared.

Requirements

  • coeffs: coefficients low power first, so [1, 0, 2] is 1 + 2x^2

  • x: the point to evaluate at

  • h: the step size

  • Return float: (f(x+h) - 2f(x) + f(x-h)) / h^2

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~12 min

4 employers weight this skill

4 quant funds. Top match scores 92.

Python
import numpy as np


def second_derivative(coeffs, x, h):
    """
    Numerical second derivative of a polynomial.

    Args:
        coeffs: coefficients low power first, so [1, 0, 2] is 1 + 2x^2
        x:      the point to evaluate at
        h:      the step size

    Returns:
        float: (f(x+h) - 2f(x) + f(x-h)) / h^2
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.