-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathGrid.cpp
More file actions
70 lines (56 loc) · 2.32 KB
/
Grid.cpp
File metadata and controls
70 lines (56 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "Grid.h"
// Assumes PG.P.Specs have been already set
template <class T>
Grid<T>::Grid(const T& X, const arma::vec& y, const GridParams<T>& PGi) {
PG = PGi;
std::tie(BetaMultiplier, meanX, meany, scaley) = Normalize(X,
y, Xscaled, yscaled, !PG.P.Specs.Classification, PG.intercept);
// Must rescale bounds by BetaMultiplier in order for final result to conform to bounds
if (PG.P.withBounds){
PG.P.Lows /= BetaMultiplier;
PG.P.Highs /= BetaMultiplier;
}
}
template <class T>
void Grid<T>::Fit() {
std::vector<std::vector<std::unique_ptr<FitResult<T>>>> G;
if (PG.P.Specs.L0) {
G.push_back(std::move(Grid1D<T>(Xscaled, yscaled, PG).Fit()));
Lambda12.push_back(0);
} else {
G = std::move(Grid2D<T>(Xscaled, yscaled, PG).Fit());
}
Lambda0 = std::vector< std::vector<double> >(G.size());
NnzCount = std::vector< std::vector<std::size_t> >(G.size());
Solutions = std::vector< std::vector<arma::sp_mat> >(G.size());
Intercepts = std::vector< std::vector<double> >(G.size());
Converged = std::vector< std::vector<bool> >(G.size());
for (std::size_t i=0; i<G.size(); ++i) {
if (PG.P.Specs.L0L1){
Lambda12.push_back(G[i][0]->ModelParams[1]);
} else if (PG.P.Specs.L0L2) {
Lambda12.push_back(G[i][0]->ModelParams[2]);
}
for (auto &g : G[i]) {
Lambda0[i].push_back(g->ModelParams[0]);
NnzCount[i].push_back(n_nonzero(g->B));
if (g->IterNum != PG.P.MaxIters){
Converged[i].push_back(true);
} else {
Converged[i].push_back(false);
}
beta_vector B_unscaled;
double b0;
std::tie(B_unscaled, b0) = DeNormalize(g->B, BetaMultiplier, meanX, meany);
Solutions[i].push_back(arma::sp_mat(B_unscaled));
/* scaley is 1 for classification problems.
* g->intercept is 0 unless specifically optimized for in:
* classification
* sparse regression and intercept = true
*/
Intercepts[i].push_back(scaley*g->b0 + b0);
}
}
}
template class Grid<arma::mat>;
template class Grid<arma::sp_mat>;