Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
def numerical_derivative(func, func_input, respect_to_index=0, h=0.0001):
"""Compute the numerical derivative of a function
Args:
func (function): A function
func_input (list): A list of inputs given to the function
respect_to_index (int): The index of the value the derivative is calculated with respect to
h (float): the amount to tweak the function with to compute the gradient
Returns:
float: the derivative of the function at the input
"""
# compute function value
f0 = func(*func_input)
# compute inputs with slightly tweaked respective input weight
new_respect_to_weight = np.add(func_input[respect_to_index], h)
func_input_h = func_input
func_input_h[respect_to_index] = new_respect_to_weight
f0_h = func(*func_input_h)
# compute the difference, the slope of the function
delta_f = f0_h - f0
delta_f_normalized = delta_f/h
return delta_f_normalized
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.