4#include "./time_index.hpp"
58 return "union_unobserved";
60 return "intersection";
62 return "forward_fill";
83 std::size_t
rows() const noexcept {
92 return instruments_.size();
134 if (instruments_[
column] == instrument) {
148 if (position >= instruments_.size()) {
149 throw std::out_of_range(
"panel column is past the end");
151 return values_.col(
static_cast<math::Index>(position));
163 throw std::out_of_range(
"panel position is past the end");
187 panel.field_ =
field;
189 if (series.empty()) {
193 std::vector<SeriesFrame> frames;
194 frames.reserve(series.size());
195 for (
const auto& one : series) {
197 panel.instruments_.push_back(one.instrument_);
201 panel.index_ =
TimeIndex(std::move(stamps));
203 panel.values_ = math::Matrix::Constant(
215 static std::vector<TimeIndex::Instant> combined_instants(
const std::vector<SeriesFrame>& frames) {
216 std::size_t total = 0;
217 for (
const auto& frame : frames) {
218 total += frame.time().size();
220 std::vector<TimeIndex::Instant> all;
222 for (
const auto& frame : frames) {
223 const auto& stamps = frame.time().stamps();
224 all.insert(all.end(), stamps.begin(), stamps.end());
226 std::sort(all.begin(), all.end());
227 all.erase(std::unique(all.begin(), all.end()), all.end());
232 static std::vector<TimeIndex::Instant> shared_instants(
const std::vector<SeriesFrame>& frames) {
233 std::vector<TimeIndex::Instant> shared = frames.front().time().stamps();
234 std::vector<TimeIndex::Instant> narrowed;
235 narrowed.reserve(shared.size());
236 for (std::size_t position = 1; position < frames.size(); ++position) {
237 const auto& other = frames[position].time().stamps();
239 std::set_intersection(shared.begin(), shared.end(), other.begin(), other.end(), std::back_inserter(narrowed));
240 shared.swap(narrowed);
247 static void fill_column(
250 const auto& target = panel.index_.stamps();
251 const auto& source = frame.time().stamps();
252 if (target.empty() || source.empty()) {
255 const auto series = frame.column(
field);
259 for (std::size_t row = 0; row < target.size(); ++row) {
260 const auto& when = target[row];
261 while (
at + 1 < source.size() && !(when < source[
at + 1])) {
267 if (when < source[
at]) {
270 if (!carry && source[
at] != when) {
278 std::vector<InstrumentId> instruments_;
math::SeriesView column(std::size_t position) const
Returns one instrument's column.
std::size_t columns() const noexcept
Returns the number of columns.
Scalar at(std::size_t row, std::size_t column) const
Reads one value.
Panel()=default
Constructs an empty panel.
const TimeIndex & time() const noexcept
Returns the instants of the rows.
std::optional< std::size_t > find(const InstrumentId &instrument) const
Finds an instrument's column.
static Panel from_bars(const std::vector< BarSeries > &series, BarField field, Alignment alignment)
Builds a panel of one bar field from several bar series.
math::MatrixView values() const
Returns all values.
Alignment alignment() const noexcept
Returns how the panel lined up its instruments.
const std::vector< InstrumentId > & instruments() const noexcept
Returns the instruments of the columns.
BarField field() const noexcept
Returns the bar field the panel holds.
std::size_t rows() const noexcept
Returns the number of rows.
The instants a frame's rows correspond to, strictly ascending.
TimeIndex()=default
Constructs an empty index.
std::size_t size() const noexcept
Returns the number of instants.
The linear algebra types of the math module: Matrix, Vector, SeriesView, MutableSeriesView,...
Series buffers and frames, the conversions to and from exact decimals, and performance metrics.
BarField
A bar field, used as a column of a SeriesFrame.
constexpr std::string_view alignment_name(Alignment alignment) noexcept
Returns a lowercase display name for an alignment, such as "intersection".
Scalar missing()
Returns the value that stands for "no observation"; see math::missing.
SeriesFrame from_bars(const BarSeries &series)
Converts a bar series into a frame of Scalar columns.
Alignment
How Panel::from_bars lines up instruments whose bars fall on different instants.
@ FORWARD_FILL
Every instant any instrument reported; a gap repeats the instrument's last value.
@ INTERSECTION
Only the instants every instrument reported.
@ UNION_UNOBSERVED
Every instant any instrument reported; an instrument with no bar there is missing().
double Scalar
The floating-point type the analytics and math layers compute in (double).
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > Matrix
A dense, column-major matrix of Scalar.
Eigen::Index Index
The signed index type of rows and columns.
Eigen::Ref< const Matrix > MatrixView
A read-only reference to a whole matrix.
Eigen::Ref< const Vector > SeriesView
A read-only reference to one series.
The library: broker-neutral types (core), and the modules below.
Identifies an instrument: its country, venue and symbol.