Skip to content

Commit

Permalink
Implemented covariance() in Matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
hosseinmoein committed Dec 12, 2024
1 parent ed26e48 commit 18fc7df
Showing 3 changed files with 58 additions and 21 deletions.
4 changes: 4 additions & 0 deletions docs/HTML/DataFrame.html
Original file line number Diff line number Diff line change
@@ -1517,6 +1517,10 @@ <H2 ID="2"><font color="blue">API Reference with code samples <font size="+4">&#
<td title="Average around average types">enum class <a href="https://htmlpreview.github.io/?https://github.com/hosseinmoein/DataFrame/blob/master/docs/HTML/MADVisitor.html">mad_type</a>{}</td>
</tr>

<tr class="item" onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
<td title="Matrix orientation">struct <a href="https://htmlpreview.github.io/?https://github.com/hosseinmoein/DataFrame/blob/master/docs/HTML/Matrix.html">matrix_orient</a>{}</td>
</tr>

<tr class="item" onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
<td title="Mean-shift clustering kernel types">enum class <a href="https://htmlpreview.github.io/?https://github.com/hosseinmoein/DataFrame/blob/master/docs/HTML/MeanShiftVisitor.html">mean_shift_kernel</a>{}</td>
</tr>
24 changes: 8 additions & 16 deletions include/DataFrame/Utils/Matrix.tcc
Original file line number Diff line number Diff line change
@@ -331,7 +331,7 @@ template<typename MA1, typename MA2>
void Matrix<T, MO>::
red_to_hessenberg_(MA1 &e_vecs, MA2 &hess_form) noexcept {

Matrix<T, matrix_orient::row_major> ortho { 1, e_vecs.cols() };
Matrix<T, matrix_orient::row_major> ortho { 1, e_vecs.cols() };

for (size_type c = 1; c <= e_vecs.cols() - 2; ++c) {
value_type scale { 0 };
@@ -1215,7 +1215,6 @@ eigen_space(MA1 &eigenvalues, MA2 &eigenvectors, bool sort_values) const {
return;
}


// ----------------------------------------------------------------------------

template<typename T, matrix_orient MO>
@@ -1227,29 +1226,22 @@ covariance(bool is_unbiased) const {
if (denom <= value_type(0))
throw DataFrameError("Matrix::covariance(): Not solvable");

Matrix result (cols(), cols());
row_iterator res_iter = result.row_begin ();
Matrix result (cols(), cols(), T(0));

// I don't know how I can take advantage of the fact this is a
// symmetric matrix by definition
//
for (size_type c = 0; c < cols(); ++c) {
for (size_type cr = 0; cr < cols(); ++cr) {
value_type col_mean { 0 };
size_type counter { 0 };

for (auto cciter = col_begin() + c * rows();
counter != rows(); ++cciter, ++counter)
col_mean += *cciter;

for (size_type r = 0; r < rows(); ++r)
col_mean += at(r, cr);
col_mean /= value_type(rows());

for (size_type cc = 0; cc < cols(); ++cc) {
for (size_type c = 0; c < cols(); ++c) {
value_type var_covar { 0 };

for (size_type r = 0; r < rows(); ++r)
var_covar += (at(r, c) - col_mean) * (at(r, cc) - col_mean);
var_covar += (at(r, cr) - col_mean) * (at(r, c) - col_mean);

*res_iter++ = var_covar / denom;
result(cr, c) = var_covar / denom;
}
}

51 changes: 46 additions & 5 deletions test/matrix_tester.cc
Original file line number Diff line number Diff line change
@@ -32,15 +32,15 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

using namespace hmdf;

// -----------------------------------------------------------------------------
// ----------------------------------------------------------------------------

using row_mat_t = Matrix<std::size_t, matrix_orient::row_major>;
using col_mat_t = Matrix<std::size_t, matrix_orient::column_major>;

static constexpr long ROWS = 5;
static constexpr long COLS = 6;

// -----------------------------------------------------------------------------
// ----------------------------------------------------------------------------

int main(int, char *[]) {

@@ -64,7 +64,7 @@ int main(int, char *[]) {
assert((row_matb(1, 1) == col_matb(1, 1) && row_matb(1, 1) == 81));
assert((row_matb(2, 1) == col_matb(2, 1) && row_matb(2, 1) == 126));
assert((row_matb(2, 2) == col_matb(2, 2) && row_matb(2, 2) == 150));

row_mat_t row_mat { ROWS, COLS };
col_mat_t col_mat { ROWS, COLS };

@@ -247,7 +247,7 @@ int main(int, char *[]) {
col_dmat_t col_mat { 10, 10 };
col_dmat_t eigenvals;
col_dmat_t eigenvecs;
std::size_t value { 0 };
std::size_t value { 0 };

// Symmetric matrix
//
@@ -297,10 +297,51 @@ int main(int, char *[]) {
assert((std::fabs(eigenvecs(9, 9) - -0.51616) < 0.00001));
}

{
using col_dmat_t = Matrix<double, matrix_orient::column_major>;

col_dmat_t col_mat { 5, 4 };

col_mat(0, 0) = 4.0;
col_mat(0, 1) = 2.0;
col_mat(0, 2) = 0.6;
col_mat(0, 3) = 3.0;

col_mat(1, 0) = 4.2;
col_mat(1, 1) = 2.1;
col_mat(1, 2) = 0.59;
col_mat(1, 3) = 3.2;

col_mat(2, 0) = 3.9;
col_mat(2, 1) = 2.0;
col_mat(2, 2) = 0.58;
col_mat(2, 3) = 2.89;

col_mat(3, 0) = 4.3;
col_mat(3, 1) = 2.1;
col_mat(3, 2) = 0.62;
col_mat(3, 3) = 3.298;

col_mat(4, 0) = 4.1;
col_mat(4, 1) = 2.2;
col_mat(4, 2) = 0.63;
col_mat(4, 3) = 3.098;

const auto cov = col_mat.covariance(true);

assert(cov.cols() == 4);
assert(cov.rows() == 4);
assert((std::fabs(cov(0, 0) - 0.025) < 0.001));
assert((std::fabs(cov(0, 3) - 0.0254) < 0.0001));
assert((std::fabs(cov(2, 3) - 0.001789) < 0.000001));
assert((std::fabs(cov(3, 1) - 0.00763) < 0.00001));
assert((std::fabs(cov(3, 3) - 0.0258172) < 0.0000001));
}

return (0);
}

// -----------------------------------------------------------------------------
// ----------------------------------------------------------------------------

// Local Variables:
// mode:C++

0 comments on commit 18fc7df

Please sign in to comment.