mirror of
https://github.com/aportelli/LatAnalyze.git
synced 2024-11-10 00:45:36 +00:00
moving variance matrix to StatArray
This commit is contained in:
parent
145155f733
commit
fde57d79f3
@ -105,8 +105,6 @@ public:
|
||||
void resizeMat(const Index nRow, const Index nCol);
|
||||
// covariance matrix
|
||||
Mat<T> covarianceMatrix(const MatSample<T> &sample) const;
|
||||
Mat<T> varianceMatrix(void) const;
|
||||
Mat<T> correlationMatrix(void) const;
|
||||
};
|
||||
|
||||
// non-member operators
|
||||
@ -417,45 +415,6 @@ Mat<T> MatSample<T>::covarianceMatrix(const MatSample<T> &sample) const
|
||||
return res;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Mat<T> MatSample<T>::varianceMatrix(void) const
|
||||
{
|
||||
if ((*this)[central].cols() != 1)
|
||||
{
|
||||
LATAN_ERROR(Size, "samples have more than one column");
|
||||
}
|
||||
|
||||
Index n1 = (*this)[central].rows();
|
||||
Index nSample = this->size();
|
||||
Mat<T> tmp1(n1, nSample), res(n1, n1);
|
||||
Mat<T> s1(n1, 1), one(nSample, 1);
|
||||
|
||||
one.fill(1.);
|
||||
s1.fill(0.);
|
||||
for (unsigned int s = 0; s < nSample; ++s)
|
||||
{
|
||||
s1 += (*this)[s];
|
||||
tmp1.col(s) = (*this)[s];
|
||||
}
|
||||
tmp1 -= s1*one.transpose()/static_cast<double>(nSample);
|
||||
res = tmp1*tmp1.transpose()/static_cast<double>(nSample - 1);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Mat<T> MatSample<T>::correlationMatrix(void) const
|
||||
{
|
||||
Mat<T> res = varianceMatrix();
|
||||
Mat<T> invDiag(res.rows(), 1);
|
||||
|
||||
invDiag = res.diagonal();
|
||||
invDiag = invDiag.cwiseInverse().cwiseSqrt();
|
||||
res = (invDiag*invDiag.transpose()).cwiseProduct(res);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
END_LATAN_NAMESPACE
|
||||
|
||||
#endif // Latan_MatSample_hpp_
|
||||
|
@ -56,6 +56,9 @@ public:
|
||||
T mean(const Index pos = 0, const Index n = -1) const;
|
||||
T covariance(const StatArray<T, os> &array) const;
|
||||
T variance(void) const;
|
||||
T varianceMatrix(void) const;
|
||||
T correlationMatrix(void) const;
|
||||
|
||||
// IO type
|
||||
virtual IoType getType(void) const;
|
||||
public:
|
||||
@ -192,6 +195,45 @@ T StatArray<T, os>::variance(void) const
|
||||
return covariance(*this);
|
||||
}
|
||||
|
||||
template <typename MatType, Index os>
|
||||
MatType StatArray<MatType, os>::varianceMatrix(void) const
|
||||
{
|
||||
if ((*this)[0].cols() != 1)
|
||||
{
|
||||
LATAN_ERROR(Size, "samples have more than one column");
|
||||
}
|
||||
|
||||
Index n1 = (*this)[0].rows();
|
||||
Index nSample = this->size();
|
||||
MatType tmp1(n1, nSample), res(n1, n1);
|
||||
MatType s1(n1, 1), one(nSample, 1);
|
||||
|
||||
one.fill(1.);
|
||||
s1.fill(0.);
|
||||
for (unsigned int s = 0; s < nSample; ++s)
|
||||
{
|
||||
s1 += (*this)[s];
|
||||
tmp1.col(s) = (*this)[s];
|
||||
}
|
||||
tmp1 -= s1*one.transpose()/static_cast<double>(nSample);
|
||||
res = tmp1*tmp1.transpose()/static_cast<double>(nSample - 1);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
template <typename MatType, Index os>
|
||||
MatType StatArray<MatType, os>::correlationMatrix(void) const
|
||||
{
|
||||
MatType res = varianceMatrix();
|
||||
MatType invDiag(res.rows(), 1);
|
||||
|
||||
invDiag = res.diagonal();
|
||||
invDiag = invDiag.cwiseInverse().cwiseSqrt();
|
||||
res = (invDiag*invDiag.transpose()).cwiseProduct(res);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
// reduction operations ////////////////////////////////////////////////////////
|
||||
namespace StatOp
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user