void TimeSeriesTest::testConstruction() { BOOST_MESSAGE("Testing time series construction..."); TimeSeries<Real> ts; ts[Date(25, March, 2005)] = 1.2; ts[Date(29, March, 2005)] = 2.3; ts[Date(15, March, 2005)] = 0.3; TimeSeries<Real>::const_iterator cur = ts.begin(); if (cur->first != Date(15, March, 2005)) { BOOST_ERROR("date does not match"); } if (cur->second != 0.3) { BOOST_ERROR("value does not match"); } ts[Date(15, March, 2005)] = 4.0; cur = ts.begin(); if (cur->second != 4.0) { BOOST_ERROR("replaced value does not match" << cur->second << "\n"); } ts[Date(15, March, 2005)] = 3.5; if (cur->second != 3.5) { BOOST_ERROR("set value operator not match" << cur->second << "\n"); } }
// Compute HammingDistance between two time series size_t NTuple::HammingDistance( TimeSeries& t1, TimeSeries& t2 ) { size_t h = 0; TimeSeriesIter iter1 = t1.begin(); TimeSeriesIter iter2 = t2.begin(); while( iter1 != t1.end() && iter2 != t2.end() ) { // Accumulate the Hamming distance for each NTuple h += *iter1++ - *iter2++; } return h; }
std::vector<Real> IntervalPrice::extractValues( const TimeSeries<IntervalPrice>& ts, IntervalPrice::Type t) { std::vector<Real> returnval; returnval.reserve(ts.size()); for (TimeSeries<IntervalPrice>::const_iterator i = ts.begin(); i != ts.end(); ++i) { returnval.push_back(i->second.value(t)); } return returnval; }
// Iterate only the k'th variable with respect to the given reference time series size_t DynSysModel::Iterate( TimeSeries& ref_series, size_t k, TimeSeries& result, size_t kov ) { // Make sure the result is empty result.clear(); // Hamming distance for bit k size_t h = 0; TimeSeriesIter t_iter = ref_series.begin(); // Empty series in => empty series out if( t_iter == ref_series.end() ) return h; // Save the initial state as the first state in the result mState = *t_iter; // Force the knockout variable to zero - no longer needed, corrected when file is read in // if( kov != 0 ) mState.Reset(kov); result.push_back( mState ); bool f; while( true ) { // Merge the next state from the time series with the function evaluation // (a) increment iterator and check for end of time series ++t_iter; if( t_iter == ref_series.end() ) break; // Evaluate the k'th polynomial at the current state // to produce a new value for the k'th variable for time i+1 f = mModel[k-1].Evaluate( mState ); // (b) assign value pointed to by iterator to mState (t[i+1]) mState = *t_iter; // Force the knockout variable to zero - no longer needed, corrected when file is read in // if( kov != 0 ) mState.Reset(kov); // Accumulate Hamming Distance = t'[i+1][k] - t[i+1][k] h += (mState[k] ^ f) ? 1 : 0; // (c) create t'[i+1] = t[i+1] with k'th element replaced by f(t'[i]) mState.Assign( k, f ); // Save t'[i+1] result.push_back( mState ); } return h; }
TimeSeries<Volatility> ConstantEstimator::calculate(const TimeSeries<Volatility>& volatilitySeries) { TimeSeries<Volatility> retval; const std::vector<Volatility> u = volatilitySeries.values(); TimeSeries<Volatility>::const_iterator prev, next, cur, start; cur = volatilitySeries.begin(); std::advance(cur, size_); // ICK. This could probably be made a lot more efficient for (Size i=size_; i < volatilitySeries.size(); i++) { Size j; Real sumu2=0.0, sumu=0.0; for (j=i-size_; j <i; j++) { sumu += u[j]; sumu2 += u[j]*u[j]; } Real s = std::sqrt(sumu2/(Real)size_ - sumu*sumu / (Real) size_ / (Real) (size_+1)); retval[cur->first] = s; ++cur; } return retval; }