28 double PosteriorShape(Eigen::VectorXd& residuals,
double a,
double b) {
29 data_size_t n = residuals.rows();
32 double PosteriorScale(Eigen::VectorXd& residuals,
double a,
double b) {
33 data_size_t n = residuals.rows();
34 double sum_sq_resid = 0.;
35 for (data_size_t i = 0; i < n; i++) {
36 sum_sq_resid += (residuals(i) * residuals(i));
38 return b + (0.5 * sum_sq_resid);
40 double PosteriorShape(Eigen::VectorXd& residuals, Eigen::VectorXd& weights,
double a,
double b) {
41 data_size_t n = residuals.rows();
44 double PosteriorScale(Eigen::VectorXd& residuals, Eigen::VectorXd& weights,
double a,
double b) {
45 data_size_t n = residuals.rows();
46 double sum_sq_resid = 0.;
47 for (data_size_t i = 0; i < n; i++) {
48 sum_sq_resid += (residuals(i) * residuals(i)) * weights(i);
50 return b + (0.5 * sum_sq_resid);
52 double SampleVarianceParameter(Eigen::VectorXd& residuals,
double a,
double b, std::mt19937& gen) {
53 double ig_shape = PosteriorShape(residuals, a, b);
54 double ig_scale = PosteriorScale(residuals, a, b);
55 return ig_sampler_.Sample(ig_shape, ig_scale, gen);
57 double SampleVarianceParameter(Eigen::VectorXd& residuals, Eigen::VectorXd& weights,
double a,
double b, std::mt19937& gen) {
58 double ig_shape = PosteriorShape(residuals, weights, a, b);
59 double ig_scale = PosteriorScale(residuals, weights, a, b);
60 return ig_sampler_.Sample(ig_shape, ig_scale, gen);
71 double PosteriorShape(
TreeEnsemble* ensemble,
double a,
double b) {
72 data_size_t num_leaves = ensemble->NumLeaves();
73 return (a/2.0) + (num_leaves/2.0);
75 double PosteriorScale(
TreeEnsemble* ensemble,
double a,
double b) {
76 double mu_sq = ensemble->SumLeafSquared();
77 return (b/2.0) + (mu_sq/2.0);
79 double SampleVarianceParameter(
TreeEnsemble* ensemble,
double a,
double b, std::mt19937& gen) {
80 double ig_shape = PosteriorShape(ensemble, a, b);
81 double ig_scale = PosteriorScale(ensemble, a, b);
82 return ig_sampler_.Sample(ig_shape, ig_scale, gen);