28 using namespace Eigen;
31 #define CREATE_SGVECTOR(vec, len, sg_type) \
33 if (!vec.vector || vec.vlen!=len) \
34 vec=SGVector<sg_type>(len); \
38 #define CREATE_SGMATRIX(mat, rows, cols, sg_type) \
40 if (!mat.matrix || mat.num_rows!=rows || mat.num_cols!=cols) \
41 mat=SGMatrix<sg_type>(rows, cols); \
60 void CEPInferenceMethod::init()
144 if (m_ttau.
vlen!=n || m_nlZ>nlZ0)
183 while ((
CMath::abs(m_nlZ-nlZ_old)>m_tol && sweep<m_max_sweep) ||
197 tau_n[i]=1.0/m_Sigma(i,i)-m_ttau[i];
198 nu_n[i]=m_mu[i]/m_Sigma(i,i)+mean[i]*tau_n[i]-m_tnu[i];
201 mu_n[i]=nu_n[i]/tau_n[i];
202 s2_n[i]=1.0/tau_n[i];
216 m_tnu[i]=mu/s2-nu_n[i];
224 Map<VectorXd> eigen_tnu(m_tnu.vector, m_tnu.vlen);
225 Map<VectorXd> eigen_mu(m_mu.
vector, m_mu.
vlen);
227 VectorXd eigen_si=eigen_Sigma.col(i);
230 eigen_Sigma=eigen_Sigma-ds2/(1.0+ds2*eigen_si(i))*eigen_si*
234 eigen_mu=eigen_Sigma*eigen_tnu;
246 if (sweep==m_max_sweep &&
CMath::abs(m_nlZ-nlZ_old)>m_tol)
248 SG_ERROR(
"Maximum number (%d) of sweeps reached, but tolerance (%f) was "
249 "not yet reached. You can manually set maximum number of sweeps "
250 "or tolerance to fix this problem.\n", m_max_sweep, m_tol);
264 Map<VectorXd> eigen_tnu(m_tnu.
vector, m_tnu.
vlen);
265 Map<VectorXd> eigen_sttau(m_sttau.
vector, m_sttau.
vlen);
273 VectorXd eigen_v=eigen_L.triangularView<Upper>().adjoint().solve(
275 eigen_v=eigen_L.triangularView<Upper>().solve(eigen_v);
280 eigen_alpha=eigen_tnu-eigen_sttau.cwiseProduct(eigen_v);
287 Map<VectorXd> eigen_sttau(m_sttau.
vector, m_sttau.
vlen);
296 LLT<MatrixXd> eigen_chol((eigen_sttau*eigen_sttau.adjoint()).cwiseProduct(
300 eigen_L=eigen_chol.matrixU();
308 Map<VectorXd> eigen_sttau(m_sttau.
vector, m_sttau.
vlen);
316 MatrixXd eigen_V=eigen_L.triangularView<Upper>().adjoint().solve(
330 Map<VectorXd> eigen_tnu(m_tnu.
vector, m_tnu.
vlen);
334 Map<VectorXd> eigen_mu(m_mu.
vector, m_mu.
vlen);
337 eigen_mu=eigen_Sigma*eigen_tnu;
345 Map<VectorXd> eigen_mu(m_mu.
vector, m_mu.
vlen);
346 Map<VectorXd> eigen_tnu(m_tnu.
vector, m_tnu.
vlen);
347 Map<VectorXd> eigen_ttau(m_ttau.
vector, m_ttau.
vlen);
354 VectorXd eigen_tau_n=(VectorXd::Ones(m_ttau.
vlen)).cwiseQuotient(
355 eigen_Sigma.diagonal())-eigen_ttau;
358 VectorXd eigen_nu_n=eigen_mu.cwiseQuotient(eigen_Sigma.diagonal())-
359 eigen_tnu+eigen_m.cwiseProduct(eigen_tau_n);
363 Map<VectorXd> eigen_mu_n(mu_n.
vector, mu_n.
vlen);
365 eigen_mu_n=eigen_nu_n.cwiseQuotient(eigen_tau_n);
369 Map<VectorXd> eigen_s2_n(s2_n.
vector, s2_n.
vlen);
371 eigen_s2_n=(VectorXd::Ones(m_ttau.
vlen)).cwiseQuotient(eigen_tau_n);
377 float64_t nlZ_part1=eigen_L.diagonal().array().log().sum()-lZ-
378 (eigen_tnu.adjoint()*eigen_Sigma).dot(eigen_tnu)/2.0;
381 float64_t nlZ_part2=(eigen_tnu.array().square()/
382 (eigen_tau_n+eigen_ttau).array()).sum()/2.0-(1.0+eigen_ttau.array()/
383 eigen_tau_n.array()).log().sum()/2.0;
387 float64_t nlZ_part3=-(eigen_nu_n-eigen_m.cwiseProduct(eigen_tau_n)).dot(
388 ((eigen_ttau.array()/eigen_tau_n.array()*(eigen_nu_n.array()-
389 eigen_m.array()*eigen_tau_n.array())-2*eigen_tnu.array())/
390 (eigen_ttau.array()+eigen_tau_n.array())).matrix())/2.0;
393 m_nlZ=nlZ_part1+nlZ_part2+nlZ_part3;
400 Map<VectorXd> eigen_sttau(m_sttau.
vector, m_sttau.
vlen);
408 MatrixXd V=eigen_L.triangularView<Upper>().adjoint().solve(
409 MatrixXd(eigen_sttau.asDiagonal()));
410 V=eigen_L.triangularView<Upper>().solve(V);
413 eigen_F=eigen_alpha*eigen_alpha.adjoint()-eigen_sttau.asDiagonal()*V;
419 REQUIRE(!strcmp(param->
m_name,
"scale"),
"Can't compute derivative of "
420 "the nagative log marginal likelihood wrt %s.%s parameter\n",
429 result[0]=-(eigen_F.cwiseProduct(eigen_K)*
m_scale*2.0).
sum()/2.0;
453 "Length of the parameter %s should not be NULL\n", param->
m_name)
virtual void update_approx_mean()
virtual SGVector< float64_t > get_diagonal_vector()
virtual SGVector< float64_t > get_alpha()
SGVector< float64_t > m_alpha
virtual void update_approx_cov()
The Inference Method base class.
The class Labels models labels, i.e. class assignments of objects.
static const float64_t INFTY
infinity
virtual SGMatrix< float64_t > get_posterior_covariance()
virtual int32_t get_num_labels() const =0
static T sum(T *vec, int32_t len)
return sum(vec)
virtual SGVector< float64_t > get_log_zeroth_moments(SGVector< float64_t > mu, SGVector< float64_t > s2, const CLabels *lab) const =0
#define SG_NOTIMPLEMENTED
virtual SGVector< float64_t > get_mean_vector(const CFeatures *features) const =0
#define CREATE_SGMATRIX(mat, rows, cols, sg_type)
virtual float64_t get_negative_log_marginal_likelihood()
virtual float64_t get_second_moment(SGVector< float64_t > mu, SGVector< float64_t > s2, const CLabels *lab, index_t i) const =0
An abstract class of the mean function.
virtual SGVector< float64_t > get_derivative_wrt_kernel(const TParameter *param)
virtual bool update_parameter_hash()
virtual SGVector< float64_t > get_posterior_mean()
void scale(T alpha)
scale vector inplace
static SGVector< T > randperm_vec(int32_t n)
SGMatrix< float64_t > m_L
virtual SGVector< float64_t > get_derivative_wrt_mean(const TParameter *param)
virtual void update_negative_ml()
#define CREATE_SGVECTOR(vec, len, sg_type)
virtual void update_chol()
static T max(T a, T b)
return the maximum of two integers
virtual SGVector< float64_t > get_derivative_wrt_likelihood_model(const TParameter *param)
virtual ~CEPInferenceMethod()
all of classes and functions are contained in the shogun namespace
virtual SGVector< float64_t > get_derivative_wrt_inference_method(const TParameter *param)
The class Features is the base class of all feature objects.
virtual SGMatrix< float64_t > get_parameter_gradient(const TParameter *param, index_t index=-1)
SGVector< T > get_diagonal_vector() const
virtual SGMatrix< float64_t > get_cholesky()
static float32_t sqrt(float32_t x)
x^0.5
virtual float64_t get_first_moment(SGVector< float64_t > mu, SGVector< float64_t > s2, const CLabels *lab, index_t i) const =0
virtual void update_alpha()
static void permute(T *vec, int32_t n)
virtual const char * get_name() const
The Likelihood model base class.
SGMatrix< float64_t > m_ktrtr
virtual void update_deriv()
CLikelihoodModel * m_model
static T abs(T a)
return the absolute value of a number