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

SHOGUN Machine Learning Toolbox - Documentation