lars.hpp
Go to the documentation of this file.
1 
24 #ifndef MLPACK_METHODS_LARS_LARS_HPP
25 #define MLPACK_METHODS_LARS_LARS_HPP
26 
27 #include <mlpack/prereqs.hpp>
28 
29 namespace mlpack {
30 namespace regression {
31 
32 // beta is the estimator
33 // yHat is the prediction from the current estimator
34 
89 class LARS
90 {
91  public:
102  LARS(const bool useCholesky = false,
103  const double lambda1 = 0.0,
104  const double lambda2 = 0.0,
105  const double tolerance = 1e-16);
106 
119  LARS(const bool useCholesky,
120  const arma::mat& gramMatrix,
121  const double lambda1 = 0.0,
122  const double lambda2 = 0.0,
123  const double tolerance = 1e-16);
124 
140  LARS(const arma::mat& data,
141  const arma::rowvec& responses,
142  const bool transposeData = true,
143  const bool useCholesky = false,
144  const double lambda1 = 0.0,
145  const double lambda2 = 0.0,
146  const double tolerance = 1e-16);
147 
164  LARS(const arma::mat& data,
165  const arma::rowvec& responses,
166  const bool transposeData,
167  const bool useCholesky,
168  const arma::mat& gramMatrix,
169  const double lambda1 = 0.0,
170  const double lambda2 = 0.0,
171  const double tolerance = 1e-16);
172 
178  LARS(const LARS& other);
179 
185  LARS(LARS&& other);
186 
192  LARS& operator=(const LARS& other);
193 
199  LARS& operator=(LARS&& other);
200 
216  double Train(const arma::mat& data,
217  const arma::rowvec& responses,
218  arma::vec& beta,
219  const bool transposeData = true);
220 
235  double Train(const arma::mat& data,
236  const arma::rowvec& responses,
237  const bool transposeData = true);
238 
248  void Predict(const arma::mat& points,
249  arma::rowvec& predictions,
250  const bool rowMajor = false) const;
251 
253  double Lambda1() const { return lambda1; }
255  double& Lambda1() { return lambda1; }
256 
258  double Lambda2() const { return lambda2; }
260  double& Lambda2() { return lambda2; }
261 
263  bool UseCholesky() const { return useCholesky; }
265  bool& UseCholesky() { return useCholesky; }
266 
268  double Tolerance() const { return tolerance; }
270  double& Tolerance() { return tolerance; }
271 
273  const std::vector<size_t>& ActiveSet() const { return activeSet; }
274 
277  const std::vector<arma::vec>& BetaPath() const { return betaPath; }
278 
280  const arma::vec& Beta() const { return betaPath.back(); }
281 
284  const std::vector<double>& LambdaPath() const { return lambdaPath; }
285 
287  const arma::mat& MatUtriCholFactor() const { return matUtriCholFactor; }
288 
292  template<typename Archive>
293  void serialize(Archive& ar, const uint32_t /* version */);
294 
307  double ComputeError(const arma::mat& matX,
308  const arma::rowvec& y,
309  const bool rowMajor = false);
310 
311  private:
313  arma::mat matGramInternal;
314 
316  const arma::mat* matGram;
317 
319  arma::mat matUtriCholFactor;
320 
322  bool useCholesky;
323 
325  bool lasso;
327  double lambda1;
328 
330  bool elasticNet;
332  double lambda2;
333 
335  double tolerance;
336 
338  std::vector<arma::vec> betaPath;
339 
341  std::vector<double> lambdaPath;
342 
344  std::vector<size_t> activeSet;
345 
347  std::vector<bool> isActive;
348 
349  // Set of variables that are ignored (if any).
350 
352  std::vector<size_t> ignoreSet;
353 
355  std::vector<bool> isIgnored;
356 
362  void Deactivate(const size_t activeVarInd);
363 
369  void Activate(const size_t varInd);
370 
376  void Ignore(const size_t varInd);
377 
378  // compute "equiangular" direction in output space
379  void ComputeYHatDirection(const arma::mat& matX,
380  const arma::vec& betaDirection,
381  arma::vec& yHatDirection);
382 
383  // interpolate to compute last solution vector
384  void InterpolateBeta();
385 
386  void CholeskyInsert(const arma::vec& newX, const arma::mat& X);
387 
388  void CholeskyInsert(double sqNormNewX, const arma::vec& newGramCol);
389 
390  void GivensRotate(const arma::vec::fixed<2>& x,
391  arma::vec::fixed<2>& rotatedX,
392  arma::mat& G);
393 
394  void CholeskyDelete(const size_t colToKill);
395 };
396 
397 } // namespace regression
398 } // namespace mlpack
399 
400 // Include implementation of serialize().
401 #include "lars_impl.hpp"
402 
403 #endif
double & Lambda1()
Modify the L1 regularization coefficient.
Definition: lars.hpp:255
bool & UseCholesky()
Modify whether to use the Cholesky decomposition.
Definition: lars.hpp:265
Linear algebra utility functions, generally performed on matrices or vectors.
double Lambda1() const
Get the L1 regularization coefficient.
Definition: lars.hpp:253
void Predict(const arma::mat &points, arma::rowvec &predictions, const bool rowMajor=false) const
Predict y_i for each data point in the given data matrix using the currently-trained LARS model...
The core includes that mlpack expects; standard C++ includes and Armadillo.
const std::vector< arma::vec > & BetaPath() const
Access the set of coefficients after each iteration; the solution is the last element.
Definition: lars.hpp:277
double Train(const arma::mat &data, const arma::rowvec &responses, arma::vec &beta, const bool transposeData=true)
Run LARS.
double Tolerance() const
Get the tolerance for maximum correlation during training.
Definition: lars.hpp:268
LARS & operator=(const LARS &other)
Copy the given LARS object.
LARS(const bool useCholesky=false, const double lambda1=0.0, const double lambda2=0.0, const double tolerance=1e-16)
Set the parameters to LARS.
An implementation of LARS, a stage-wise homotopy-based algorithm for l1-regularized linear regression...
Definition: lars.hpp:89
double & Lambda2()
Modify the L2 regularization coefficient.
Definition: lars.hpp:260
const arma::mat & MatUtriCholFactor() const
Access the upper triangular cholesky factor.
Definition: lars.hpp:287
double ComputeError(const arma::mat &matX, const arma::rowvec &y, const bool rowMajor=false)
Compute cost error of the given data matrix using the currently-trained LARS model.
const std::vector< double > & LambdaPath() const
Access the set of values for lambda1 after each iteration; the solution is the last element...
Definition: lars.hpp:284
const std::vector< size_t > & ActiveSet() const
Access the set of active dimensions.
Definition: lars.hpp:273
double Lambda2() const
Get the L2 regularization coefficient.
Definition: lars.hpp:258
void serialize(Archive &ar, const uint32_t)
Serialize the LARS model.
bool UseCholesky() const
Get whether to use the Cholesky decomposition.
Definition: lars.hpp:263
const arma::vec & Beta() const
Access the solution coefficients.
Definition: lars.hpp:280
double & Tolerance()
Modify the tolerance for maximum correlation during training.
Definition: lars.hpp:270