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 test cases for bond.key_rate_duration method #179

Closed sagayev closed 1 year ago

sagayev commented 1 year ago

Dear @domokane,

I wrote to test functions. I will create a notebook tomorrow to show how to use the method.

Thank you, Samir

domokane commented 1 year ago

Hi - I am looking at this carefully. There is a big difference between the bloomberg numbers and your numbers. Do you know why ? I was expecting agreement to 4 significant figures at least. Or am I misunderstanding ?

sagayev commented 1 year ago

Hi, yes. I also realized that. They have also negative numbers. I guess they take the actual yield curve then shift the key rates. And the actual yield curve is currently is not "normal".

domokane commented 1 year ago

OK. So why did you do a pull request ? We need to agree with Bloomberg. It is the market standard.

I am looking at it. Did you follow the approach on

https://www.blog.deriscope.com/index.php/en/excel-quantlib-key-rate-duration

Can you try to follow this ? I have made some changes to the code and will push in a moment.

sagayev commented 1 year ago

But I have created another notebook that calculates key rates durations using Quantlib library. If you are interested I can send it for your review on Monday.

sagayev commented 1 year ago

OK. So why did you do a pull request ? We need to agree with Bloomberg. It is the market standard.

I am looking at it. Did you follow the approach on

https://www.blog.deriscope.com/index.php/en/excel-quantlib-key-rate-duration

Can you try to follow this ? I have made some changes to the code and will push in a moment.

Yes. I used their approach. They also create flat yield curve as I did. I think due to market data it is not possible to match Bloomberg key rates durations.

domokane commented 1 year ago

What market data do you mean ? You had the bond price and matched the yield ?

Did you get the price and the yield to match ? I see a yield ytm = 3.803140/100

Did that agree with financepy - what price was the bond ? I don't see it in your bloomberg ?

Best D

domokane commented 1 year ago

Two comments:

sagayev commented 1 year ago

What market data do you mean ? You had the bond price and matched the yield ?

Did you get the price and the yield to match ? I see a yield ytm = 3.803140/100

Did that agree with financepy - what price was the bond ? I don't see it in your Bloomberg ?

Best D

With market data I mean the current US Treasury actives curve (I25 on Bloomberg). Currently, for example 6M yield is around 5%, while 3Y rate is around 3.7%. When I believe Bloomberg uses this curve. While what I did (and also Periscope example did) to take all the rate equal to current YTM. Therefore, I said, we cannot replicate Bloomberg numbers. In key_rate_durations method I take YTM (from input) and create a flat yield curve, all the rates equaling to YTM. Then I start bumping each rate up an down, but keeping the rates for other tenors constant. I believe this is acceptable.

Alternatively, we can have a rates parameter (defaulting to None). If a user wants to input rates for 3M, 6M, 1Y, 2Y etc, can enter the input then the user can get key rate durations somewhat closer to Bloomberg's values.

If you need I would love to have a Zoom call to share my screen and continue the discussion on Monday through Thursday.

domokane commented 1 year ago

Are you sure Bloomberg uses the entire curve like that ? I am not sure.

Suppose you have a 1Y, 5Y and 10Y KRD and the bond's yield curve is at 5%. If you treat these term yields as zero rates, then bumping up the 5Y rate to 5.01% must change the implied 10Y rate. This should not happen as it means that when you are changing the 5Y rate you are ALSO changing the 10Y rate. So you need to adjust the 10Y curve to stop this from happening by reducing the 5Y-10Y rates.

This would explain the negative KRDs from Bloomberg - if you bump the 6M rate by 1bp then you need to reduce the 6M-TY rates (T=bond maturity) to ensure their yields do not change. You need a curve point for each coupon date and you interpolate the par rates linearly. As there are more flows in the lowered rate part of the curve, the bond price goes up and the KRD is negative.

Can I suggest you consider the case of 1Y, 5Y, 10Y KRDs and see what you would need to do. It's a bit like bootstrapping a swap curve. I think.

sagayev commented 1 year ago

Ok. I will look into it during the week.

domokane commented 1 year ago

Ok. Thanks.DOn 16 Apr 2023, at 17:36, Samir Aghayev @.***> wrote: Ok. I will look into it during the week.

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you modified the open/close state.Message ID: @.***>