OpenWalnut  1.5.0dev
WCovarianceSolver.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <vector>
26 #include "WCovarianceSolver.h"
27 
29 {
30 }
31 
33 {
34 }
35 void WCovarianceSolver::analyzeData( const vector<WPosition>& dataSet )
36 {
37  calculateMean( dataSet );
38  calculateCovariance( dataSet );
39 }
41 {
42  return m_mean;
43 }
44 void WCovarianceSolver::calculateMean( const vector<WPosition>& dataSet )
45 {
46  if( dataSet.size() == 0 )
47  return;
48  size_t dimensions = dataSet[0].size();
49  for( size_t dimension = 0; dimension < dimensions; dimension++ )
50  m_mean[dimension] = 0.0;
51  for( size_t index = 0; index < dataSet.size(); index++ )
52  for( size_t dimension = 0; dimension < dataSet[index].size(); dimension++ )
53  m_mean[dimension] += dataSet[index][dimension];
54  for( size_t dimension = 0; dimension < dimensions; dimension++ )
55  m_mean[dimension] /= static_cast<double>( dataSet.size() );
56 }
57 void WCovarianceSolver::calculateCovariance( const vector<WPosition>& dataSet )
58 {
59  size_t width = m_mean.size();
60  MatrixXd newCovariance( width, width );
61  m_covariance = newCovariance;
62  for( size_t row = 0; row < width; row++ )
63  for( size_t col = 0; col < width; col++ )
64  m_covariance( row, col ) = 0.0;
65  for( size_t index = 0; index < dataSet.size(); index++ )
66  addPointToCovariance( dataSet[index] );
67  for( size_t row = 0; row < width; row++ )
68  for( size_t col = 0; col < width; col++ )
69  m_covariance( row, col ) /= static_cast<double>( dataSet.size() ) - 1.0;
70 }
72 {
73  size_t width = m_mean.size();
74  for( size_t row = 0; row < width; row++ )
75  {
76  double rowValue = point[row] - m_mean[row];
77  for( size_t col = 0; col < width; col++ )
78  {
79  double colValue = point[col] - m_mean[col];
80  m_covariance( row, col ) += rowValue * colValue;
81  }
82  }
83 }
85 {
86  return m_covariance;
87 }
WPosition getMean()
Returns the mean coordinate of the input point data set.
virtual ~WCovarianceSolver()
Destroys the instance of the Covariance solver.
void calculateCovariance(const vector< WPosition > &dataSet)
Calculates the covariance of the input point data.
void addPointToCovariance(const WPosition &point)
Adds a point to the covariance matrix.
WPosition m_mean
The mean of all input points.
MatrixXd m_covariance
The calculated covariance matrix.
MatrixXd getCovariance()
Returns the covariance matrix corresponding to the input point data set.
void analyzeData(const vector< WPosition > &dataSet)
Analyzes the dimension covariances of a point data set.
void calculateMean(const vector< WPosition > &dataSet)
Calculates the mean of the input point data coordinates.
WCovarianceSolver()
Creates an instance of the Covariance solver.
This only is a 3d double vector.