Created
September 17, 2017 22:30
-
-
Save lunaluxie/c88e695c6d72e77bdb781d345e19c8a2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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