8 MixtureDistribution::MixtureDistribution(std::vector<std::shared_ptr<Distribution>>
const& componentsIn,
9 Eigen::VectorXd
const& probsIn) :
Distribution(componentsIn.at(0)->varSize, componentsIn.at(0)->hyperSizes),
10 components(componentsIn), probs(probsIn)
18 std::stringstream msg;
19 msg <<
"Could not construct MixtureDistribution. Number of components is " <<
components.size() <<
" but " <<
probs.size() <<
" probabilities were given.";
20 throw std::runtime_error(msg.str());
24 for(
unsigned int i=1; i<
components.size(); ++i)
27 std::stringstream msg;
28 msg <<
"Could not construct MixtureDistribution. Component " << i <<
" has a dimension of " <<
components.at(i)->varSize <<
" but component 0 has a dimension of " <<
varSize;
29 throw std::runtime_error(msg.str());
33 std::stringstream msg;
34 msg <<
"Could not construct MixtureDistribution. Component " << i <<
" has " <<
components.at(i)->hyperSizes.size() <<
" hyperparameters but component 0 has " <<
hyperSizes.size() <<
" hyperparameters.";
35 throw std::runtime_error(msg.str());
38 for(
unsigned int j=0; j<
hyperSizes.size(); ++j){
40 std::stringstream msg;
41 msg <<
"Could not construct MixtureDistribution. Hyperparameter " << j <<
" of component " << i <<
" has dimension " <<
components.at(i)->hyperSizes(j) <<
" but this hyperparameter in component 0 has dimension " <<
hyperSizes(j);
42 throw std::runtime_error(msg.str());
53 for(
unsigned int i=0; i<
components.size(); ++i)
65 Eigen::VectorXd grad_pdf = Eigen::VectorXd::Zero(gradDim);
67 for(
unsigned int i=0; i<
components.size(); ++i){
68 double comp_pdf = std::exp(
components.at(i)->LogDensity(inputs));
69 pdf +=
probs(i)*comp_pdf;
70 grad_pdf +=
probs(i)*comp_pdf *
components.at(i)->GradLogDensity(wrt,inputs);
73 return (1.0/pdf) * grad_pdf;
80 int randInd = RandomGenerator::GetDiscrete(
probs);
const unsigned int varSize
const Eigen::VectorXi hyperSizes
virtual Eigen::VectorXd GradLogDensityImpl(unsigned int wrt, ref_vector< Eigen::VectorXd > const &inputs) override
virtual Eigen::VectorXd SampleImpl(ref_vector< Eigen::VectorXd > const &inputs) override
Sample the distribution.
std::vector< std::shared_ptr< Distribution > > components
virtual double LogDensityImpl(ref_vector< Eigen::VectorXd > const &inputs) override
std::vector< std::reference_wrapper< const T > > ref_vector
A vector of references to something ...