history_iterators.cpp

This code exemplifies how to use History iterators to perform gaussianstatistics analyses on historical data.
// initialize a History History h(...); // print out the mean value and its standard deviation. GaussianStatistics s; s.addSequence(h.vdbegin(),h.vdend()); cout << "Historical mean: " << s.mean() << endl; cout << "Std. deviation: " << s.standardDeviation() << endl; // Another possibility: print out the maximum value. History::const_valid_iterator max = h.vbegin(), i=max, end = h.vend(); for (i++; i!=end; i++) if (i->value() > max->value()) max = i; cout << "Maximum value: " << max->value() << " assumed " << DateFormatter::toString(max->date()) << endl; // or the minimum, this time the STL way: bool lessthan(const History::Entry& i, const History::Entry& j) { return i.value() < j.value(); } History::const_valid_iterator min = std::min_element(h.vbegin(),h.vend(),lessthan); cout << "Minimum value: " << min->value() << " assumed " << DateFormatter::toString(min->date()) << endl;
00001 00002 // initialize a History 00003 History h(...); 00004 00005 // print out the mean value and its standard deviation. 00006 00007 GaussianStatistics s; 00008 s.addSequence(h.vdbegin(),h.vdend()); 00009 cout << "Historical mean: " << s.mean() << endl; 00010 cout << "Std. deviation: " << s.standardDeviation() << endl; 00011 00012 // Another possibility: print out the maximum value. 00013 00014 History::const_valid_iterator max = h.vbegin(), i=max, end = h.vend(); 00015 for (i++; i!=end; i++) 00016 if (i->value() > max->value()) 00017 max = i; 00018 cout << "Maximum value: " << max->value() 00019 << " assumed " << DateFormatter::toString(max->date()) << endl; 00020 00021 // or the minimum, this time the STL way: 00022 00023 bool lessthan(const History::Entry& i, const History::Entry& j) { 00024 return i.value() < j.value(); 00025 } 00026 00027 History::const_valid_iterator min = 00028 std::min_element(h.vbegin(),h.vend(),lessthan); 00029 cout << "Minimum value: " << min->value() 00030 << " assumed " << DateFormatter::toString(min->date()) << endl; 00031 00032

QuantLib.org
QuantLib
Hosted by
SourceForge.net Logo
Documentation generated by
doxygen