0% found this document useful (0 votes)
26 views7 pages

Python Tools-Yield Curves 1

Uploaded by

cinvoke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
26 views7 pages

Python Tools-Yield Curves 1

Uploaded by

cinvoke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 7
12126124, 1257 AM Course: Financial Darvative: A Quantitative Finance View | Udemy The fixed income module contains a class for yield curves. Import the fixed income module with import fixedincome as fi For creating a yield curve object, it is recommended to use the curve_factory function. The most straightforward way to create a curve is to simply specify the spot rates and the corresponding tenors. The signature for the curve_factory function to use in this case is yield_curve = fi.curve_factory(dates, rates) where dates is assumed to be a list of tenors specified by year fractions, and rates are a list of spot rates corresponding to those tenors which must be entered as percentages, and are assumed to be continuously compounded. These lists must be the same length or an error message will be returned and the function will be aborted. For instance, one could build a curve object as tenors = [@.25, 0.5, 1.0, 1.5, 2.0, 3.0, 4.0, 5.0] spot_rates = [0.8, 0.95, 1.16, 1.4, 1.65, 2.1, 2.6, 3.0] yield_curve = fi.curve_factory(dates = tenors, rates = spot_rates) This returns a curve object implementing a yield curve with a 0.8% spot rate at the 3 month tenor, a 0.95% spot rate at the 6 month tenor, a 1.16% spot rate at the 1 year tenor, etc. Please note that the spot rates entered through the curve_factory function are treated as continuously titpshwwycucemy.com/coursefinancial-derivavesiearectue/ 1508941 6#overview w tan 1267 AM Cours: Fanci Devas: Quartiatve Franc View| ery compounded rates, which is also the internal representation of the rates in the curve object. You will get surprising results if you enter rates corresponding to a different compounding convention. It is also possible to specify in invoking the curve_factory an interpolation method for the curve. In addition to the basic piecewise linear method, which is the default, there is now, available, 2 cubic spline methods, Hermite interpolation based on the Catmull-Rom procedure, and cubic splines with natural boundary conditions. These can be specified using the method argument in the curve_factory function. To build the curve using Hermite interpolation, make the above function call as yield_curve = fi.curve_factory(dates = tenors, rates = spot_rates, method="hermite") For a natural cubic spline interpolation, invoke the function as yield_curve = fi.curve_factory(dates = tenors, rates = spot_rates, method="natural") One may also specify to build the curve with piecewise linear interpolation by calling the function as yield_curve = fi.curve_factory(dates = tenors, rates = spot_rates, method="pwlinear") However, as this is the default method, one may simply leave this argument unspecified and the curve will be constructed with piecewise linear interpolation. Iitpshwwvcuceny.com/coursefinancial-derivatvesiearlectue/ 1508941 6#overview an 124, 1257 AM Course Fnancl Dewvathes:AGuanitatve France Vw | Usery NOTE: At present the cubic spline interpolation methods are only available when building the curve from specified, continuously compounded, rates. Yield curves may also be built by specifying either a list of bonds or from money market instruments (a LIBOR curve). These curve building procedures will be discussed in separate articles. Once built, the spot rates and tenors of a yield curve may be displayed by the getter functions: yield_curve.get_rates() yield_curve.get_tenors() One may recover the implied spot interest rate, for an arbitrary maturity, with the get_yield method: yield_curve.get_yield(time) where time is the desired maturity for the spot rate. The rates returned by both the get_rates and get_yield methods are continuously compounded. The yield curve object can also return spot rates with arbitrary compounding convention using the spot_rate method. The signature of this method is yield_curve.spot_rate(time, compounding) where time, as before, is the maturity of the desired spot rate, and compounding is an integer specifying the compounding frequency. In particular, set compounding=k to return a rate corresponding to k times per year compounding. Specify Iitpshwwvcuceny.com/coursefinancial-derivatvesiearlectue/ 1508941 6#overview 37 124, 1257 AM Course Fnancl Dewvathes:AGuanitatve France Vw | Usery compounding=0 for a simply compounded rate. For instance, to return the semiannually compounded spot interest rate corresponding to a 5 year loan term, make the call interest_rate = yield_curve.spot_rate(5.0, compounding=2) To return a simply compounded rate, the call is interest_rate = yield_curve.spot_rate(5.0, compounding=0) Note that if left unspecified the default compounding convention used for spot rates is simple. Discount factors can be returned with yield_curve.discount_factor(time) Also one may apply the len function: len(yield_curve) which returns the number of specified rates. The curve class includes a utility function for carrying out a parallel shift of the curve. By entering a shift in percentage terms, the internally stored continuously compounded rates will be shifted by the given amount. So yield_curve2 = yield_curve.shift(1.@) returns, into yield_curve2, a curve object where all the rates have been increased by 1 percentage point. Note that this function returns a new curve object, leaving the original curve object unchanged. titpshwwvcucemy.com/courseffinancial-derivatvesiearectue/ 1508941 6#overviow an 124, 1257 AM Course Fnancl Dewvathes:AGuanitatve France Vw | Usery For instance, in the example above, the spot rates in yield_curve2, will be 1.8, 1.95, 2.16, 2.4, 2.65, 3.1, 3.6. and 4.0. Yield curve objects can be used to price bonds. If we have a bond object we may call its price method together with a yield curve object, to get the price implied by the yield curve: price = bond.price(curve = yield_curve) The curve class also has methods for visualization. These require that Numpy and Matplotlib be available. invoking yield_curve.plot_yields() will plot the continuously compounded yields and yield_curve.plot_discount_factors() will plot the discount factors. EXAMPLE: As an example of the basic usage of the yield curve functionality, try instantiating a flat yield curve. To do this, simply build a yield curve using one tenor and one interest rate. To create a flat yield curve with a 5% spot rate, call the curve factor function like this: yc = fi.curve_factory(dates = [10.0], rates = [5.0]) Note that the one tenor entered, 10 years in this case, can be any positive value at all, but it will be most convenient to use a value far enough out that any applications you have for the curve will not require a yield for any date beyond that. Otherwise, the curve object will need to extrapolate beyond Iitpshwwvcuceny.com/coursefinancial-derivatvesiearlectue/ 1508941 6#overview 57 tan 1267 AM Cours: Fanci Devas: Quartiatve Franc View| ery that date, and will produce a warning message. This has no impact on any computations, but might be a bit annoying. With our flat yield curve built, we can now do some computations. As always, it's best to check that our object is what we think it is, so just check that the basic data is correct by viewing the tenors and rates: yc.get_tenors() yc.get_rates() These should be what you expect. Now, calculate some yields from this curve using different dates: yc.get_yield(1.0) yc.get_yield(5) yc.get_yield(10.0) yc.get_yield(15) Note that the argument can be either an integer or a float. In this case, all these calls should just confirm that this is a flat yield curve, as was our intention. More instructive is to now calculate some discount factors: yc.discount_factor(1/12) yc.discount_factor(@.5) yc.discount_factor(2) yc.discount_factor(7) yc.discount_factor(12) This gives some idea of the discounting effect of a 5% interest rate. Iitpshwwvcuceny.com/coursefinancial-derivatvesiearlectue/ 1508941 6#overview er tan 1267 AM Cours: Fanci Devas: Quartiatve Franc View| ery One may also see this by plotting the discount factors using the plotting function yc.plot_discount_factors() One should invoke both this and the other plotting method yc.plot_yields() to get a feel for how yield curves really behave. A final application to try with the yield curve object is pricing a bond. For instance, try creating a bond paying a 5% coupon, and pricing it with our yield curve: bonds = fi.create_coupon_bond(maturity = 5, face = 10000, rate = 5, frequency = 2) bonds. price(yc) Is this result what you expect? If it surprises you, remember the compounding convention implicit in our construction of a yield curve titpshwwvcucemy.com/courseffinancial-derivatvesiearectue/ 1508941 6#overviow 7

You might also like