Skip to content

Commit

Permalink
fix unit test of CQuadraticTimeMMD and TwoDistribut
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeLing authored and vigsterkr committed Aug 9, 2017
1 parent 8497404 commit c0a7ef3
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 35 deletions.
4 changes: 2 additions & 2 deletions examples/undocumented/libshogun/kernel_custom.cpp
Expand Up @@ -33,11 +33,11 @@ void test_custom_kernel_subsets()

/* create a random permutation */
SGVector<index_t> subset(m);

auto prng = std::unique_ptr<CRandom>(new CRandom());
for (index_t run=0; run<100; ++run)
{
subset.range_fill();
CMath::permute(subset);
CMath::permute(subset, prng.get());
// subset.display_vector("permutation");
features->add_subset(subset);
k->init(features, features);
Expand Down
4 changes: 2 additions & 2 deletions examples/undocumented/libshogun/kernel_custom_kernel.cpp
Expand Up @@ -31,11 +31,11 @@ void test_custom_kernel_subsets()

/* create a random permutation */
SGVector<index_t> subset(m);

auto prng = std::unique_ptr<CRandom>(new CRandom());
for (index_t run=0; run<100; ++run)
{
subset.range_fill();
CMath::permute(subset);
CMath::permute(subset, prng.get());
// subset.display_vector("permutation");
features->add_subset(subset);
k->init(features, features);
Expand Down
4 changes: 2 additions & 2 deletions src/shogun/machine/gp/EPInferenceMethod.cpp
Expand Up @@ -230,15 +230,15 @@ void CEPInferenceMethod::update()

float64_t nlZ_old=CMath::INFTY;
uint32_t sweep=0;

auto prng = std::unique_ptr<CRandom>(new CRandom());
while ((CMath::abs(m_nlZ-nlZ_old)>m_tol && sweep<m_max_sweep) ||
sweep<m_min_sweep)
{
nlZ_old=m_nlZ;
sweep++;

// shuffle random permutation
CMath::permute(v);
CMath::permute(v, prng.get());

for (index_t j=0; j<n; j++)
{
Expand Down
Expand Up @@ -114,11 +114,11 @@ struct CrossValidationMMD : PermutationMMD
m_permuted_inds=SGVector<index_t>(m_xy_inds.size());

m_inverted_permuted_inds.set_const(-1);

auto prng = std::unique_ptr<CRandom>(new CRandom());
for (auto n=0; n<m_num_null_samples; ++n)
{
std::iota(m_permuted_inds.data(), m_permuted_inds.data()+m_permuted_inds.size(), 0);
CMath::permute(m_permuted_inds);
CMath::permute(m_permuted_inds, prng.get());

m_stack->add_subset(m_permuted_inds);
SGVector<index_t> inds=m_stack->get_last_subset()->get_subset_idx();
Expand Down
Expand Up @@ -200,10 +200,11 @@ struct PermutationMMD : ComputeMMD
{
ASSERT(m_num_null_samples>0);
allocate_permutation_inds();
auto prng = std::unique_ptr<CRandom>(new CRandom());
for (auto n=0; n<m_num_null_samples; ++n)
{
std::iota(m_permuted_inds.data(), m_permuted_inds.data()+m_permuted_inds.size(), 0);
CMath::permute(m_permuted_inds);
CMath::permute(m_permuted_inds, prng.get());
if (m_save_inds)
{
auto offset=n*m_permuted_inds.size();
Expand Down
3 changes: 2 additions & 1 deletion src/shogun/structure/TwoStateModel.cpp
Expand Up @@ -309,10 +309,11 @@ CHMSVMModel* CTwoStateModel::simulate_data(int32_t num_exm, int32_t exm_len,
SGMatrix< float64_t > signal(num_features, distort.vlen);

distort.range_fill();
auto prng = std::unique_ptr<CRandom>(new CRandom());
for ( int32_t i = 0 ; i < num_features ; ++i )
{
lf = ll;
CMath::permute(distort);
CMath::permute(distort, prng.get());

for ( int32_t j = 0 ; j < d1.vlen ; ++j )
d1[j] = distort[j];
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/kernel/CustomKernel_unittest.cc
Expand Up @@ -35,9 +35,10 @@ TEST(CustomKernelTest,add_row_subset)
inds.range_fill();

index_t num_runs=10;
auto prng = std::unique_ptr<CRandom>(new CRandom());
for (index_t i=0; i<num_runs; ++i)
{
CMath::permute(inds);
CMath::permute(inds, prng.get());

feats->add_subset(inds);
custom->add_row_subset(inds);
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/neuralnets/NeuralLinearLayer_unittest.cc
Expand Up @@ -168,7 +168,7 @@ TEST(NeuralLinearLayer, compute_error)
error_ref += 0.5*CMath::pow(y[i]-A[i],2)/y.num_cols;

// compare
EXPECT_NEAR(error_ref, error, 1e-12);
EXPECT_NEAR(error_ref, error, 1e-10);

SG_UNREF(layers);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/statistical_testing/KernelSelection_unittest.cc
Expand Up @@ -287,7 +287,7 @@ TEST(KernelSelectionMaxCrossValidation, quadratic_time_single_kernel_dense)
mmd->set_train_test_mode(false);

auto selected_kernel=static_cast<CGaussianKernel*>(mmd->get_kernel());
EXPECT_NEAR(selected_kernel->get_width(), 0.0625, 1E-10);
EXPECT_NEAR(selected_kernel->get_width(), 0.03125, 1E-10);
}

TEST(KernelSelectionMaxCrossValidation, linear_time_single_kernel_dense)
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/statistical_testing/QuadraticTimeMMD_unittest.cc
Expand Up @@ -354,7 +354,7 @@ TEST(QuadraticTimeMMD, perform_test_permutation_biased_full)
// assert against local machine computed result
mmd->set_statistic_type(ST_BIASED_FULL);
float64_t p_value=mmd->compute_p_value(mmd->compute_statistic());
EXPECT_NEAR(p_value, 0.0, 1E-10);
EXPECT_NEAR(p_value, 0.8, 1E-10);
}

TEST(QuadraticTimeMMD, perform_test_permutation_unbiased_full)
Expand Down Expand Up @@ -393,7 +393,7 @@ TEST(QuadraticTimeMMD, perform_test_permutation_unbiased_full)
// assert against local machine computed result
mmd->set_statistic_type(ST_UNBIASED_FULL);
float64_t p_value=mmd->compute_p_value(mmd->compute_statistic());
EXPECT_NEAR(p_value, 0.0, 1E-10);
EXPECT_NEAR(p_value, 0.8, 1E-10);
}

TEST(QuadraticTimeMMD, perform_test_permutation_unbiased_incomplete)
Expand Down Expand Up @@ -432,7 +432,7 @@ TEST(QuadraticTimeMMD, perform_test_permutation_unbiased_incomplete)
// assert against local machine computed result
mmd->set_statistic_type(ST_UNBIASED_INCOMPLETE);
float64_t p_value=mmd->compute_p_value(mmd->compute_statistic());
EXPECT_NEAR(p_value, 0.0, 1E-10);
EXPECT_NEAR(p_value, 0.6, 1E-10);
}

TEST(QuadraticTimeMMD, perform_test_spectrum)
Expand Down Expand Up @@ -475,15 +475,15 @@ TEST(QuadraticTimeMMD, perform_test_spectrum)
// assert against local machine computed result
mmd->set_statistic_type(ST_BIASED_FULL);
float64_t p_value_spectrum=mmd->compute_p_value(mmd->compute_statistic());
EXPECT_NEAR(p_value_spectrum, 0.0, 1E-10);
EXPECT_NEAR(p_value_spectrum, 0.8, 1E-10);

// unbiased case

// compute p-value using spectrum approximation for null distribution and
// assert against local machine computed result
mmd->set_statistic_type(ST_UNBIASED_FULL);
p_value_spectrum=mmd->compute_p_value(mmd->compute_statistic());
EXPECT_NEAR(p_value_spectrum, 0.0, 1E-10);
EXPECT_NEAR(p_value_spectrum, 0.8, 1E-10);
}

TEST(QuadraticTimeMMD, precomputed_vs_nonprecomputed)
Expand Down
20 changes: 15 additions & 5 deletions tests/unit/statistical_testing/TwoDistributionTest_unittest.cc
Expand Up @@ -130,23 +130,28 @@ TEST(TwoDistributionTest, compute_distance_streaming)
const index_t n=10;
const index_t dim=1;
const float64_t difference=0.5;
set_global_seed(12345);

auto gen_p=new CMeanShiftDataGenerator(0, dim, 0);
auto gen_q=new CMeanShiftDataGenerator(difference, dim, 0);

auto gen_p1 = new CMeanShiftDataGenerator(0, dim, 0);
auto gen_q1 = new CMeanShiftDataGenerator(difference, dim, 0);

auto mock_obj=some<CTwoDistributionTestMock>();
mock_obj->set_p(gen_p);
mock_obj->set_q(gen_q);
mock_obj->set_num_samples_p(m);
mock_obj->set_num_samples_q(n);

set_global_seed(12345);
auto euclidean_distance=some<CEuclideanDistance>();
auto distance=mock_obj->compute_distance(euclidean_distance);
auto distance_mat1=distance->get_distance_matrix();

auto feats_p=static_cast<CDenseFeatures<float64_t>*>(gen_p->get_streamed_features(m));
auto feats_q=static_cast<CDenseFeatures<float64_t>*>(gen_q->get_streamed_features(n));
auto feats_p = static_cast<CDenseFeatures<float64_t>*>(
gen_p1->get_streamed_features(m));
auto feats_q = static_cast<CDenseFeatures<float64_t>*>(
gen_q1->get_streamed_features(n));
euclidean_distance->init(feats_p, feats_q);
auto distance_mat2=euclidean_distance->get_distance_matrix();

Expand All @@ -168,6 +173,9 @@ TEST(TwoDistributionTest, compute_joint_distance_streaming)
auto gen_p=new CMeanShiftDataGenerator(0, dim, 0);
auto gen_q=new CMeanShiftDataGenerator(difference, dim, 0);

auto gen_p1 = new CMeanShiftDataGenerator(0, dim, 0);
auto gen_q1 = new CMeanShiftDataGenerator(difference, dim, 0);

auto mock_obj=some<CTwoDistributionTestMock>();
mock_obj->set_p(gen_p);
mock_obj->set_q(gen_q);
Expand All @@ -179,8 +187,10 @@ TEST(TwoDistributionTest, compute_joint_distance_streaming)
auto distance=mock_obj->compute_joint_distance(euclidean_distance);
auto distance_mat1=distance->get_distance_matrix();

auto feats_p=static_cast<CDenseFeatures<float64_t>*>(gen_p->get_streamed_features(m));
auto feats_q=static_cast<CDenseFeatures<float64_t>*>(gen_q->get_streamed_features(n));
auto feats_p = static_cast<CDenseFeatures<float64_t>*>(
gen_p1->get_streamed_features(m));
auto feats_q = static_cast<CDenseFeatures<float64_t>*>(
gen_q1->get_streamed_features(n));

SGMatrix<float64_t> data_p_and_q(dim, m+n);
auto data_p=feats_p->get_feature_matrix();
Expand Down
Expand Up @@ -282,8 +282,6 @@ TEST(CrossValidationMMD, unbiased_incomplete)
cv.m_num_runs=num_runs;
cv.m_rejections=SGMatrix<float64_t>(num_runs*num_folds, num_kernels);

set_global_seed(12345);
set_global_seed(12345);
set_global_seed(12345);
cv(kernel_mgr);
kernel_mgr.unset_precomputed_distance();
Expand Down
23 changes: 13 additions & 10 deletions tests/unit/statistical_testing/internals/PermutationMMD_unittest.cc
Expand Up @@ -102,24 +102,26 @@ TEST(PermutationMMD, biased_full_single_kernel)
Map<MatrixXf> map(kernel_matrix.matrix, kernel_matrix.num_rows, kernel_matrix.num_cols);
SGVector<float32_t> result_2(num_null_samples);
set_global_seed(12345);
auto prng = std::unique_ptr<CRandom>(new CRandom());
for (auto i=0; i<num_null_samples; ++i)
{
PermutationMatrix<Dynamic, Dynamic> perm(kernel_matrix.num_rows);
perm.setIdentity();
SGVector<int> perminds(perm.indices().data(), perm.indices().size(), false);
CMath::permute(perminds);
CMath::permute(perminds, prng.get());
MatrixXf permuted = perm.transpose()*map*perm;
SGMatrix<float32_t> permuted_km(permuted.data(), permuted.rows(), permuted.cols(), false);
result_2[i]=compute_mmd(permuted_km);
}

SGVector<index_t> inds(kernel_matrix.num_rows);
SGVector<float32_t> result_3(num_null_samples);
set_global_seed(12345);

prng->set_seed(12345);
for (auto i=0; i<num_null_samples; ++i)
{
std::iota(inds.vector, inds.vector+inds.vlen, 0);
CMath::permute(inds);
CMath::permute(inds, prng.get());
feats->add_subset(inds);
kernel->init(feats, feats);
kernel_matrix=kernel->get_kernel_matrix<float32_t>();
Expand Down Expand Up @@ -181,13 +183,13 @@ TEST(PermutationMMD, unbiased_full_single_kernel)
set_global_seed(12345);
Map<MatrixXf> map(kernel_matrix.matrix, kernel_matrix.num_rows, kernel_matrix.num_cols);
SGVector<float32_t> result_2(num_null_samples);

auto prng = std::unique_ptr<CRandom>(new CRandom());
for (auto i=0; i<num_null_samples; ++i)
{
PermutationMatrix<Dynamic, Dynamic> perm(kernel_matrix.num_rows);
perm.setIdentity();
SGVector<int> perminds(perm.indices().data(), perm.indices().size(), false);
CMath::permute(perminds);
CMath::permute(perminds, prng.get());
MatrixXf permuted = perm.transpose()*map*perm;
SGMatrix<float32_t> permuted_km(permuted.data(), permuted.rows(), permuted.cols(), false);
result_2[i]=compute_mmd(permuted_km);
Expand All @@ -196,11 +198,11 @@ TEST(PermutationMMD, unbiased_full_single_kernel)
SGVector<index_t> inds(kernel_matrix.num_rows);
SGVector<float32_t> result_3(num_null_samples);

set_global_seed(12345);
prng->set_seed(12345);
for (auto i=0; i<num_null_samples; ++i)
{
std::iota(inds.vector, inds.vector+inds.vlen, 0);
CMath::permute(inds);
CMath::permute(inds, prng.get());
feats->add_subset(inds);
kernel->init(feats, feats);
kernel_matrix=kernel->get_kernel_matrix<float32_t>();
Expand Down Expand Up @@ -262,26 +264,27 @@ TEST(PermutationMMD, unbiased_incomplete_single_kernel)
Map<MatrixXf> map(kernel_matrix.matrix, kernel_matrix.num_rows, kernel_matrix.num_cols);

set_global_seed(12345);
auto prng = std::unique_ptr<CRandom>(new CRandom());
SGVector<float32_t> result_2(num_null_samples);
for (auto i=0; i<num_null_samples; ++i)
{
PermutationMatrix<Dynamic, Dynamic> perm(kernel_matrix.num_rows);
perm.setIdentity();
SGVector<int> perminds(perm.indices().data(), perm.indices().size(), false);
CMath::permute(perminds);
CMath::permute(perminds, prng.get());
MatrixXf permuted = perm.transpose()*map*perm;
SGMatrix<float32_t> permuted_km(permuted.data(), permuted.rows(), permuted.cols(), false);
result_2[i]=compute_mmd(permuted_km);
}

set_global_seed(12345);
prng->set_seed(12345);
SGVector<index_t> inds(kernel_matrix.num_rows);
SGVector<float32_t> result_3(num_null_samples);

for (auto i=0; i<num_null_samples; ++i)
{
std::iota(inds.vector, inds.vector+inds.vlen, 0);
CMath::permute(inds);
CMath::permute(inds, prng.get());
feats->add_subset(inds);
kernel->init(feats, feats);
kernel_matrix=kernel->get_kernel_matrix<float32_t>();
Expand Down

0 comments on commit c0a7ef3

Please sign in to comment.