# Full value function of an American option with QuantLib FD

I am looking at the Equity Option example of QuantLib: http://quantlib.org/reference/_equity_option_8cpp-example.html and more particularly the FDAmericanEngine. However, I am not interested in the point value of the Finite Difference evaluation that is provided by the NPV function, but rather the full value function, for all asset prices (in [x_min, x_max]) and times to maturity (in [0, T]) in some grid of times and asset prices that I can define.

Surely the Finite Difference solver produces the full value function on a mesh of points in order to produce point value, how can I access this value function?

QuantLib does give you the value function, but it's very well hidden. Also, it's only for $t=0$.

Once you have your option built and your finite-difference engine set, you can write for instance:

SampledCurve prices = option.result<SampledCurve>("priceCurve");
for (Size i=0; i<prices.size(); ++i)
std::cout << prices.gridValue(i) << "\t" << prices.value(i) << "\n";


You can also retrieve the full grid or the full set of values at once; see the <ql/math/sampledcurve.hpp> header for the full interface available from the SampledCurve class.

Unfortunately, there's no reference for the values one can possibly retrieve from any given engine via the Instrument::result function; you'll have to look them up in the code for each engine.

• Perhaps this is a stupid question, but I'll ask just to make sure: when you say I can only get T=0, you mean essentially that I can only get the values of the payoff function? – Yu-Ho Haeppoelae Aug 19 '16 at 0:23
• No, the payoff would be the value at maturity. By value at $T=0$ I meant the present value of the option. (But you're right, I should have written $t=0$ instead. I'll edit the answer.) – Luigi Ballabio Aug 19 '16 at 5:55
• So do I understand correctly: if I still insisted on solving the full value function on a mesh of maturities and using QuantLib for it, I'd have to specify the mesh, and run a sequence of solver calls, using the previous solver call's solution (value function) as the payoff for the next? – Yu-Ho Haeppoelae Aug 20 '16 at 19:42
• Yes, that would work. – Luigi Ballabio Aug 20 '16 at 20:03
• Would this work for FdBlackScholesBarrierEngine ? I'm trying to make it work but so far I can´t – Tulio Carnelossi Oct 15 '18 at 4:06

I think you mean the state of the simulation in all grid points?

QuantLib doesn't have anything to store all grid points, because that'd be very memory consuming. Instead, QuantLib updates an internal vector while doing rollback. You can always do the rollback yourself to any maturity, and save the vector to your own data structure.

• I am not sure I would call the finite-difference timestepper a simulation, but yes. I probably would not need the state of the timestepper after each time step, but in a reasonably dense grid anyways, say a couple of thousand maturities. I don't think a couple of million numbers would be that memory consuming. The numbers are so inaccurate anyways that a single-precision would do the job. In any case I take it from you that it's best to look elsewher. – Yu-Ho Haeppoelae Aug 18 '16 at 5:39