SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
KernelDensity.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (w) 2014 Parijat Mazumdar
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * The views and conclusions contained in the software and documentation are those
27  * of the authors and should not be interpreted as representing official policies,
28  * either expressed or implied, of the Shogun Development Team.
29  */
30 
35 
36 using namespace shogun;
37 
38 CKernelDensity::CKernelDensity(float64_t bandwidth, EKernelType kernel, EDistanceType dist, EEvaluationMode eval, int32_t leaf_size, float64_t atol, float64_t rtol)
39 : CDistribution()
40 {
41  init();
42 
43  m_bandwidth=bandwidth;
44  m_eval=eval;
45  m_leaf_size=leaf_size;
46  m_atol=atol;
47  m_rtol=rtol;
48  m_dist=dist;
49  m_kernel_type=kernel;
50 }
51 
53 {
54  SG_UNREF(tree);
55 }
56 
58 {
59  REQUIRE(data,"Data not supplied\n")
61 
62  SG_UNREF(tree);
63  switch (m_eval)
64  {
65  case EM_KDTREE_SINGLE:
66  {
67  tree=new CKDTree(m_leaf_size,m_dist);
68  break;
69  }
70  case EM_BALLTREE_SINGLE:
71  {
72  tree=new CBallTree(m_leaf_size,m_dist);
73  break;
74  }
75  case EM_KDTREE_DUAL:
76  {
77  tree=new CKDTree(m_leaf_size,m_dist);
78  break;
79  }
80  case EM_BALLTREE_DUAL:
81  {
82  tree=new CBallTree(m_leaf_size,m_dist);
83  break;
84  }
85  default:
86  {
87  SG_ERROR("Evaluation mode not recognized\n");
88  }
89  }
90 
91  tree->build_tree(dense_data);
92  return true;
93 }
94 
96 {
97  REQUIRE(test,"data not supplied\n")
98 
99  if ((m_eval==EM_KDTREE_SINGLE) || (m_eval==EM_BALLTREE_SINGLE))
100  return tree->log_kernel_density(test->get_feature_matrix(),m_kernel_type,m_bandwidth,m_atol,m_rtol);
101 
102  CNbodyTree* query_tree=NULL;
103  if (m_eval==EM_KDTREE_DUAL)
104  query_tree=new CKDTree(leaf_size,m_dist);
105  else if (m_eval==EM_BALLTREE_DUAL)
106  query_tree=new CBallTree(leaf_size,m_dist);
107  else
108  SG_ERROR("Evaluation mode not identified\n");
109 
110  query_tree->build_tree(test);
112  CTreeMachineNode<NbodyTreeNodeData>* root=query_tree->get_root();
113  if (root)
114  qroot=dynamic_cast<CBinaryTreeMachineNode<NbodyTreeNodeData>*>(root);
115  else
116  SG_ERROR("Query tree root not found!\n")
117 
118  SGVector<index_t> qid=query_tree->get_rearranged_vector_ids();
119  SGVector<float64_t> ret=tree->log_kernel_density_dual(test->get_feature_matrix(),qid,qroot,m_kernel_type,m_bandwidth,m_atol,m_rtol);
120 
121  SG_UNREF(root);
122  SG_UNREF(query_tree);
123 
124  return ret;
125 }
126 
128 {
130  return 0;
131 }
132 
134 {
136  return 0;
137 }
138 
139 float64_t CKernelDensity::get_log_derivative(int32_t num_param, int32_t num_example)
140 {
142  return 0;
143 }
144 
146 {
148  return 0;
149 }
150 
151 void CKernelDensity::init()
152 {
153  m_bandwidth=1.0;
154  m_eval=EM_KDTREE_SINGLE;
155  m_kernel_type=K_GAUSSIAN;
156  m_dist=D_EUCLIDEAN;
157  m_leaf_size=1;
158  m_atol=0;
159  m_rtol=0;
160  tree=NULL;
161 
162  SG_ADD(&m_bandwidth,"m_bandwidth","bandwidth",MS_NOT_AVAILABLE);
163  SG_ADD(&m_leaf_size,"m_leaf_size","leaf size",MS_NOT_AVAILABLE);
164  SG_ADD(&m_atol,"m_atol","absolute tolerance",MS_NOT_AVAILABLE);
165  SG_ADD(&m_rtol,"m_rtol","relative tolerance",MS_NOT_AVAILABLE);
166  SG_ADD((CSGObject**) &tree,"tree","tree",MS_NOT_AVAILABLE);
167 }

SHOGUN Machine Learning Toolbox - Documentation