FL::ParseResult AB::analyze( const TimeSeries &ts, Forest &forest, Patterns::Matcher &matcher, PatternsSet &patterns, MetricsSet &metrics) { ParseResult result; try { if (ts.size() < 2) throw EAnalyze(E_INVALID_INPUT); Tree *tree = new Tree(ts); const int up = IDGenerator::idOf("a"); const int down = IDGenerator::idOf("b"); for (int i = 0; i < ts.size()-1; i += 1) { const int &id = (ts.value(i) <= ts.value(i+1)) ? up : down; tree->add(new Node(NULL, id, i, i+1, 0)); } forest.push_back(tree); result.treesAdded = 1; result.nodesAdded = (ts.size() + 1) / 2; } catch (const EAnalyze &e) { m_lastError = e; } return result; }
template <typename TimeSeries> typename TimeSeries::value_type empirical_average(TimeSeries const &t) { auto si = t.size(); if (si == 0) return typename TimeSeries::value_type{}; auto sum = t[0]; for (int i = 1; i < si; ++i) sum += t[i]; return sum / t.size(); }
template <typename TimeSeries> typename TimeSeries::value_type empirical_variance(TimeSeries const &t) { auto si = t.size(); if (si == 0) return typename TimeSeries::value_type{}; auto avg = empirical_average(t); decltype(avg) sum2 = (t[0] - avg) * (t[0] - avg); // also valid if t[0] is an array e.g., i.e. no trivial contructor... for (int i = 1; i < si; ++i) sum2 += (t[i] - avg) * (t[i] - avg); return sum2 / t.size(); }
binned_series(TimeSeries const& t, int bin_size_) : bin_size(bin_size_), binned(t.size() / bin_size_, value_type{}) { if (bin_size_ > t.size()) TRIQS_RUNTIME_ERROR << "bin size (" << bin_size_ << ") cannot be larger than size (" << t.size() << ") of time series"; for (int i = 0; i < size(); i++) { for (int j = 0; j < bin_size; j++) binned[i] += t[i * bin_size + j]; binned[i] /= bin_size; } }
Datum blocker(const RealMatrix& Ua, const RealMatrix sa, const vGroup& ensemble, const uint blocksize, uint repeats, ExtractPolicy& policy) { TimeSeries<double> coverlaps; for (uint i=0; i<repeats; ++i) { vector<uint> picks = pickFrames(ensemble.size(), blocksize); if (debug) { cerr << "***Block " << blocksize << ", replica " << i << ", picks " << picks.size() << endl; dumpPicks(picks); } vGroup subset = subgroup(ensemble, picks); boost::tuple<RealMatrix, RealMatrix> pca_result = pca(subset, policy); RealMatrix s = boost::get<0>(pca_result); RealMatrix U = boost::get<1>(pca_result); if (length_normalize) for (uint j=0; j<s.rows(); ++j) s[j] /= blocksize; coverlaps.push_back(covarianceOverlap(sa, Ua, s, U)); } return( Datum(coverlaps.average(), coverlaps.variance(), coverlaps.size()) ); }
MOTIF_BEGIN inline double EuclideanDistance(TimeSeries &t1, TimeSeries &t2, string name) { double dist = 0.0; if(t1.size() != t2.size()) { cout << t1.size() << "," << t2.size() << endl; cout << "Called by " << name << endl; system("pause"); } for(size_t i = 0; i < t1.size(); ++i) { dist += pow(t1[i] - t2[i], 2.0); } return sqrt(dist); }
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; }
BIGDECIMAL maxval::operator() (TimeSeries& timeSeries) { int size = timeSeries.size(); BIGDECIMAL currentMax = -10000000000000.0; for(int i = 0 ; i < size ; i++ ) { if(timeSeries[i].val() > currentMax) { currentMax = timeSeries[i].val(); } } return currentMax ; }
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; }
bool FileCSV::save(const std::string &fileName, TimeSeries &ts) { std::ofstream file(fileName.c_str()); if (!file.is_open()) return false; // Write header for (unsigned int i = 0; i < ts.header().size(); ++i) { file << ts.header()[i]; if (i < ts.header().size() - 1) file << m_separator; } // Write data for (int row = 0; row < ts.size(); ++row) { file << std::endl << ts.time(row) << m_separator << ts.value(row); } file << std::endl; file.close(); return true; }
int size() const { return ts.size(); }
double NSSKF::logp(const TimeSeries<DoubleData> & ts)const{ initialize(); double ans = 0; for(uint i=0; i<ts.size(); ++i) ans += update(ts[i]->value()); return ans; }