bpp-core3  3.0.0
CorrespondenceAnalysis.cpp
Go to the documentation of this file.
1 //
2 // File: CorrespondenceAnalysis.cpp
3 // Authors:
4 // Mathieu Groussin
5 // Created: 2011-03-06 10:03:00
6 //
7 
8 /*
9  Copyright or © or Copr. Bio++ Development Team, (November 16, 2004)
10 
11  This software is a computer program whose purpose is to provide classes
12  for phylogenetic data analysis.
13 
14  This software is governed by the CeCILL license under French law and
15  abiding by the rules of distribution of free software. You can use,
16  modify and/ or redistribute the software under the terms of the CeCILL
17  license as circulated by CEA, CNRS and INRIA at the following URL
18  "http://www.cecill.info".
19 
20  As a counterpart to the access to the source code and rights to copy,
21  modify and redistribute granted by the license, users are provided only
22  with a limited warranty and the software's author, the holder of the
23  economic rights, and the successive licensors have only limited
24  liability.
25 
26  In this respect, the user's attention is drawn to the risks associated
27  with loading, using, modifying and/or developing or reproducing the
28  software by the user in light of its specific status of free software,
29  that may mean that it is complicated to manipulate, and that also
30  therefore means that it is reserved for developers and experienced
31  professionals having in-depth computer knowledge. Users are therefore
32  encouraged to load and test the software's suitability as regards their
33  requirements in conditions enabling the security of their systems and/or
34  data to be ensured and, more generally, to use and operate it in the
35  same conditions as regards security.
36 
37  The fact that you are presently reading this means that you have had
38  knowledge of the CeCILL license and that you accept its terms.
39 */
40 
41 
42 #include "../../Matrix/Matrix.h"
43 #include "../../Matrix/MatrixTools.h"
44 #include "CorrespondenceAnalysis.h"
45 #include "DualityDiagram.h"
46 
47 using namespace bpp;
48 using namespace std;
49 
51  const Matrix<double>& data,
52  unsigned int nbAxes,
53  double tol, bool verbose) :
55  n_()
56 {
57  size_t nRow = data.getNumberOfRows();
58  size_t nCol = data.getNumberOfColumns();
59 
60  double tmp = 0.;
61  for (size_t i = 0; i < nRow; i++)
62  {
63  for (unsigned int j = 0; j < nCol; j++)
64  {
65  if (data(i, j) < 0.)
66  throw Exception("CorrespondenceAnalysis (constructor). Negative value(s) in the input data. This is not allowed !");
67  tmp += data(i, j);
68  }
69  }
70  n_ = tmp;
71 
72  if (n_ == 0)
73  throw Exception("CorrespondenceAnalysis (constructor). All frequencies in the input data are zero...");
74 
75  RowMatrix<double> dataTmp(data);
76  MatrixTools::scale(dataTmp, (1. / n_), 0.);
77 
78  vector<double> rowWeights(nRow);
79  vector<double> colWeights(nCol);
80 
81  for (size_t i = 0; i < nRow; i++)
82  {
83  for (unsigned int j = 0; j < nCol; j++)
84  {
85  rowWeights[i] += dataTmp(i, j);
86  colWeights[j] += dataTmp(i, j);
87  }
88  }
89 
90  vector<double> tmpRowWeigths(nRow);
91  vector<double> tmpColWeigths(nCol);
92  for (size_t i = 0; i < rowWeights.size(); i++)
93  {
94  if (rowWeights[i] == 0.)
95  tmpRowWeigths[i] = 0.;
96  else
97  tmpRowWeigths[i] = 1. / rowWeights[i];
98  }
99  for (size_t j = 0; j < colWeights.size(); j++)
100  {
101  if (colWeights[j] == 0.)
102  tmpColWeigths[j] = 0.;
103  else
104  tmpColWeigths[j] = 1. / colWeights[j];
105  }
106 
107  RowMatrix<double> tmpWeightedData(nRow, nCol);
108  RowMatrix<double> weightedData(nRow, nCol);
109  MatrixTools::hadamardMult(dataTmp, tmpRowWeigths, tmpWeightedData, true);
110  MatrixTools::hadamardMult(tmpWeightedData, tmpColWeigths, weightedData, false);
111  MatrixTools::scale(weightedData, 1., -1.);
112 
113  setData(weightedData, rowWeights, colWeights, nbAxes, tol, verbose);
114 }
CorrespondenceAnalysis(const Matrix< double > &data, unsigned int nbAxes, double tol=0.0000001, bool verbose=true)
Build a new CorrespondenceAnalysis object.
The core class of a multivariate analysis.
void setData(const Matrix< double > &matrix, const std::vector< double > &rowWeights, const std::vector< double > &colWeights, unsigned int nbAxes, double tol=0.0000001, bool verbose=true)
Set the data and perform computations.
Exception base class. Overload exception constructor (to control the exceptions mechanism)....
Definition: Exceptions.h:59
static void hadamardMult(const Matrix< Scalar > &A, const Matrix< Scalar > &B, Matrix< Scalar > &O)
Compute the Hadamard product of two row matrices with same dimensions.
Definition: MatrixTools.h:1055
static void scale(Matrix &A, Scalar a, Scalar b=0)
Multiply all elements of a matrix by a given value, and add a constant.
Definition: MatrixTools.h:239
The matrix template interface.
Definition: Matrix.h:61
virtual size_t getNumberOfColumns() const =0
virtual size_t getNumberOfRows() const =0