- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想充分了解 Quantlib 的求解器如何在给定预测期限结构和贴现期限结构的情况下计算 float 利率债券的 Z 价差。为此,我首先创建了一个简单的辅助类来计算债券的到期 yield ,我将使用它与求解器(我选择 Brent)一起将 yield 计算与 BondFunctions::yield 进行比较。尽管如此,对于三个样本债券,我得到了两个不同的结果,但我不明白为什么。
首先,我创建了一个辅助类来使用 quantlib 的求解器以数字方式计算债券的到期 yield
class ytmsolver{
private:
const Real obsPrice;
const Bond& bondObject;
const Date& date;
DayCounter dayCounter;
Compounding compounding;
Frequency frequency;
public:
//constructor
ytmsolver(const Bond &bond, Real &price, Date &settlementDate, DayCounter& dc, Compounding& comp,
Frequency& freq):bondObject(bond),obsPrice(price),date(settlementDate), dayCounter(dc),
compounding(comp),frequency(freq){};
//overloaded operator to be used in the solver
Real operator()(const Rate& rate)const{
return (bondObject.cleanPrice(rate,dayCounter,compounding,frequency)-obsPrice);
}
};
然后我创建了一个 float 利率债券工厂,该工厂创建了一个带有索引的 float 利率、一个预测期限结构(为了计算简单起见假设是固定的)和一个定价引擎。
FloatingRateBond flatTermStructureFloaterFactory(Natural indexTenor, Frequency freq, Date tradeDate,
Date settlementDate,Natural settlementDays, Real faceAmount, const Schedule &schedule,
const Calendar& calendar,const Real ¤tLiborFixing,const Real& lastResetDateLiborFixing,
const DayCounter &accrualDayCounter,
BusinessDayConvention paymentConvention=Following, Natural fixingDays=Null< Natural >(),
const std::vector< Real > &gearings=std::vector< Real >(1, 1.0),
const std::vector< Spread > &spreads=std::vector< Spread >(1, 0.0),
const std::vector< Rate > &caps=std::vector< Rate >(),
const std::vector< Rate > &floors=std::vector< Rate >(),
bool inArrears=false, Real redemption=100.0, const Date &issueDate=Date()){
//***********Term structure declaration***********
//term structure for the cash flows using a libor index
RelinkableHandle<YieldTermStructure> liborTermStructure;
//Libor index which is tied to the Frequency of payments or index tenor
boost::shared_ptr<IborIndex> libor(new USDLibor(Period(indexTenor,Months),liborTermStructure));
//term structure to forecast rest of cash flows
boost::shared_ptr<YieldTermStructure> flatforecast(
new FlatForward(settlementDate, currentLiborFixing, accrualDayCounter, Simple, freq));
liborTermStructure.linkTo(flatforecast);
//Relinkable handle to assign to the price engine.
RelinkableHandle<YieldTermStructure> discountingTermStructure;
//***********Bond object creation***********
FloatingRateBond floatingRateBondInstance(settlementDays, faceAmount,
schedule, libor, accrualDayCounter,
paymentConvention, fixingDays,
// gearings
gearings,
// spreads
spreads);
//*********Finds the last reset date****************
Date lastResetDate;
Leg cashflows=floatingRateBondInstance.cashflows();
/*
Finds the last reset date by browsing through the cashflow dates and offsetting them by
the number of fixing days and a provided calendar.
(ONLY WORKS WITH BONDS WITH THE SAME INDEX AS PERIODICITY)
If this date is provided by the flat file then this search is completely unnecessary
*/
for (Size i=0; i<cashflows.size()-1; i++) {
//Takes the lastResetDate to be the las ocurred date prior the the tradeDate
if ((cashflows[i]->hasOccurred(tradeDate, true))) {
lastResetDate=calendar.advance(cashflows[i]->date(),-fixingDays, Days,paymentConvention);
//cout<<lastResetDate<<endl; //used to print the dates as a debug method.
}
}
cout<<"lastResetDate: "<<lastResetDate<<endl; //prints it to ensure that its correct.
//*********Adds the previous libor rate associated to the last reset date*************
libor->addFixing(lastResetDate, lastResetDateLiborFixing); //last reset date minus fixing days
//***********Bond Engine declaration***********
boost::shared_ptr<PricingEngine> bondEngine(new DiscountingBondEngine (discountingTermStructure));
floatingRateBondInstance.setPricingEngine(bondEngine); //setting the pricing engine for the bond
return floatingRateBondInstance;
在此之后,我创建了一个简单的函数,它调用 Quantlib 的债券函数并计算债券的 yield (我将其用作计算给定 float 利率债券 yield 的一种方法。
Real priceToYieldFlatTermStructure(const Bond& bond,
Real cleanPrice,
const DayCounter& dayCounter,
Compounding compounding,
Frequency frequency,
Date settlement,
Real accuracy=1e-50,
Size maxIterations=10000){
//Calls the bond function yield which takes a bond, a clean price, a day count, a compounding,
//a frequency of payments, a settlement date, a degree of accuracy and the number of max iterations to
//reach the yield.
Real irr=BondFunctions::yield(bond,cleanPrice,dayCounter,compounding,frequency,
settlement,accuracy,maxIterations);
return irr;
然后我尝试使用 Quantlib 的求解器在给定干净价格的情况下获得这些债券的 yield ,我使用以下代码得到了不同的结果:
int main(){
try {
Brent solver;
Real accuracy=1e-30, guess=0.00, min=-1.0, max=0.5;
cout<<"*******************************************"<<endl;
cout<<"Bond # 1: US4042Q0HC65"<<endl;
cout<<"*******************************************"<<endl;
//***********Input declaration***********
Natural settlementDays = 3;
Natural fixingdays=2;
Natural indexTenor=6;
Date tradeDate(02,Mar,2015);
Date issueDate(9,Aug,2006);
Date maturityDate(22,Aug,2016);
Real resetMargin=0.016;
Real indexMultiplier=1.0;
Frequency frequency=Semiannual;
Calendar holidayCalendar=UnitedStates(UnitedStates::NYSE);
BusinessDayConvention businessDayConvention= BusinessDayConvention(ModifiedFollowing);
DayCounter dayCounter=Actual360();
Real lastResetDateLiborFixing=0.003853;
Real currentLiborFixing=0.003842;
Real redemption=100;
string settlementcode="BDY"; //internal settlementcode
string settlementvalue="3"; //internal settlementvalue
Date settlementDate=getSettlementDate(tradeDate,holidayCalendar,settlementcode,settlementvalue); //function call to get the settlement date (this is working properly)
cout<<"settlementDate :"<<settlementDate<<endl;
Compounding compounding=Compounded;
Real faceAmount = redemption;
Real obsprice=101.431;
Schedule schedule(issueDate, maturityDate, Period(frequency),
holidayCalendar, businessDayConvention, businessDayConvention,
DateGeneration::Backward, true);
//***********Bond creation to be priced***********
FloatingRateBond floatingRateBondInstance1=flatTermStructureFloaterFactory(indexTenor,frequency,tradeDate,settlementDate,
settlementDays,faceAmount,schedule,holidayCalendar,currentLiborFixing,lastResetDateLiborFixing,
dayCounter,businessDayConvention,fixingdays,std::vector<Real>(1, indexMultiplier),
std::vector<Rate>(1, resetMargin));
Real ytm=priceToYieldFlatTermStructure(floatingRateBondInstance1,obsprice,dayCounter,compounding,frequency,settlementDate);
//***********Bond pricing, yield and discount marging computation***********
cout<<"Clean price: "<<floatingRateBondInstance1.cleanPrice(ytm,dayCounter,compounding,frequency,settlementDate)<<endl;
cout<<"Dirty price: "<<floatingRateBondInstance1.dirtyPrice(ytm,dayCounter,compounding,frequency,settlementDate)<<endl;
cout<<"Accrued interest: "<<floatingRateBondInstance1.accruedAmount(settlementDate)<<endl;
cout<<"Yield: "<<ytm*100<<"%"<<endl;
cout<<"Discount Margin: "<<(ytm-currentLiborFixing)*100<<"%"<<endl;
//***************solver testing***************
Real irr=solver.solve(ytmsolver(floatingRateBondInstance1,obsprice,settlementDate,dayCounter,
compounding,frequency),accuracy,guess,min,max);
cout<<"irr: "<<irr*100<<"%"<<endl;
cout<<"*******************************************"<<endl;
cout<<"Bond # 2: US4042Q0HB82"<<endl;
cout<<"*******************************************"<<endl;
//***********Input declaration***********
indexTenor=6;
issueDate=Date(27,Jul,2006);
maturityDate=Date(20,Jul,2016);
resetMargin=0.0151;
indexMultiplier=1.0;
frequency=Semiannual;
holidayCalendar=TARGET();
//holidayCalendar=UnitedStates(UnitedStates::NYSE); //not counting martin luther king day, jan 15,15 as last reset date
businessDayConvention=BusinessDayConvention(ModifiedFollowing);
dayCounter=Actual360();
lastResetDateLiborFixing=0.003549;
currentLiborFixing=0.003842;
redemption=100;
settlementcode="BDY"; //internal settlement code
settlementvalue="3"; //internal settlement value
settlementDate=getSettlementDate(tradeDate,holidayCalendar,settlementcode,settlementvalue); //function call to get the settlement date (this is working properly)
cout<<"settlementDate :"<<settlementDate<<endl;
compounding=Compounded;
faceAmount = redemption;
obsprice=100.429;
schedule=Schedule(issueDate, maturityDate, Period(frequency),
holidayCalendar, businessDayConvention, businessDayConvention,
DateGeneration::Backward, true);
//***********Bond creation to be priced***********
FloatingRateBond floatingRateBondInstance2=flatTermStructureFloaterFactory(indexTenor,frequency,tradeDate,settlementDate,
settlementDays,faceAmount,schedule,holidayCalendar,currentLiborFixing,lastResetDateLiborFixing,
dayCounter,businessDayConvention,fixingdays,std::vector<Real>(1, indexMultiplier),
std::vector<Rate>(1, resetMargin));
ytm=priceToYieldFlatTermStructure(floatingRateBondInstance2,obsprice,dayCounter,compounding,frequency,settlementDate);
//***********Bond pricing, yield and discount marging computation***********
cout<<"Clean price: "<<floatingRateBondInstance2.cleanPrice(ytm,dayCounter,compounding,frequency,settlementDate)<<endl;
cout<<"Dirty price: "<<floatingRateBondInstance2.dirtyPrice(ytm,dayCounter,compounding,frequency,settlementDate)<<endl;
cout<<"Accrued interest: "<<floatingRateBondInstance2.accruedAmount(settlementDate)<<endl;
cout<<"Yield: "<<ytm*100<<"%"<<endl;
cout<<"Discount Margin: "<<(ytm-currentLiborFixing)*100<<"%"<<endl;
//***************solver testing***************
irr=solver.solve(ytmsolver(floatingRateBondInstance2,obsprice,settlementDate,dayCounter,
compounding,frequency),accuracy,guess,min,max);
cout<<"irr: "<<irr*100<<"%"<<endl;
cout<<"*******************************************"<<endl;
cout<<"Bond # 3: US59022CCT80"<<endl;
cout<<"*******************************************"<<endl;
//***********Input declaration***********
indexTenor=3;
tradeDate=Date(10,Jun,2015);
issueDate=Date(02,May,2007);
maturityDate=Date(02,May,2017);
resetMargin=0.0055;
indexMultiplier=1.0;
frequency=Quarterly;
holidayCalendar=UnitedStates(UnitedStates::NYSE); //not counting martin luther kind day, jan 15,15 as last reset date
businessDayConvention=BusinessDayConvention(ModifiedFollowing);
dayCounter=Actual360();
lastResetDateLiborFixing=0.0027875;
currentLiborFixing=0.0028785;
redemption=100;
settlementcode="BDY"; //internal settlement code
settlementvalue="3"; //internal settlement value
settlementDate=getSettlementDate(tradeDate,holidayCalendar,settlementcode,settlementvalue); //function call to get the settlement date (this is working properly)
cout<<"settlementDate :"<<settlementDate<<endl;
compounding=Compounded;
faceAmount = redemption;
obsprice=99.794;
schedule=Schedule(issueDate, maturityDate, Period(frequency),
holidayCalendar, businessDayConvention, businessDayConvention,
DateGeneration::Backward, true);
//***********Bond pricing, yield and discount marging computation***********
FloatingRateBond floatingRateBondInstance3=flatTermStructureFloaterFactory(indexTenor,frequency,tradeDate,settlementDate,
settlementDays,faceAmount,schedule,holidayCalendar,currentLiborFixing,lastResetDateLiborFixing,
dayCounter,businessDayConvention,fixingdays,std::vector<Real>(1, indexMultiplier),
std::vector<Rate>(1, resetMargin));
ytm=priceToYieldFlatTermStructure(floatingRateBondInstance3,obsprice,dayCounter,compounding,frequency,settlementDate);
//***********Bond pricing, yield and discount marging computation***********
cout<<"Clean price: "<<floatingRateBondInstance3.cleanPrice(ytm,dayCounter,compounding,frequency,settlementDate)<<endl;
cout<<"Dirty price: "<<floatingRateBondInstance3.dirtyPrice(ytm,dayCounter,compounding,frequency,settlementDate)<<endl;
cout<<"Accrued interest: "<<floatingRateBondInstance3.accruedAmount(settlementDate)<<endl;
cout<<"Yield: "<<ytm*100<<"%"<<endl;
cout<<"Discount Margin: "<<(ytm-currentLiborFixing)*100<<"%"<<endl;
//***************solver testing***************
irr=solver.solve(ytmsolver(floatingRateBondInstance3,obsprice,settlementDate,dayCounter,
compounding,frequency),accuracy,guess,min,max);
cout<<"irr: "<<irr*100<<"%"<<endl;
return 0;
} catch (exception& e) {
cerr << e.what() << endl;
return 1;
} catch (...) {
cerr << "unknown error" << endl;
return 1;
}
最后我得到以下结果:
键#1:US4042Q0HC65
结算日期:2015年3月5日最后重置日期:2015 年 2 月 19 日净价:101.431脏价:101.486应计利息:0.0551472产率:1.01286%折扣 margin :0.628665%内部 yield :0.72216%
债券#2:US4042Q0HB82
结算日期:2015年3月5日最后重置日期:2015 年 1 月 16 日净价:100.429脏价:100.657应计利息:0.227932 yield :1.57325%折扣 margin :1.18905%内部 yield :1.47977%
债券#3:US59022CCT80
结算日期:2015年6月15日最后重置日期:2015 年 4 月 30 日净价:99.794脏价:99.8907应计利息:0.0966875产率:0.945517%折扣 margin :0.657667%内部 yield :0.949541%
我这里做错了什么?我不明白为什么求解器没有返回与 yield 的债券函数相同的数字。关于为什么或我在这里做错了什么有什么想法吗?
最佳答案
我假设返回 QuantLib BondFunctions::yield
函数的 yield 是正确的,因为当您将它传递给债券的 cleanPrice
方法时,您取回您用作输入的观察到的价格。
这让我们猜测您的函数中有什么问题。通过查看您的 ytmsolver
类,我注意到您没有像在 main 中那样将结算日期传递给债券对象的
重新定价代码时。cleanPrice
方法
如果缺少结算日期,该方法会假定它是今天的结算日期,即从今天算起的三个工作日。这明显晚于您想要的和您在 main
函数中输出的结算日期,因此您得到错误的 yield 。一旦您将结算日期传递给 cleanPrice
,求解器就会返回预期值。
关于c++ - Quantlib 求解器未产生与 BondFunctions::yield 相同的到期 yield ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31193013/
我正在尝试在 ubuntu 13.04 中使用 quantlib-swig (1.2) 学习 quantlib (1.3) 和 python 绑定(bind)。作为初学者,我尝试使用 30/360 欧
我有一个以 quantlib 的日期格式定义的日期列表。我怎样才能将这些转换成日期时间格式。我问的原因是,我想绘制它,但收到以下错误: TypeError: float() argument must
普通欧洲 EURUSD 看涨期权的 Quantlib 价格与彭博 OVML 价格不匹配。 例如对于以下选项 Quantlib 值 =4.60991,BBG 值 =4.6137,错误 =0.0038(虽
普通欧洲 EURUSD 看涨期权的 Quantlib 价格与彭博 OVML 价格不匹配。 例如对于以下选项 Quantlib 值 =4.60991,BBG 值 =4.6137,错误 =0.0038(虽
我试图让 QL 在 Debian 上运行,但在最后一步失败了。我已经下载了源代码,构建并安装了库(“make”、“make install”),没有任何错误,但是我使用 QuantLib 库的应用程序
我已经在 visual studio 2010 中从他们的网站下载并构建了 QuantLib(在谷歌上搜索 quantlib 并检查安装链接)。但是因为我对 C++ 的了解很少,所以我想使用 SWIG
我在 ubuntu16.04 上通过 apt-get install 从官方源安装了 boost。然后按照 QuantLib 的安装指南进行操作 然后我复制一个 quantlib 示例 (Exampl
我已经通过二进制文件安装了 boost 库 v1.57.0 (x64),它在我的 VS2010Pro 下可以正常工作。 但是,当我尝试编译最新版本 (v1.5) 的 QuantLib 时,通过打开 Q
我从 github 下载了 Quantlib-SWIG 1.12.x 和 Quantlib 1.12.x。 Quantlib 编译时没有问题。这些示例正常运行。但是,当运行 python setup.
我检查这个slides但仍然没有得到: 1) what problem does Handle sovled? 2) what is the benefit to add the Handle cla
我正在一个项目中使用 Quantlib 来执行一些债券计算,例如 yield 和久期。插入上市日期到期日、面值、日历、天数惯例等并得出 yield 和持续时间值相当简单。 看起来给定发行日期、到期日期
当我使用 Quantlib 为普通利率掉期定价时,每笔现金流的支付日期始终与应计期结束日期相同。这是我通常用来设置普通交换的方式: Schedule fixedSchedule(previousRes
我正在尝试学习 QuantLib,这是我的第一个程序,我打算用它来检查我的环境是否正常并且我能够链接到 quantlib: #include using namespace QuantLib; in
我正在尝试构建 QuantLib 但是,在运行构建过程时,它返回 18 次成功和 1 次失败(解决方案中的 19 个项目)。失败的应该是 testsuite。我正在根据其网站说明构建一个 x64 版本
我正在使用 QuantLib 1.7.1 并尝试运行这些代码: Date begin(30, September, 2009), end(15, Jun, 2012); Calendar myCal
我正在学习如何使用 quantlib 为衍生品定价。将一些 Quantlib 特定类输出到控制台窗口的最佳方法是什么?例如 shared_ptr forwardCurve(new Interpolat
下面是我使用 Quantlib blackvariance 曲面的代码。但它提示。你能建议吗?在这个函数中,我用 6 个变量调用 Blackvariance 函数 from QuantLib impo
在 quantlib ( http://quantlib.org ) 上有好的入门文档吗?这些示例没有很好的记录,帮助也没有提供太多见解。 最佳答案 还有 数百个单元测试, 一打或更多的例子 超过 1
我开始使用 C++ 中的 Quantlib 并尝试运行示例代码。我一直在关注 中的教程 https://www.youtube.com/watch?v=Wn_D19c2ABU&t=569s 当他在 5
我也在 Wilmott 上发布了这个,不确定哪个会得到更多的回应。 我对 Quantlib(和 C++ ......)的世界还比较陌生,所以也许这是很明显的。我试图弄清楚 Quantlib 是否可以为
我是一名优秀的程序员,十分优秀!