ir.term_structure package

ir.term_structure.bootstrap module

bootstrap module

class ir.term_structure.bootstrap.Bootstrap(input_helper)

Bases: object

Bootstrap class

Examples

  • Input set-up

‘bootstrap_input.json’ file is located at ir/term_structure/test

>>> helper = bootstrap_input_helper.extract_json('bootstrap_input.json', is_print=False)
2016-05-26 15:16:31 WARNING Valuation date is changed to [March 8th, 2010]
>>> val_date = helper[0]
>>> sim_year = 5
>>> schedule = ql.Schedule(val_date, val_date + ql.Period(sim_year, ql.Years), ql.Period(1, ql.Months), ql.UnitedStates(), ql.Following, ql.Following, ql.DateGeneration.Forward, False)
  • Bootstrap by linear and cubic interpolation on zero rates
>>> curve_linear = PiecewiseLinear(helper)
>>> zero_linear = curve_linear.get_zero(list(schedule))
>>> curve_cubic = PiecewiseCubic(helper)
>>> zero_cubic = curve_cubic.get_zero(list(schedule))
>>> curves.plot('zero', val_date, {**zero_linear, **zero_cubic})
_images/bootstrap_example.png
piecewise_cubic(daycount=<DayCount.ActAct: <QuantLib.QuantLib.ActualActual; proxy of <Swig Object of type 'QuantLib::ActualActual *' at 0x000000000696E1B0> >>)

Interpolate based on piecewise cubic for zero rate Valuation date is set as same as settle date defined during input_helper

Parameters:daycount (ir.date_time.enum.DayCount) – Daycount; default: DayCount.ActAct
Returns:bootstrapped curve object.
Return type:ir.term_structure.curves.Curve
piecewise_linear(daycount=<DayCount.ActAct: <QuantLib.QuantLib.ActualActual; proxy of <Swig Object of type 'QuantLib::ActualActual *' at 0x000000000696E1B0> >>)

Interpolate based on piecewise linear for zero rate Valuation date is set as same as settle date defined during input_helper

Parameters:daycount (ir.date_time.enum.DayCount) – Daycount; default: DayCount.ActAct
Returns:bootstrapped curve object.
Return type:ir.term_structure.curves.Curve
piecewise_log_cubic_discount(daycount=<DayCount.ActAct: <QuantLib.QuantLib.ActualActual; proxy of <Swig Object of type 'QuantLib::ActualActual *' at 0x000000000696E1B0> >>)

Interpolate based on piecewise log cubic for discount rate Valuation date is set as same as settle date defined during input_helper This is what QuantLib recommended but it failed on convergence. QuantLib-Python does not have access to change the convergence.

Parameters:daycount (ir.date_time.enum.DayCount) – Daycount; default: DayCount.ActAct
Returns:bootstrapped curve object.
Return type:ir.term_structure.curves.Curve

ir.term_structure.bootstrap_input_helper module

ir.term_structure.bootstrap_input_helper.deposit(settle, rate, tenor, daycount=<DayCount.Act360: <QuantLib.QuantLib.Actual360; proxy of <Swig Object of type 'QuantLib::Actual360 *' at 0x000000000696E0F0> >>, val_date=None)

Input rate from deposit

Parameters:
  • settle – Settlement date. I assume valuation date is same as settlement date
  • rate – Annualized rate in list (in decimal format)
  • tenor – Maturity in ql.Period
  • daycount – Use date_time.ir.DayCount enum (Default: DayCount.Thirty_360)
  • val_date (ql.Date) – Valuation date (Default: None to set same as settle)
Returns:

tuple contains:

  • settle (ql.Date): Settle date
  • helper (list): q.DepositRateHelper

Return type:

tuple

ir.term_structure.bootstrap_input_helper.extract_json(json_file, is_print=False)

The json file is for the most widely used inputs for US market. Input types are deposit, eurodollar future, and interest rate swap on Libor 3M

If different inputs need to be implemented then directly use helpers above.

Parameters:
  • json_file – json file name
  • is_print – print in console; default to False

tuple: tuple contains:

  • settle (ql.Date): Settle date
  • helper (list): helper for bootstrapping
