Functions
test_ress_largem.cxx File Reference
#include "WireCellRess/LassoModel.h"
#include "WireCellRess/ElasticNetModel.h"
#include <Eigen/Dense>
#include <iostream>

Go to the source code of this file.

Functions

void test_model (WireCell::LinearModel &m, MatrixXd &G, VectorXd &W)
 
void test_lasso (WireCell::LassoModel &m, MatrixXd &G, VectorXd &W)
 
void print_results (WireCell::LinearModel &m, VectorXd &C)
 
int main (int argc, char *argv[])
 

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 14 of file test_ress_largem.cxx.

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 }
void print_results(WireCell::LinearModel &m, VectorXd &C)
const double e
void test_lasso(WireCell::LassoModel &m, MatrixXd &G, VectorXd &W)
static const double m2
Definition: Units.h:80
void print_results ( WireCell::LinearModel m,
VectorXd &  C 
)

Definition at line 75 of file test_ress_largem.cxx.

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 }
Eigen::VectorXd & Getbeta()
Definition: LinearModel.h:16
QTextStream & endl(QTextStream &s)
void test_lasso ( WireCell::LassoModel m,
MatrixXd &  G,
VectorXd &  W 
)

Definition at line 64 of file test_ress_largem.cxx.

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 }
virtual void SetData(Eigen::MatrixXd X, Eigen::VectorXd y)
Definition: LinearModel.h:18
void test_model ( WireCell::LinearModel m,
MatrixXd &  G,
VectorXd &  W 
)

Definition at line 57 of file test_ress_largem.cxx.

58 {
59  m.SetData(G, W);
60  m.Fit();
61 
62 }
virtual void SetData(Eigen::MatrixXd X, Eigen::VectorXd y)
Definition: LinearModel.h:18
virtual void Fit()
Definition: LinearModel.h:23