2
$\begingroup$

I have a complex monte-carlo cashflow model that traditionally uses the finite difference (FD) method to calculate its derivative at any given point. To improve model performance, I coded forward-mode automatic differentiation (AD) to directly calculate the cashflow derivative. The model is significantly faster using AD, and when the shocks (h) are small under the FD method--[f(x+h)-f(x-h)]/2h--the two approaches reach fairly equivalent estimates of the derivative. When the shocks h are larger, the two approaches diverge.

The problem arises in predictive accuracy. The predictive accuracy of my first order derivative actually improves when I widen my shock; AD provides a poorer prediction than the FD method. I've proven this by plotting the predicted versus actual values. The first-order Taylor Series approximation of my function f(x) is more accurate when I use the FD method at nearly all values of x.

Given all the material I've read about AD, this was a very surprising outcome. I would think that a more accurate estimate of the derivative would provide more accurate predictions using a Taylor Series expansion. From what I've seen, that is simply not the case.

Why is this happening, and is there anything I can do to remedy it? Is it because my finite difference method captures second-order effects when the shock is large, or is it a consequence of using a discrete cashflow model that by its very nature has jump points?

I've devoted significant time to expanding this model, and the time saved in calculation is almost irresistible. Without an equivalent or better prediction, my project will likely be dead, so any help is really appreciated.

EDIT: I'm adding a few graphs that may help visualize my function. The first quadrant shows the actual values of f(x) and its first derivative. The bottom left shows how the finite difference method is more accurate. The top right shows the difference in derivative for each scenario generated by the monte-carlo process. The bottom right plots the function itself. enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ Not the main point, but I was a little surprised by the fact that AD is significantly faster than FD. At a first glance, I would have said that both methods cost about 2x the time you need for a function evaluation. $\endgroup$ Commented Sep 6, 2023 at 17:14

2 Answers 2

4
$\begingroup$

Finite differences, when applied to a function from $\mathbb{R}$ to $\mathbb{R}$ with a discontinuity, will do a better job of capturing the nature of the derivative, which is no longer a function but a distribution. Automatic differentiation will not notice the jump and simply ignore it.

Consider the function

f(x) = 0 if x<0.5 else 1

and plot of the function $$g(x):=\frac{f(x+h)-f(x-h)}{2h}.$$ You will see a large spike around $x=0.5$ that grows higher and narrower with $h$. Automatic differentiation will simply not see the jump, and thus provide a poorer approximation of the true derivative.

EDIT: Of course, you are outside of reasonable assumptions, but this explains why finite differences work "better" in this case.

$\endgroup$
4
  • $\begingroup$ I was afraid of something like this. I thought about the case of a stair-step function, similar to what you posted. While there are jumps in the function on a scenario level, it does seem smooth when I plot the average (like I did in my edit above). Are there any methods to capture jumps if it turns out that is the problem? $\endgroup$ Commented Sep 7, 2023 at 14:43
  • $\begingroup$ If you can reformulate the part of the problem where the discontinuous functions have their impact using distributions then yes. As long as you know the location and size of the jumps. The derivative of f in the sense of distributions is zero plus a delta distribution centered on 0.5. $\endgroup$
    – MSMommer
    Commented Sep 7, 2023 at 15:06
  • $\begingroup$ These responses are awesome. Thank you. I guess my question is at this point is: am I to conclude that automatic differentiation not work as well as finite difference on any model containing an if-then statement that depends on the independent variable? That seems like a huge constraint that's not largely discussed: scicomp.stackexchange.com/questions/35620/… $\endgroup$ Commented Sep 8, 2023 at 15:24
  • $\begingroup$ An if-then item is not automatically a problem. Consider f(x) = 0 if x<0, else x. The problem is when the derivative does not exist in the classical sense. In that case FD does not really work better, It's just that you can be lucky. It's generally a bad idea to violate the assumptions behind the methods used. $\endgroup$
    – MSMommer
    Commented Sep 10, 2023 at 7:42
2
$\begingroup$

What you call Automatic Differentiation is apparently a method for evaluating the derivative (gradient) of Ef(x), where E is mathematical expectation, by intechanging the order of differentiation and expectation, i.e., differentiating under the integral. Such interchange is known as Infinitesimal Perturbation Analysis (don't blame me for the crazy name). So you are performing the operation E(gradient of f(x)), using AD to calculate gradient of f(x) for each Monte Carlo replication. Such interchange is generally only valid when f(x) is continuous per sample path (Monte Carlo replication). Continuity of Ef(x) is not sufficient to ensure validity of the interchange. It is not the AD per se which is invalid in this case, but the interchange of differentiation and expectation, regardless of the method used to compute the derivative of f(x).

Given f(x) is not continuous per sample path, other options (besides finite differences) might include the Likelihood Ratio Method (which differentiates the probability measure) or Measure valued Differentiation (a.k.a. weak derivatives). Another possibility might be to apply some type of smoothing to f(x), and automatically differentiate the smoothed version. There are also various hybrids of these methods.

Finite differences might work well if common random numbers inducing high positive correlation in Ef(x) values, are used for the points being differenced. The big potential downside in that case would be if the dimension of the optimization variable were very large, which it often isn't in financial simulations. Performance of finite differences can be poor if the Ef(x) values being differenced are uncorrelated (for example due to not using common random numbers, or not getting good synchronization of the random numbers).

There is a much more in depth elaboration on all of this in my Operations Research Stack Exchange answer at Optimization for Stochastic Simulations

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.