domokane / FinancePy

A Python Finance Library that focuses on the pricing and risk-management of Financial Derivatives, including fixed-income, equity, FX and credit derivatives.
GNU General Public License v3.0
2.15k stars 321 forks source link

Created independent key_rate_durations function #182

Closed sagayev closed 1 year ago

sagayev commented 1 year ago

I have created an independent key_rate_durations function. Since this function uses bond objects inside the definition, I thought it would not be safe to create it as a method of the Bond class. This way, it can be used for other bond classes.

I have updated the notebook accordingly.

domokane commented 1 year ago

Hi - Thanks for this. I am not sure why this should not be in the bond class. It calculates the KRD of a specific bond with reference to other bonds. I think this is fine. Best D

priyanf commented 2 months ago

Getting an error when running the Key Rate Duration method:

File ~\anaconda3\Lib\site-packages\financepy\products\bonds\bond_zero_curve.py:90 in _bootstrap_zero_rates optimize.newton(_f, x0=df, fprime=None, args=argtuple,

File ~\anaconda3\Lib\site-packages\scipy\optimize_zeros_py.py:381 in newton raise RuntimeError(msg)

RuntimeError: Failed to converge after 100 iterations, value is nan.

Example to replicate: OBJECT TYPE: Bond ISSUE DATE: 15-JAN-2022 MATURITY DATE: 15-JAN-2025 COUPON (%): 22.5 FREQUENCY: FrequencyTypes.SEMI_ANNUAL DAY COUNT TYPE: DayCountTypes.ACT_ACT_ICMA EX_DIV DAYS: 0

settle_dt = Date(30, 7, 2024) bond_current_date_ytm = 0.1005

This then gives the error above: key_rate_dur = bond_1.key_rate_durations(settle_dt, bond_current_date_ytm)

domokane commented 2 months ago

I have fixed this. There was an instability for the solver that made rates go negative and the solver fail. I have set a new initial value which fixes this and means it will be faster. I have pushed the change.

priyanf commented 2 months ago

Thank you for the fix.

On Thu, 29 Aug 2024, 14:24 domokane, @.***> wrote:

I have fixed this. There was an instability for the solver that made rates go negative and the solver fail. I have set a new initial value which fixes this and means it will be faster. I have pushed the change.

— Reply to this email directly, view it on GitHub https://github.com/domokane/FinancePy/pull/182#issuecomment-2317064175, or unsubscribe https://github.com/notifications/unsubscribe-auth/AE5S2IB4LAJFAU5Z3XMUXRLZT3OT7AVCNFSM6AAAAAAXG4RZUSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMJXGA3DIMJXGU . You are receiving this because you commented.Message ID: @.***>