2

I'm a total neophyte when it comes to Autograd, so I feel like I may be doing something very obviously wrong. However, I've spent a full day struggling on this.

I am trying to calculate the derivative of a function that includes a normal cdf. I had no success with Autograd, so I broke it up into smaller and smaller parts until I isolated the source of error as the Normal CDF. Here is a minimal working example:

import autograd.numpy as np
from autograd.misc.optimizers import adam, rmsprop, sgd
from autograd import grad
from autograd.scipy.stats import norm

def test_func(x,t):
    return norm.cdf(x)

grad_test = grad(test_func)

def callback(params, t, g):
    print(params, " Gradient: ", g, " Derivative: ", norm.pdf(params))

print(adam(grad_test, 0.0 ,num_iters = 10, step_size= 0.1, callback = callback))

I know it doesn't make sense to optimize a CDF, I just did this to get the output from the callback, which is below:

0.0  Gradient:  0.24197072451914337  Derivative:  0.398942280401
-0.09999999586726883  Gradient:  0.26608524890906193  Derivative:  0.396952547641
-0.20013469787000787  Gradient:  0.2897227684832687  Derivative:  0.391032156046
-0.30048876968878535  Gradient:  0.31236074851454854  Derivative:  0.38133185077
-0.40113882898768705  Gradient:  0.33345215596996947  Derivative:  0.368102181121
-0.5021500752550674  Gradient:  0.35244319911051986  Derivative:  0.351686233767
-0.6035729426432459  Gradient:  0.3687944858147128  Derivative:  0.332508890217
-0.7054397817605528  Gradient:  0.38200507149861374  Derivative:  0.311062576619
-0.8077615706004915  Gradient:  0.391638389745137  Derivative:  0.287889684926
-0.910524672771251  Gradient:  0.3973485394632615  Derivative:  0.263562137446

The gradients calculated by Autograd dont match the theoretical derivative, represented by the normal PDF.

Am I implementing this incorrectly? I feel it's unlikely that such a widely used package would have a hole like this. How can I use Autograd (or other packages in Python) in order to get efficient numerical derivatives of a function that outputs a scalar.

0 Answers0