google / tf-quant-finance

High-performance TensorFlow library for quantitative finance.
Apache License 2.0
4.57k stars 581 forks source link

IRS Delta #76

Open rajflume opened 2 years ago

rajflume commented 2 years ago

In https://github.com/google/tf-quant-finance/tree/master/tf_quant_finance/experimental/pricing_platform/framework/rate_instruments/interest_rate_swap/interest_rate_swap_impl.py

under def ir_delta it says raise NotImplementedError("Coming soon.")

In ir_delta_parallel_leg, I see that: if shock_size is None: bump = tf.constant(0, dtype=self._dtype, name="bump") return tff_math.fwd_gradient(price_fn, bump)

So when no shocks are supplied - its just fwd_gradient. Isn't that the AAD equiv of the bump and reval change in PV?

But when a shock size is supplied, you bump the base curve (as a parallel bump) and then: price_no_bump = leg.price(processed_market_data) price_with_bump = price_fn(shock_size) delta = (price_with_bump - price_no_bump) / shock_size return delta

So in effect, ir_delta_parallel has the potential to supply both delta's? (AAD equiv and parallel shocked

cyrilchim commented 2 years ago

Hi Raj,

You can use tff.math.gradient to compute deltas. Note these are then aggregated for each instrument. If you want delta for each instrument, you should use tff.math.jacobian. I have extended the example colab to demonstrate the point