1 votos

SOLUCIONADO Recálculo manual de tasas forward desde QuantLib Python

He usado QuantLib Python para calcular las tasas forward de 1 mes a partir de tasas cero en la fecha 05 de diciembre de 2019.

Mi códigos se pueden encontrar a continuación:

import QuantLib as ql

calendar = ql.NullCalendar()
compounding = ql.Compounded
day_convention = ql.Actual360()
valuation_date = ql.Date(5, 12, 2019)
ql.Settings.instance().evaluationDate = valuation_date
dates = [calendar.advance(valuation_date, ql.Period(n, ql.Years)) for n in range(0, 4, 1)]
zero_rates = [0.01, 0.02, 0.03, 0.05]
zero_curve = ql.ZeroCurve(dates, zero_rates, day_convention)
forward_term_structure = ql.YieldTermStructureHandle(zero_curve)
forward_term_structure.enableExtrapolation()
forward_rates_quantlib = []
value_dates = []
maturity_dates = []
discount_factor_value_dates = []
discount_factor_maturity_dates = []
year_frac_value_maturity_dates = []

for date in dates:
    value_date = date
    value_dates.append(value_date)
    maturity_date = date + ql.Period('1M')
    maturity_dates.append(maturity_date)
    forward_rate = forward_term_structure.forwardRate(value_date, maturity_date, day_convention, compounding).rate()
    forward_rates_quantlib.append(forward_rate)
    discount_factor_value_date = zero_curve.discount(value_date)
    discount_factor_value_dates.append(discount_factor_value_date)
    discount_factor_maturity_date = zero_curve.discount(maturity_date)
    discount_factor_maturity_dates.append(discount_factor_maturity_date)
    year_frac_value_maturity_date = day_convention.yearFraction(value_date, maturity_date)
    year_frac_value_maturity_dates.append(year_frac_value_maturity_date)

import pandas as pd
manual_calculation = pd.DataFrame()
manual_calculation['Value_Date'] = value_dates
manual_calculation['Discount_Factor_Value_Date'] = discount_factor_value_dates
manual_calculation['Maturity_Date'] = maturity_dates
manual_calculation['Discount_Factor_Maturity_Date'] = discount_factor_maturity_dates
manual_calculation['Year_Frac_Value_Maturity'] = year_frac_value_maturity_dates
manual_calculation['Forward_Rate_QuantLib'] = forward_rates_quantlib

forward_rates_manual = []

for i in range(0, len(manual_calculation), 1):
    df_value = manual_calculation.loc[i, 'Discount_Factor_Value_Date']
    df_mat = manual_calculation.loc[i, 'Discount_Factor_Maturity_Date']
    year_frac = manual_calculation.loc[i, 'Year_Frac_Value_Maturity']
    forward_rates_manual.append(((df_value/df_mat) - 1) * (1/year_frac))

manual_calculation['Forward_Rate_Manual'] = forward_rates_manual

import matplotlib.pyplot as plt  
plt.plot(zero_rates, 'o-', label = "Spot")
plt.plot(forward_rates, 'o-', label = "1M Forward (QuantLib)")
plt.plot(forward_rates_manual, 'o-', label = "1M Forward (Manual)")
plt.legend()

Como se puede ver en la lista forward_rates_manual, he intentado volver a calcular manualmente las tasas forward de 1 mes utilizando la siguiente fórmula:

donde

significa factor de descuento desde la fecha de valoración hasta la fecha de valor

significa factor de descuento desde la fecha de vencimiento hasta la fecha de valoración, donde la fecha de vencimiento es la fecha de valor más 1 mes, y

significa la fracción de año entre la fecha de valor y la fecha de vencimiento.

Sin embargo, al comparar forward_rates_quantlib con forward_rates_manual, veo que surgen ligeras diferencias, y no puedo entender de dónde vienen.

Agradezco cualquier aclaración, gracias.

-1voto

Lemon Puntos 96
import QuantLib as ql

calendar = ql.NullCalendar()
compounding = ql.Compounded
day_convention = ql.Actual360()
valuation_date = ql.Date(5, 12, 2019)
ql.Settings.instance().evaluationDate = valuation_date
dates = [calendar.advance(valuation_date, ql.Period(n, ql.Years)) for n in range(0, 4, 1)]
zero_rates = [0.01, 0.02, 0.03, 0.05]
zero_curve = ql.ZeroCurve(dates, zero_rates, day_convention)
forward_term_structure = ql.YieldTermStructureHandle(zero_curve)
forward_term_structure.enableExtrapolation()
forward_rates_quantlib = []
value_dates = []
maturity_dates = []
discount_factor_value_dates = []
discount_factor_maturity_dates = []
year_frac_value_maturity_dates = []

for date in dates:
    value_date = date
    value_dates.append(value_date)
    maturity_date = date + ql.Period('1M')
    maturity_dates.append(maturity_date)
    forward_rate = forward_term_structure.forwardRate(value_date, maturity_date, day_convention, compounding).rate()
    forward_rates_quantlib.append(forward_rate)
    discount_factor_value_date = zero_curve.discount(value_date)
    discount_factor_value_dates.append(discount_factor_value_date)
    discount_factor_maturity_date = zero_curve.discount(maturity_date)
    discount_factor_maturity_dates.append(discount_factor_maturity_date)
    year_frac_value_maturity_date = day_convention.yearFraction(value_date, maturity_date)
    year_frac_value_maturity_dates.append(year_frac_value_maturity_date)

import pandas as pd

manual_calculation = pd.DataFrame()
manual_calculation['Value_Date'] = value_dates
manual_calculation['Discount_Factor_Value_Date'] = discount_factor_value_dates
manual_calculation['Maturity_Date'] = maturity_dates
manual_calculation['Discount_Factor_Maturity_Date'] = discount_factor_maturity_dates
manual_calculation['Year_Frac_Value_Maturity'] = year_frac_value_maturity_dates
manual_calculation['Forward_Rate_QuantLib'] = forward_rates_quantlib
manual_calculation['ZC'] = zero_rates

forward_rates_manual = []

for i in range(0, len(manual_calculation), 1):
    df_value = manual_calculation.loc[i, 'Discount_Factor_Value_Date']
    df_mat = manual_calculation.loc[i, 'Discount_Factor_Maturity_Date']
    year_frac = manual_calculation.loc[i, 'Year_Frac_Value_Maturity']
    forward_rates_manual.append(((df_value / df_mat) - 1) * (1 / year_frac))

manual_calculation['Forward_Rate_Manual'] = forward_rates_manual
manual_calculation= manual_calculation.set_index('Value_Date')
import matplotlib.pyplot as plt

manual_calculation[['ZC','Forward_Rate_Manual','Forward_Rate_QuantLib']].plot()
plt.legend()
plt.show()

Finanhelp.com

FinanHelp es una comunidad para personas con conocimientos de economía y finanzas, o quiere aprender. Puedes hacer tus propias preguntas o resolver las de los demás.

Powered by:

X