ir.term_structure.bootstrap_input_helper.future(settle, rate, mat_date, daycount=<DayCount.Act360: <QuantLib.QuantLib.Actual360; proxy of <Swig Object of type 'QuantLib::Actual360 *' at 0x000000000696E0F0> >>, convexity_adj=<QuantLib.QuantLib.QuoteHandle; proxy of <Swig Object of type 'std::vector< Handle< Quote > >::value_type *' at 0x000000000A97C5A0> >, months=3, val_date=None)

The default setting is for Eurodollar future.

Parameters:
  • settle – Settle date in ql.Date
  • rate – Rate
  • mat_date – Maturity date in ql.Date, QuantLib checks for the eligible eurodollar future date
  • daycount – Act / 360
  • convexity_adj – convexity adjustment; Default to 0.0
  • months – Libor month; Default to 3 month
  • val_date (ql.Date) – Valuation date (Default: None to set same as settle)
Returns:

tuple contains:

  • settle (ql.Date): Settle date
  • helper (list): ql.FutureRateHelper

Return type:

tuple

ir.term_structure.bootstrap_input_helper.swap(settle, rate, tenor, fixed_freq=<Freq.Semiannual: 2>, fixed_daycount=<DayCount.Act360: <QuantLib.QuantLib.Actual360; proxy of <Swig Object of type 'QuantLib::Actual360 *' at 0x000000000696E0F0> >>, floating_index=<QuantLib.QuantLib.USDLibor; proxy of <Swig Object of type 'USDLiborPtr *' at 0x000000000A97C570> >)

This is assuming USD Libor swap

Parameters:
  • settle – Settle date
  • rate – (par) swap rate
  • tenor – swap tenor in ql.Period
  • fixed_freq – Fixed leg payment frequency; Default to semiannual
  • fixed_daycount – Act / 360 based on Matlab and following specification
  • floating_index – Floating leg rate index: Assuming Libor swap Default: ql.USDLibor(ql.Period(‘3M’))
Returns:

tuple contains:

  • settle (ql.Date): Settle date
  • helper (list): ql.SwapRateHelper

Return type:

tuple

ir.term_structure.curves module

curves module

Examples

  • Get spot rate and discount factor
>>> val_date = ql.Date(15, 1, 2015)
2016-05-26 15:28:48 WARNING Valuation date is changed to [January 15th, 2015]
>>> schedule = ql.Schedule(ql.Date(15, 1, 2015), ql.Date(15, 1, 2016), ql.Period(1, ql.Months), ql.UnitedStates(), ql.Following, ql.Following, ql.DateGeneration.Forward, False)
>>> spot_curve = SpotCurve(val_date, [ql.Date(15, 1, 2015), ql.Date(15, 7, 2015), ql.Date(15, 1, 2016)], [0.0, 0.005, 0.007])
>>> zeros = spot_curve.get_zero(list(schedule))
>>> curves.plot('zero', val_date, zeros)
>>> spot_curve.get_discount_factor(list(schedule))
>>> curves.plot('discount', val_date, df)
_images/curve_example_spot.png _images/curve_example_df.png
class ir.term_structure.curves.Curve(settle, val_date=None)

Bases: object

Base class for all term_structure curve

curve

ir.term_structure.curves.Curve object

Implement this in super class

curve_name

Implement this in super class

get_discount_factor(sim_ql_date, daycount=<DayCount.Act360: <QuantLib.QuantLib.Actual360; proxy of <Swig Object of type 'QuantLib::Actual360 *' at 0x000000000696E0F0> >>)

Get discount factor

Parameters:
Returns:

{curve_name: pandas.DataFrame with ‘Date’, ‘Rate’ column}

Return type:

dict

get_forward(sim_ql_sdate, sim_ql_edate, daycount=None, compounding=None, freq=None)

Get forward rate

Parameters:
Returns:

{curve_name: pandas.DataFrame with ‘Date’, ‘Rate’ column}

Return type:

dict

get_zero(sim_ql_date, daycount=None, compounding=None, freq=None)

Get zero (spot) rate Unlike Matlab, extrapolation does not work for the shorter tenors. It only works on longer tenors

Parameters:
Returns:

{curve_name: pandas.DataFrame with ‘Date’, ‘Rate’ column}

Return type:

dict

class ir.term_structure.curves.FlatForward(settle, forward_rate, daycount=<DayCount.Act360: <QuantLib.QuantLib.Actual360; proxy of <Swig Object of type 'QuantLib::Actual360 *' at 0x000000000696E0F0> >>, compounding=<Compounding.Compounded: 1>, compound_freq=<Freq.Annual: 1>, val_date=None)

