Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions include/DataFrame/DataFrameFinancialVisitors.h
Original file line number Diff line number Diff line change
Expand Up @@ -1584,11 +1584,11 @@ struct RVIVisitor {
}
else {
result.clear();
result.reserve(col_s);
result.resize(col_s);
for (size_type i { 0 }; i < col_s; ++i) [[likely]]
result.push_back(
result[i] =
(T(100) * ewm_pos_.get_result()[i]) /
(ewm_pos_.get_result()[i] + ewm_neg_.get_result()[i]));
(ewm_pos_.get_result()[i] + ewm_neg_.get_result()[i]);
}
}

Expand Down Expand Up @@ -2108,16 +2108,16 @@ struct DrawdownVisitor {
for (auto &fut : futures) fut.get();
}
else {
drawdown_.reserve(col_s);
pct_drawdown_.reserve(col_s);
log_drawdown_.reserve(col_s);
drawdown_.resize(col_s);
pct_drawdown_.resize(col_s);
log_drawdown_.resize(col_s);
for (size_type i { 0 }; i < col_s; ++i) [[likely]] {
const value_type col_val { *(column_begin + i) };
const value_type cm_val { cm_result[i] };

drawdown_.push_back(cm_val - col_val);
pct_drawdown_.push_back(one - col_val / cm_val);
log_drawdown_.push_back(std::log(cm_val / col_val));
drawdown_[i] = cm_val - col_val;
pct_drawdown_[i] = one - col_val / cm_val;
log_drawdown_[i] = std::log(cm_val / col_val);
}
}
}
Expand Down Expand Up @@ -2299,12 +2299,11 @@ struct PSLVisitor {
for (auto &fut : futures) fut.get();
}
else {
result_.reserve(col_s);
result_.push_back(0);
result_.resize(col_s, 0);
for (size_type i { 1 }; i < col_s; ++i) [[likely]]
result_.push_back(
result_[i] =
(*(close_begin + i) - *(close_begin + (i - 1)) > 0)
? 1 : 0);
? 1 : 0;
}
calculate_(idx_begin, idx_end);
}
Expand Down Expand Up @@ -2453,11 +2452,11 @@ struct CCIVisitor {
for (auto &fut : futures) fut.get();
}
else {
result.reserve(col_s);
result.resize(col_s);
for (size_type i { 0 }; i < col_s; ++i) [[likely]]
result.push_back((*(low_begin + i) +
*(high_begin + i) +
*(close_begin + i)) / T(3));
result[i] = (*(low_begin + i) +
*(high_begin + i) +
*(close_begin + i)) / T(3);
}

SimpleRollAdopter<MeanVisitor<T, I>, T, I, A> avg_v {
Expand Down
21 changes: 10 additions & 11 deletions include/DataFrame/DataFrameMLVisitors.h
Original file line number Diff line number Diff line change
Expand Up @@ -3927,7 +3927,7 @@ struct ARIMAVisitor {
result_type diffed = y_;
result_type preds;

preds.reserve(periods_);
preds.resize(periods_);
for (long t { 0 }; t < periods_; ++t) {
value_type ar_part { 0 };
value_type ma_part { 0 };
Expand All @@ -3940,7 +3940,7 @@ struct ARIMAVisitor {
//
const value_type pred { ar_part + ma_part};

preds.push_back(pred);
preds[t] = pred;
diffed.push_back(pred);
}

Expand Down Expand Up @@ -4143,10 +4143,10 @@ struct ARIMAVisitor {
const size_type diff_s = std::distance(diffed_begin, diffed_end);
result_type inverted;

inverted.reserve(diff_s);
inverted.resize(diff_s);
for (size_type i { 0 }; i < diff_s; ++i) {
last_data += *(diffed_begin + i);
inverted.push_back(last_data);
inverted[i] = last_data;
}

return (inverted);
Expand Down Expand Up @@ -5004,16 +5004,15 @@ struct LSTMForecastVisitor {
matrix_t h { batch, H, 0 }, c { batch, H, 0 };
std::vector<matrix_t> outs;

caches.clear();
caches.reserve(time_steps);
outs.reserve(time_steps);
caches.resize(time_steps);
outs.resize(time_steps);
for (long t { 0 }; t < time_steps; ++t) {
const auto cache { cell.forward(X[t], h, c) };

h = cache.h;
c = cache.c;
caches.push_back(cache);
outs.push_back(h);
caches[t] = cache;
outs[t] = h;
}

h_last = h;
Expand Down Expand Up @@ -5156,7 +5155,7 @@ struct LSTMForecastVisitor {
batch_size_, input_size_, norm_data[long(col_s) - seq_len_ + t]
};

result_.reserve(periods_);
result_.resize(periods_);
for (long step { 0 }; step < periods_; ++step) {
const std::vector<matrix_t> outputs = lstm.forward(sequence);
const matrix_t next_pred {
Expand All @@ -5166,7 +5165,7 @@ struct LSTMForecastVisitor {

// Denormalize prediction
//
result_.push_back(norm_pred * stdv.get_result() + stdv.get_mean());
result_[step] = norm_pred * stdv.get_result() + stdv.get_mean();

// Feed predicted value as next input
//
Expand Down
62 changes: 30 additions & 32 deletions include/DataFrame/DataFrameStatsVisitors.h
Original file line number Diff line number Diff line change
Expand Up @@ -3019,7 +3019,7 @@ struct FixedAutoCorrVisitor {
if (policy_ == roll_policy::blocks) {
const size_type calc_size { col_s / lag_ };

result.reserve(calc_size);
result.resize(calc_size);
for (size_type i = 0; i < calc_size; ++i) {
auto far_end = i * lag_ + 2 * lag_;
auto near_end = i * lag_ + lag_;
Expand All @@ -3040,13 +3040,13 @@ struct FixedAutoCorrVisitor {
column_begin + far_end);
corr.post();

result.push_back(corr.get_result());
result[i] = corr.get_result();
}
}
else { // roll_policy::continuous
const size_type calc_size { col_s - lag_ };

result.reserve(calc_size);
result.resize(calc_size);
for (size_type i = 0; i < calc_size; ++i) {
auto far_end = i + 2 * lag_;
auto near_end = i + lag_;
Expand All @@ -3066,7 +3066,7 @@ struct FixedAutoCorrVisitor {
column_begin + far_end);
corr.post();

result.push_back(corr.get_result());
result[i] = corr.get_result();
}

}
Expand Down Expand Up @@ -4328,10 +4328,10 @@ struct MADVisitor {
MedianVisitor<T, I, A> median_mean_visitor;
vec_t mean_dists;

mean_dists.reserve(col_s);
mean_dists.resize(col_s);
for (std::size_t i = 0; i < col_s; ++i) [[likely]]
mean_dists.push_back(
std::fabs(*(column_begin + i) - mean_visitor.get_result()));
mean_dists[i] =
std::fabs(*(column_begin + i) - mean_visitor.get_result());
median_mean_visitor.pre();
median_mean_visitor(idx_begin, idx_end,
mean_dists.begin(), mean_dists.end());
Expand Down Expand Up @@ -4360,10 +4360,10 @@ struct MADVisitor {
MedianVisitor<T, I, A> median_median_visitor;
vec_t median_dists;

median_dists.reserve(col_s);
median_dists.resize(col_s);
for (std::size_t i = 0; i < col_s; ++i) [[likely]]
median_dists.push_back(
std::fabs(*(column_begin + i) - median_visitor.get_result()));
median_dists[i] =
std::fabs(*(column_begin + i) - median_visitor.get_result());
median_median_visitor.pre();
median_median_visitor(idx_begin, idx_end,
median_dists.begin(), median_dists.end());
Expand Down Expand Up @@ -5855,13 +5855,13 @@ struct PolyFitVisitor {
coeffs_[i] = coeffs_[i] / eq_mat[index_(i, i, nrows)];
}

y_fits_.reserve(col_s);
y_fits_.resize(col_s);
for (size_type i = 0; i < col_s; ++i) [[likely]] {
value_type pred = 0;

for (size_type j = 0; j < deg; ++j)
pred += coeffs_[j] * std::pow(*(x_begin + i), j);
y_fits_.push_back(pred);
y_fits_[i] = pred;

const value_type w = weights_(*(idx_begin + i), i);

Expand Down Expand Up @@ -6726,9 +6726,9 @@ struct CubicSplineFitVisitor {
for (auto &fut : futures) fut.get();
}
else {
h.reserve(col_s - 1);
h.resize(col_s - 1);
for(size_type i = 0; i < col_s - 1; ++i) [[likely]]
h.push_back (*(x_begin + (i + 1)) - *(x_begin + i));
h[i] = *(x_begin + (i + 1)) - *(x_begin + i);
}

result_type mu (col_s, 0);
Expand Down Expand Up @@ -7150,7 +7150,6 @@ struct LowessVisitor {
dist_i_j_.clear();
x_j_.reserve(right_end - left_end);
dist_i_j_.reserve(right_end - left_end);

for (size_type j = left_end; j < right_end; ++j) [[likely]] {
x_j_.push_back(*(x_begin + j));
dist_i_j_.push_back(std::fabs(*(x_begin + j) - xval) / radius);
Expand Down Expand Up @@ -7943,12 +7942,12 @@ struct NonZeroRangeVisitor {
}
}
else {
result.reserve(col_s);
result.resize(col_s);
for (size_type i = 0; i < col_s; ++i) [[likely]] {
const value_type v =
*(column1_begin + i) - *(column2_begin + i);

result.push_back(v);
result[i] = v;
if (v == 0) there_is_zero = true;
}
if (there_is_zero)
Expand Down Expand Up @@ -9135,10 +9134,10 @@ struct DivideToBinsVisitor {
const auto adjusted_max { *max_val + range_extension };
const auto bin_width = (adjusted_max - adjusted_min) / T(bins_);

bins_list_.reserve(bins_ + 1);
bins_list_.resize(bins_ + 1);
for (size_type i { 0 }; i <= bins_; ++i)
bins_list_.push_back(
value_type(size_type(adjusted_min + T(i) * bin_width)));
bins_list_[i] =
value_type(size_type(adjusted_min + T(i) * bin_width));
}

const bool labels { ! input_lbls_.empty() };
Expand Down Expand Up @@ -9305,8 +9304,8 @@ struct DivideToQuantilesVisitor {

std::vector<value_type> edges;

result_.reserve(col_s);
if (labels) labels_.reserve(col_s);
result_.resize(col_s);
if (labels) labels_.resize(col_s);
if (quantiles_ > 0) { // Number of Q's
#ifdef HMDF_SANITY_EXCEPTIONS
if (quantiles_ < 2)
Expand All @@ -9318,10 +9317,9 @@ struct DivideToQuantilesVisitor {
"of labels");
#endif // HMDF_SANITY_EXCEPTIONS

edges.reserve(quantiles_ + 1);
edges.resize(quantiles_ + 1);
for (size_type i { 0 }; i <= quantiles_; ++i)
edges.push_back(
percentile_(sorted, T(i) / T(quantiles_), col_s));
edges[i] = percentile_(sorted, T(i) / T(quantiles_), col_s);

for (size_type i { 0 }; i < col_s; ++i) {
const auto val = *(column_begin + i);
Expand All @@ -9331,8 +9329,8 @@ struct DivideToQuantilesVisitor {

if (bin == quantiles_)
bin = quantiles_ - 1;
result_.emplace_back(edges[bin], edges[bin + 1]);
if (labels) labels_.push_back(input_lbls_[bin]);
result_[i] = { edges[bin], edges[bin + 1] };
if (labels) labels_[i] = input_lbls_[bin];
}
}
else { // Explicit quantiles
Expand All @@ -9343,9 +9341,9 @@ struct DivideToQuantilesVisitor {
"of quantiles - 1");
#endif // HMDF_SANITY_EXCEPTIONS

edges.reserve(quantiles_list_.size());
for (const auto q : quantiles_list_)
edges.push_back(percentile_(sorted, q, col_s));
edges.resize(quantiles_list_.size());
for (size_type i { 0 }; const auto q : quantiles_list_)
edges[i++] = percentile_(sorted, q, col_s);

for (size_type i { 0 }; i < col_s; ++i) {
const auto val = *(column_begin + i);
Expand All @@ -9355,8 +9353,8 @@ struct DivideToQuantilesVisitor {

if (bin == quantiles_list_.size() - 1)
bin = quantiles_list_.size() - 2;
result_.emplace_back(edges[bin], edges[bin + 1]);
if (labels) labels_.push_back(input_lbls_[bin]);
result_[i] = { edges[bin], edges[bin + 1] };
if (labels) labels_[i] = input_lbls_[bin];
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions include/DataFrame/Utils/Matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,26 @@ class Matrix {
unsigned int seed = static_cast<unsigned int>(-1))
requires std::floating_point<T>;

//
// Friends
//

template<typename TT, matrix_orient MO1, matrix_orient MO2,
bool IS_SYM1, bool IS_SYM2>
friend typename std::conditional<IS_SYM1 && IS_SYM2,
Matrix<TT, MO1, true>,
Matrix<TT, MO1, false>>::type
operator + (const Matrix<TT, MO1, IS_SYM1> &lhs,
const Matrix<TT, MO2, IS_SYM2> &rhs);

template<typename TT, matrix_orient MO1, matrix_orient MO2,
bool IS_SYM1, bool IS_SYM2>
friend typename std::conditional<IS_SYM1 && IS_SYM2,
Matrix<TT, MO1, true>,
Matrix<TT, MO1, false>>::type
operator - (const Matrix<TT, MO1, IS_SYM1> &lhs,
const Matrix<TT, MO2, IS_SYM2> &rhs);

private:

static constexpr size_type NOPOS_ = static_cast<size_type>(-9);
Expand Down
Loading