slope 0.31.0
Loading...
Searching...
No Matches
normalize.h
Go to the documentation of this file.
1
6#pragma once
7
8#include "jit_normalization.h"
9#include "math.h"
10#include <Eigen/SparseCore>
11
12namespace slope {
13
31template<typename T>
32void
33computeCenters(Eigen::VectorXd& x_centers, const T& x, const std::string& type)
34{
35 int p = x.cols();
36
37 if (type == "manual") {
38 if (x_centers.size() != p) {
39 throw std::invalid_argument("Invalid dimensions in centers");
40 }
41
42 if (!x_centers.allFinite()) {
43 throw std::invalid_argument("Centers must be finite");
44 }
45
46 } else if (type == "mean") {
47 x_centers = means(x);
48 } else if (type == "min") {
49 x_centers = mins(x);
50 } else if (type != "none") {
51 throw std::invalid_argument("Invalid centering type");
52 }
53}
54
72template<typename T>
73void
74computeScales(Eigen::VectorXd& x_scales, const T& x, const std::string& type)
75{
76 int p = x.cols();
77
78 if (type == "manual") {
79 if (x_scales.size() != p) {
80 throw std::invalid_argument("Invalid dimensions in scales");
81 }
82 if (!x_scales.allFinite()) {
83 throw std::invalid_argument("Scales must be finite");
84 }
85 } else if (type == "sd") {
86 x_scales = stdDevs(x);
87 } else if (type == "l1") {
88 x_scales = l1Norms(x);
89 } else if (type == "l2") {
90 x_scales = l2Norms(x);
91 } else if (type == "max_abs") {
92 x_scales = maxAbs(x);
93 } else if (type == "range") {
94 x_scales = ranges(x);
95 } else if (type != "none") {
96 throw std::invalid_argument("Invalid scaling type");
97 }
98}
99
118template<typename Derived>
120normalize(Eigen::Map<Derived>& x,
121 Eigen::VectorXd& x_centers,
122 Eigen::VectorXd& x_scales,
123 const std::string& centering_type,
124 const std::string& scaling_type,
125 const bool)
126{
127 computeCenters(x_centers, x, centering_type);
128 computeScales(x_scales, x, scaling_type);
129
130 bool center = centering_type != "none";
131 bool scale = scaling_type != "none";
132
133 if (center && scale) {
135 } else if (center) {
137 } else if (scale) {
139 } else {
141 }
142}
143
166normalize(Eigen::MatrixXd& x,
167 Eigen::VectorXd& x_centers,
168 Eigen::VectorXd& x_scales,
169 const std::string& centering_type,
170 const std::string& scaling_type,
171 const bool modify_x);
172
197normalize(Eigen::SparseMatrix<double>& x,
198 Eigen::VectorXd& x_centers,
199 Eigen::VectorXd& x_scales,
200 const std::string& centering_type,
201 const std::string& scaling_type,
202 const bool modify_x);
203
224std::tuple<Eigen::VectorXd, Eigen::MatrixXd>
225rescaleCoefficients(const Eigen::VectorXd& beta0,
226 const Eigen::SparseMatrix<double>& beta,
227 const Eigen::VectorXd& x_centers,
228 const Eigen::VectorXd& x_scales,
229 const bool intercept);
230
231} // namespace slope
Enums to control predictor standardization behavior.
Mathematical support functions for the slope package.
Namespace containing SLOPE regression implementation.
Definition clusters.cpp:5
Eigen::VectorXd l1Norms(const T &x)
Computes the L1 (Manhattan) norms for each column of a matrix.
Definition math.h:443
Eigen::VectorXd maxAbs(const Eigen::SparseMatrix< double > &x)
Computes the maximum absolute value for each column of a matrix.
Definition math.cpp:126
Eigen::VectorXd means(const Eigen::SparseMatrix< double > &x)
Computes the arithmetic mean for each column of a sparse matrix.
Definition math.cpp:76
JitNormalization
Enums to control predictor standardization behavior.
@ None
No JIT normalization.
Eigen::VectorXd stdDevs(const Eigen::SparseMatrix< double > &x)
Computes the standard deviation for each column of a matrix.
Definition math.cpp:180
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
Eigen::VectorXd ranges(const Eigen::SparseMatrix< double > &x)
Computes the range (max - min) for each column of a matrix.
Definition math.cpp:97
void computeScales(Eigen::VectorXd &x_scales, const T &x, const std::string &type)
Definition normalize.h:74
Eigen::VectorXd l2Norms(const Eigen::SparseMatrix< double > &x)
Computes the L2 (Euclidean) norms for each column of a sparse matrix.
Definition math.cpp:56
Eigen::VectorXd mins(const Eigen::SparseMatrix< double > &x)
Computes the minimum value for each column of a sparse matrix.
Definition math.cpp:153
void computeCenters(Eigen::VectorXd &x_centers, const T &x, const std::string &type)
Definition normalize.h:33