17 using namespace Eigen;
31 "covariance, which is %dx%d, but is %d\n",
45 LLT<MatrixXd> llt(eigen_cov);
46 if (llt.info()==NumericalIssue)
49 SelfAdjointEigenSolver<MatrixXd> solver(eigen_cov);
50 if (solver.info() == Success)
52 VectorXd ev=solver.eigenvalues();
53 SG_ERROR(
"Error computing Cholesky of Gaussian's covariance. "
54 "Smallest Eigenvalue is %f.\n", ev[0]);
58 eigen_L=llt.matrixL();
72 REQUIRE(num_samples>0,
"Number of samples (%d) must be positive\n",
80 " (%d) does not match dimension of Gaussian (%d)\n",
84 " (%d) does not desired number of samples (%d)\n",
93 for (
index_t i=0; i<m_dimension*num_samples; ++i)
101 eigen_samples=eigen_L*eigen_samples;
105 eigen_samples.colwise()+=eigen_mean;
112 REQUIRE(samples.
num_cols>0,
"Number of samples must be positive, but is %d\n",
125 VectorXd diag=eigen_L.diagonal();
126 log_det_part=-diag.array().log().sum();
139 for (
index_t sample_idx=0; sample_idx<num_samples; ++sample_idx)
140 centred(dim,sample_idx)=samples(dim,sample_idx)-
m_mean[dim];
144 MatrixXd solved=eigen_L.triangularView<Lower>().solve(eigen_centred);
145 solved=eigen_L.transpose().triangularView<Upper>().solve(solved);
150 for (
index_t i=0; i<num_samples; ++i)
153 VectorXd left=eigen_centred.block(0, i, m_dimension, 1);
156 VectorXd right=solved.block(0,i,m_dimension,1);
157 result[i]=-0.5*left.dot(right);
161 eigen_result=eigen_result.array()+(log_det_part+const_part);
168 void CGaussianDistribution::init()
171 SG_ADD(&
m_L,
"L",
"Lower factor of covariance matrix, "
virtual ~CGaussianDistribution()
float64_t std_normal_distrib() const
A base class for representing n-dimensional probability distribution over the real numbers (64bit) fo...
virtual SGVector< float64_t > log_pdf_multiple(SGMatrix< float64_t > samples) const
all of classes and functions are contained in the shogun namespace
SGVector< float64_t > m_mean
static float64_t log(float64_t v)
SGMatrix< float64_t > m_L
virtual SGVector< float64_t > sample() const
static const float64_t PI