bpp-core3  3.0.0
CorrespondenceAnalysis.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #include "../../Matrix/Matrix.h"
6 #include "../../Matrix/MatrixTools.h"
8 #include "DualityDiagram.h"
9 
10 using namespace bpp;
11 using namespace std;
12 
14  const Matrix<double>& data,
15  unsigned int nbAxes,
16  double tol, bool verbose) :
18  n_()
19 {
20  size_t nRow = data.getNumberOfRows();
21  size_t nCol = data.getNumberOfColumns();
22 
23  double tmp = 0.;
24  for (size_t i = 0; i < nRow; i++)
25  {
26  for (unsigned int j = 0; j < nCol; j++)
27  {
28  if (data(i, j) < 0.)
29  throw Exception("CorrespondenceAnalysis (constructor). Negative value(s) in the input data. This is not allowed !");
30  tmp += data(i, j);
31  }
32  }
33  n_ = tmp;
34 
35  if (n_ == 0)
36  throw Exception("CorrespondenceAnalysis (constructor). All frequencies in the input data are zero...");
37 
38  RowMatrix<double> dataTmp(data);
39  MatrixTools::scale(dataTmp, (1. / n_), 0.);
40 
41  vector<double> rowWeights(nRow);
42  vector<double> colWeights(nCol);
43 
44  for (size_t i = 0; i < nRow; i++)
45  {
46  for (unsigned int j = 0; j < nCol; j++)
47  {
48  rowWeights[i] += dataTmp(i, j);
49  colWeights[j] += dataTmp(i, j);
50  }
51  }
52 
53  vector<double> tmpRowWeigths(nRow);
54  vector<double> tmpColWeigths(nCol);
55  for (size_t i = 0; i < rowWeights.size(); i++)
56  {
57  if (rowWeights[i] == 0.)
58  tmpRowWeigths[i] = 0.;
59  else
60  tmpRowWeigths[i] = 1. / rowWeights[i];
61  }
62  for (size_t j = 0; j < colWeights.size(); j++)
63  {
64  if (colWeights[j] == 0.)
65  tmpColWeigths[j] = 0.;
66  else
67  tmpColWeigths[j] = 1. / colWeights[j];
68  }
69 
70  RowMatrix<double> tmpWeightedData(nRow, nCol);
71  RowMatrix<double> weightedData(nRow, nCol);
72  MatrixTools::hadamardMult(dataTmp, tmpRowWeigths, tmpWeightedData, true);
73  MatrixTools::hadamardMult(tmpWeightedData, tmpColWeigths, weightedData, false);
74  MatrixTools::scale(weightedData, 1., -1.);
75 
76  setData(weightedData, rowWeights, colWeights, nbAxes, tol, verbose);
77 }
The matrix template interface.
Definition: Matrix.h:22
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.
STL namespace.
virtual size_t getNumberOfColumns() const =0
Exception base class. Overload exception constructor (to control the exceptions mechanism). Destructor is already virtual (from std::exception)
Definition: Exceptions.h:20
CorrespondenceAnalysis(const Matrix< double > &data, unsigned int nbAxes, double tol=0.0000001, bool verbose=true)
Build a new CorrespondenceAnalysis object.
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:201
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:1017
virtual size_t getNumberOfRows() const =0
The core class of a multivariate analysis.