SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
LeastAngleRegression.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2012 Chiyuan Zhang
8  * Copyright (C) 2012 Chiyuan Zhang
9  */
10 
11 #include <shogun/lib/config.h>
12 
13 #include <vector>
14 #include <limits>
15 #include <algorithm>
16 
22 
23 using namespace Eigen;
24 using namespace shogun;
25 using namespace std;
26 
27 CLeastAngleRegression::CLeastAngleRegression(bool lasso) :
28  CLinearMachine(), m_lasso(lasso),
29  m_max_nonz(0), m_max_l1_norm(0)
30 {
31  m_epsilon = CMath::MACHINE_EPSILON;
32  SG_ADD(&m_epsilon, "epsilon", "Epsilon for early stopping", MS_AVAILABLE);
33  SG_ADD(&m_max_nonz, "max_nonz", "Max number of non-zero variables", MS_AVAILABLE);
34  SG_ADD(&m_max_l1_norm, "max_l1_norm", "Max l1-norm of estimator", MS_AVAILABLE);
35 }
36 
38 {
39 
40 }
41 
42 template <typename ST>
43 void CLeastAngleRegression::find_max_abs(const std::vector<ST> &vec, const std::vector<bool> &ignore_mask,
44  int32_t &imax, ST& vmax)
45 {
46  imax = -1;
47  vmax = -1;
48  for (index_t i=0; i < vec.size(); ++i)
49  {
50  if (ignore_mask[i])
51  continue;
52 
53  if (CMath::abs(vec[i]) > vmax)
54  {
55  vmax = CMath::abs(vec[i]);
56  imax = i;
57  }
58  }
59 }
60 
61 template <typename ST>
63  ST &y0, ST &y1, SGMatrix<ST> &G)
64 {
65  memset(G.matrix, 0, G.num_rows * G.num_cols * sizeof(ST));
66 
67  if (x1 == 0)
68  {
69  G(0, 0) = G(1, 1) = 1;
70  y0 = x0;
71  y1 = x1;
72  }
73  else
74  {
75  ST r = CMath::sqrt(x0*x0+x1*x1) ;
76  ST sx0 = x0 / r;
77  ST sx1 = x1 / r;
78 
79  G(0,0) = sx0;
80  G(1,0) = -sx1;
81  G(0,1) = sx1;
82  G(1,1) = sx0;
83 
84  y0 = r;
85  y1 = 0;
86  }
87 }
88 
90 {
91  if (!m_labels)
92  SG_ERROR("No labels set\n")
93 
95  SG_ERROR("Provided labels (%s) are of type (%d) - they should be regression labels (%d) instead.\n",
97 
98  if (!data)
99  {
100  if(!features)
101  SG_ERROR("No features provided.\n")
102 
104  SG_ERROR("Feature-class (%d) must be of type C_DENSE (%d)\n", features->get_feature_class(), C_DENSE)
105 
106  data = features;
107  }
108  else
109  if (data->get_feature_class() != C_DENSE)
110  SG_ERROR("Feature-class (%d) must be of type C_DENSE (%d)\n", features->get_feature_class(), C_DENSE)
111 
112  if (data->get_num_vectors() != m_labels->get_num_labels())
113  SG_ERROR("Number of training vectors (%d) does not match number of labels (%d)\n",
115 
116  //check for type of CFeatures, then call the appropriate template method
117  if(data->get_feature_type() == F_DREAL)
118  return CLeastAngleRegression::train_machine_templated((CDenseFeatures<float64_t> *) data);
119  else if(data->get_feature_type() == F_SHORTREAL)
120  return CLeastAngleRegression::train_machine_templated((CDenseFeatures<float32_t> *) data);
121  else if(data->get_feature_type() == F_LONGREAL)
122  return CLeastAngleRegression::train_machine_templated((CDenseFeatures<floatmax_t> *) data);
123  else
124  SG_ERROR("Feature-type (%d) must be of type F_SHORTREAL (%d), F_DREAL (%d) or F_LONGREAL (%d).\n",
126 }
127 
128 template <typename ST>
129 bool CLeastAngleRegression::train_machine_templated(CDenseFeatures<ST> * data)
130 {
131 
132  std::vector<std::vector<ST>> m_beta_path_t;
133 
134  int32_t n_fea = data->get_num_features();
135  int32_t n_vec = data->get_num_vectors();
136 
137  bool lasso_cond = false;
138  bool stop_cond = false;
139 
140  // init facilities
141  m_beta_idx.clear();
142  m_beta_path_t.clear();
143  m_num_active = 0;
144  m_active_set.clear();
145  m_is_active.resize(n_fea);
146  fill(m_is_active.begin(), m_is_active.end(), false);
147 
148  SGVector<ST> y = ((CRegressionLabels*) m_labels)->template get_labels_t<ST>();
149  typename SGVector<ST>::EigenVectorXtMap map_y(y.vector, y.size());
150 
151  // transpose(X) is more convenient to work with since we care
152  // about features here. After transpose, each row will be a data
153  // point while each column corresponds to a feature
154  SGMatrix<ST> X (n_vec, n_fea);
155  typename SGMatrix<ST>::EigenMatrixXtMap map_Xr = data->get_feature_matrix();
156  typename SGMatrix<ST>::EigenMatrixXtMap map_X(X.matrix, n_vec, n_fea);
157  map_X = map_Xr.transpose();
158 
159  SGMatrix<ST> X_active(n_vec, n_fea);
160 
161  // beta is the estimator
162  vector<ST> beta(n_fea);
163 
164  vector<ST> Xy(n_fea);
165  typename SGVector<ST>::EigenVectorXtMap map_Xy(&Xy[0], n_fea);
166  // Xy = X' * y
167  map_Xy=map_Xr*map_y;
168 
169  // mu is the prediction
170  vector<ST> mu(n_vec);
171 
172  // correlation
173  vector<ST> corr(n_fea);
174  // sign of correlation
175  vector<ST> corr_sign(n_fea);
176 
177  // Cholesky factorization R'R = X'X, R is upper triangular
178  SGMatrix<ST> R;
179 
180  ST max_corr = 1;
181  int32_t i_max_corr = 1;
182 
183  // first entry: all coefficients are zero
184  m_beta_path_t.push_back(beta);
185  m_beta_idx.push_back(0);
186 
187  //maximum allowed active variables at a time
188  int32_t max_active_allowed = CMath::min(n_vec-1, n_fea);
189 
190  //========================================
191  // main loop
192  //========================================
193  int32_t nloop=0;
194  while (m_num_active < max_active_allowed && max_corr/n_vec > m_epsilon && !stop_cond)
195  {
196  // corr = X' * (y-mu) = - X'*mu + Xy
197  typename SGVector<ST>::EigenVectorXtMap map_corr(&corr[0], n_fea);
198  typename SGVector<ST>::EigenVectorXtMap map_mu(&mu[0], n_vec);
199 
200  map_corr = map_Xy - (map_Xr*map_mu);
201 
202  // corr_sign = sign(corr)
203  for (index_t i=0; i < corr.size(); ++i)
204  corr_sign[i] = CMath::sign(corr[i]);
205 
206  // find max absolute correlation in inactive set
207  find_max_abs(corr, m_is_active, i_max_corr, max_corr);
208 
209  if (!lasso_cond)
210  {
211  // update Cholesky factorization matrix
212  if (m_num_active == 0)
213  {
214  // R isn't allocated yet
215  R=SGMatrix<ST>(1,1);
216  ST diag_k = map_X.col(i_max_corr).dot(map_X.col(i_max_corr));
217  R(0,0) = CMath::sqrt( diag_k);
218  }
219  else
220  R=cholesky_insert(X, X_active, R, i_max_corr, m_num_active);
221  activate_variable(i_max_corr);
222  }
223 
224  // Active variables
225  typename SGMatrix<ST>::EigenMatrixXtMap map_Xa(X_active.matrix, n_vec, m_num_active);
226  if (!lasso_cond)
227  map_Xa.col(m_num_active-1)=map_X.col(i_max_corr);
228 
229  SGVector<ST> corr_sign_a(m_num_active);
230  for (index_t i=0; i < m_num_active; ++i)
231  corr_sign_a[i] = corr_sign[m_active_set[i]];
232 
233  typename SGVector<ST>::EigenVectorXtMap map_corr_sign_a(corr_sign_a.vector, corr_sign_a.size());
234  typename SGMatrix<ST>::EigenMatrixXtMap map_R(R.matrix, R.num_rows, R.num_cols);
235  typename SGVector<ST>::EigenVectorXt solve = map_R.transpose().template triangularView<Lower>().template solve<OnTheLeft>(map_corr_sign_a);
236 
237  typename SGVector<ST>::EigenVectorXt GA1 = map_R.template triangularView<Upper>().template solve<OnTheLeft>(solve);
238 
239  // AA = 1/sqrt(GA1' * corr_sign_a)
240  ST AA = GA1.dot(map_corr_sign_a);
241  AA = 1/CMath::sqrt( AA);
242 
243  typename SGVector<ST>::EigenVectorXt wA = AA*GA1;
244 
245  // equiangular direction (unit vector)
246  vector<ST> u(n_vec);
247  typename SGVector<ST>::EigenVectorXtMap map_u(&u[0], n_vec);
248 
249  map_u = map_Xa*wA;
250 
251  ST gamma = max_corr / AA;
252  if (m_num_active < n_fea)
253  {
254  #pragma omp parallel for shared(gamma)
255  for (index_t i=0; i < n_fea; ++i)
256  {
257  if (m_is_active[i])
258  continue;
259 
260  // correlation between X[:,i] and u
261  ST dir_corr = map_u.dot(map_X.col(i));
262 
263  ST tmp1 = (max_corr-corr[i])/(AA-dir_corr);
264  ST tmp2 = (max_corr+corr[i])/(AA+dir_corr);
265  #pragma omp critical
266  {
267  if (tmp1 > CMath::MACHINE_EPSILON && tmp1 < gamma)
268  gamma = tmp1;
269  if (tmp2 > CMath::MACHINE_EPSILON && tmp2 < gamma)
270  gamma = tmp2;
271  }
272  }
273  }
274 
275  int32_t i_kick=-1;
276  int32_t i_change=i_max_corr;
277  if (m_lasso)
278  {
279  // lasso modification to further refine gamma
280  lasso_cond = false;
281  ST lasso_bound = numeric_limits<ST>::max();
282 
283  for (index_t i=0; i < m_num_active; ++i)
284  {
285  ST tmp = -beta[m_active_set[i]] / wA(i);
286  if (tmp > CMath::MACHINE_EPSILON && tmp < lasso_bound)
287  {
288  lasso_bound = tmp;
289  i_kick = i;
290  }
291  }
292 
293  if (lasso_bound < gamma)
294  {
295  gamma = lasso_bound;
296  lasso_cond = true;
297  i_change = m_active_set[i_kick];
298  }
299  }
300 
301  // update prediction: mu = mu + gamma * u
302  map_mu += gamma*map_u;
303 
304  // update estimator
305  for (index_t i=0; i < m_num_active; ++i)
306  beta[m_active_set[i]] += gamma * wA(i);
307  // early stopping on max l1-norm
308  if (m_max_l1_norm > 0)
309  {
310  ST l1 = SGVector<ST>::onenorm(&beta[0], n_fea);
311  if (l1 > m_max_l1_norm)
312  {
313  // stopping with interpolated beta
314  stop_cond = true;
315  lasso_cond = false;
316  ST l1_prev = (ST) SGVector<ST>::onenorm(&m_beta_path_t[nloop][0], n_fea);
317  ST s = (m_max_l1_norm-l1_prev)/(l1-l1_prev);
318 
319  typename SGVector<ST>::EigenVectorXtMap map_beta(&beta[0], n_fea);
320  typename SGVector<ST>::EigenVectorXtMap map_beta_prev(&m_beta_path_t[nloop][0], n_fea);
321  map_beta = (1-s)*map_beta_prev + s*map_beta;
322  }
323  }
324 
325  // if lasso cond, drop the variable from active set
326  if (lasso_cond)
327  {
328  beta[i_change] = 0;
329  R=cholesky_delete(R, i_kick);
330  deactivate_variable(i_kick);
331 
332  // Remove column from active set
333  int32_t numRows = map_Xa.rows();
334  int32_t numCols = map_Xa.cols()-1;
335  if( i_kick < numCols )
336  map_Xa.block(0, i_kick, numRows, numCols-i_kick) =
337  map_Xa.block(0, i_kick+1, numRows, numCols-i_kick).eval();
338  }
339 
340  nloop++;
341  m_beta_path_t.push_back(beta);
342  if (size_t(m_num_active) >= m_beta_idx.size())
343  m_beta_idx.push_back(nloop);
344  else
345  m_beta_idx[m_num_active] = nloop;
346 
347  // early stopping with max number of non-zero variables
348  if (m_max_nonz > 0 && m_num_active >= m_max_nonz)
349  stop_cond = true;
350  SG_DEBUG("Added : %d , Dropped %d, Active set size %d max_corr %.17f \n", i_max_corr, i_kick, m_num_active, max_corr);
351  }
352 
353  //copy m_beta_path_t (of type ST) into m_beta_path
354  for(index_t i = 0; i < m_beta_path_t.size(); ++i)
355  {
356  std::vector<float64_t> va;
357  for(index_t p = 0; p < m_beta_path_t[i].size(); ++p){
358  va.push_back((float64_t) m_beta_path_t[i][p]);
359  }
360  m_beta_path.push_back(va);
361  }
362 
363  // assign default estimator
364  w.vlen = n_fea;
365  switch_w(m_beta_idx.size()-1);
366 
367  return true;
368 }
369 
370 template <typename ST>
372  const SGMatrix<ST>& X_active, SGMatrix<ST>& R, int32_t i_max_corr, int32_t num_active)
373 {
374  typename SGMatrix<ST>::EigenMatrixXtMap map_X(X.matrix, X.num_rows, X.num_cols);
375  typename SGMatrix<ST>::EigenMatrixXtMap map_X_active(X_active.matrix, X.num_rows, num_active);
376  ST diag_k = map_X.col(i_max_corr).dot(map_X.col(i_max_corr));
377 
378  // col_k is the k-th column of (X'X)
379  typename SGVector<ST>::EigenVectorXtMap map_i_max(X.get_column_vector(i_max_corr), X.num_rows);
380  typename SGVector<ST>::EigenVectorXt R_k = map_X_active.transpose()*map_i_max;
381  typename SGMatrix<ST>::EigenMatrixXtMap map_R(R.matrix, R.num_rows, R.num_cols);
382 
383  // R' * R_k = (X' * X)_k = col_k, solving to get R_k
384  map_R.transpose().template triangularView<Lower>().template solveInPlace<OnTheLeft>(R_k);
385  ST R_kk = CMath::sqrt(diag_k - R_k.dot(R_k));
386 
387  SGMatrix<ST> R_new(num_active+1, num_active+1);
388  typename SGMatrix<ST>::EigenMatrixXtMap map_R_new(R_new.matrix, R_new.num_rows, R_new.num_cols);
389 
390  map_R_new.block(0, 0, num_active, num_active) = map_R;
391  memcpy(R_new.matrix+num_active*(num_active+1), R_k.data(), sizeof(ST)*(num_active));
392  map_R_new.row(num_active).setZero();
393  map_R_new(num_active, num_active) = R_kk;
394  return R_new;
395 }
396 
397 template <typename ST>
399 {
400  if (i_kick != m_num_active-1)
401  {
402  // remove i_kick-th column
403  for (index_t j=i_kick; j < m_num_active-1; ++j)
404  for (index_t i=0; i < m_num_active; ++i)
405  R(i,j) = R(i,j+1);
406 
407  SGMatrix<ST> G(2,2);
408  for (index_t i=i_kick; i < m_num_active-1; ++i)
409  {
410  plane_rot(R(i,i),R(i+1,i), R(i,i), R(i+1,i), G);
411  if (i < m_num_active-2)
412  {
413  for (index_t k=i+1; k < m_num_active-1; ++k)
414  {
415  // R[i:i+1, k] = G*R[i:i+1, k]
416  ST Rik = R(i,k), Ri1k = R(i+1,k);
417  R(i,k) = G(0,0)*Rik + G(0,1)*Ri1k;
418  R(i+1,k) = G(1,0)*Rik+G(1,1)*Ri1k;
419  }
420  }
421  }
422  }
423 
424  SGMatrix<ST> nR(m_num_active-1, m_num_active-1);
425  for (index_t i=0; i < m_num_active-1; ++i)
426  for (index_t j=0; j < m_num_active-1; ++j)
427  nR(i,j) = R(i,j);
428 
429  return nR;
430 }
431 
432 template bool CLeastAngleRegression::train_machine_templated<float64_t>(CDenseFeatures<float64_t> * data);
433 template bool CLeastAngleRegression::train_machine_templated<float32_t>(CDenseFeatures<float32_t> * data);
virtual const char * get_name() const =0
static const float64_t MACHINE_EPSILON
Definition: Math.h:2058
SGMatrix< ST > cholesky_delete(SGMatrix< ST > &R, int32_t i_kick)
virtual ELabelType get_label_type() const =0
Real Labels are real-valued labels.
int32_t get_num_features() const
int32_t index_t
Definition: common.h:62
virtual int32_t get_num_labels() const =0
real valued labels (e.g. for regression, classifier outputs)
Definition: LabelTypes.h:22
SGMatrix< ST > get_feature_matrix()
Definition: SGMatrix.h:20
virtual int32_t get_num_vectors() const =0
Definition: basetag.h:132
CLabels * m_labels
Definition: Machine.h:361
#define SG_ERROR(...)
Definition: SGIO.h:129
index_t num_cols
Definition: SGMatrix.h:376
static void find_max_abs(const std::vector< ST > &vec, const std::vector< bool > &ignore_mask, int32_t &imax, ST &vmax)
index_t num_rows
Definition: SGMatrix.h:374
int32_t size() const
Definition: SGVector.h:113
index_t vlen
Definition: SGVector.h:492
static void plane_rot(ST x0, ST x1, ST &y0, ST &y1, SGMatrix< ST > &G)
void switch_w(int32_t num_variable)
virtual int32_t get_num_vectors() const
shogun vector
double float64_t
Definition: common.h:50
SGVector< float64_t > w
virtual EFeatureClass get_feature_class() const =0
T * get_column_vector(index_t col) const
Definition: SGMatrix.h:113
Class LinearMachine is a generic interface for all kinds of linear machines like classifiers.
Definition: LinearMachine.h:63
CDotFeatures * features
#define SG_DEBUG(...)
Definition: SGIO.h:107
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
SGMatrix< ST > cholesky_insert(const SGMatrix< ST > &X, const SGMatrix< ST > &X_active, SGMatrix< ST > &R, int32_t i_max_corr, int32_t num_active)
static T sign(T a)
Definition: Math.h:426
The class Features is the base class of all feature objects.
Definition: Features.h:68
static T min(T a, T b)
Definition: Math.h:157
static float64_t onenorm(T *x, int32_t len)
|| x ||_1
Definition: SGVector.cpp:713
Matrix::Scalar max(Matrix m)
Definition: Redux.h:68
#define SG_ADD(...)
Definition: SGObject.h:84
static float32_t sqrt(float32_t x)
Definition: Math.h:459
T * data() const
Definition: SGVector.h:116
virtual EFeatureType get_feature_type() const =0
static T abs(T a)
Definition: Math.h:179

SHOGUN Machine Learning Toolbox - Documentation