⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tslib.test.cpp

📁 神经网络是序列预测,C++实现
💻 CPP
字号:
#include <boost/test/included/unit_test_framework.hpp>#include <boost/test/unit_test.hpp>#include <ctime>#include "tseries.hpp"#include "util.hpp"using namespace boost::unit_test_framework;void null_constructor_test() {  tseries<time_t> t;    // so we have the same type  unsigned int zero = 0;  BOOST_CHECK_EQUAL( t.nrow(), zero );  BOOST_CHECK_EQUAL( t.ncol(), zero );  BOOST_CHECK_EQUAL( t.getData(), static_cast<double*>(NULL) );  BOOST_CHECK_EQUAL( t.getDates(), static_cast<time_t*>(NULL) );  BOOST_CHECK_EQUAL( t.getColNames().size(), zero );}void external_data_constructor_valid() {	  unsigned int rows = 10;  unsigned int cols = 1;  double *data = new double[rows];  time_t *dates = new time_t[rows];  tseries<time_t> t(data,dates,rows,cols,1);  BOOST_CHECK_EQUAL( t.nrow(), rows );  BOOST_CHECK_EQUAL( t.ncol(), static_cast<unsigned int>(1) );  BOOST_CHECK_EQUAL( t.getData(), data );  BOOST_CHECK_EQUAL( t.getDates(), dates );}void external_data_constructor_invalid() {	  unsigned int rows = 10;  double *data = NULL;  time_t *dates = new time_t[rows];  cout << "****  intentional error message  ****" << endl;  // since data is null, should create a null tseries  tseries<time_t> t(data,dates,rows,1,false);  BOOST_CHECK_EQUAL( t.nrow(), static_cast<unsigned int>(0) );  BOOST_CHECK_EQUAL( t.ncol(), static_cast<unsigned int>(0) );  BOOST_CHECK_EQUAL( t.getData(), static_cast<double*>(NULL) );  BOOST_CHECK_EQUAL( t.getDates(), static_cast<time_t*>(NULL) );  delete []dates;}void init_const_constructor() {  unsigned int nr = 100;  unsigned int nc = 100;  tseries<time_t> t(nr,nc,100.0);  double *d = t.getData();  BOOST_CHECK_EQUAL ( nr, t.nrow() );  BOOST_CHECK_EQUAL ( nc, t.ncol() );  for(unsigned int i=0; i < t.nrow()*t.ncol(); i++) {    BOOST_CHECK_EQUAL ( d[i],100.0 );  }}void cpy_constructor_test() {  unsigned int nr = 10;  unsigned int nc = 5;  tseries<time_t> t2(nr,nc,100.0);  tseries<time_t> t3(t2);	  BOOST_CHECK( all_equal(t2, t3) );}void cbind_test() {  unsigned int yrow = 10;  unsigned int ycol = 10;  unsigned int xrow = 15;  unsigned int xcol = 3;  tseries<time_t> x(xrow,xcol,1000.0);  x.fakeDates();  tseries<time_t> y(yrow,yrow,1.0);  y.fakeDates();  vector<tseries<time_t>*> ts_vec;  ts_vec.push_back(&x);  ts_vec.push_back(&y);    tseries<time_t>* cb_union = cbind(ts_vec,false);  BOOST_CHECK(cb_union->ncol()==(xcol+ycol));  BOOST_CHECK(cb_union->nrow()==xrow);		  tseries<time_t>* cb_intersection = cbind(ts_vec,true);  BOOST_CHECK(cb_intersection->ncol()==(xcol+ycol));  BOOST_CHECK(cb_intersection->nrow() == (xcol < ycol) ? xcol : ycol);  }void big_test() {  unsigned int nc = 4;  //unsigned int nr = 1000000; // big data test  unsigned int nr = 100;  const double myConst = 100.0;  tseries<time_t> increasingTS(nr,1);  tseries<time_t> naTS(nr,nc,NAN);  tseries<time_t> constTS(nr,nc,myConst);  for(unsigned int i=0; i < nr; i++)    increasingTS.setElement(i,0,(double)i);  // testing that cols were set up properly  BOOST_CHECK(naTS.ncol()==nc);  BOOST_CHECK(naTS.nrow()==nr);  BOOST_CHECK(increasingTS.ncol()==1);  // make sure these get set properly  BOOST_CHECK(isnan(naTS.getElement(0,0)));  BOOST_CHECK(constTS.getElement(0,0)==myConst);  // get col out of range  cout << "**** intentional getCol error ****" << endl;  BOOST_CHECK(constTS.getCol(nc)==NULL);  cout << "getCol errors over" << endl;	  // get and set elements  constTS.setElement(0,1,12.0);  BOOST_CHECK(constTS.getElement(0,1)==12.0);	  // set elements then get col and dereference  constTS.setElement(0,0,1.0);  constTS.setElement(0,1,2.0);  constTS.setElement(0,2,3.0);  constTS.setElement(0,3,4.0);	  BOOST_CHECK(*constTS.getCol(0)==1.0);  BOOST_CHECK(*constTS.getCol(1)==2.0);  BOOST_CHECK(*constTS.getCol(2)==3.0);  BOOST_CHECK(*constTS.getCol(3)==4.0);	  // FIXME add tests to check for windows  // that are bigger than the lenght of the tseries  // **************************  // tseries operations  tseries<time_t> lagTS = increasingTS.lag(1);  tseries<time_t> leadTS = increasingTS.lead(1);  // first row should be NA  BOOST_CHECK(isnan(lagTS.getElement(0,0)));	  // last row should be NA  BOOST_CHECK(isnan(leadTS.getElement(leadTS.nrow()-1,0)));  // start from row 2 (i=1)  // each element should be equal to the previous element  // in the original tseries  for(unsigned int i=1; i < lagTS.nrow(); i++)    BOOST_CHECK(lagTS.getElement(i,0)==increasingTS.getElement(i-1,0));  // start from row 1, but go to row N-1  // each value is equal to the next value in the original ts	  for(unsigned int i=0; i < (leadTS.nrow() - 1); i++)    BOOST_CHECK(leadTS.getElement(i,0)==increasingTS.getElement(i+1,0));	  // test ABS  tseries<time_t> newTS(100,1);  newTS.initConst(-10.0);  tseries<time_t> absTS = newTS.abs();  for(unsigned int i=0; i < absTS.nrow(); i++)    BOOST_CHECK(absTS.getElement(i,0)>=0);  // test diff  constTS.initConst(12.0);  tseries<time_t> diffTS = constTS.diff(1);  // first row should be all NA  BOOST_CHECK(isnan(diffTS.getElement(0,0)));  BOOST_CHECK(isnan(diffTS.getElement(0,1)));  BOOST_CHECK(isnan(diffTS.getElement(0,2)));  BOOST_CHECK(isnan(diffTS.getElement(0,3)));	  // all other cells should be zero  // start on row 2 (i = 1)  for(unsigned int i=1; i < diffTS.nrow();i++) {    BOOST_CHECK(diffTS.getElement(i,0)==0);    BOOST_CHECK(diffTS.getElement(i,1)==0);    BOOST_CHECK(diffTS.getElement(i,2)==0);    BOOST_CHECK(diffTS.getElement(i,3)==0);  }  // fill functions  // this is all NA  // so filled it should be all equal to the fill value  naTS.initConst(NAN);  tseries<time_t> naFilled(naTS);  naFilled.fillval(10.0);  for(unsigned int i=0; i < naFilled.nrow(); i++)    for(unsigned int j=0; j < naFilled.ncol(); j++)      BOOST_CHECK(naFilled.getElement(i,j)==10.0);  constTS.initConst(100.0);  // init row 4 to a number  constTS.setElement(3,0,2.0);  constTS.setElement(3,1,2.0);  constTS.setElement(3,2,2.0);  constTS.setElement(3,3,2.0);	  // init Row 5 to NAs  constTS.setElement(4,0,NAN);  constTS.setElement(4,1,NAN);  constTS.setElement(4,2,NAN);  constTS.setElement(4,3,NAN);	  // test that 2.0 is carried forward  constTS.fillf();  BOOST_CHECK(constTS.getElement(4,0)==2.0);  BOOST_CHECK(constTS.getElement(4,1)==2.0);  BOOST_CHECK(constTS.getElement(4,2)==2.0);  BOOST_CHECK(constTS.getElement(4,3)==2.0);  constTS.initConst(100.0);  // init row 4 to a number  constTS.setElement(3,0,5.0);  constTS.setElement(3,1,5.0);  constTS.setElement(3,2,5.0);  constTS.setElement(3,3,5.0);	  // init Row 3 to NAs  constTS.setElement(2,0,NAN);  constTS.setElement(2,1,NAN);  constTS.setElement(2,2,NAN);  constTS.setElement(2,3,NAN);	  // test that 5.0 is carried backwards  constTS.fillb();  BOOST_CHECK(constTS.getElement(2,0)==5.0);  BOOST_CHECK(constTS.getElement(2,1)==5.0);  BOOST_CHECK(constTS.getElement(2,2)==5.0);  BOOST_CHECK(constTS.getElement(2,3)==5.0);  // init to all NA and make sure that Fill  // forward and bwd do not set any values  naTS.initConst(NAN);  tseries<time_t> naFillBwd(naTS);  tseries<time_t> naFillFwd(naTS);  naFillBwd.fillb();  naFillFwd.fillf();  // test all rows and all cols  for(unsigned int i=0; i < naTS.nrow(); i++)    for(unsigned int j=0; j < naTS.ncol(); j++) {      BOOST_CHECK(isnan(naFillBwd.getElement(i,j)));      BOOST_CHECK(isnan(naFillFwd.getElement(i,j)));    }	  unsigned int testrows = 50;  unsigned int anaPer = 10;  tseries<time_t> ana1(testrows,1);  ana1.fakeDates();  //ana1.print();  // init these to some random values  for(unsigned int i=0; i < ana1.nrow();i++) {    srand ( i );    ana1.setElement(i,0,rand());    //ana1.setElement(i,0,(double)i);  }     // analog of a series against itself  // last value should be 1.00  tseries<time_t> ana_ans = analog(ana1,ana1,anaPer);	  BOOST_CHECK(roundf(ana_ans.getElement(ana_ans.nrow()-1,0))==1.00); 	  // now lag it by 1  ana_ans = analog(ana1.lag(1),ana1,anaPer);  //BOOST_CHECK(fround(ana_ans.getElement(ana_ans.nrow()-2,0),3)==1.00);  BOOST_CHECK(roundf(ana_ans.getElement(ana_ans.nrow()-2,0))==1.00);  // make sure that values 1..49 are NA  for(unsigned int i=0; i<(anaPer-1); i++)    BOOST_CHECK(isnan(ana_ans.getElement(i,0)));	    tseries<time_t> left(10, 1, 100.0);  tseries<time_t> right(20, 1, 5.0);  left.fakeDates();  right.fakeDates();    tseries<time_t> add_ans = left + right;  tseries<time_t> sub_ans = left - right;  tseries<time_t> mul_ans = left * right;  tseries<time_t> div_ans = left / right;  // make sure it has the right number of rows  BOOST_CHECK(add_ans.nrow()==10);  BOOST_CHECK(sub_ans.nrow()==10);  BOOST_CHECK(mul_ans.nrow()==10);  BOOST_CHECK(div_ans.nrow()==10);  for(unsigned int i=0; i < left.nrow(); i++) {    BOOST_CHECK(add_ans.getElement(i,0)==105.0);    BOOST_CHECK(sub_ans.getElement(i,0)==95.0);    BOOST_CHECK(mul_ans.getElement(i,0)==500.0);    BOOST_CHECK(div_ans.getElement(i,0)==20.0);  }    // test for column access  tseries<time_t> onecol(10,1,100.0);	  double *ele = onecol.getCol(0);  BOOST_CHECK(*ele==100);    // trim test  tseries<time_t> trimTS(10,1);  for(unsigned int r=0; r < trimTS.nrow(); r++) {    trimTS.setElement(r,0,static_cast<double>(r));  }  trimTS.fakeDates();  tseries<time_t> origTS = trimTS;  vector<time_t> newDts;  // these are rows 1, 3, and 5 [0 indexed]  newDts.push_back(1*86400-19*3600);  newDts.push_back(3*86400-19*3600);  newDts.push_back(5*86400-19*3600);  trimTS.trim(newDts);  BOOST_CHECK(trimTS.nrow()==3);  time_t *trimDts = trimTS.getDates();  BOOST_CHECK(trimDts[0]==newDts[0]);  BOOST_CHECK(trimDts[1]==newDts[1]);  BOOST_CHECK(trimDts[2]==newDts[2]);  BOOST_CHECK(origTS.getElement(1,0)==trimTS.getElement(0,0));  BOOST_CHECK(origTS.getElement(3,0)==trimTS.getElement(1,0));  BOOST_CHECK(origTS.getElement(5,0)==trimTS.getElement(2,0));  /*    printDate(newDts[0],"%Y %m %d");    cout <<  endl;    printDate(newDts[1],"%Y %m %d");    cout <<  endl;    printDate(newDts[2],"%Y %m %d");    cout <<  endl;  */  //trimTS.print();  //cout << "*****" << endl;  //origTS.print();}void read_write_test() {  tseries<time_t> x(10,10,100.0);  x.fakeDates();  vector<string> cnms;  for(unsigned int i = 0; i < x.ncol(); i++) {    cnms.push_back("cat");  }  x.setColNames(cnms);  //x.print();  x.writecsv("test.csv","%Y-%m-%d");  tseries<time_t> readCSV2;  readCSV2.csv2tseries("test.csv","%Y-%m-%d");  BOOST_CHECK((readCSV2==x).all());  //readCSV2.print();  x.write("test.ts");  tseries<time_t> y;  y.read_tseries("test.ts");  //tseries<time_t> cat = read_tseries("/home/data/EURUSD.ts");  //cat.print();  //y.print();  BOOST_CHECK((x==y).all());  vector<string> xnms, ynms;  xnms = x.getColNames();  ynms = y.getColNames();  for(unsigned int c = 0; c < x.ncol(); c++) {    BOOST_CHECK(xnms[c]==ynms[c]);    //cout << xnms[c] << endl;    //cout << ynms[c] << endl;  }	}void arith_tests() {  tseries<time_t> hats(100,1,100.0);  tseries<time_t> cats(50,1,20.0);  tseries<time_t> nullts;  //tseries<time_t> null_test = hats + nullts;  //BOOST_CHECK(null_test.isnull());  tseries<time_t> add_test = hats + cats;  BOOST_CHECK(add_test.nrow()==cats.nrow());  BOOST_CHECK((add_test==120.0).all());		  //clock_t ticks = clock();  for(int i = 0; i < 100000; i++)    add_test = hats+cats;  //cout << "time: " << (clock() - ticks) << endl;  tseries<time_t> sub_test = hats - cats;  BOOST_CHECK(sub_test.nrow()==cats.nrow());  BOOST_CHECK((sub_test==80.0).all());  tseries<time_t> mul_test = hats * cats;  BOOST_CHECK(mul_test.nrow()==cats.nrow());  BOOST_CHECK((mul_test==100.0*20.0).all());  tseries<time_t> div_test = hats / cats;  BOOST_CHECK(div_test.nrow()==cats.nrow());  BOOST_CHECK((div_test==5).all());  tseries<time_t> const_ts(100,1,5.0);  // test for operator=  const_ts = 10.0;  BOOST_CHECK((const_ts==10.0).all());	}void comparison_tests() {  tseries<time_t> x(10,1,2.0);;  tseries<time_t> y(5,1,1.0);;  mask m = x > y;  //m.print();  BOOST_CHECK(m.size()==5);  BOOST_CHECK((x > y).all());  BOOST_CHECK((y < x).all());  tseries<time_t> xx(2,1);  tseries<time_t> yy(2,1);  xx.setElement(0,0,1.0);  xx.setElement(1,0,2.0);  xx.fakeDates();  yy.setElement(0,0,1.0);  yy.setElement(1,0,1.0);  yy.fakeDates();  BOOST_CHECK(!(xx==yy).all());	}void string_tests() {  string s1("hat;cat;sat");  vector<string> sv1 = splitString(s1,';');  BOOST_CHECK(sv1.size()==3);  BOOST_CHECK(sv1[0]=="hat");  BOOST_CHECK(sv1[1]=="cat");  BOOST_CHECK(sv1[2]=="sat");  string s2("hat");  vector<string> sv2 =  splitString(s2,';');  BOOST_CHECK(sv2.size()==1);  BOOST_CHECK(sv2[0]=="hat");	  string s3("hat;");  vector<string> sv3 =  splitString(s3,';');  BOOST_CHECK(sv3.size()==2);  BOOST_CHECK(sv3[0]=="hat");  BOOST_CHECK(sv3[1]=="");}void subscript_tests() {	  tseries<time_t> cat(100,1);  cat.fakeData();  cat.fakeDates();  // cat.print();  mask m = cat >= 50;  //m.print();  tseries<time_t> cat50 = cat[m];  BOOST_CHECK(cat50.nrow()==50);  BOOST_CHECK(cat50.ncol()==1);  BOOST_CHECK((cat50 >= 50).all());}void RegressionTest() {  tseries<time_t> x(10000,1);  tseries<time_t> y(10000,1);  x.fakeDates();  y.fakeDates();  x.randomize();  y.randomize();  //x.print();  /*  double* b = linear_model(x,y);  delete []b;  */}	test_suite*init_unit_test_suite( int argc, char* argv[] ) {  test_suite* test= BOOST_TEST_SUITE("tslib test");  test->add( BOOST_TEST_CASE( &null_constructor_test ) );  test->add( BOOST_TEST_CASE( &external_data_constructor_valid ) );  test->add( BOOST_TEST_CASE( &external_data_constructor_invalid ) );  test->add( BOOST_TEST_CASE( &cpy_constructor_test ) );  test->add( BOOST_TEST_CASE( &cbind_test ) );  test->add( BOOST_TEST_CASE( &big_test ) );  test->add( BOOST_TEST_CASE( &arith_tests ) );  test->add( BOOST_TEST_CASE( &read_write_test ) );  test->add( BOOST_TEST_CASE( &comparison_tests) );  test->add( BOOST_TEST_CASE( &string_tests ) );  test->add( BOOST_TEST_CASE( &subscript_tests ) );  //test->add( BOOST_TEST_CASE( &RegressionTest ) );  return test;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -