slope 0.31.0
Loading...
Searching...
No Matches
normalize.cpp
1#include "normalize.h"
2
3namespace slope {
4
6normalize(Eigen::MatrixXd& x,
7 Eigen::VectorXd& x_centers,
8 Eigen::VectorXd& x_scales,
9 const std::string& centering_type,
10 const std::string& scaling_type,
11 const bool modify_x)
12{
13 const int p = x.cols();
14
15 computeCenters(x_centers, x, centering_type);
16 computeScales(x_scales, x, scaling_type);
17
18 if ((x_scales.array().abs() == 0.0).any()) {
19 throw std::invalid_argument("One or more columns have zero variance");
20 }
21
22 bool center = centering_type != "none";
23 bool scale = scaling_type != "none";
24 bool center_jit = center && !modify_x;
25 bool scale_jit = scale && !modify_x;
26
27 JitNormalization jit_normalization;
28
29 if (center_jit && scale_jit) {
30 jit_normalization = JitNormalization::Both;
31 } else if (center_jit) {
32 jit_normalization = JitNormalization::Center;
33 } else if (scale_jit) {
34 jit_normalization = JitNormalization::Scale;
35 } else {
36 jit_normalization = JitNormalization::None;
37 }
38
39 if (modify_x && (center || scale)) {
40 for (int j = 0; j < p; ++j) {
41 if (center) {
42 x.col(j).array() -= x_centers(j);
43 }
44 if (scale) {
45 x.col(j).array() /= x_scales(j);
46 }
47 }
48 }
49
50 return jit_normalization;
51}
52
54normalize(Eigen::SparseMatrix<double>& x,
55 Eigen::VectorXd& x_centers,
56 Eigen::VectorXd& x_scales,
57 const std::string& centering_type,
58 const std::string& scaling_type,
59 const bool)
60{
61 computeCenters(x_centers, x, centering_type);
62 computeScales(x_scales, x, scaling_type);
63
64 bool center = centering_type != "none";
65 bool scale = scaling_type != "none";
66 bool center_jit = center;
67 bool scale_jit = scale;
68
69 JitNormalization jit_normalization;
70
71 if (center_jit && scale_jit) {
72 jit_normalization = JitNormalization::Both;
73 } else if (center_jit) {
74 jit_normalization = JitNormalization::Center;
75 } else if (scale_jit) {
76 jit_normalization = JitNormalization::Scale;
77 } else {
78 jit_normalization = JitNormalization::None;
79 }
80
81 // TODO: Implement in-place scaling for sparse matrices.
82 // if (modify_x && scaling_type != "none") {
83 // for (int j = 0; j < x.cols(); ++j) {
84 // for (Eigen::SparseMatrix<double>::InnerIterator it(x, j); it; ++it) {
85 // it.valueRef() = it.value() / x_scales(j);
86 // }
87 // }
88 // }
89
90 return jit_normalization;
91}
92
93// TODO: Make this work for sparse beta
94std::tuple<Eigen::VectorXd, Eigen::MatrixXd>
95rescaleCoefficients(const Eigen::VectorXd& beta0,
96 const Eigen::SparseMatrix<double>& beta,
97 const Eigen::VectorXd& x_centers,
98 const Eigen::VectorXd& x_scales,
99 const bool intercept)
100{
101 int m = beta0.size();
102
103 bool centering = x_centers.size() > 0;
104 bool scaling = x_scales.size() > 0;
105
106 Eigen::MatrixXd beta0_out = beta0;
107 Eigen::SparseMatrix<double> beta_out = beta;
108
109 if (centering || scaling) {
110 for (int k = 0; k < m; ++k) {
111 double x_bar_beta_sum = 0.0;
112
113 for (Eigen::SparseMatrix<double>::InnerIterator it(beta_out, k); it;
114 ++it) {
115 int j = it.row();
116
117 if (scaling) {
118 it.valueRef() /= x_scales(j);
119 // beta_out(j, k) /= x_scales(j);
120 }
121 if (centering) {
122 x_bar_beta_sum += x_centers(j) * it.valueRef();
123 }
124 }
125
126 if (intercept) {
127 beta0_out(k) -= x_bar_beta_sum;
128 }
129 }
130 }
131
132 return { beta0_out, beta_out };
133}
134
135} // namespace slope
Namespace containing SLOPE regression implementation.
Definition clusters.cpp:5
JitNormalization
Enums to control predictor standardization behavior.
@ None
No JIT normalization.
JitNormalization normalize(Eigen::MatrixXd &x, Eigen::VectorXd &x_centers, Eigen::VectorXd &x_scales, const std::string &centering_type, const std::string &scaling_type, const bool modify_x)
Definition normalize.cpp:6
std::tuple< Eigen::VectorXd, Eigen::MatrixXd > rescaleCoefficients(const Eigen::VectorXd &beta0, const Eigen::SparseMatrix< double > &beta, const Eigen::VectorXd &x_centers, const Eigen::VectorXd &x_scales, const bool intercept)
Rescales the coefficients using the given parameters.
Definition normalize.cpp:95
void computeScales(Eigen::VectorXd &x_scales, const T &x, const std::string &type)
Definition normalize.h:74
void computeCenters(Eigen::VectorXd &x_centers, const T &x, const std::string &type)
Definition normalize.h:33
Functions to normalize the design matrix and rescale coefficients in case the design was normalized.