[mlpack-git] master: add correlation coeff sdp test from wiki (6ad2c10)
gitdub at big.cc.gt.atl.ga.us
gitdub at big.cc.gt.atl.ga.us
Thu Mar 5 22:13:02 EST 2015
Repository : https://github.com/mlpack/mlpack
On branch : master
Link : https://github.com/mlpack/mlpack/compare/904762495c039e345beba14c1142fd719b3bd50e...f94823c800ad6f7266995c700b1b630d5ffdcf40
>---------------------------------------------------------------
commit 6ad2c1014d21e533ff8d906a42aaa719cb2eab1d
Author: Stephen Tu <tu.stephenl at gmail.com>
Date: Fri Jan 16 13:50:48 2015 -0800
add correlation coeff sdp test from wiki
>---------------------------------------------------------------
6ad2c1014d21e533ff8d906a42aaa719cb2eab1d
src/mlpack/tests/sdp_primal_dual_test.cpp | 109 ++++++++++++++++++++++++++++++
1 file changed, 109 insertions(+)
diff --git a/src/mlpack/tests/sdp_primal_dual_test.cpp b/src/mlpack/tests/sdp_primal_dual_test.cpp
index 4f22a7d..2d1b013 100644
--- a/src/mlpack/tests/sdp_primal_dual_test.cpp
+++ b/src/mlpack/tests/sdp_primal_dual_test.cpp
@@ -391,6 +391,115 @@ BOOST_AUTO_TEST_CASE(LogChebychevApproxSdp)
BOOST_REQUIRE(stat1.first);
}
+/**
+ * Example 1 on the SDP wiki
+ *
+ * min x_13
+ * s.t.
+ * -0.2 <= x_12 <= -0.1
+ * 0.4 <= x_23 <= 0.5
+ * x_11 = x_22 = x_33 = 1
+ * X >= 0
+ *
+ */
+BOOST_AUTO_TEST_CASE(CorrelationCoeffToySdp)
+{
+ // The semi-definite constraint looks like:
+ //
+ // [ 1 x_12 x_13 0 0 0 0 ]
+ // [ 1 x_23 0 0 0 0 ]
+ // [ 1 0 0 0 0 ]
+ // [ s1 0 0 0 ] >= 0
+ // [ s2 0 0 ]
+ // [ s3 0 ]
+ // [ s4 ]
+
+
+ // x_11 == 0
+ arma::sp_mat A0(7, 7); A0.zeros();
+ A0(0, 0) = 1.;
+
+ // x_22 == 0
+ arma::sp_mat A1(7, 7); A1.zeros();
+ A1(1, 1) = 1.;
+
+ // x_33 == 0
+ arma::sp_mat A2(7, 7); A2.zeros();
+ A2(2, 2) = 1.;
+
+ // x_12 <= -0.1 <==> x_12 + s1 == -0.1, s1 >= 0
+ arma::sp_mat A3(7, 7); A3.zeros();
+ A3(1, 0) = A3(0, 1) = 1.; A3(3, 3) = 2.;
+
+ // -0.2 <= x_12 <==> x_12 - s2 == -0.2, s2 >= 0
+ arma::sp_mat A4(7, 7); A4.zeros();
+ A4(1, 0) = A4(0, 1) = 1.; A4(4, 4) = -2.;
+
+ // x_23 <= 0.5 <==> x_23 + s3 == 0.5, s3 >= 0
+ arma::sp_mat A5(7, 7); A5.zeros();
+ A5(2, 1) = A5(1, 2) = 1.; A5(5, 5) = 2.;
+
+ // 0.4 <= x_23 <==> x_23 - s4 == 0.4, s4 >= 0
+ arma::sp_mat A6(7, 7); A6.zeros();
+ A6(2, 1) = A6(1, 2) = 1.; A6(6, 6) = -2.;
+
+ std::vector<arma::sp_mat> ais({A0, A1, A2, A3, A4, A5, A6});
+
+ SDP sdp(7, 7 + 4 + 4 + 4 + 3 + 2 + 1, 0);
+
+ for (size_t j = 0; j < 3; j++)
+ {
+ // x_j4 == x_j5 == x_j6 == x_j7 == 0
+ for (size_t i = 0; i < 4; i++)
+ {
+ arma::sp_mat A(7, 7); A.zeros();
+ A(i + 3, j) = A(j, i + 3) = 1;
+ ais.emplace_back(A);
+ }
+ }
+
+ // x_45 == x_46 == x_47 == 0
+ for (size_t i = 0; i < 3; i++)
+ {
+ arma::sp_mat A(7, 7); A.zeros();
+ A(i + 4, 3) = A(3, i + 4) = 1;
+ ais.emplace_back(A);
+ }
+
+ // x_56 == x_57 == 0
+ for (size_t i = 0; i < 2; i++)
+ {
+ arma::sp_mat A(7, 7); A.zeros();
+ A(i + 5, 4) = A(4, i + 5) = 1;
+ ais.emplace_back(A);
+ }
+
+ // x_67 == 0
+ arma::sp_mat A(7, 7); A.zeros();
+ A(6, 5) = A(5, 6) = 1;
+ ais.emplace_back(A);
+
+ std::swap(sdp.SparseA(), ais);
+
+ sdp.SparseB().zeros();
+
+ sdp.SparseB()[0] = sdp.SparseB()[1] = sdp.SparseB()[2] = 1.;
+
+ sdp.SparseB()[3] = -0.2; sdp.SparseB()[4] = -0.4;
+
+ sdp.SparseB()[5] = 1.; sdp.SparseB()[6] = 0.8;
+
+ sdp.SparseC().zeros();
+ sdp.SparseC()(0, 2) = sdp.SparseC()(2, 0) = 1.;
+
+ PrimalDualSolver solver(sdp);
+ arma::mat X, Z;
+ arma::vec ysparse, ydense;
+ const auto p = solver.Optimize(X, ysparse, ydense, Z);
+ BOOST_REQUIRE(p.first);
+ BOOST_REQUIRE_CLOSE(p.second, 2 * (-0.978), 1e-3);
+}
+
///**
// * Maximum variance unfolding (MVU) SDP to learn the unrolled gram matrix. For
// * the SDP formulation, see:
More information about the mlpack-git
mailing list