40#ifndef LIBQE_GENERALIZED_ROTATION_HPP
41#define LIBQE_GENERALIZED_ROTATION_HPP
43#include <libqe/linalg_fallback.hpp>
44#include <libqe/lasso.hpp>
73 const arma::uvec& labels,
76 const arma::uword p = V.n_cols;
77 arma::rowvec mu = arma::mean(V, 0);
78 arma::mat SB(p, p, arma::fill::zeros);
80 for (arma::uword g = 0; g < n_groups; ++g) {
81 arma::uvec idx = arma::find(labels == g);
82 if (idx.is_empty())
continue;
83 arma::vec diff = (arma::mean(V.rows(idx), 0) - mu).t();
84 SB +=
static_cast<double>(idx.n_elem) * diff * diff.t();
125 const arma::vec& target,
127 arma::uword n_groups = 0
133 throw std::runtime_error(
134 "gmr_direction: x_n_groups must be >= 2 for categorical target");
135 arma::uvec labels = arma::conv_to<arma::uvec>::from(target);
138 arma::vec eigval; arma::mat eigvec;
139 qe::linalg::eig_sym(eigval, eigvec, SB);
140 r = eigvec.col(eigvec.n_cols - 1);
143 arma::vec tc = target - arma::mean(target);
144 double tTt = arma::dot(tc, tc);
146 throw std::runtime_error(
147 "gmr_direction: target has zero variance after centering");
148 arma::rowvec beta = (tc.t() * Vx) / tTt;
152 double norm = arma::norm(r);
154 throw std::runtime_error(
"gmr_direction: resulting direction has zero norm");
203 const arma::mat& X_model,
204 const arma::vec& target,
205 const arma::uvec& x1_cols,
207 arma::uword n_groups,
208 const arma::uvec& subset,
211 double lasso_eps = 0.01
213 const arma::uword n = V.n_rows;
214 const arma::uword p = X_model.n_cols;
217 arma::uvec rows = subset.is_empty()
218 ? arma::regspace<arma::uvec>(0, n - 1)
221 const arma::mat V_sub = V.rows(rows);
222 const arma::mat X_sub = X_model.rows(rows);
223 const arma::vec t_sub = target.rows(rows);
227 if (x1_cols.n_elem >= p) {
230 arma::vec tc = t_sub - arma::mean(t_sub);
231 double tTt = arma::dot(tc, tc);
233 arma::rowvec b1 = (tc.t() * V_sub) / tTt;
236 Vx_sub.zeros(V_sub.n_rows, V_sub.n_cols);
239 arma::vec pf(p, arma::fill::ones);
240 for (arma::uword j : x1_cols) pf(j) = 0.0;
242 Vx_sub = lasso_x1_contribution(
243 X_sub, V_sub, x1_cols, pf, n_lambda, k_folds, lasso_eps);
247 arma::vec direction =
gmr_direction(Vx_sub, t_sub, categorical, n_groups);
357 const arma::vec x_vector = x_gmr.
direction;
361 const arma::mat defA = V - V * x_vector * x_vector.t();
375 arma::uvec labels = arma::conv_to<arma::uvec>::from(params.
x_target);
376 arma::uvec g0 = arma::find(labels == 0);
377 arma::uvec g1 = arma::find(labels == 1);
379 if (!g0.is_empty() && !g1.is_empty()) {
380 arma::rowvec m0 = arma::mean(V.rows(g0), 0);
381 arma::rowvec m1 = arma::mean(V.rows(g1), 0);
382 arma::vec diff = (m0 - m1).t();
383 double dlen = arma::norm(diff);
384 if (dlen > 1e-10) x1 = diff / dlen;
391 std::string y_name =
"SVD2";
417 y_vector -= arma::dot(y_vector, x_vector) * x_vector;
419 y_vector -= arma::dot(y_vector, x1) * x1;
421 double yn = arma::norm(y_vector);
423 throw std::runtime_error(
424 "generalized_means_rotation: y_vector has zero norm after orthogonalization");
431 arma::mat defA_final = V
432 - V * x_vector * x_vector.t()
433 - V * y_vector * y_vector.t();
435 arma::mat named_axes = arma::join_rows(x_vector, y_vector);
Definition cxxwrap.hpp:36
RotationResult generalized_means_rotation(const arma::mat &V, const GeneralizedRotationParams ¶ms)
Full Generalized Means Rotation (GMR) producing a complete q×q rotation matrix.
Definition generalized_rotation.hpp:340
arma::mat between_group_scatter(const arma::mat &V, const arma::uvec &labels, arma::uword n_groups)
Compute the between-group scatter matrix S_B.
Definition generalized_rotation.hpp:71
RotationResult ena_svd(const arma::mat &points)
Compute a full SVD rotation of a (pre-centered) points matrix.
Definition rotation.hpp:79
arma::vec gmr_direction(const arma::mat &Vx, const arma::vec &target, bool categorical, arma::uword n_groups=0)
Compute the unit rotation direction from a covariate-adjusted contribution matrix.
Definition generalized_rotation.hpp:123
RotationResult complete_rotation(const arma::mat &data, const arma::mat &named_axes, const std::vector< std::string > &named_labels)
Keep named axes verbatim and fill remaining axes from an SVD of the data after parallel deflation by ...
Definition rotation.hpp:256
GMRStepResult gmr_step(const arma::mat &V, const arma::mat &X_model, const arma::vec &target, const arma::uvec &x1_cols, bool categorical, arma::uword n_groups, const arma::uvec &subset, int n_lambda=50, int k_folds=5, double lasso_eps=0.01)
Compute the rotation direction for one axis of the Generalized Means Rotation.
Definition generalized_rotation.hpp:201
Rotation routines for Epistemic Network Analysis (ENA): SVD, deflation, orthogonal-SVD,...
Result of one GMR axis step.
Definition generalized_rotation.hpp:163
arma::vec direction
Unit vector in connection space (p×1).
Definition generalized_rotation.hpp:164
Parameters for generalized_means_rotation().
Definition generalized_rotation.hpp:262
arma::uword y_n_groups
Definition generalized_rotation.hpp:281
int n_lambda
Length of the lambda path.
Definition generalized_rotation.hpp:287
arma::uvec x_subset
Optional row subset (0-based); empty means use all rows.
Definition generalized_rotation.hpp:270
bool x_categorical
true if the X target is categorical.
Definition generalized_rotation.hpp:268
arma::mat y_model_matrix
n×p model matrix for the Y axis.
Definition generalized_rotation.hpp:277
bool y_categorical
true if the Y target is categorical.
Definition generalized_rotation.hpp:280
arma::uvec y1_cols
0-based target column indices in y_model_matrix.
Definition generalized_rotation.hpp:279
double lasso_eps
lambda_min = lasso_eps * lambda_max.
Definition generalized_rotation.hpp:289
bool has_y
true if a Y-axis GMR target is provided.
Definition generalized_rotation.hpp:276
arma::mat x_model_matrix
n×p model matrix for the X axis.
Definition generalized_rotation.hpp:265
arma::vec y_target
n×1 target variable for the Y axis.
Definition generalized_rotation.hpp:278
arma::uword x_n_groups
Number of distinct groups (used when x_categorical is true).
Definition generalized_rotation.hpp:269
arma::uvec x1_cols
0-based target column indices in x_model_matrix.
Definition generalized_rotation.hpp:267
arma::vec x_target
n×1 target variable (raw float or 0-based integer codes).
Definition generalized_rotation.hpp:266
int k_folds
Number of cross-validation folds.
Definition generalized_rotation.hpp:288
Aggregated result returned by every rotation routine.
Definition rotation.hpp:33
arma::mat rotation
p x p orthogonal matrix; column j is rotation axis j.
Definition rotation.hpp:34