Curve Fitting With QuantLib

Curve Fitting With QuantLib

Introduction

In this article, we will delve further into bonds. Instead of valuation, pricing or risk measurement, we will look at how to use observe bond prices and derive a curve from the observation.

Observed Prices

We will be using actual last done yield traded for MGS (Malaysian Government Securities) on August 12, 2020. The data was obtained from Bond Info Hub, a portal run by Bank Negara Malaysia. The raw data was converted to csv file which we read using python csv package. The data are shown in the following table (some of the fields are hidden).

Last Done Yield for MGS on August 12, 2020

Reading The File and Data Formatting

The following snippet shows how we read the csv file, kept the data in a dictionary and change the type from string to relevant type.

Preparing The Data

As usual, we then created a custom calendar for Malaysia, join the calendar with ql.WeekendsOnly and added custom holidays. Parameters of the bond are then defined. We iterate through our dictionary to create a schedule for each traded bond, calculate the clean price, create a helper and append the helper to the list. There isnt much of a documentation on the ql.FixedRateBondHelper but the parameters for the class is here

The following are the code snippets of what we mentioned.

Fitting The Curve To Data

This is the final part - fitting a curve to the data based on methods available. ql.FittedBondDiscountCurve was used and the documentation is here. The code nippet is below with plotting made available using matplotlib.

As illustration we dumped the result of all the fitting methods in one simple chart for comparison using Plotly. User can turn off a method by tapping on the respective legend.

Summary

We use our data from csv file and prepare the data for processing with straightforward processes and methods. The final step is using ql.FittedBondDiscountCurve, a new class that fits the task. Values were retrieved for plotting using matplotlib package.