-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patheigen_ridge.hpp
More file actions
23 lines (18 loc) · 819 Bytes
/
eigen_ridge.hpp
File metadata and controls
23 lines (18 loc) · 819 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef _EIGEN_RIDGE_HPP_
#define _EIGEN_RIDGE_HPP_
/*
* L2-regularized (ridge) linear regression without intercept in c++11 as Eigen3 template function
* Uses singular value decomposition, works with both tall and wide design matrices
* Written by Carlos Guerreiro carlos@perceptiveconstructs.com
* This is free and unencumbered software released into the public domain.
*/
#include <Eigen/Dense>
template<typename M, typename V, typename P>
M ridge(const M& A, const V& y, P alpha) {
const auto& svd = A.jacobiSvd(Eigen::ComputeFullU | Eigen::ComputeFullV);
const auto& s = svd.singularValues();
const auto r = s.rows();
const auto& D = s.cwiseQuotient((s.array().square() + alpha).matrix()).asDiagonal();
return svd.matrixV().leftCols(r) * D * svd.matrixU().transpose().topRows(r) * y;
}
#endif