簡體   English   中英

在 Quantlib 中更快地執行 Python 中的香草交換

[英]Faster execution of Vanilla Swaps in Python in Quantlib

我試圖在 Quantlib 中為一千種不同工具的數千條曲線上的多個 ccys 和結算日期定價 Vanilla Swap。 我試圖找到一種方法來從本質上擴展我的代碼。

我正在使用 Python 庫,並且使用多處理組件確實對性能有所幫助。 由於許多曲線和交易將一次又一次地重復使用,我試圖找出是否有辦法禁用一些有助於提高性能的默認設置。 除了單獨定價掉期掉期,有沒有辦法聚合它們並計算 NPV,就像將它們添加到投資組合中一樣? 此外,我閱讀了一些關於禁用觀察者通知的帖子,但我看不到如何更新此設置。 不確定這是否會有所幫助或導致更多問題。

我還在下面發布了一個代碼片段,以防有明顯的東西我可以更改以使代碼運行得更快。 (為簡潔起見,我只保留了 2 個數據點並從下面的代碼中刪除了多處理組件)

import QuantLib as ql

execution_date = ql.Date(10, 9, 2019)
ql.Settings.instance().evaluationDate = execution_date
periods = ['1D', '50Y', ]
market_rates = [-0.0043, 0.002140, ]
calendar = ql.UnitedStates()
trade_count = 100
scenarios = 100
num = 0.000001

curves = []
for _ in range(scenarios):
    swap_rate_helpers = []
    for i in range(len(market_rates)):
        market_rate = market_rates[i] + num
        quote = ql.SimpleQuote(market_rate)
        helper = ql.SwapRateHelper(ql.QuoteHandle(quote),
                                   ql.Period(periods[i]),
                                   calendar,
                                   ql.Annual,
                                   ql.Unadjusted,
                                   ql.Actual360(),
                                   ql.Euribor6M()
                                   )
        swap_rate_helpers.append(helper)

    curve = ql.PiecewiseLogLinearDiscount(execution_date, swap_rate_helpers, ql.Actual360())
    curves.append(curve)
    num += 0.000001

start_date = calendar.advance(execution_date, 2, ql.Days)
end_date = calendar.advance(start_date, 30, ql.Years)

fixed_schedule = ql.Schedule(start_date, end_date, ql.Period(12, ql.Months), calendar, ql.ModifiedFollowing, ql.ModifiedFollowing, ql.DateGeneration.Forward, False)
float_schedule = ql.Schedule(start_date, end_date, ql.Period(6, ql.Months), calendar, ql.ModifiedFollowing, ql.ModifiedFollowing, ql.DateGeneration.Forward, False)

term_structure = ql.RelinkableYieldTermStructureHandle(curves[0])
float_index = ql.Euribor6M(term_structure)
swaps = []
swap_engine = ql.DiscountingSwapEngine(term_structure)
for _ in range(trade_count):
    ir_swap = ql.VanillaSwap(ql.VanillaSwap.Payer, 100000000, fixed_schedule, 0.008, ql.Actual360(), float_schedule, float_index, 0, ql.Actual360())
    ir_swap.setPricingEngine(swap_engine)
    swaps.append(ir_swap)

for curve in curves:
    term_structure.linkTo(curve)
    for swap in swaps:
        swap.NPV()

print 'Trade Count: {0}'.format(trade_count)
print 'Swaps Priced: {0}'.format(scenarios)

特別是關於啟用和禁用通知, ObservableSettings C++ class 上提供了一些方法:

QuantLib::ObservableSettings::instance().disableUpdates(true);

    // do the setup

QuantLib::ObservableSettings::instance().enableUpdates();

希望這些已通過 python 綁定導出。 但是,我不完全確定這會對您的表現產生重大影響。 有關這方面的功能,請參閱原始帖子 [1]。

[1] https://sourceforge.net/p/quantlib/mailman/message/31650848/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM