SHOGUN  v3.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
MMDKernelSelectionCombMaxL2.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) 2013 Heiko Strathmann
00008  */
00009 
00010 #include <shogun/statistics/MMDKernelSelectionCombMaxL2.h>
00011 #include <shogun/statistics/LinearTimeMMD.h>
00012 #include <shogun/kernel/CombinedKernel.h>
00013 #include <shogun/mathematics/Statistics.h>
00014 
00015 
00016 using namespace shogun;
00017 
00018 CMMDKernelSelectionCombMaxL2::CMMDKernelSelectionCombMaxL2() :
00019         CMMDKernelSelectionComb()
00020 {
00021 }
00022 
00023 CMMDKernelSelectionCombMaxL2::CMMDKernelSelectionCombMaxL2(
00024         CKernelTwoSampleTestStatistic* mmd) : CMMDKernelSelectionComb(mmd)
00025 {
00026     /* currently, this method is only developed for the linear time MMD */
00027     REQUIRE(mmd->get_statistic_type()==S_QUADRATIC_TIME_MMD ||
00028             mmd->get_statistic_type()==S_LINEAR_TIME_MMD, "%s::%s(): Only "
00029             "CLinearTimeMMD is currently supported! Provided instance is "
00030             "\"%s\"\n", get_name(), get_name(), mmd->get_name());
00031 }
00032 
00033 CMMDKernelSelectionCombMaxL2::~CMMDKernelSelectionCombMaxL2()
00034 {
00035 }
00036 
00037 #ifdef HAVE_LAPACK
00038 SGVector<float64_t> CMMDKernelSelectionCombMaxL2::compute_measures()
00039 {
00040     /* cast is safe due to assertion in constructor */
00041     CCombinedKernel* kernel=(CCombinedKernel*)m_mmd->get_kernel();
00042     index_t num_kernels=kernel->get_num_subkernels();
00043     SG_UNREF(kernel);
00044 
00045     /* compute mmds for all underlying kernels and create identity matrix Q
00046      * (see NIPS paper) */
00047     SGVector<float64_t> mmds=m_mmd->compute_statistic(true);
00048 
00049     /* free matrix by hand since it is static */
00050     SG_FREE(m_Q.matrix);
00051     m_Q.matrix=NULL;
00052     m_Q.num_rows=0;
00053     m_Q.num_cols=0;
00054     m_Q=SGMatrix<float64_t>(num_kernels, num_kernels, false);
00055     for (index_t i=0; i<num_kernels; ++i)
00056     {
00057         for (index_t j=0; j<num_kernels; ++j)
00058             m_Q(i, j)=i==j ? 1 : 0;
00059     }
00060 
00061     /* solve the generated problem */
00062     SGVector<float64_t> result=CMMDKernelSelectionComb::solve_optimization(mmds);
00063 
00064     /* free matrix by hand since it is static (again) */
00065     SG_FREE(m_Q.matrix);
00066     m_Q.matrix=NULL;
00067     m_Q.num_rows=0;
00068     m_Q.num_cols=0;
00069 
00070     return result;
00071 }
00072 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation