KernelIndependenceTestStatistic.cpp

Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 2012 Heiko Strathmann
00008  */
00009 
00010 #include <shogun/statistics/KernelIndependenceTestStatistic.h>
00011 #include <shogun/features/Features.h>
00012 #include <shogun/kernel/Kernel.h>
00013 #include <shogun/kernel/CustomKernel.h>
00014 
00015 using namespace shogun;
00016 
00017 CKernelIndependenceTestStatistic::CKernelIndependenceTestStatistic() :
00018         CTwoDistributionsTestStatistic()
00019 {
00020     init();
00021 }
00022 
00023 CKernelIndependenceTestStatistic::CKernelIndependenceTestStatistic(
00024         CKernel* kernel_p, CKernel* kernel_q, CFeatures* p_and_q,
00025         index_t q_start) : CTwoDistributionsTestStatistic(m_p_and_q, q_start)
00026 {
00027     init();
00028 
00029     m_kernel_p=kernel_p;
00030     m_kernel_q=kernel_q;
00031     SG_REF(kernel_p);
00032     SG_REF(kernel_q);
00033 }
00034 
00035 CKernelIndependenceTestStatistic::CKernelIndependenceTestStatistic(
00036         CKernel* kernel_p, CKernel* kernel_q, CFeatures* p, CFeatures* q) :
00037         CTwoDistributionsTestStatistic(p, q)
00038 {
00039     init();
00040 
00041     m_kernel_p=kernel_p;
00042     SG_REF(kernel_p);
00043 
00044     m_kernel_q=kernel_q;
00045     SG_REF(kernel_q);
00046 }
00047 
00048 CKernelIndependenceTestStatistic::~CKernelIndependenceTestStatistic()
00049 {
00050     SG_UNREF(m_kernel_p);
00051     SG_UNREF(m_kernel_q);
00052 }
00053 
00054 void CKernelIndependenceTestStatistic::init()
00055 {
00056     SG_ADD((CSGObject**)&m_kernel_p, "kernel_p", "Kernel for samples from p",
00057             MS_AVAILABLE);
00058     SG_ADD((CSGObject**)&m_kernel_q, "kernel_q", "Kernel for samples from q",
00059             MS_AVAILABLE);
00060     m_kernel_p=NULL;
00061     m_kernel_q=NULL;
00062 }
00063 
00064 SGVector<float64_t> CKernelIndependenceTestStatistic::bootstrap_null()
00065 {
00066     SG_DEBUG("entering CKernelIndependenceTestStatistic::bootstrap_null()\n");
00067 
00068     /* compute bootstrap statistics for null distribution */
00069     SGVector<float64_t> results;
00070 
00071     /* only do something if a custom kernel is used: use the power of pre-
00072      * computed kernel matrices
00073      */
00074     if (m_kernel_p->get_kernel_type()==K_CUSTOM &&
00075             m_kernel_q->get_kernel_type()==K_CUSTOM)
00076     {
00077         /* allocate memory */
00078         results=SGVector<float64_t>(m_bootstrap_iterations);
00079 
00080         /* memory for index permutations, (would slow down loop) */
00081         SGVector<index_t> ind_permutation(m_p_and_q->get_num_vectors());
00082         ind_permutation.range_fill();
00083 
00084         /* check if kernel is a custom kernel. In that case, changing features is
00085          * not what we want but just subsetting the kernel itself */
00086         CCustomKernel* custom_kernel_p=(CCustomKernel*)m_kernel_p;
00087         CCustomKernel* custom_kernel_q=(CCustomKernel*)m_kernel_q;
00088 
00089         for (index_t i=0; i<m_bootstrap_iterations; ++i)
00090         {
00091             /* idea: merge features of p and q, shuffle, and compute statistic.
00092              * This is done using subsets here. add to custom kernel since
00093              * it has no features to subset. CustomKernel has not to be
00094              * re-initialised after each subset setting */
00095             SGVector<int32_t>::permute_vector(ind_permutation);
00096 
00097             custom_kernel_p->add_row_subset(ind_permutation);
00098             custom_kernel_p->add_col_subset(ind_permutation);
00099             custom_kernel_q->add_row_subset(ind_permutation);
00100             custom_kernel_q->add_col_subset(ind_permutation);
00101 
00102             /* compute statistic for this permutation of mixed samples */
00103             results[i]=compute_statistic();
00104 
00105             /* remove subsets */
00106             custom_kernel_p->remove_row_subset();
00107             custom_kernel_p->remove_col_subset();
00108             custom_kernel_q->remove_row_subset();
00109             custom_kernel_q->remove_col_subset();
00110         }
00111     }
00112     else
00113     {
00114         /* in this case, just use superclass method */
00115         results=CTwoDistributionsTestStatistic::bootstrap_null();
00116     }
00117 
00118 
00119     SG_DEBUG("leaving CKernelIndependenceTestStatistic::bootstrap_null()\n");
00120     return results;
00121 }
00122 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation