mlpack  2.0.1
lars.hpp
Go to the documentation of this file.
1 
26 #ifndef __MLPACK_METHODS_LARS_LARS_HPP
27 #define __MLPACK_METHODS_LARS_LARS_HPP
28 
29 #include <mlpack/core.hpp>
30 
31 namespace mlpack {
32 namespace regression {
33 
34 // beta is the estimator
35 // yHat is the prediction from the current estimator
36 
91 class LARS
92 {
93  public:
104  LARS(const bool useCholesky,
105  const double lambda1 = 0.0,
106  const double lambda2 = 0.0,
107  const double tolerance = 1e-16);
108 
121  LARS(const bool useCholesky,
122  const arma::mat& gramMatrix,
123  const double lambda1 = 0.0,
124  const double lambda2 = 0.0,
125  const double tolerance = 1e-16);
126 
141  void Train(const arma::mat& data,
142  const arma::vec& responses,
143  arma::vec& beta,
144  const bool transposeData = true);
145 
155  void Predict(const arma::mat& points,
156  arma::vec& predictions,
157  const bool rowMajor = false) const;
158 
160  const std::vector<size_t>& ActiveSet() const { return activeSet; }
161 
164  const std::vector<arma::vec>& BetaPath() const { return betaPath; }
165 
168  const std::vector<double>& LambdaPath() const { return lambdaPath; }
169 
171  const arma::mat& MatUtriCholFactor() const { return matUtriCholFactor; }
172 
176  template<typename Archive>
177  void Serialize(Archive& ar, const unsigned int /* version */);
178 
179  private:
181  arma::mat matGramInternal;
182 
184  const arma::mat* matGram;
185 
187  arma::mat matUtriCholFactor;
188 
191 
193  bool lasso;
195  double lambda1;
196 
200  double lambda2;
201 
203  double tolerance;
204 
206  std::vector<arma::vec> betaPath;
207 
209  std::vector<double> lambdaPath;
210 
212  std::vector<size_t> activeSet;
213 
215  std::vector<bool> isActive;
216 
217  // Set of variables that are ignored (if any).
218 
220  std::vector<size_t> ignoreSet;
221 
223  std::vector<bool> isIgnored;
224 
230  void Deactivate(const size_t activeVarInd);
231 
237  void Activate(const size_t varInd);
238 
244  void Ignore(const size_t varInd);
245 
246  // compute "equiangular" direction in output space
247  void ComputeYHatDirection(const arma::mat& matX,
248  const arma::vec& betaDirection,
249  arma::vec& yHatDirection);
250 
251  // interpolate to compute last solution vector
252  void InterpolateBeta();
253 
254  void CholeskyInsert(const arma::vec& newX, const arma::mat& X);
255 
256  void CholeskyInsert(double sqNormNewX, const arma::vec& newGramCol);
257 
258  void GivensRotate(const arma::vec::fixed<2>& x,
259  arma::vec::fixed<2>& rotatedX,
260  arma::mat& G);
261 
262  void CholeskyDelete(const size_t colToKill);
263 };
264 
265 } // namespace regression
266 } // namespace mlpack
267 
268 // Include implementation of Serialize().
269 #include "lars_impl.hpp"
270 
271 #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:171
std::vector< bool > isIgnored
Membership indicator for set of ignored variables.
Definition: lars.hpp:223
std::vector< bool > isActive
Active set membership indicator (for each dimension).
Definition: lars.hpp:215
double tolerance
Tolerance for main loop.
Definition: lars.hpp:203
Linear algebra utility functions, generally performed on matrices or vectors.
std::vector< double > lambdaPath
Value of lambda_1 for each solution in solution path.
Definition: lars.hpp:209
bool lasso
True if this is the LASSO problem.
Definition: lars.hpp:193
std::vector< size_t > activeSet
Active set of dimensions.
Definition: lars.hpp:212
const std::vector< size_t > & ActiveSet() const
Access the set of active dimensions.
Definition: lars.hpp:160
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:187
double lambda1
Regularization parameter for l1 penalty.
Definition: lars.hpp:195
void Ignore(const size_t varInd)
Add dimension varInd to ignores set (never removed).
std::vector< arma::vec > betaPath
Solution path.
Definition: lars.hpp:206
std::vector< size_t > ignoreSet
Set of ignored variables (for dimensions in span{active set dimensions}).
Definition: lars.hpp:220
bool elasticNet
True if this is the elastic net problem.
Definition: lars.hpp:198
An implementation of LARS, a stage-wise homotopy-based algorithm for l1-regularized linear regression...
Definition: lars.hpp:91
bool useCholesky
Whether or not to use Cholesky decomposition when solving linear system.
Definition: lars.hpp:190
void CholeskyInsert(const arma::vec &newX, const arma::mat &X)
Include all of the base components required to write MLPACK methods, and the main MLPACK Doxygen docu...
void CholeskyDelete(const size_t colToKill)
double lambda2
Regularization parameter for l2 penalty.
Definition: lars.hpp:200
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:168
void Deactivate(const size_t activeVarInd)
Remove activeVarInd'th element from active set.
arma::mat matGramInternal
Gram matrix.
Definition: lars.hpp:181
const std::vector< arma::vec > & BetaPath() const
Access the set of coefficients after each iteration; the solution is the last element.
Definition: lars.hpp:164
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.
LARS(const bool useCholesky, 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::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:184