24 #include <boost/math/special_functions/fpclassify.hpp> 25 #include <boost/numeric/ublas/banded.hpp> 28 #include "ublas_trace.hpp" 35 namespace ublas = boost::numeric::ublas;
37 StudentTProcessNIG::StudentTProcessNIG(
size_t dim, Parameters params,
39 MeanModel& mean, randEngine& eng):
40 HierarchicalGaussianProcess(dim,params,data, mean, eng),
41 mAlpha(params.alpha), mBeta (params.beta),
42 mW0(params.mean.coef_mean.size()), mInvVarW(params.mean.coef_mean.size()),
43 mD(params.mean.coef_mean.size(),params.mean.coef_mean.size())
45 mW0 = params.mean.coef_mean;
46 for (
size_t ii = 0; ii < params.mean.coef_mean.size(); ++ii)
48 double varii = params.mean.coef_std[ii] * params.mean.coef_std[ii];
49 mInvVarW(ii) = 1/varii;
51 d_ =
new StudentTDistribution(eng);
56 StudentTProcessNIG::~StudentTProcessNIG()
64 double kq = computeSelfCorrelation(query);
65 vectord kn = computeCrossCorrelation(query);
66 vectord phi = mMean.getFeatures(query);
69 inplace_solve(
mL,v,ublas::lower_tag());
71 vectord rq = phi - prod(v,mKF);
74 inplace_solve(mD,rho,ublas::lower_tag());
76 double yPred = inner_prod(phi,mWMap) + inner_prod(v,mVf);
77 double sPred = sqrt(
mSigma * (kq - inner_prod(v,v)
78 + inner_prod(rho,rho)));
80 if ((boost::math::isnan(yPred)) || (boost::math::isnan(sPred)))
82 throw std::runtime_error(
"Error in prediction. NaN found.");
94 const size_t n = KK.size1();
95 const size_t p = mMean.nFeatures();
96 const size_t nalpha = (n+2*mAlpha);
98 vectord v0 = mData.
mY - prod(trans(mMean.
mFeatM),mW0);
99 matrixd WW = zmatrixd(p,p);
100 utils::add_to_diagonal(WW,mInvVarW);
101 matrixd FW = prod(trans(mMean.
mFeatM),WW);
102 KK += prod(FW,mMean.
mFeatM);
105 inplace_solve(BB,v0,ublas::lower_tag());
106 double zz = inner_prod(v0,v0);
107 double sigmaMap = (mBeta/mAlpha + zz)/nalpha;
109 double lik = nalpha/2 * std::log(1+zz/(2*mBeta*sigmaMap));
110 lik += utils::log_trace(BB);
111 lik += n/2 * std::log(sigmaMap);
119 size_t n = mData.getNSamples();
120 size_t p = mMean.nFeatures();
122 mKF = trans(mMean.
mFeatM);
123 inplace_solve(
mL,mKF,ublas::lower_tag());
126 DD = prod(trans(mKF),mKF);
127 utils::add_to_diagonal(DD,mInvVarW);
130 vectord vn = mData.
mY;
131 inplace_solve(
mL,vn,ublas::lower_tag());
135 mVf = mData.
mY - prod(trans(mMean.
mFeatM),mWMap);
136 inplace_solve(
mL,mVf,ublas::lower_tag());
138 vectord v0 = mData.
mY - prod(trans(mMean.
mFeatM),mW0);
142 matrixd WW = zmatrixd(p,p);
143 utils::add_to_diagonal(WW,mInvVarW);
144 const matrixd FW = prod(trans(mMean.
mFeatM),WW);
145 KK += prod(FW,mMean.
mFeatM);
148 inplace_solve(BB,v0,ublas::lower_tag());
149 mSigma = (mBeta/mAlpha + inner_prod(v0,v0))/(n+2*mAlpha);
151 int dof =
static_cast<int>(n+2*mAlpha);
153 if ((boost::math::isnan(mWMap(0))) || (boost::math::isnan(
mSigma)))
155 throw std::runtime_error(
"Error in precomputed prediction. NaN found.");
162 FILE_LOG(logERROR) <<
"ERROR: Incorrect alpha. Dof invalid." 163 <<
"Forcing Dof <= num of points.";
matrixd computeCorrMatrix()
Computes the Correlation (Kernel or Gram) matrix.
Namespace of the library interface.
Student's t process with Normal-Inverse-Gamma hyperprior on mean and signal variance parameters...
StudentTDistribution * d_
Predictive distributions.
ProbabilityDistribution * prediction(const vectord &query)
Function that returns the prediction of the GP for a query point in the hypercube [0...
void precomputePrediction()
Precompute some values of the prediction that do not depends on the query.
Elementwise operations for ublas vector/matrix.
void setMeanAndStd(double mean, double std)
Sets the mean and std of the distribution.
void setDof(size_t dof)
Sets the degrees of freedom (dof) the distribution.
matrixd mL
Cholesky decomposition of the Correlation matrix.
v1 ublas_elementwise_prod(const v1 &a, const v2 &b)
Computes the elementwise product of two vectors or matrices.
double mSigma
Signal variance.
size_t cholesky_decompose(const MATRIX &A, TRIA &L)
decompose the symmetric positive definit matrix A into product L L^T.
void cholesky_solve(const TRIA &L, MATRIX &x, ublas::lower)
solve system L L^T x = b inplace
Modules and helper macros for logging.
matrixd mFeatM
Value of the mean features at the input points.
double negativeLogLikelihood()
Computes the negative log likelihood and its gradient of the data.
Student's t probability distribution.