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 
188  double Train(const arma::mat& data,
189  const arma::rowvec& responses,
190  arma::vec& beta,
191  const bool transposeData = true);
192 
207  double Train(const arma::mat& data,
208  const arma::rowvec& responses,
209  const bool transposeData = true);
210 
220  void Predict(const arma::mat& points,
221  arma::rowvec& predictions,
222  const bool rowMajor = false) const;
223 
225  const std::vector<size_t>& ActiveSet() const { return activeSet; }
226 
229  const std::vector<arma::vec>& BetaPath() const { return betaPath; }
230 
232  const arma::vec& Beta() const { return betaPath.back(); }
233 
236  const std::vector<double>& LambdaPath() const { return lambdaPath; }
237 
239  const arma::mat& MatUtriCholFactor() const { return matUtriCholFactor; }
240 
244  template<typename Archive>
245  void serialize(Archive& ar, const unsigned int /* version */);
246 
247  private:
249  arma::mat matGramInternal;
250 
252  const arma::mat* matGram;
253 
255  arma::mat matUtriCholFactor;
256 
258  bool useCholesky;
259 
261  bool lasso;
263  double lambda1;
264 
266  bool elasticNet;
268  double lambda2;
269 
271  double tolerance;
272 
274  std::vector<arma::vec> betaPath;
275 
277  std::vector<double> lambdaPath;
278 
280  std::vector<size_t> activeSet;
281 
283  std::vector<bool> isActive;
284 
285  // Set of variables that are ignored (if any).
286 
288  std::vector<size_t> ignoreSet;
289 
291  std::vector<bool> isIgnored;
292 
298  void Deactivate(const size_t activeVarInd);
299 
305  void Activate(const size_t varInd);
306 
312  void Ignore(const size_t varInd);
313 
314  // compute "equiangular" direction in output space
315  void ComputeYHatDirection(const arma::mat& matX,
316  const arma::vec& betaDirection,
317  arma::vec& yHatDirection);
318 
319  // interpolate to compute last solution vector
320  void InterpolateBeta();
321 
322  void CholeskyInsert(const arma::vec& newX, const arma::mat& X);
323 
324  void CholeskyInsert(double sqNormNewX, const arma::vec& newGramCol);
325 
326  void GivensRotate(const arma::vec::fixed<2>& x,
327  arma::vec::fixed<2>& rotatedX,
328  arma::mat& G);
329 
330  void CholeskyDelete(const size_t colToKill);
331 };
332 
333 } // namespace regression
334 } // namespace mlpack
335 
336 // Include implementation of serialize().
337 #include "lars_impl.hpp"
338 
339 #endif
void serialize(Archive &ar, const unsigned int)
Serialize the LARS model.
strip_type.hpp
Definition: add_to_po.hpp:21
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:229
double Train(const arma::mat &data, const arma::rowvec &responses, arma::vec &beta, const bool transposeData=true)
Run LARS.
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
const arma::mat & MatUtriCholFactor() const
Access the upper triangular cholesky factor.
Definition: lars.hpp:239
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:236
const std::vector< size_t > & ActiveSet() const
Access the set of active dimensions.
Definition: lars.hpp:225
const arma::vec & Beta() const
Access the solution coefficients.
Definition: lars.hpp:232