Bases: ir.term_structure.curves.Curve

FlatForward class This is used for curve construction without bootstrapping

Examples

>>> val_date, maturity_date = ql.Date(5, 1, 2010), ql.Date(5, 1, 2014)
>>> schedule = ql.Schedule(val_date, maturity_date, ql.Period(ql.Quarterly), ql.NullCalendar(), ql.Unadjusted, ql.Unadjusted, ql.DateGeneration.Forward, False)
>>> flat_forward_curve = FlatForward(val_date, 0.1, compound_freq=Freq.Quarterly)
>>> forwards = flat_forward_curve.get_forward(list(schedule), [schedule_ele + ql.Period('3M') for schedule_ele in list(schedule)], freq=Freq.Quarterly)
curve

Returns curve instance

Returns: ql.FlatForward(...)

curve_name

Returns curve name

Returns: str: ‘FlatForward’

class ir.term_structure.curves.PiecewiseCubic(input_helper, daycount=<DayCount.ActAct: <QuantLib.QuantLib.ActualActual; proxy of <Swig Object of type 'QuantLib::ActualActual *' at 0x000000000696E1B0> >>, val_date=None)

Bases: ir.term_structure.curves.PiecewiseCurve

Interpolate based on piecewise cubic for zero rate

curve

Returns curve instance

Returns: ql.PiecewiseCubicZero(...)

curve_name

Returns curve name

Returns: str: ‘PiecewiseCubic’

class ir.term_structure.curves.PiecewiseCurve(input_helper, daycount=<DayCount.ActAct: <QuantLib.QuantLib.ActualActual; proxy of <Swig Object of type 'QuantLib::ActualActual *' at 0x000000000696E1B0> >>)

Bases: ir.term_structure.curves.Curve

Base class for curve constructed by bootstrap Valuation date is set as same as settle date defined during input_helper

curve
curve_name
class ir.term_structure.curves.PiecewiseLinear(input_helper, daycount=<DayCount.ActAct: <QuantLib.QuantLib.ActualActual; proxy of <Swig Object of type 'QuantLib::ActualActual *' at 0x000000000696E1B0> >>, val_date=None)

Bases: ir.term_structure.curves.PiecewiseCurve

Interpolate based on piecewise linear for zero rate

curve

Returns curve instance

Returns: ql.PiecewiseLinearZero(...)

curve_name

Returns curve name

Returns: str: ‘FlatForward’

class ir.term_structure.curves.PiecewiseLogCubicDiscount(input_helper, daycount=<DayCount.ActAct: <QuantLib.QuantLib.ActualActual; proxy of <Swig Object of type 'QuantLib::ActualActual *' at 0x000000000696E1B0> >>)

Bases: ir.term_structure.curves.PiecewiseCurve

Interpolate based on piecewise log cubic for discount rate Valuation date is set as same as settle date defined during input_helper

curve

Returns curve instance

Returns: ql.PiecewiseLogCubicDiscount(...)

curve_name

Returns curve name

Returns: str: ‘PiecewiseLogCubicDiscount’

class ir.term_structure.curves.SpotCurve(settle, spot_date, spot_rate, daycount=<DayCount.Act360: <QuantLib.QuantLib.Actual360; proxy of <Swig Object of type 'QuantLib::Actual360 *' at 0x000000000696E0F0> >>, compounding=<Compounding.Compounded: 1>, compound_freq=<Freq.Annual: 1>, val_date=None)

Bases: ir.term_structure.curves.Curve

SpotCurve class This is used for curve construction without bootstrapping

curve

Returns curve instance

Returns: ql.ZeroCurve(...)

curve_name

Returns curve name

Returns: str: ‘Spot’

ir.term_structure.curves.plot(rate_type, val_date, rate_dict, title=None, x_label=None)
Parameters:
  • rate_type (str) – either ‘zero’, ‘forward’, ‘discount’
  • val_date (ql.Date) – Valuation date
  • rate_dict (dict) – dictionary with {curve_name: rates) rates are generated by ir.term_structure.curves.Curve.get_zero / get_forward / get_discount_factor
  • title (str) – title string (Default: None)
  • x_label (str) – x label name (Default: None to be ‘Date’, other options are ‘day’, ‘year’)