The pricing process of Finite-Difference G2 Bermudan Swaption engine is shown in the figure below:
As we can see from the figure, the engine has three main modules: Initialization, calculate and NPV. The former one is responsible for engine initialization: reading specific time points of the swaption from BRAM and setting initial values. The second part is the main process that contains model initialization, meshers initialization using Ornstein-Uhlenbeck process, and builds up the derivative on x and y dimension.
The main logic of the engine is coded in the rollback method. We go backwards from a later time(maturity) to an earlier time in a given number of steps. This gives us a length \(dt\) for each step, which is set as the time step for the evolver. Then we start going back step by step, each time going from the current time t to the next time, \(t-dt\). We have just made the step from \(t_{4}\) to \(t_{3}\) as described in the following paragraph, and it will set the control variable hit to true. It will also decrease the step size of the evolver \(t_{4}-s\). It will lead to the stopping time point, immediately after performing the step. Another similar change of step would happen if there were more than one stopping times in a single step; the code would then step from one to the other. Finally, the code will enter the hitted branch. It performs the remaining step \(s-t_{3}\) and then resets the step of the evolver to the default value.So the process is ready for the next regular step to \(t_{2}\). Notice the last time is \(stopping\_time[1]\) instead of \(stopping\_time[0]\).