SHOGUN  4.1.0
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
LogitVGPiecewiseBoundLikelihood.cpp
浏览该文件的文档.
1 /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (w) 2014 Wu Lin
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  * Code adapted from
31  * https://github.com/emtiyaz/VariationalApproxExample
32  * and the reference paper is
33  * Marlin, Benjamin M., Mohammad Emtiyaz Khan, and Kevin P. Murphy.
34  * "Piecewise Bounds for Estimating Bernoulli-Logistic Latent Gaussian Models." ICML. 2011.
35  *
36  * This code specifically adapted from ElogLik.m
37  * and from the formula of the appendix
38  * http://www.cs.ubc.ca/~emtiyaz/papers/truncatedGaussianMoments.pdf
39  */
40 
42 
43 #ifdef HAVE_EIGEN3
49 
50 using namespace Eigen;
51 
52 namespace shogun
53 {
54 
55 CLogitVGPiecewiseBoundLikelihood::CLogitVGPiecewiseBoundLikelihood()
57 {
58  init();
59 }
60 
62 {
63 }
64 
66 {
67  m_bound = bound;
68 }
69 
71 {
72  index_t row = 20;
73  index_t col = 5;
74  m_bound = SGMatrix<float64_t>(row, col);
75 
76  m_bound(0,0)=0.000188712193000;
77  m_bound(1,0)=0.028090310300000;
78  m_bound(2,0)=0.110211757000000;
79  m_bound(3,0)=0.232736440000000;
80  m_bound(4,0)=0.372524706000000;
81  m_bound(5,0)=0.504567936000000;
82  m_bound(6,0)=0.606280283000000;
83  m_bound(7,0)=0.666125432000000;
84  m_bound(8,0)=0.689334264000000;
85  m_bound(9,0)=0.693147181000000;
86  m_bound(10,0)=0.693147181000000;
87  m_bound(11,0)=0.689334264000000;
88  m_bound(12,0)=0.666125432000000;
89  m_bound(13,0)=0.606280283000000;
90  m_bound(14,0)=0.504567936000000;
91  m_bound(15,0)=0.372524706000000;
92  m_bound(16,0)=0.232736440000000;
93  m_bound(17,0)=0.110211757000000;
94  m_bound(18,0)=0.028090310400000;
95  m_bound(19,0)=0.000188712000000;
96 
97  m_bound(0,1)=0;
98  m_bound(1,1)=0.006648614600000;
99  m_bound(2,1)=0.034432684600000;
100  m_bound(3,1)=0.088701969900000;
101  m_bound(4,1)=0.168024214000000;
102  m_bound(5,1)=0.264032863000000;
103  m_bound(6,1)=0.360755794000000;
104  m_bound(7,1)=0.439094482000000;
105  m_bound(8,1)=0.485091758000000;
106  m_bound(9,1)=0.499419205000000;
107  m_bound(10,1)=0.500580795000000;
108  m_bound(11,1)=0.514908242000000;
109  m_bound(12,1)=0.560905518000000;
110  m_bound(13,1)=0.639244206000000;
111  m_bound(14,1)=0.735967137000000;
112  m_bound(15,1)=0.831975786000000;
113  m_bound(16,1)=0.911298030000000;
114  m_bound(17,1)=0.965567315000000;
115  m_bound(18,1)=0.993351385000000;
116  m_bound(19,1)=1.000000000000000;
117 
118  m_bound(0,2)=0;
119  m_bound(1,2)=0.000397791059000;
120  m_bound(2,2)=0.002753100850000;
121  m_bound(3,2)=0.008770186980000;
122  m_bound(4,2)=0.020034759300000;
123  m_bound(5,2)=0.037511596000000;
124  m_bound(6,2)=0.060543032900000;
125  m_bound(7,2)=0.086256780600000;
126  m_bound(8,2)=0.109213531000000;
127  m_bound(9,2)=0.123026104000000;
128  m_bound(10,2)=0.123026104000000;
129  m_bound(11,2)=0.109213531000000;
130  m_bound(12,2)=0.086256780600000;
131  m_bound(13,2)=0.060543032900000;
132  m_bound(14,2)=0.037511596000000;
133  m_bound(15,2)=0.020034759300000;
134  m_bound(16,2)=0.008770186980000;
135  m_bound(17,2)=0.002753100850000;
136  m_bound(18,2)=0.000397791059000;
137  m_bound(19,2)=0;
138 
139  m_bound(0,3)=-CMath::INFTY;
140  m_bound(1,3)=-8.575194939999999;
141  m_bound(2,3)=-5.933689180000000;
142  m_bound(3,3)=-4.525933600000000;
143  m_bound(4,3)=-3.528107790000000;
144  m_bound(5,3)=-2.751548540000000;
145  m_bound(6,3)=-2.097898790000000;
146  m_bound(7,3)=-1.519690830000000;
147  m_bound(8,3)=-0.989533382000000;
148  m_bound(9,3)=-0.491473077000000;
149  m_bound(10,3)=0;
150  m_bound(11,3)=0.491473077000000;
151  m_bound(12,3)=0.989533382000000;
152  m_bound(13,3)=1.519690830000000;
153  m_bound(14,3)=2.097898790000000;
154  m_bound(15,3)=2.751548540000000;
155  m_bound(16,3)=3.528107790000000;
156  m_bound(17,3)=4.525933600000000;
157  m_bound(18,3)=5.933689180000000;
158  m_bound(19,3)=8.575194939999999;
159 
160  m_bound(0,4)=-8.575194939999999;
161  m_bound(1,4)=-5.933689180000000;
162  m_bound(2,4)=-4.525933600000000;
163  m_bound(3,4)=-3.528107790000000;
164  m_bound(4,4)=-2.751548540000000;
165  m_bound(5,4)=-2.097898790000000;
166  m_bound(6,4)=-1.519690830000000;
167  m_bound(7,4)=-0.989533382000000;
168  m_bound(8,4)=-0.491473077000000;
169  m_bound(9,4)=0;
170  m_bound(10,4)=0.491473077000000;
171  m_bound(11,4)=0.989533382000000;
172  m_bound(12,4)=1.519690830000000;
173  m_bound(13,4)=2.097898790000000;
174  m_bound(14,4)=2.751548540000000;
175  m_bound(15,4)=3.528107790000000;
176  m_bound(16,4)=4.525933600000000;
177  m_bound(17,4)=5.933689180000000;
178  m_bound(18,4)=8.575194939999999;
179  m_bound(19,4)= CMath::INFTY;
180 }
181 
183 {
184  //This function is based on the Matlab code,
185  //function [f, gm, gv] = Ellp(m, v, bound, ind), to compute f
186  //and the formula of the appendix
187 
188  const Map<VectorXd> eigen_c(m_bound.get_column_vector(0), m_bound.num_rows);
189  const Map<VectorXd> eigen_b(m_bound.get_column_vector(1), m_bound.num_rows);
190  const Map<VectorXd> eigen_a(m_bound.get_column_vector(2), m_bound.num_rows);
191  const Map<VectorXd> eigen_mu(m_mu.vector, m_mu.vlen);
192  const Map<VectorXd> eigen_s2(m_s2.vector, m_s2.vlen);
193 
194  Map<VectorXd> eigen_l(m_bound.get_column_vector(3), m_bound.num_rows);
195  Map<VectorXd> eigen_h(m_bound.get_column_vector(4), m_bound.num_rows);
196 
197  index_t num_rows = m_bound.num_rows;
198  index_t num_cols = m_mu.vlen;
199 
200  const Map<MatrixXd> eigen_cdf_diff(m_cdf_diff.matrix, num_rows, num_cols);
201  const Map<MatrixXd> eigen_pl(m_pl.matrix, num_rows, num_cols);
202  const Map<MatrixXd> eigen_ph(m_ph.matrix, num_rows, num_cols);
203 
204  float64_t l_bak = eigen_l(0);
205  //l(1) = 0;
206  eigen_l(0) = 0;
207 
208  float64_t h_bak = eigen_h(eigen_h.size()-1);
209  //h(end) = 0;
210  eigen_h(eigen_h.size()-1) = 0;
211 
212  //ex0 = ch-cl;
213  const Map<MatrixXd> & eigen_ex0 = eigen_cdf_diff;
214 
215  //%ex1= v.*(pl-ph) + m.*(ch-cl);
216  MatrixXd eigen_ex1 = ((eigen_pl - eigen_ph).array().rowwise()*eigen_s2.array().transpose()
217  + eigen_cdf_diff.array().rowwise()*eigen_mu.array().transpose()).matrix();
218 
219  //ex2 = bsxfun(@times, v, (bsxfun(@plus, l, m)).*pl - (bsxfun(@plus, h, m)).*ph) + bsxfun(@times, (v+m.^2), ex0);
220 
221  //bsxfun(@plus, l, m)).*pl - (bsxfun(@plus, h, m)).*ph
222  MatrixXd eigen_ex2 = ((eigen_mu.replicate(1,eigen_l.rows()).array().transpose().colwise() + eigen_l.array())*eigen_pl.array()
223  - (eigen_mu.replicate(1,eigen_h.rows()).array().transpose().colwise() + eigen_h.array())*eigen_ph.array()).matrix();
224 
225  //bsxfun(@times, v, (bsxfun(@plus, l, m).*pl - bsxfun(@plus, h, m).*ph
226  eigen_ex2 = (eigen_ex2.array().rowwise()*eigen_s2.array().transpose()).matrix();
227 
228  //ex2 = bsxfun(@times, v, (bsxfun(@plus, l, m)).*pl - (bsxfun(@plus, h, m)).*ph) + bsxfun(@times, (v+m.^2), ex0);
229  eigen_ex2 += (eigen_cdf_diff.array().rowwise()*(eigen_mu.array().pow(2)
230  + eigen_s2.array()).transpose()).matrix();
231 
233  Map<VectorXd> eigen_f(f.vector, f.vlen);
234 
235  //%f = sum((a.*ex2 + b.*ex1 + c.*ex0),1);
236  eigen_f = (eigen_ex2.array().colwise()*eigen_a.array()
237  + eigen_ex1.array().colwise()*eigen_b.array()
238  + eigen_ex0.array().colwise()*eigen_c.array()).colwise().sum().matrix();
239 
240  eigen_l(0) = l_bak;
241  eigen_h(eigen_h.size()-1) = h_bak;
242 
243  Map<VectorXd>eigen_lab(m_lab.vector, m_lab.vlen);
244  eigen_f = eigen_lab.cwiseProduct(eigen_mu) - eigen_f;
245  return f;
246 }
247 
248 
250  const TParameter* param) const
251 {
252  //This function is based on the Matlab code
253  //function [f, gm, gv] = Ellp(m, v, bound, ind), to compute gm and gv
254  //and the formula of the appendix
255  REQUIRE(param, "Param is required (param should not be NULL)\n");
256  REQUIRE(param->m_name, "Param name is required (param->m_name should not be NULL)\n");
257  //We take the derivative wrt to param. Only mu or sigma2 can be the param
258  REQUIRE(!(strcmp(param->m_name, "mu") && strcmp(param->m_name, "sigma2")),
259  "Can't compute derivative of the variational expection ",
260  "of log LogitLikelihood using the piecewise bound ",
261  "wrt %s.%s parameter. The function only accepts mu and sigma2 as parameter",
262  get_name(), param->m_name);
263 
264  const Map<VectorXd> eigen_c(m_bound.get_column_vector(0), m_bound.num_rows);
265  const Map<VectorXd> eigen_b(m_bound.get_column_vector(1), m_bound.num_rows);
266  const Map<VectorXd> eigen_a(m_bound.get_column_vector(2), m_bound.num_rows);
267  const Map<VectorXd> eigen_mu(m_mu.vector, m_mu.vlen);
268  const Map<VectorXd> eigen_s2(m_s2.vector, m_s2.vlen);
269 
270  Map<VectorXd> eigen_l(m_bound.get_column_vector(3), m_bound.num_rows);
271  Map<VectorXd> eigen_h(m_bound.get_column_vector(4), m_bound.num_rows);
272 
273  index_t num_rows = m_bound.num_rows;
274  index_t num_cols = m_mu.vlen;
275 
276  const Map<MatrixXd> eigen_cdf_diff(m_cdf_diff.matrix, num_rows, num_cols);
277  const Map<MatrixXd> eigen_pl(m_pl.matrix, num_rows, num_cols);
278  const Map<MatrixXd> eigen_ph(m_ph.matrix, num_rows, num_cols);
279  const Map<MatrixXd> eigen_weighted_pdf_diff(m_weighted_pdf_diff.matrix, num_rows, num_cols);
280  const Map<MatrixXd> eigen_h2_plus_s2(m_h2_plus_s2.matrix, num_rows, num_cols);
281  const Map<MatrixXd> eigen_l2_plus_s2(m_l2_plus_s2.matrix, num_rows, num_cols);
282 
283  float64_t l_bak = eigen_l(0);
284  //l(1) = 0;
285  eigen_l(0) = 0;
286  float64_t h_bak = eigen_h(eigen_h.size()-1);
287  //h(end) = 0;
288  eigen_h(eigen_h.size()-1) = 0;
289 
290  SGVector<float64_t> result(m_mu.vlen);
291 
292  if (strcmp(param->m_name, "mu") == 0)
293  {
294  //Compute the derivative wrt mu
295 
296  //bsxfun(@plus, bsxfun(@plus, l.^2, v), v).*pl - bsxfun(@plus, bsxfun(@plus, h.^2, v), v).*ph;
297  MatrixXd eigen_dmu2 = ((eigen_l2_plus_s2.array().rowwise()+eigen_s2.array().transpose())*eigen_pl.array()
298  - (eigen_h2_plus_s2.array().rowwise()+eigen_s2.array().transpose())*eigen_ph.array()).matrix();
299  //bsxfun(@times, ch-cl, (2.0*mu))
300  eigen_dmu2 += (eigen_cdf_diff.array().rowwise()*(2.0*eigen_mu).array().transpose()).matrix();
301 
302  SGVector<float64_t> & gmu = result;
303  Map<VectorXd> eigen_gmu(gmu.vector, gmu.vlen);
304 
305  //gmu = bsxfun(@times, dmu2, _a) + bsxfun(@times, pl.*l - ph.*h + ch - chl, b) + bsxfun(@times, pl - ph, c)
306  //gmu = sum(gmu,1)
307  eigen_gmu = ((eigen_dmu2.array().colwise()*eigen_a.array())
308  + ((eigen_weighted_pdf_diff + eigen_cdf_diff).array().colwise()*eigen_b.array())
309  + ( (eigen_pl - eigen_ph).array().colwise()*eigen_c.array())).colwise().sum().matrix();
310 
311  Map<VectorXd>eigen_lab(m_lab.vector, m_lab.vlen);
312  eigen_gmu = eigen_lab - eigen_gmu;
313  }
314  else
315  {
316  //Compute the derivative wrt sigma2
317 
318  //gv_0 = bsxfun(@plus, l, -mu).*pl - bsxfun(@plus, h, -mu).*ph;
319  MatrixXd eigen_gs2_0 = (((-eigen_mu).replicate(1,eigen_l.rows()).array().transpose().colwise() + eigen_l.array())*eigen_pl.array()
320  - ((-eigen_mu).replicate(1,eigen_h.rows()).array().transpose().colwise() + eigen_h.array())*eigen_ph.array()).matrix();
321  //gv_0 = bsxfun(times, gv_0, c);
322  eigen_gs2_0 = (eigen_gs2_0.array().colwise()*eigen_c.array()).matrix();
323 
324  //tmpl = l2_plus_v - bsxfun(@times, l, mu)
325  MatrixXd tmpl = (eigen_l2_plus_s2 - (eigen_mu.replicate(1,eigen_l.rows()).array().transpose().colwise()*eigen_l.array()).matrix()
326  ).cwiseProduct(eigen_pl);
327 
328  //tmph = h2_plus_v - bsxfun(@times, h, mu)
329  MatrixXd tmph = (eigen_h2_plus_s2 - (eigen_mu.replicate(1,eigen_h.rows()).array().transpose().colwise()*eigen_h.array()).matrix()
330  ).cwiseProduct(eigen_ph);
331 
332  //gv_1 = bsxfun(@times, tmpl - tmph, b);
333  MatrixXd eigen_gs2_1 = ((tmpl - tmph).array().colwise()*eigen_b.array()).matrix();
334 
335  //gv_2 = bsxfun(@times, tmpl, l) - bsxfun(@times, tmph, h);
336  MatrixXd eigen_gs2_2 = (tmpl.array().colwise()*eigen_l.array() - tmph.array().colwise()*eigen_h.array()).matrix();
337 
338  //gv_2 = bsxfun(@times, gv_2, a);
339  eigen_gs2_2 = (eigen_gs2_2.array().colwise()*eigen_a.array()).matrix();
340 
341  SGVector<float64_t> & gs2 = result;
342  Map<VectorXd> eigen_gs2(gs2.vector, gs2.vlen);
343 
344  //gv = (bsxfun(@times, ch - cl + 0.5*pl.*l - ph.*h, a) + bsxfun(@times, gv_0 + gv_1 + gv_2, 1.0/(2.0*v))
345  //gv = sum(gv,1);
346  eigen_gs2 = ((eigen_cdf_diff + 0.5*eigen_weighted_pdf_diff).array().colwise()*eigen_a.array()
347  + (eigen_gs2_0 + eigen_gs2_1 + eigen_gs2_2).array().rowwise()/(2.0*eigen_s2).array().transpose()
348  ).colwise().sum().matrix();
349 
350  //gv = -gv
351  eigen_gs2 = -eigen_gs2;
352  }
353  eigen_l(0) = l_bak;
354  eigen_h(eigen_h.size()-1) = h_bak;
355 
356  return result;
357 }
358 
360  SGVector<float64_t> s2, const CLabels* lab)
361 {
362  bool status = true;
364  if (status)
365  {
367  "Labels must be type of CBinaryLabels\n");
368 
369  m_lab = (((CBinaryLabels*)lab)->get_labels()).clone();
370 
371  //Convert the input label to standard label used in the class
372  //Note that Shogun uses -1 and 1 as labels and this class internally uses
373  //0 and 1 repectively.
374  for(index_t i = 0; i < m_lab.size(); ++i)
375  m_lab[i] = CMath::max(m_lab[i], 0.0);
376 
377  precompute();
378 
379  }
380  return status;
381 }
382 
384 {
386 }
387 
388 void CLogitVGPiecewiseBoundLikelihood::init()
389 {
390  SG_ADD(&m_bound, "bound",
391  "Variational piecewise bound for logit likelihood",
393  SG_ADD(&m_pl, "pdf_l",
394  "The pdf given the lower range and parameters(mu and variance)",
396  SG_ADD(&m_ph, "pdf_h",
397  "The pdf given the higher range and parameters(mu and variance)",
399  SG_ADD(&m_cdf_diff, "cdf_h_minus_cdf_l",
400  "The CDF difference between the lower and higher range given the parameters(mu and variance)",
402  SG_ADD(&m_l2_plus_s2, "l2_plus_sigma2",
403  "The result of l^2 + sigma^2",
405  SG_ADD(&m_h2_plus_s2, "h2_plus_sigma2",
406  "The result of h^2 + sigma^2",
408  SG_ADD(&m_weighted_pdf_diff, "weighted_pdf_diff",
409  "The result of l*pdf(l_norm)-h*pdf(h_norm)",
411 
412  init_likelihood();
413 }
414 
415 void CLogitVGPiecewiseBoundLikelihood::precompute()
416 {
417  //This function is based on the Matlab code
418  //function [f, gm, gv] = Ellp(m, v, bound, ind), to compute common variables later
419  //used in get_variational_expection and get_variational_first_derivative
420 
421  const Map<VectorXd> eigen_c(m_bound.get_column_vector(0), m_bound.num_rows);
422  const Map<VectorXd> eigen_b(m_bound.get_column_vector(1), m_bound.num_rows);
423  const Map<VectorXd> eigen_a(m_bound.get_column_vector(2), m_bound.num_rows);
424  const Map<VectorXd> eigen_mu(m_mu.vector, m_mu.vlen);
425  const Map<VectorXd> eigen_s2(m_s2.vector, m_s2.vlen);
426 
427  Map<VectorXd> eigen_l(m_bound.get_column_vector(3), m_bound.num_rows);
428  Map<VectorXd> eigen_h(m_bound.get_column_vector(4), m_bound.num_rows);
429 
430  index_t num_rows = m_bound.num_rows;
431  index_t num_cols = m_mu.vlen;
432 
433  m_pl = SGMatrix<float64_t>(num_rows,num_cols);
434  m_ph = SGMatrix<float64_t>(num_rows,num_cols);
435  m_cdf_diff = SGMatrix<float64_t>(num_rows,num_cols);
436  m_l2_plus_s2 = SGMatrix<float64_t>(num_rows,num_cols);
437  m_h2_plus_s2 = SGMatrix<float64_t>(num_rows,num_cols);
438  m_weighted_pdf_diff = SGMatrix<float64_t>(num_rows,num_cols);
439 
440  Map<MatrixXd> eigen_pl(m_pl.matrix, num_rows, num_cols);
441  Map<MatrixXd> eigen_ph(m_ph.matrix, num_rows, num_cols);
442  Map<MatrixXd> eigen_cdf_diff(m_cdf_diff.matrix, num_rows, num_cols);
443  Map<MatrixXd> eigen_l2_plus_s2(m_l2_plus_s2.matrix, num_rows, num_cols);
444  Map<MatrixXd> eigen_h2_plus_s2(m_h2_plus_s2.matrix, num_rows, num_cols);
445  Map<MatrixXd> eigen_weighted_pdf_diff(m_weighted_pdf_diff.matrix, num_rows, num_cols);
446 
447  SGMatrix<float64_t> zl(num_rows, num_cols);
448  Map<MatrixXd> eigen_zl(zl.matrix, num_rows, num_cols);
449  SGMatrix<float64_t> zh(num_rows, num_cols);
450  Map<MatrixXd> eigen_zh(zh.matrix, num_rows, num_cols);
451 
452  //bsxfun(@minus,l,m)
453  eigen_zl = ((-eigen_mu).replicate(1,eigen_l.rows()).array().transpose().colwise() + eigen_l.array()).matrix();
454  //bsxfun(@minus,h,m)
455  eigen_zh = ((-eigen_mu).replicate(1,eigen_h.rows()).array().transpose().colwise() + eigen_h.array()).matrix();
456 
457  VectorXd eigen_s_inv = eigen_s2.array().sqrt().inverse().matrix();
458 
459  //zl = bsxfun(@times, bsxfun(@minus,l,m), 1./sqrt(v))
460  eigen_zl = (eigen_zl.array().rowwise()*eigen_s_inv.array().transpose()).matrix();
461  //zh = bsxfun(@times, bsxfun(@minus,h,m), 1./sqrt(v))
462  eigen_zh = (eigen_zh.array().rowwise()*eigen_s_inv.array().transpose()).matrix();
463 
464  //Usually we use pdf in log-domain and the log_sum_exp trick
465  //to avoid numerical underflow in particular for IID samples
466  for (index_t r = 0; r < zl.num_rows; r++)
467  {
468  for (index_t c = 0; c < zl.num_cols; c++)
469  {
470  if (CMath::abs(zl(r, c)) == CMath::INFTY)
471  m_pl(r, c) = 0;
472  else
474 
475  if (CMath::abs(zh(r, c)) == CMath::INFTY)
476  m_ph(r, c) = 0;
477  else
479  }
480  }
481 
482  //pl = bsxfun(@times, normpdf(zl), 1./sqrt(v));
483  eigen_pl = (eigen_pl.array().rowwise()*eigen_s_inv.array().transpose()).matrix();
484  //ph = bsxfun(@times, normpdf(zh), 1./sqrt(v));
485  eigen_ph = (eigen_ph.array().rowwise()*eigen_s_inv.array().transpose()).matrix();
486 
487  SGMatrix<float64_t> & cl = zl;
488  SGMatrix<float64_t> & ch = zh;
489 
490  for (index_t r = 0; r < zl.num_rows; r++)
491  {
492  for (index_t c = 0; c < zl.num_cols; c++)
493  {
494  //cl = 0.5*erf(zl/sqrt(2)); %normal cdf -const
495  cl(r, c) = CStatistics::normal_cdf(zl(r, c)) - 0.5;
496  //ch = 0.5*erf(zl/sqrt(2)); %normal cdf -const
497  ch(r, c) = CStatistics::normal_cdf(zh(r, c)) - 0.5;
498  }
499  }
500 
501  Map<MatrixXd> eigen_cl(cl.matrix, num_rows, num_cols);
502  Map<MatrixXd> eigen_ch(ch.matrix, num_rows, num_cols);
503 
504  eigen_cdf_diff = eigen_ch - eigen_cl;
505 
506  float64_t l_bak = eigen_l(0);
507  eigen_l(0) = 0;
508 
509  float64_t h_bak = eigen_h(eigen_h.size()-1);
510  eigen_h(eigen_h.size()-1) = 0;
511 
512  //bsxfun(@plus, l.^2, v)
513  eigen_l2_plus_s2 = (eigen_s2.replicate(1,eigen_l.rows()).array().transpose().colwise() + eigen_l.array().pow(2)).matrix();
514  //bsxfun(@plus, h.^2, v)
515  eigen_h2_plus_s2 = (eigen_s2.replicate(1,eigen_h.rows()).array().transpose().colwise() + eigen_h.array().pow(2)).matrix();
516  //pl.*l - ph.*h
517  eigen_weighted_pdf_diff = (eigen_pl.array().colwise() * eigen_l.array() - eigen_ph.array().colwise() * eigen_h.array()).matrix();
518 
519  eigen_l(0) = l_bak;
520  eigen_h(eigen_h.size()-1) = h_bak;
521 }
522 
524  const TParameter* param) const
525 {
526  return SGVector<float64_t>();
527 }
528 
529 } /* namespace shogun */
530 #endif /* HAVE_EIGEN3 */
virtual bool set_variational_distribution(SGVector< float64_t > mu, SGVector< float64_t > s2, const CLabels *lab)
Class that models Logit likelihood.
virtual ELabelType get_label_type() const =0
binary labels +1/-1
Definition: LabelTypes.h:18
int32_t index_t
Definition: common.h:62
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
static const float64_t INFTY
infinity
Definition: Math.h:2048
virtual CSGObject * clone()
Definition: SGObject.cpp:714
The variational Gaussian Likelihood base class. The variational distribution is Gaussian.
Definition: SGMatrix.h:20
parameter struct
#define REQUIRE(x,...)
Definition: SGIO.h:206
index_t num_rows
Definition: SGMatrix.h:376
virtual void set_variational_bound(SGMatrix< float64_t > bound)
index_t vlen
Definition: SGVector.h:494
virtual void set_likelihood(CLikelihoodModel *lik)
virtual SGVector< float64_t > get_variational_first_derivative(const TParameter *param) const
virtual SGVector< float64_t > get_first_derivative_wrt_hyperparameter(const TParameter *param) const
double float64_t
Definition: common.h:50
T * get_column_vector(index_t col) const
Definition: SGMatrix.h:115
static T max(T a, T b)
Definition: Math.h:168
static float64_t univariate_log_pdf(float64_t sample, float64_t mu=0.0, float64_t sigma2=1.0)
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
static float64_t exp(float64_t x)
Definition: Math.h:621
static float64_t normal_cdf(float64_t x, float64_t std_dev=1)
Binary Labels for binary classification.
Definition: BinaryLabels.h:37
virtual bool set_variational_distribution(SGVector< float64_t > mu, SGVector< float64_t > s2, const CLabels *lab)
#define SG_ADD(...)
Definition: SGObject.h:81
static T abs(T a)
Definition: Math.h:179

SHOGUN 机器学习工具包 - 项目文档