slope 6.6.0
Loading...
Searching...
No Matches
Getting Started

First, we define our model. Let's use logistic regression, by setting the loss to "logistic".

#include <slope/slope.h>
model.setLoss("logistic");
The SLOPE model.
Definition slope.h:62
void setLoss(const std::string &loss_type)
Sets the loss function type.
SLOPE (Sorted L-One Penalized Estimation) optimization.

Next, we set the data matrix x and the response vector y. Here we use some toy data.

Eigen::MatrixXd x(3, 2);
Eigen::VectorXd y(3);
x << 1.1, 2.3, 0.2, 1.5, 0.5, 0.2;
y << 0, 1, 0;

Finally, we call the path() method to fit the full SLOPE path.

auto res = model.path(x, y);
SlopePath path(Eigen::EigenBase< T > &x, const Eigen::MatrixXd &y_in, Eigen::ArrayXd alpha=Eigen::ArrayXd::Zero(0), Eigen::ArrayXd lambda=Eigen::ArrayXd::Zero(0), std::function< bool()> check_interrupt=defaultInterruptChecker, const Eigen::VectorXd &observation_weights=Eigen::VectorXd())
Computes SLOPE regression solution path for multiple alpha and lambda values.
Definition slope.h:416

Now we can retrieve the coefficients by calling res.getCoefs().

Relative alpha values

By default, fit() and path() interpret supplied alpha values as absolute penalty scales. To choose them relative to the null-model threshold, set the alpha scale to "relative":

model.setAlphaScale("relative");
auto fit = model.fit(x, y, 0.1);
Eigen::ArrayXd fractions(4);
fractions << 1.0, 0.5, 0.1, 0.01;
auto path = model.path(x, y, fractions);
SlopeFit fit(Eigen::EigenBase< T > &x, const Eigen::MatrixXd &y_in, const double alpha=1.0, Eigen::ArrayXd lambda=Eigen::ArrayXd::Zero(0), std::function< bool()> check_interrupt=defaultInterruptChecker, const Eigen::VectorXd &observation_weights=Eigen::VectorXd())
Fits a single SLOPE regression model for given alpha and lambda values.
Definition slope.h:911
void setAlphaScale(const std::string &alpha_scale)
Sets the scale of user-supplied alpha values.

Each supplied value multiplies alpha_max, the smallest alpha that gives zero coefficients. The model computes this threshold using the current loss, intercept, normalization, observation weights, and lambda sequence. A value of 1.0 gives the null model, 0.1 uses 10% of that penalty scale, and 0.0 removes the penalty. Values above 1.0 are also allowed. Values must be finite and nonnegative, and path values must be in nonincreasing order. An all-zero lambda sequence leaves the model unpenalized regardless of alpha.

Calling path(x, y) still generates the usual automatic path. Noise-based alpha estimation and cross-validation retain their absolute penalty scales. SlopeFit::getAlpha() and SlopePath::getAlpha() always return the absolute values used by the solver. Set model.setAlphaScale("absolute") before passing those values back to fit() or path().

Observation weights

Pass one observation weight per row to fit a weighted model. Weights must be finite and nonnegative, and at least one weight must be positive. An empty weight vector gives every observation the same weight. The weighted objective is

\[ \frac{1}{\sum_i w_i}\sum_i w_i\ell_i(\beta_0,\beta) + \alpha\sum_j\lambda_j|\beta|_{(j)}. \]

Multiplying every weight by the same positive number leaves the fit unchanged. Zero-weight rows do not contribute to the objective or to computed feature centers and scales. Inputs must still satisfy the usual data validation rules. Standardization uses the weighted mean and population standard deviation. Manual centers and scales retain their supplied values. L1 and L2 feature scales use weighted norms with weights normalized to the number of positive-weight rows. Minima, ranges, and maximum absolute values use only positive-weight rows. Integer weights reproduce repeated observations with default standardization; L1 and L2 scales do not promise that equivalence. Multinomial classes are inferred from all supplied responses, including zero-weight rows.

Weights are the final optional argument to fit() and path(), after the interrupt callback:

Eigen::VectorXd weights(3);
weights << 1.0, 2.0, 0.5;
Eigen::ArrayXd lambda;
double alpha = 0.1;
auto fit = model.fit(
x, y, alpha, lambda, slope::defaultInterruptChecker, weights);
auto path = model.path(
x, y, {}, lambda, slope::defaultInterruptChecker, weights);
bool defaultInterruptChecker()
Default no-op interrupt checker.
Definition slope.h:48

Supply weights again when relaxing a fit. For a single fit, weights follow the optional intercept and coefficient warm starts. Pass the original predictors, before normalization; relaxation uses just-in-time normalization and does not modify them. For a path, weights follow gamma:

auto relaxed_fit = model.relax(fit, x, y, 0.0, {}, {}, weights);
auto relaxed_path = model.relax(path, x, y, 0.0, weights);
SlopeFit relax(const SlopeFit &fit, T &x, const Eigen::VectorXd &y_in, const double gamma=0.0, Eigen::VectorXd beta0=Eigen::VectorXd(0), Eigen::VectorXd beta=Eigen::VectorXd(0), const Eigen::VectorXd &observation_weights=Eigen::VectorXd())
Relaxes a fitted SLOPE model.
Definition slope.h:1039

Cross-validation accepts weights after its configuration:

#include <slope/cv.h>
config.n_folds = 3;
auto cv = slope::crossValidate(model, x, y, config, weights);
Cross-validation functionality for SLOPE models.
CvResult crossValidate(Slope model, Eigen::EigenBase< T > &x, const Eigen::MatrixXd &y_in, const CvConfig &config=CvConfig(), const Eigen::VectorXd &observation_weights=Eigen::VectorXd(0))
Performs cross-validation on a SLOPE model to select optimal hyperparameters.
Definition cv.h:264
Configuration settings for cross-validation.
Definition cv.h:92
int n_folds
Number of folds for cross-validation (default: 10).
Definition cv.h:94

Automatically generated folds are identical across platforms for the same sample count, fold count, number of repetitions, and config.random_seed. Fold assignments differ from releases that used std::shuffle. Set config.predefined_folds to reuse saved fold assignments.

Each training and validation subset normalizes its own weights. Validation metrics use observation weights, and the reported mean and standard error continue to give each fold equal weight. Every training and validation subset must have positive total weight.

The same row weights apply to every response column, including multiresponse quadratic models and multinomial models. Automatic noise-based alpha estimation and automatic Gaussian lambda calibration do not support nonuniform weights. Use the default BH sequence, another supported sequence, or an explicit lambda vector instead.

The Julia bindings retain the original entry points and add fit_slope_dense_weighted, fit_slope_sparse_weighted, cv_slope_dense_weighted, and cv_slope_sparse_weighted. These functions take the same arguments as their original counterparts, followed by a Vector{Float64} of observation weights.