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 
187  void Train(const arma::mat& data,
188  const arma::rowvec& responses,
189  arma::vec& beta,
190  const bool transposeData = true);
191 
205  void Train(const arma::mat& data,
206  const arma::rowvec& responses,
207  const bool transposeData = true);
208 
218  void Predict(const arma::mat& points,
219  arma::rowvec& predictions,
220  const bool rowMajor = false) const;
221 
223  const std::vector<size_t>& ActiveSet() const { return activeSet; }
224 
227  const std::vector<arma::vec>& BetaPath() const { return betaPath; }
228 
230  const arma::vec& Beta() const { return betaPath.back(); }
231 
234  const std::vector<double>& LambdaPath() const { return lambdaPath; }
235 
237  const arma::mat& MatUtriCholFactor() const { return matUtriCholFactor; }
238 
242  template<typename Archive>
243  void serialize(Archive& ar, const unsigned int /* version */);
244 
245  private:
247  arma::mat matGramInternal;
248 
250  const arma::mat* matGram;
251 
253  arma::mat matUtriCholFactor;
254 
256  bool useCholesky;
257 
259  bool lasso;
261  double lambda1;
262 
264  bool elasticNet;
266  double lambda2;
267 
269  double tolerance;
270 
272  std::vector<arma::vec> betaPath;
273 
275  std::vector<double> lambdaPath;
276 
278  std::vector<size_t> activeSet;
279 
281  std::vector<bool> isActive;
282 
283  // Set of variables that are ignored (if any).
284 
286  std::vector<size_t> ignoreSet;
287 
289  std::vector<bool> isIgnored;
290 
296  void Deactivate(const size_t activeVarInd);
297 
303  void Activate(const size_t varInd);
304 
310  void Ignore(const size_t varInd);
311 
312  // compute "equiangular" direction in output space
313  void ComputeYHatDirection(const arma::mat& matX,
314  const arma::vec& betaDirection,
315  arma::vec& yHatDirection);
316 
317  // interpolate to compute last solution vector
318  void InterpolateBeta();
319 
320  void CholeskyInsert(const arma::vec& newX, const arma::mat& X);
321 
322  void CholeskyInsert(double sqNormNewX, const arma::vec& newGramCol);
323 
324  void GivensRotate(const arma::vec::fixed<2>& x,
325  arma::vec::fixed<2>& rotatedX,
326  arma::mat& G);
327 
328  void CholeskyDelete(const size_t colToKill);
329 };
330 
331 } // namespace regression
332 } // namespace mlpack
333 
334 // Include implementation of serialize().
335 #include "lars_impl.hpp"
336 
337 #endif
void serialize(Archive &ar, const unsigned int)
Serialize the LARS model.
.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:227
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.
void Train(const arma::mat &data, const arma::rowvec &responses, arma::vec &beta, const bool transposeData=true)
Run 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:237
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:234
const std::vector< size_t > & ActiveSet() const
Access the set of active dimensions.
Definition: lars.hpp:223
const arma::vec & Beta() const
Access the solution coefficients.
Definition: lars.hpp:230