Skip to content

Commit

Permalink
Merge pull request #694 from karlnapf/master
Browse files Browse the repository at this point in the history
added test for HSIC bootstrapping
  • Loading branch information
karlnapf committed Aug 8, 2012
2 parents be0b772 + 9296faf commit 5bb8506
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
48 changes: 47 additions & 1 deletion examples/undocumented/libshogun/statistics_hsic.cpp
Expand Up @@ -89,13 +89,17 @@ void test_hsic_fixed()

index_t m=features_p->get_num_vectors();

/* unref features since convienience constructor is HSIC was used */
CHSIC* hsic=new CHSIC(kernel_p, kernel_q, features_p, features_q);
SG_UNREF(features_p);
SG_UNREF(features_q);

/* assert matlab result, note that compute statistic computes m*hsic */
float64_t difference=hsic->compute_statistic();
SG_SPRINT("hsic fixed: %f\n", difference);
ASSERT(CMath::abs(difference-m*0.164761446385339)<10E-16);


SG_UNREF(hsic);
}

Expand All @@ -107,16 +111,57 @@ void test_hsic_gamma()
CKernel* kernel_q=NULL;
create_fixed_data_kernel_big(features_p, features_q, kernel_p, kernel_q);

/* unref features since convienience constructor is HSIC was used */
CHSIC* hsic=new CHSIC(kernel_p, kernel_q, features_p, features_q);
hsic->set_null_approximation_method(HSIC_GAMMA);
SG_UNREF(features_p);
SG_UNREF(features_q);

hsic->set_null_approximation_method(HSIC_GAMMA);
float64_t p=hsic->compute_p_value(0.05);
SG_SPRINT("p-value: %f\n", p);
ASSERT(CMath::abs(p-0.172182287884256)<10E-15);

SG_UNREF(hsic);
}

void test_hsic_bootstrap()
{
CFeatures* features_p=NULL;
CFeatures* features_q=NULL;
CKernel* kernel_p=NULL;
CKernel* kernel_q=NULL;
create_fixed_data_kernel_big(features_p, features_q, kernel_p, kernel_q);

/* unref features since convienience constructor is HSIC was used */
CHSIC* hsic=new CHSIC(kernel_p, kernel_q, features_p, features_q);
SG_UNREF(features_p);
SG_UNREF(features_q);

/* do bootstrapping */
hsic->set_null_approximation_method(BOOTSTRAP);
float64_t p=hsic->compute_p_value(0.05);
SG_SPRINT("p-value: %f\n", p);

/* ensure that bootstrapping of hsic leads to same results as using
* CKernelIndependenceTestStatistic */
CMath::init_random(1);
float64_t mean1=CStatistics::mean(hsic->bootstrap_null());
float64_t var1=CStatistics::variance(hsic->bootstrap_null());
SG_SPRINT("mean1=%f, var1=%f\n", mean1, var1);

CMath::init_random(1);
float64_t mean2=CStatistics::mean(
hsic->CKernelIndependenceTestStatistic::bootstrap_null());
float64_t var2=CStatistics::variance(hsic->bootstrap_null());
SG_SPRINT("mean2=%f, var2=%f\n", mean2, var2);

/* assert than results are the same from bot bootstrapping impl. */
ASSERT(CMath::abs(mean1-mean2)<10E-8);
ASSERT(CMath::abs(var1-var2)<10E-8);

SG_UNREF(hsic);
}

int main(int argc, char** argv)
{
init_shogun_with_defaults();
Expand All @@ -125,6 +170,7 @@ int main(int argc, char** argv)

test_hsic_fixed();
test_hsic_gamma();
test_hsic_bootstrap();

exit_shogun();
return 0;
Expand Down
4 changes: 4 additions & 0 deletions src/shogun/statistics/HSIC.cpp
Expand Up @@ -290,6 +290,8 @@ SGMatrix<float64_t> CHSIC::get_kernel_matrix_L()

SGVector<float64_t> CHSIC::bootstrap_null()
{
SG_DEBUG("entering CHSIC::bootstrap_null()\n");

/* replace current kernel via precomputed custom kernel and call superclass
* method */

Expand Down Expand Up @@ -322,5 +324,7 @@ SGVector<float64_t> CHSIC::bootstrap_null()
SG_UNREF(precomputed_p);
SG_UNREF(precomputed_q);


SG_DEBUG("leaving CHSIC::bootstrap_null()\n");
return null_samples;
}
4 changes: 4 additions & 0 deletions src/shogun/statistics/KernelIndependenceTestStatistic.cpp
Expand Up @@ -63,6 +63,8 @@ void CKernelIndependenceTestStatistic::init()

SGVector<float64_t> CKernelIndependenceTestStatistic::bootstrap_null()
{
SG_DEBUG("entering CKernelIndependenceTestStatistic::bootstrap_null()\n");

/* compute bootstrap statistics for null distribution */
SGVector<float64_t> results;

Expand Down Expand Up @@ -113,6 +115,8 @@ SGVector<float64_t> CKernelIndependenceTestStatistic::bootstrap_null()
results=CTwoDistributionsTestStatistic::bootstrap_null();
}


SG_DEBUG("leaving CKernelIndependenceTestStatistic::bootstrap_null()\n");
return results;
}

4 changes: 3 additions & 1 deletion src/shogun/statistics/TwoDistributionsTestStatistic.cpp
Expand Up @@ -60,6 +60,8 @@ void CTwoDistributionsTestStatistic::init()

SGVector<float64_t> CTwoDistributionsTestStatistic::bootstrap_null()
{
SG_DEBUG("entering CTwoDistributionsTestStatistic::bootstrap_null()\n");

/* compute bootstrap statistics for null distribution */
SGVector<float64_t> results(m_bootstrap_iterations);

Expand All @@ -84,7 +86,7 @@ SGVector<float64_t> CTwoDistributionsTestStatistic::bootstrap_null()
/* clean up */
m_p_and_q->remove_subset();

/* clean up and return */
SG_DEBUG("leaving CTwoDistributionsTestStatistic::bootstrap_null()\n");
return results;
}

Expand Down

0 comments on commit 5bb8506

Please sign in to comment.