SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
KernelTwoSampleTestStatistic.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/KernelTwoSampleTestStatistic.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 CKernelTwoSampleTestStatistic::CKernelTwoSampleTestStatistic() :
00018         CTwoDistributionsTestStatistic()
00019 {
00020     init();
00021 }
00022 
00023 CKernelTwoSampleTestStatistic::CKernelTwoSampleTestStatistic(CKernel* kernel,
00024         CFeatures* p_and_q, index_t q_start) :
00025         CTwoDistributionsTestStatistic(p_and_q, q_start)
00026 {
00027     init();
00028 
00029     m_kernel=kernel;
00030     SG_REF(kernel);
00031 
00032     /* init kernel once in the beginning */
00033     m_kernel->init(m_p_and_q, m_p_and_q);
00034 }
00035 
00036 CKernelTwoSampleTestStatistic::CKernelTwoSampleTestStatistic(CKernel* kernel,
00037         CFeatures* p, CFeatures* q) : CTwoDistributionsTestStatistic(p, q)
00038 {
00039     init();
00040 
00041     m_kernel=kernel;
00042     SG_REF(kernel);
00043 
00044     /* init kernel once in the beginning */
00045     m_kernel->init(m_p_and_q, m_p_and_q);
00046 }
00047 
00048 CKernelTwoSampleTestStatistic::~CKernelTwoSampleTestStatistic()
00049 {
00050     SG_UNREF(m_kernel);
00051 }
00052 
00053 void CKernelTwoSampleTestStatistic::init()
00054 {
00055     SG_ADD((CSGObject**)&m_kernel, "kernel", "Kernel for two sample test",
00056             MS_AVAILABLE);
00057     m_kernel=NULL;
00058 }
00059 
00060 SGVector<float64_t> CKernelTwoSampleTestStatistic::bootstrap_null()
00061 {
00062     /* compute bootstrap statistics for null distribution */
00063     SGVector<float64_t> results;
00064 
00065     /* only do something if a custom kernel is used: use the power of pre-
00066      * computed kernel matrices
00067      */
00068     if (m_kernel->get_kernel_type()==K_CUSTOM)
00069     {
00070         /* allocate memory */
00071         results=SGVector<float64_t>(m_bootstrap_iterations);
00072 
00073         /* memory for index permutations, (would slow down loop) */
00074         SGVector<index_t> ind_permutation(m_p_and_q->get_num_vectors());
00075         ind_permutation.range_fill();
00076 
00077         /* check if kernel is a custom kernel. In that case, changing features is
00078          * not what we want but just subsetting the kernel itself */
00079         CCustomKernel* custom_kernel=(CCustomKernel*)m_kernel;
00080 
00081         for (index_t i=0; i<m_bootstrap_iterations; ++i)
00082         {
00083             /* idea: merge features of p and q, shuffle, and compute statistic.
00084              * This is done using subsets here. add to custom kernel since
00085              * it has no features to subset. CustomKernel has not to be
00086              * re-initialised after each subset setting */
00087             SGVector<int32_t>::permute_vector(ind_permutation);
00088 
00089             custom_kernel->add_row_subset(ind_permutation);
00090             custom_kernel->add_col_subset(ind_permutation);
00091 
00092             /* compute statistic for this permutation of mixed samples */
00093             results[i]=compute_statistic();
00094 
00095             /* remove subsets */
00096             custom_kernel->remove_row_subset();
00097             custom_kernel->remove_col_subset();
00098         }
00099     }
00100     else
00101     {
00102         /* in this case, just use superclass method */
00103         results=CTwoDistributionsTestStatistic::bootstrap_null();
00104     }
00105 
00106     return results;
00107 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation