6namespace slope::detail {
9uniformObservationWeights(
const Eigen::VectorXd& weights)
11 return weights.size() == 0 ||
12 (weights(0) > 0.0 && (weights.array() == weights(0)).all());
17normalizeObservationWeights(
const Eigen::VectorXd& weights, Eigen::Index n)
20 throw std::invalid_argument(
"Observation weights require at least one row");
22 if (weights.size() == 0) {
23 return Eigen::VectorXd::Ones(n);
25 if (weights.size() != n) {
26 throw std::invalid_argument(
27 "Observation weights must have one entry per row");
29 if (!weights.allFinite() || (weights.array() < 0.0).any()) {
30 throw std::invalid_argument(
31 "Observation weights must be finite and nonnegative");
33 if (weights.maxCoeff() == 0.0) {
34 throw std::invalid_argument(
35 "Observation weights must have positive total weight");
37 if (uniformObservationWeights(weights)) {
38 return Eigen::VectorXd::Ones(n);
41 Eigen::VectorXd normalized = weights / weights.maxCoeff();
42 normalized *=
static_cast<double>(n) / normalized.sum();
47positiveWeightCount(
const Eigen::VectorXd& weights)
49 return (weights.array() > 0.0).count();
53maskZeroWeightRows(Eigen::MatrixXd& values,
const Eigen::VectorXd& weights)
55 for (Eigen::Index i = 0; i < weights.size(); ++i) {
56 if (weights(i) == 0.0) {
57 values.row(i).setZero();
62inline Eigen::RowVectorXd
63weightedMean(
const Eigen::MatrixXd& values,
const Eigen::VectorXd& weights)
65 Eigen::RowVectorXd mean = Eigen::RowVectorXd::Zero(values.cols());
66 const double total = weights.sum();
67 for (Eigen::Index i = 0; i < values.rows(); ++i) {
68 if (weights(i) > 0.0) {
69 mean += (weights(i) / total) * values.row(i);