mlpack  master
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 
139  void Train(const arma::mat& data,
140  const arma::vec& responses,
141  arma::vec& beta,
142  const bool transposeData = true);
143 
153  void Predict(const arma::mat& points,
154  arma::vec& predictions,
155  const bool rowMajor = false) const;
156 
158  const std::vector<size_t>& ActiveSet() const { return activeSet; }
159 
162  const std::vector<arma::vec>& BetaPath() const { return betaPath; }
163 
166  const std::vector<double>& LambdaPath() const { return lambdaPath; }
167 
169  const arma::mat& MatUtriCholFactor() const { return matUtriCholFactor; }
170 
174  template<typename Archive>
175  void Serialize(Archive& ar, const unsigned int /* version */);
176 
177  private:
179  arma::mat matGramInternal;
180 
182  const arma::mat* matGram;
183 
185  arma::mat matUtriCholFactor;
186 
189 
191  bool lasso;
193  double lambda1;
194 
198  double lambda2;
199 
201  double tolerance;
202 
204  std::vector<arma::vec> betaPath;
205 
207  std::vector<double> lambdaPath;
208 
210  std::vector<size_t> activeSet;
211 
213  std::vector<bool> isActive;
214 
215  // Set of variables that are ignored (if any).
216 
218  std::vector<size_t> ignoreSet;
219 
221  std::vector<bool> isIgnored;
222 
228  void Deactivate(const size_t activeVarInd);
229 
235  void Activate(const size_t varInd);
236 
242  void Ignore(const size_t varInd);
243 
244  // compute "equiangular" direction in output space
245  void ComputeYHatDirection(const arma::mat& matX,
246  const arma::vec& betaDirection,
247  arma::vec& yHatDirection);
248 
249  // interpolate to compute last solution vector
250  void InterpolateBeta();
251 
252  void CholeskyInsert(const arma::vec& newX, const arma::mat& X);
253 
254  void CholeskyInsert(double sqNormNewX, const arma::vec& newGramCol);
255 
256  void GivensRotate(const arma::vec::fixed<2>& x,
257  arma::vec::fixed<2>& rotatedX,
258  arma::mat& G);
259 
260  void CholeskyDelete(const size_t colToKill);
261 };
262 
263 } // namespace regression
264 } // namespace mlpack
265 
266 // Include implementation of Serialize().
267 #include "lars_impl.hpp"
268 
269 #endif
void ComputeYHatDirection(const arma::mat &matX, const arma::vec &betaDirection, arma::vec &yHatDirection)
const arma::mat & MatUtriCholFactor() const
Access the upper triangular cholesky factor.
Definition: lars.hpp:169
std::vector< bool > isIgnored
Membership indicator for set of ignored variables.
Definition: lars.hpp:221
std::vector< bool > isActive
Active set membership indicator (for each dimension).
Definition: lars.hpp:213
double tolerance
Tolerance for main loop.
Definition: lars.hpp:201
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: binarize.hpp:18
std::vector< double > lambdaPath
Value of lambda_1 for each solution in solution path.
Definition: lars.hpp:207
bool lasso
True if this is the LASSO problem.
Definition: lars.hpp:191
The core includes that mlpack expects; standard C++ includes and Armadillo.
std::vector< size_t > activeSet
Active set of dimensions.
Definition: lars.hpp:210
const std::vector< size_t > & ActiveSet() const
Access the set of active dimensions.
Definition: lars.hpp:158
void Predict(const arma::mat &points, arma::vec &predictions, const bool rowMajor=false) const
Predict y_i for each data point in the given data matrix, using the currently-trained LARS model (so ...
arma::mat matUtriCholFactor
Upper triangular cholesky factor; initially 0x0 matrix.
Definition: lars.hpp:185
double lambda1
Regularization parameter for l1 penalty.
Definition: lars.hpp:193
void Ignore(const size_t varInd)
Add dimension varInd to ignores set (never removed).
std::vector< arma::vec > betaPath
Solution path.
Definition: lars.hpp:204
std::vector< size_t > ignoreSet
Set of ignored variables (for dimensions in span{active set dimensions}).
Definition: lars.hpp:218
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.
bool elasticNet
True if this is the elastic net problem.
Definition: lars.hpp:196
An implementation of LARS, a stage-wise homotopy-based algorithm for l1-regularized linear regression...
Definition: lars.hpp:89
bool useCholesky
Whether or not to use Cholesky decomposition when solving linear system.
Definition: lars.hpp:188
void CholeskyInsert(const arma::vec &newX, const arma::mat &X)
void CholeskyDelete(const size_t colToKill)
double lambda2
Regularization parameter for l2 penalty.
Definition: lars.hpp:198
void Activate(const size_t varInd)
Add dimension varInd to active set.
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:166
void Deactivate(const size_t activeVarInd)
Remove activeVarInd&#39;th element from active set.
arma::mat matGramInternal
Gram matrix.
Definition: lars.hpp:179
const std::vector< arma::vec > & BetaPath() const
Access the set of coefficients after each iteration; the solution is the last element.
Definition: lars.hpp:162
void GivensRotate(const arma::vec::fixed< 2 > &x, arma::vec::fixed< 2 > &rotatedX, arma::mat &G)
void Serialize(Archive &ar, const unsigned int)
Serialize the LARS model.
void Train(const arma::mat &data, const arma::vec &responses, arma::vec &beta, const bool transposeData=true)
Run LARS.
const arma::mat * matGram
Pointer to the Gram matrix we will use.
Definition: lars.hpp:182