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()
146 if (m_ttau.
vlen!=n || m_nlZ>nlZ0)
186 while ((
CMath::abs(m_nlZ-nlZ_old)>m_tol && sweep<m_max_sweep) ||
200 tau_n[i]=1.0/m_Sigma(i,i)-m_ttau[i];
201 nu_n[i]=m_mu[i]/m_Sigma(i,i)+mean[i]*tau_n[i]-m_tnu[i];
204 mu_n[i]=nu_n[i]/tau_n[i];
205 s2_n[i]=1.0/tau_n[i];
219 m_tnu[i]=mu/s2-nu_n[i];
230 VectorXd eigen_si=eigen_Sigma.col(i);
233 eigen_Sigma=eigen_Sigma-ds2/(1.0+ds2*eigen_si(i))*eigen_si*
237 eigen_mu=eigen_Sigma*eigen_tnu;
249 if (sweep==m_max_sweep &&
CMath::abs(m_nlZ-nlZ_old)>m_tol)
251 SG_ERROR(
"Maximum number (%d) of sweeps reached, but tolerance (%f) was "
252 "not yet reached. You can manually set maximum number of sweeps "
253 "or tolerance to fix this problem.\n", m_max_sweep, m_tol);
281 VectorXd eigen_v=eigen_L.triangularView<Upper>().adjoint().solve(
283 eigen_v=eigen_L.triangularView<Upper>().solve(eigen_v);
288 eigen_alpha=eigen_tnu-eigen_sttau.cwiseProduct(eigen_v);
304 LLT<MatrixXd> eigen_chol((eigen_sttau*eigen_sttau.adjoint()).cwiseProduct(
308 eigen_L=eigen_chol.matrixU();
324 MatrixXd eigen_V=eigen_L.triangularView<Upper>().adjoint().solve(
345 eigen_mu=eigen_Sigma*eigen_tnu;
362 VectorXd eigen_tau_n=(VectorXd::Ones(m_ttau.
vlen)).cwiseQuotient(
363 eigen_Sigma.diagonal())-eigen_ttau;
366 VectorXd eigen_nu_n=eigen_mu.cwiseQuotient(eigen_Sigma.diagonal())-
367 eigen_tnu+eigen_m.cwiseProduct(eigen_tau_n);
373 eigen_mu_n=eigen_nu_n.cwiseQuotient(eigen_tau_n);
379 eigen_s2_n=(VectorXd::Ones(m_ttau.
vlen)).cwiseQuotient(eigen_tau_n);
385 float64_t nlZ_part1=eigen_L.diagonal().array().log().sum()-lZ-
386 (eigen_tnu.adjoint()*eigen_Sigma).
dot(eigen_tnu)/2.0;
389 float64_t nlZ_part2=(eigen_tnu.array().square()/
390 (eigen_tau_n+eigen_ttau).array()).
sum()/2.0-(1.0+eigen_ttau.array()/
391 eigen_tau_n.array()).log().sum()/2.0;
395 float64_t nlZ_part3=-(eigen_nu_n-eigen_m.cwiseProduct(eigen_tau_n)).
dot(
396 ((eigen_ttau.array()/eigen_tau_n.array()*(eigen_nu_n.array()-
397 eigen_m.array()*eigen_tau_n.array())-2*eigen_tnu.array())/
398 (eigen_ttau.array()+eigen_tau_n.array())).matrix())/2.0;
401 m_nlZ=nlZ_part1+nlZ_part2+nlZ_part3;
416 MatrixXd V=eigen_L.triangularView<Upper>().adjoint().solve(
417 MatrixXd(eigen_sttau.asDiagonal()));
418 V=eigen_L.triangularView<Upper>().solve(V);
421 eigen_F=eigen_alpha*eigen_alpha.adjoint()-eigen_sttau.asDiagonal()*V;
427 REQUIRE(!strcmp(param->
m_name,
"scale"),
"Can't compute derivative of "
428 "the nagative log marginal likelihood wrt %s.%s parameter\n",
437 result[0]=-(eigen_F.cwiseProduct(eigen_K)*
m_scale*2.0).
sum()/2.0;
461 "Length of the parameter %s should not be NULL\n", param->
m_name)
virtual void update_approx_mean()
static void permute(SGVector< T > v, CRandom *rand=NULL)
virtual SGVector< float64_t > get_diagonal_vector()
virtual void update_parameter_hash()
virtual SGVector< float64_t > get_alpha()
SGVector< float64_t > m_alpha
virtual void update_approx_cov()
The Inference Method base class.
Vector::Scalar dot(Vector a, Vector b)
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 SGVector< float64_t > get_posterior_mean()
void scale(T alpha)
Scale vector inplace.
SGMatrix< float64_t > m_L
virtual SGVector< float64_t > get_derivative_wrt_mean(const TParameter *param)
virtual void update_negative_ml()
Matrix::Scalar sum(Matrix m, bool no_diag=false)
#define CREATE_SGVECTOR(vec, len, sg_type)
void range_fill(T start=0)
virtual void update_chol()
Matrix< float64_t,-1,-1, 0,-1,-1 > MatrixXd
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)
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()
virtual const char * get_name() const
virtual bool parameter_hash_changed()
The Likelihood model base class.
SGMatrix< float64_t > m_ktrtr
virtual void update_deriv()
CLikelihoodModel * m_model