SHOGUN  6.1.3
WithinBlockPermutation.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (w) 2012 - 2013 Heiko Strathmann
4  * Written (w) 2016 - 2017 Soumyajit De
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * The views and conclusions contained in the software and documentation are those
28  * of the authors and should not be interpreted as representing official policies,
29  * either expressed or implied, of the Shogun Development Team.
30  */
31 
32 #include <numeric>
33 #include <shogun/io/SGIO.h>
34 #include <shogun/lib/SGMatrix.h>
35 #include <shogun/lib/GPUMatrix.h>
40 
41 using namespace shogun;
42 using namespace internal;
43 using namespace mmd;
44 
45 WithinBlockPermutation::WithinBlockPermutation(index_t nx, index_t ny, EStatisticType type)
46 : n_x(nx), n_y(ny), stype(type), terms()
47 {
48  SG_SDEBUG("number of samples are %d and %d!\n", n_x, n_y);
49  permuted_inds=SGVector<index_t>(n_x+n_y);
50  inverted_permuted_inds=SGVector<index_t>(permuted_inds.vlen);
51 }
52 
53 void WithinBlockPermutation::add_term(float32_t val, index_t i, index_t j)
54 {
55  if (i<n_x && j<n_x && i<=j)
56  {
57  SG_SDEBUG("Adding Kernel(%d,%d)=%f to term_0!\n", i, j, val);
58  terms.term[0]+=val;
59  if (i==j)
60  terms.diag[0]+=val;
61  }
62  else if (i>=n_x && j>=n_x && i<=j)
63  {
64  SG_SDEBUG("Adding Kernel(%d,%d)=%f to term_1!\n", i, j, val);
65  terms.term[1]+=val;
66  if (i==j)
67  terms.diag[1]+=val;
68  }
69  else if (i>=n_x && j<n_x)
70  {
71  SG_SDEBUG("Adding Kernel(%d,%d)=%f to term_2!\n", i, j, val);
72  terms.term[2]+=val;
73  if (i-n_x==j)
74  terms.diag[2]+=val;
75  }
76 }
77 
78 float32_t WithinBlockPermutation::operator()(const SGMatrix<float32_t>& km)
79 {
80  SG_SDEBUG("Entering!\n");
81 
82  std::iota(permuted_inds.vector, permuted_inds.vector+permuted_inds.vlen, 0);
83  CMath::permute(permuted_inds);
84  for (int i=0; i<permuted_inds.vlen; ++i)
85  inverted_permuted_inds[permuted_inds[i]]=i;
86 
87  std::fill(&terms.term[0], &terms.term[2]+1, 0);
88  std::fill(&terms.diag[0], &terms.diag[2]+1, 0);
89 
90  for (auto j=0; j<n_x+n_y; ++j)
91  {
92  for (auto i=0; i<n_x+n_y; ++i)
93  add_term(km(i, j), inverted_permuted_inds[i], inverted_permuted_inds[j]);
94  }
95 
96  terms.term[0]=2*(terms.term[0]-terms.diag[0]);
97  terms.term[1]=2*(terms.term[1]-terms.diag[1]);
98  SG_SDEBUG("term_0 sum (without diagonal) = %f!\n", terms.term[0]);
99  SG_SDEBUG("term_1 sum (without diagonal) = %f!\n", terms.term[1]);
100  if (stype!=ST_BIASED_FULL)
101  {
102  terms.term[0]/=n_x*(n_x-1);
103  terms.term[1]/=n_y*(n_y-1);
104  }
105  else
106  {
107  terms.term[0]+=terms.diag[0];
108  terms.term[1]+=terms.diag[1];
109  SG_SDEBUG("term_0 sum (with diagonal) = %f!\n", terms.term[0]);
110  SG_SDEBUG("term_1 sum (with diagonal) = %f!\n", terms.term[1]);
111  terms.term[0]/=n_x*n_x;
112  terms.term[1]/=n_y*n_y;
113  }
114  SG_SDEBUG("term_0 (normalized) = %f!\n", terms.term[0]);
115  SG_SDEBUG("term_1 (normalized) = %f!\n", terms.term[1]);
116 
117  SG_SDEBUG("term_2 sum (with diagonal) = %f!\n", terms.term[2]);
118  if (stype==ST_UNBIASED_INCOMPLETE)
119  {
120  terms.term[2]-=terms.diag[2];
121  SG_SDEBUG("term_2 sum (without diagonal) = %f!\n", terms.term[2]);
122  terms.term[2]/=n_x*(n_x-1);
123  }
124  else
125  terms.term[2]/=n_x*n_y;
126  SG_SDEBUG("term_2 (normalized) = %f!\n", terms.term[2]);
127 
128  SG_SDEBUG("Leaving!\n");
129  return terms.term[0]+terms.term[1]-2*terms.term[2];
130 }
static void permute(SGVector< T > v, CRandom *rand=NULL)
Definition: Math.h:962
int32_t index_t
Definition: common.h:72
EStatisticType
Definition: TestEnums.h:40
float float32_t
Definition: common.h:59
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
#define SG_SDEBUG(...)
Definition: SGIO.h:153

SHOGUN Machine Learning Toolbox - Documentation