C++:实现量化相关的各类数据测试实例

#include "dates.hpp"
#include "utilities.hpp"
#include <ql/time/date.hpp>
#include <ql/time/timeunit.hpp>
#include <ql/time/imm.hpp>
#include <ql/time/ecb.hpp>
#include <ql/time/asx.hpp>
#include <ql/utilities/dataparsers.hpp>#include <sstream>
#include <unordered_set>using namespace QuantLib;
using namespace boost::unit_test_framework;void DateTest::ecbDates() {BOOST_TEST_MESSAGE("Testing ECB dates...");std::set<Date> knownDates = ECB::knownDates();if (knownDates.empty())BOOST_FAIL("empty EBC date vector");Size n = ECB::nextDates(Date::minDate()).size();if (n != knownDates.size())BOOST_FAIL("nextDates(minDate) returns "  << n <<" instead of " << knownDates.size() << " dates");std::set<Date>::const_iterator i;Date previousEcbDate = Date::minDate(),currentEcbDate, ecbDateMinusOne;for (i=knownDates.begin(); i!=knownDates.end(); ++i) {currentEcbDate = *i;if (!ECB::isECBdate(currentEcbDate))BOOST_FAIL(currentEcbDate << " fails isECBdate check");ecbDateMinusOne = currentEcbDate-1;if (ECB::isECBdate(ecbDateMinusOne))BOOST_FAIL(ecbDateMinusOne << " fails isECBdate check");if (ECB::nextDate(ecbDateMinusOne)!=currentEcbDate)BOOST_FAIL("next EBC date following " << ecbDateMinusOne <<" must be " << currentEcbDate);if (ECB::nextDate(previousEcbDate)!=currentEcbDate)BOOST_FAIL("next EBC date following " << previousEcbDate <<" must be " << currentEcbDate);previousEcbDate = currentEcbDate;}Date knownDate = *knownDates.begin();ECB::removeDate(knownDate);if (ECB::isECBdate(knownDate))BOOST_FAIL("unable to remove an EBC date");ECB::addDate(knownDate);if (!ECB::isECBdate(knownDate))BOOST_FAIL("unable to add an EBC date");
}void DateTest::immDates() {BOOST_TEST_MESSAGE("Testing IMM dates...");const std::string IMMcodes[] = {"F0", "G0", "H0", "J0", "K0", "M0", "N0", "Q0", "U0", "V0", "X0", "Z0","F1", "G1", "H1", "J1", "K1", "M1", "N1", "Q1", "U1", "V1", "X1", "Z1","F2", "G2", "H2", "J2", "K2", "M2", "N2", "Q2", "U2", "V2", "X2", "Z2","F3", "G3", "H3", "J3", "K3", "M3", "N3", "Q3", "U3", "V3", "X3", "Z3","F4", "G4", "H4", "J4", "K4", "M4", "N4", "Q4", "U4", "V4", "X4", "Z4","F5", "G5", "H5", "J5", "K5", "M5", "N5", "Q5", "U5", "V5", "X5", "Z5","F6", "G6", "H6", "J6", "K6", "M6", "N6", "Q6", "U6", "V6", "X6", "Z6","F7", "G7", "H7", "J7", "K7", "M7", "N7", "Q7", "U7", "V7", "X7", "Z7","F8", "G8", "H8", "J8", "K8", "M8", "N8", "Q8", "U8", "V8", "X8", "Z8","F9", "G9", "H9", "J9", "K9", "M9", "N9", "Q9", "U9", "V9", "X9", "Z9"};Date counter = { 1, January, 2000 };Date last = { 1, January, 2040 };Date imm;while (counter<=last) {imm = IMM::nextDate(counter, false);// check that imm is greater than counterif (imm<=counter)BOOST_FAIL(imm.weekday() << " " << imm<< " is not greater than "<< counter.weekday() << " " << counter);// check that imm is an IMM dateif (!IMM::isIMMdate(imm, false))BOOST_FAIL(imm.weekday() << " " << imm<< " is not an IMM date (calculated from "<< counter.weekday() << " " << counter << ")");// check that imm is <= to the next IMM date in the main cycleif (imm>IMM::nextDate(counter, true))BOOST_FAIL(imm.weekday() << " " << imm<< " is not less than or equal to the next future in the main cycle "<< IMM::nextDate(counter, true));// check that for every date IMMdate is the inverse of IMMcodeif (IMM::date(IMM::code(imm), counter) != imm)BOOST_FAIL(IMM::code(imm)<< " at calendar day " << counter<< " is not the IMM code matching " << imm);// check that for every date the 120 IMM codes refer to future datesfor (int i=0; i<40; ++i) {if (IMM::date(IMMcodes[i], counter)<counter)BOOST_FAIL(IMM::date(IMMcodes[i], counter)<< " is wrong for " << IMMcodes[i]<< " at reference date " << counter);}counter = counter + 1;}
}void DateTest::asxDates() {BOOST_TEST_MESSAGE("Testing ASX dates...");const std::string ASXcodes[] = {"F0", "G0", "H0", "J0", "K0", "M0", "N0", "Q0", "U0", "V0", "X0", "Z0","F1", "G1", "H1", "J1", "K1", "M1", "N1", "Q1", "U1", "V1", "X1", "Z1","F2", "G2", "H2", "J2", "K2", "M2", "N2", "Q2", "U2", "V2", "X2", "Z2","F3", "G3", "H3", "J3", "K3", "M3", "N3", "Q3", "U3", "V3", "X3", "Z3","F4", "G4", "H4", "J4", "K4", "M4", "N4", "Q4", "U4", "V4", "X4", "Z4","F5", "G5", "H5", "J5", "K5", "M5", "N5", "Q5", "U5", "V5", "X5", "Z5","F6", "G6", "H6", "J6", "K6", "M6", "N6", "Q6", "U6", "V6", "X6", "Z6","F7", "G7", "H7", "J7", "K7", "M7", "N7", "Q7", "U7", "V7", "X7", "Z7","F8", "G8", "H8", "J8", "K8", "M8", "N8", "Q8", "U8", "V8", "X8", "Z8","F9", "G9", "H9", "J9", "K9", "M9", "N9", "Q9", "U9", "V9", "X9", "Z9"};Date counter = { 1, January, 2000 };Date last = { 1, January, 2040 };Date asx;while (counter <= last) {asx = ASX::nextDate(counter, false);// check that asx is greater than counterif (asx <= counter)BOOST_FAIL(asx.weekday() << " " << asx<< " is not greater than "<< counter.weekday() << " " << counter);// check that asx is an ASX dateif (!ASX::isASXdate(asx, false))BOOST_FAIL(asx.weekday() << " " << asx<< " is not an ASX date (calculated from "<< counter.weekday() << " " << counter << ")");// check that asx is <= to the next ASX date in the main cycleif (asx>ASX::nextDate(counter, true))BOOST_FAIL(asx.weekday() << " " << asx<< " is not less than or equal to the next future in the main cycle "<< ASX::nextDate(counter, true));// check that for every date ASXdate is the inverse of ASXcodeif (ASX::date(ASX::code(asx), counter) != asx)BOOST_FAIL(ASX::code(asx)<< " at calendar day " << counter<< " is not the ASX code matching " << asx);// check that for every date the 120 ASX codes refer to future datesfor (const auto& ASXcode : ASXcodes) {if (ASX::date(ASXcode, counter) < counter)BOOST_FAIL(ASX::date(ASXcode, counter) << " is wrong for " << ASXcode<< " at reference date " << counter);}counter = counter + 1;}
}void DateTest::testConsistency() {BOOST_TEST_MESSAGE("Testing dates...");Date::serial_type minDate = Date::minDate().serialNumber()+1,maxDate = Date::maxDate().serialNumber();Date::serial_type dyold = Date(minDate-1).dayOfYear(),dold  = Date(minDate-1).dayOfMonth(),mold  = Date(minDate-1).month(),yold  = Date(minDate-1).year(),wdold = Date(minDate-1).weekday();for (Date::serial_type i=minDate; i<=maxDate; i++) {Date t(i);Date::serial_type serial = t.serialNumber();// check serial number consistencyif (serial != i)BOOST_FAIL("inconsistent serial number:\n"<< "    original:      " << i << "\n"<< "    date:          " << t << "\n"<< "    serial number: " << serial);Integer dy = t.dayOfYear(),d  = t.dayOfMonth(),m  = t.month(),y  = t.year(),wd = t.weekday();// check if skipping any dateif (!((dy == dyold+1) ||(dy == 1 && dyold == 365 && !Date::isLeap(yold)) ||(dy == 1 && dyold == 366 && Date::isLeap(yold))))BOOST_FAIL("wrong day of year increment: \n"<< "    date: " << t << "\n"<< "    day of year: " << dy << "\n"<< "    previous:    " << dyold);dyold = dy;if (!((d == dold+1 && m == mold   && y == yold) ||(d == 1      && m == mold+1 && y == yold) ||(d == 1      && m == 1      && y == yold+1)))BOOST_FAIL("wrong day,month,year increment: \n"<< "    date: " << t << "\n"<< "    day,month,year: "<< d << "," << Integer(m) << "," << y << "\n"<< "    previous:       "<< dold<< "," << Integer(mold) << "," << yold);dold = d; mold = m; yold = y;// check month definitionif (m < 1 || m > 12)BOOST_FAIL("invalid month: \n"<< "    date:  " << t << "\n"<< "    month: " << Integer(m));// check day definitionif (d < 1)BOOST_FAIL("invalid day of month: \n"<< "    date:  " << t << "\n"<< "    day: " << d);if (!((m == 1  && d <= 31) ||(m == 2  && d <= 28) ||(m == 2  && d == 29 && Date::isLeap(y)) ||(m == 3  && d <= 31) ||(m == 4  && d <= 30) ||(m == 5  && d <= 31) ||(m == 6  && d <= 30) ||(m == 7  && d <= 31) ||(m == 8  && d <= 31) ||(m == 9  && d <= 30) ||(m == 10 && d <= 31) ||(m == 11 && d <= 30) ||(m == 12 && d <= 31)))BOOST_FAIL("invalid day of month: \n"<< "    date:  " << t << "\n"<< "    day: " << d);// check weekday definitionif (!((Integer(wd) == Integer(wdold+1)) ||(Integer(wd) == 1 && Integer(wdold) == 7)))BOOST_FAIL("invalid weekday: \n"<< "    date:  " << t << "\n"<< "    weekday:  " << Integer(wd) << "\n"<< "    previous: " << Integer(wdold));wdold = wd;// create the same date with a different constructorDate s(d,Month(m),y);// check serial number consistencyserial = s.serialNumber();if (serial != i)BOOST_FAIL("inconsistent serial number:\n"<< "    date:          " << t << "\n"<< "    serial number: " << i << "\n"<< "    cloned date:   " <<  s << "\n"<< "    serial number: " << serial);}}void DateTest::isoDates() {BOOST_TEST_MESSAGE("Testing ISO dates...");std::string input_date("2006-01-15");Date d = DateParser::parseISO(input_date);if (d.dayOfMonth() != 15 ||d.month() != January ||d.year() != 2006) {BOOST_FAIL("Iso date failed\n"<< " input date:    " << input_date << "\n"<< " day of month:  " << d.dayOfMonth() << "\n"<< " month:         " << d.month() << "\n"<< " year:          " << d.year());}
}void DateTest::parseDates() {BOOST_TEST_MESSAGE("Testing parsing of dates...");std::string input_date("2006-01-15");Date d = DateParser::parseFormatted(input_date, "%Y-%m-%d");if (d != Date(15, January, 2006)) {BOOST_FAIL("date parsing failed\n"<< " input date:  " << input_date << "\n"<< " parsed date: " << d);}input_date = "12/02/2012";d = DateParser::parseFormatted(input_date, "%m/%d/%Y");if (d != Date(2, December, 2012)) {BOOST_FAIL("date parsing failed\n"<< " input date:  " << input_date << "\n"<< " parsed date: " << d);}d = DateParser::parseFormatted(input_date, "%d/%m/%Y");if (d != Date(12, February, 2012)) {BOOST_FAIL("date parsing failed\n"<< " input date:  " << input_date << "\n"<< " parsed date: " << d);}input_date = "20011002";d = DateParser::parseFormatted(input_date, "%Y%m%d");if (d != Date(2, October, 2001)) {BOOST_FAIL("date parsing failed\n"<< " input date:  " << input_date << "\n"<< " parsed date: " << d);}
}void DateTest::intraday() {#ifdef QL_HIGH_RESOLUTION_DATEBOOST_TEST_MESSAGE("Testing intraday information of dates...");const Date d1 = Date(12, February, 2015, 10, 45, 12, 1234, 76253);BOOST_CHECK_MESSAGE(d1.year() == 2015, "failed to reproduce year");BOOST_CHECK_MESSAGE(d1.month() == February, "failed to reproduce month");BOOST_CHECK_MESSAGE(d1.dayOfMonth() == 12, "failed to reproduce day");BOOST_CHECK_MESSAGE(d1.hours() == 10, "failed to reproduce hour of day");BOOST_CHECK_MESSAGE(d1.minutes() == 45,"failed to reproduce minute of hour");BOOST_CHECK_MESSAGE(d1.seconds() == 13,"failed to reproduce second of minute");if (Date::ticksPerSecond() == 1000)BOOST_CHECK_MESSAGE(d1.fractionOfSecond() == 0.234,"failed to reproduce fraction of second");else if (Date::ticksPerSecond() >= 1000000)BOOST_CHECK_MESSAGE(d1.fractionOfSecond() == (234000 + 76253)/1000000.0,"failed to reproduce fraction of second");if (Date::ticksPerSecond() >= 1000)BOOST_CHECK_MESSAGE(d1.milliseconds() == 234 + 76,"failed to reproduce number of milliseconds");if (Date::ticksPerSecond() >= 1000000)BOOST_CHECK_MESSAGE(d1.microseconds() == 253,"failed to reproduce number of microseconds");const Date d2 = Date(28, February, 2015, 50, 165, 476, 1234, 253);BOOST_CHECK_MESSAGE(d2.year() == 2015, "failed to reproduce year");BOOST_CHECK_MESSAGE(d2.month() == March, "failed to reproduce month");BOOST_CHECK_MESSAGE(d2.dayOfMonth() == 2, "failed to reproduce day");BOOST_CHECK_MESSAGE(d2.hours() == 4, "failed to reproduce hour of day");BOOST_CHECK_MESSAGE(d2.minutes() == 52,"failed to reproduce minute of hour");BOOST_CHECK_MESSAGE(d2.seconds() == 57,"failed to reproduce second of minute");if (Date::ticksPerSecond() >= 1000)BOOST_CHECK_MESSAGE(d2.milliseconds() == 234,"failed to reproduce number of milliseconds");if (Date::ticksPerSecond() >= 1000000)BOOST_CHECK_MESSAGE(d2.microseconds() == 253,"failed to reproduce number of microseconds");std::ostringstream s;s << io::iso_datetime(Date(7, February, 2015, 1, 4, 2, 3, 4));BOOST_CHECK_MESSAGE(s.str() == std::string("2015-02-07T01:04:02,003004"),"datetime to string failed to reproduce expected result");#endif
}void DateTest::canHash() {BOOST_TEST_MESSAGE("Testing hashing of dates...");Date start_date = Date(1, Jan, 2020);int nb_tests = 500;std::hash<Date> hasher;// Check hash valuesfor (int i = 0; i < nb_tests; ++i) {for (int j = 0; j < nb_tests; ++j) {Date lhs = start_date + i;Date rhs = start_date + j;if (lhs == rhs && hasher(lhs) != hasher(rhs)) {BOOST_FAIL("Equal dates are expected to have same hash value\n"<< "rhs = " << lhs << '\n'<< "lhs = " << rhs << '\n'<< "hash(lhs) = " << hasher(lhs) << '\n'<< "hash(rhs) = " << hasher(rhs) << '\n');}if (lhs != rhs && hasher(lhs) == hasher(rhs)) {BOOST_FAIL("Different dates are expected to have different hash value\n"<< "rhs = " << lhs << '\n'<< "lhs = " << rhs << '\n'<< "hash(lhs) = " << hasher(lhs) << '\n'<< "hash(rhs) = " << hasher(rhs) << '\n');}}}// Check if Date can be used as unordered_set keystd::unordered_set<Date> set;set.insert(start_date);if (set.count(start_date) == 0) {BOOST_FAIL("Expected to find date " << start_date << " in unordered_set\n");}
}test_suite* DateTest::suite(SpeedLevel speed) {auto* suite = BOOST_TEST_SUITE("Date tests");suite->add(QUANTLIB_TEST_CASE(&DateTest::testConsistency));suite->add(QUANTLIB_TEST_CASE(&DateTest::ecbDates));suite->add(QUANTLIB_TEST_CASE(&DateTest::immDates));suite->add(QUANTLIB_TEST_CASE(&DateTest::asxDates));suite->add(QUANTLIB_TEST_CASE(&DateTest::isoDates));#ifndef QL_PATCH_SOLARISsuite->add(QUANTLIB_TEST_CASE(&DateTest::parseDates));#endifsuite->add(QUANTLIB_TEST_CASE(&DateTest::intraday));suite->add(QUANTLIB_TEST_CASE(&DateTest::canHash));return suite;
}

该博文为原创文章,未经博主同意不得转。
本文章博客地址:https://cplusplus.blog.csdn.net/article/details/128350066

C++:实现量化相关的各类数据测试实例相关推荐

  1. C++:实现量化相关的Interpolation插值测试实例

    C++:实现量化相关的Interpolation插值测试实例 #include "interpolations.hpp" #include "utilities.hpp& ...

  2. vue element table 相关页面跳转实例代码

    vue element table 相关页面跳转实例代码 <el-table-column width="100px" align="center" la ...

  3. C++:实现量化daycounters 日计数器测试实例

    C++:实现量化daycounters 日计数器测试实例 #include "daycounters.hpp" #include "utilities.hpp" ...

  4. C++:实现量化CPI债券交换测试实例

    C++:实现量化CPI债券交换测试实例 #include "utilities.hpp" #include "inflationcpiswap.hpp" #in ...

  5. C++:实现量化Piecewise yield曲线测试实例

    C++:实现量化Piecewise yield曲线测试实例 #include "piecewiseyieldcurve.hpp" #include "utilities. ...

  6. C++:实现量化N阶导数运算测试实例

    C++:实现量化N阶导数运算测试实例 #include "nthorderderivativeop.hpp" #include "utilities.hpp"# ...

  7. C++:实现量化Libor市场模型流程测试实例

    C++:实现量化Libor市场模型流程测试实例 #include "libormarketmodelprocess.hpp" #include "utilities.hp ...

  8. C++:实现量化SMM Caplet校准测试实例

    C++:实现量化SMM Caplet校准测试实例 #include "marketmodel_smmcapletcalibration.hpp" #include "ut ...

  9. C++:实现量化SMM Caplet α 校准测试实例

    C++:实现量化SMM Caplet α 校准测试实例 #include "marketmodel_smmcapletalphacalibration.hpp" #include ...

最新文章

  1. iOS 快速存储到本地
  2. 【gradle】问题及解决
  3. Spring配置多数据源错误总结
  4. sql 写query_为什么需要动态SQL
  5. DM9000网卡原理与基地址设置
  6. oracle有入参的试图,Oracle 带参视图
  7. linux安装源码mysql失败,linux停mysql源码安装
  8. Postgresql - MATERIALIZED VIEW
  9. [LeetCode]Trapping Rain Water
  10. 用Vue-cli3+element+mockjs 实现后台管理权限系统及顶栏三级菜单显示
  11. webtrends 分析
  12. intel c语言面试题,Intel的一道C语言笔试题
  13. 数据结构之平衡二叉树C语言版
  14. 解方程计算器,一款数学神器APP,有需要的自己收藏!
  15. cpu上干硅脂怎么清理_电脑清灰CPU怎么涂硅脂 导热硅脂涂抹方法教程
  16. sql命令手册(转载)http://www.fanqiang.com
  17. android音频文件存放目录,Android系统声音文件目录
  18. ZigBee无线温度传感网络设计
  19. 【文末福利】用Python画了一幅《海上生明月》的画
  20. 绝佳表现电商各类促销活动插画素材|玩转大促购物节

热门文章

  1. CodeForces-1016C Vasya And The Mushrooms(模拟+思维+前缀和的前缀和) 解题报告 Apare_xzc
  2. 软件工程实践结对作业一
  3. antd中table组件中如何进行换行操作(react中)
  4. 关于原生table表单在vue中的遍历和合并行
  5. 海康威视rtsp转rtmp
  6. Android 蓝牙连接
  7. 谈谈数据决策平台搭建的必要性
  8. php 之session 进行时
  9. html 自动加载activex控件,管理加载项未显示加载控件
  10. Android平台以WebView方式集成H5+SDK和支付宝登录授权插件开发思路总结