test_ress_largem.cxx
Go to the documentation of this file.
3 
4 #include <Eigen/Dense>
5 using namespace Eigen;
6 
7 #include <iostream>
8 using namespace std;
9 
10 void test_model(WireCell::LinearModel& m, MatrixXd& G, VectorXd& W);
11 void test_lasso(WireCell::LassoModel& m, MatrixXd& G, VectorXd& W);
12 void print_results(WireCell::LinearModel& m, VectorXd& C);
13 
14 int main(int argc, char* argv[])
15 {
16  // std::srand((unsigned int) time(0));
17 
18  const int N_CELL = 500;
19  const int N_NZERO = 50;
20  const int N_WIRE = int(N_CELL * 0.5);
21 
22  VectorXd C = VectorXd::Zero(N_CELL);
23  for (int i=0; i<N_NZERO; i++) {
24  int index = int( N_CELL/2 * (VectorXd::Random(1)(0)+1) );
25  // cout << index << endl;
26  C(index) = VectorXd::Random(1)(0)*50 + 150;
27  }
28 
29  // initialize G matrix: N_WIRE rows and N_CELL columns. (geometry matrix)
30  MatrixXd G = MatrixXd::Random(N_WIRE, N_CELL);
31 
32 
33  // W vector is the measured charge on wires.
34  VectorXd W = G * C;
35 
36  // cout << "geometry matrix:" << endl;
37  // cout << G << endl << endl;
38 
39  // cout << "measured charge on each wire:" << endl;
40  // cout << W.transpose() << endl << endl;
41 
42  // cout << "true charge of each cell:" << endl;
43  // cout << C.transpose() << endl << endl;
44  double lambda = 1;
45 
46  // WireCell::ElasticNetModel m(lambda, 0.95, 100000, 1e-4);
47  // test_model(m, G, W);
48  // print_results(m, C);
49 
50  WireCell::LassoModel m2(lambda, 10000, 1e-3);
51  test_lasso(m2, G, W);
52  print_results(m2, C);
53 
54  return 0;
55 }
56 
57 void test_model(WireCell::LinearModel& m, MatrixXd& G, VectorXd& W)
58 {
59  m.SetData(G, W);
60  m.Fit();
61 
62 }
63 
64 void test_lasso(WireCell::LassoModel& m, MatrixXd& G, VectorXd& W)
65 {
66  m.SetData(G, W);
67 
68  // one can set the weight of each cell's regularization.
69  // m.SetLambdaWeight(0, 10.);
70  // m.SetLambdaWeight(2, 10.);
71  // m.SetLambdaWeight(6, 10.);
72  m.Fit();
73 }
74 
76 {
77  VectorXd beta = m.Getbeta();
78 
79  // cout << "fitted charge of each cell: " << m.name << endl;
80  // cout << beta.transpose() << endl << endl;
81 
82  // cout << "predicted charge on each wire: Lasso" << endl;
83  // cout << m.Predict().transpose() << endl << endl;
84 
85  cout << "average residual charge difference per wire: " << m.name << ": "
86  << m.MeanResidual() << endl;
87 
88  int nbeta = beta.size();
89 
90  int n_zero_true = 0;
91  int n_zero_beta = 0;
92  int n_zero_correct = 0;
93 
94  for (int i=0; i<nbeta; i++) {
95  if (fabs(C(i))<0.1) n_zero_true++;
96  if (fabs(beta(i))<5) n_zero_beta++;
97  if (fabs(C(i))<0.1 && (fabs(C(i) - beta(i)) < 10)) n_zero_correct++;
98  }
99  cout << "true zeros: " << n_zero_true << endl;
100  cout << "fitted zeros: " << n_zero_beta << endl;
101  cout << "correct fitted zeros: " << n_zero_correct << endl;
102 
103  cout << endl;
104 }
static const double m
Definition: Units.h:79
void print_results(WireCell::LinearModel &m, VectorXd &C)
void test_model(WireCell::LinearModel &m, MatrixXd &G, VectorXd &W)
STL namespace.
int main(int argc, char *argv[])
const double e
Eigen::VectorXd & Getbeta()
Definition: LinearModel.h:16
virtual void SetData(Eigen::MatrixXd X, Eigen::VectorXd y)
Definition: LinearModel.h:18
void test_lasso(WireCell::LassoModel &m, MatrixXd &G, VectorXd &W)
static const double m2
Definition: Units.h:80
virtual void Fit()
Definition: LinearModel.h:23
QTextStream & endl(QTextStream &s)