Second Derivatives and Curvature
Implement second_derivative(coeffs, x, h) using the formula above, with coeffs given low power first as in the earlier problem.
Note the h2 in the denominator: use a larger h than you would for a first derivative, because dividing noise by h2 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^2x: the point to evaluate ath: the step sizeReturn 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
4 employers weight this skill
4 quant funds. Top match scores 92.
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