double InvGammaLogPdf::f(double alpha, double beta, const double* xs, size_t n) { double part = 0.0; for (size_t i = 0; i < n; ++i) { part += (alpha + 1) * fastlog(xs[i]) + beta / xs[i]; } return n * (alpha * fastlog(beta) - lgamma(alpha)) - part; }
double InvGammaLogPdf::df_dalpha(double alpha, double beta, const double* xs, size_t n) { double part = 0.0; for (size_t i = 0; i < n; ++i) { part += fastlog(xs[i]); } return n * (fastlog(beta) - boost::math::digamma(alpha)) - part; }
double AltGammaLogPdf::df_dshape(double mean, double shape, const double* xs, size_t n) { double scale = mean / shape; double part = 0.0; for (size_t i = 0; i < n; ++i) { part += fastlog(xs[i]) - xs[i] / mean; } return (double) n * (-boost::math::digamma(shape) + fastlog(scale) * (mean/sq(shape))) + part; }
float AltGammaLogPdf::df_dshape(float mean, float shape, const float* xs, size_t n) { float scale = mean / shape; float part = 0.0; for (size_t i = 0; i < n; ++i) { part += fastlog(xs[i]) - xs[i] / mean; } return (float) n * (-boost::math::digamma(shape) + fastlog(scale) * (mean/sq(shape))) + part; }
double LogNormalLogPdf::f(double mu, double sigma, const double* xs, size_t n) { double part1 = n * (NEG_LOG_2_PI_DIV_2 - fastlog(sigma)); double part2 = 0.0; for (size_t i = 0; i < n; ++i) { double logx = fastlog(xs[i]); part2 += sq(logx - mu) / (2 * sq(sigma)) + logx; } return part1 - part2; }
double AltGammaLogPdf::f(double mean, double shape, const double* xs, size_t n) { double scale = mean / shape; double part = 0.0; for (size_t i = 0; i < n; ++i) { part += (shape - 1.0) * fastlog(xs[i]) - xs[i] / scale; } double ans = -(double) n * (lgamma(shape) + shape * fastlog(scale)) + part; assert_finite(ans); return ans; }
float AltGammaLogPdf::f(float mean, float shape, const float* xs, size_t n) { float scale = mean / shape; float part = 0.0; for (size_t i = 0; i < n; ++i) { part += (shape - 1.0) * fastlog(xs[i]) - xs[i] / scale; } float ans = -(float) n * (lgammaf(shape) + shape * fastlog(scale)) + part; assert_finite(ans); return ans; }
float GammaLogPdf::f(float alpha, float beta, const float* xs, size_t n) { float part1 = 0.0, part2 = 0.0; for (size_t i = 0; i < n; ++i) { part1 += fastlog(xs[i]); part2 += xs[i]; } return n * (alpha * fastlog(beta) - lgammaf(alpha)) + (alpha - 1) * part1 - beta * part2; }
double GammaLogPdf::f(double alpha, double beta, const double* xs, size_t n) { double part1 = 0.0, part2 = 0.0; for (size_t i = 0; i < n; ++i) { part1 += fastlog(xs[i]); part2 += xs[i]; } return n * (alpha * fastlog(beta) - lgamma(alpha)) + (alpha - 1) * part1 - beta * part2; }
double Shredder::sample(rng_t& rng, double x0) { double d0; double lp0 = f(x0, d0); assert_finite(lp0); double slice_height = fastlog( std::max<double>(constants::zero_eps, random_uniform_01(rng))) + lp0; assert_finite(slice_height); x_min = find_slice_edge(x0, slice_height, lp0, d0, -1); x_max = find_slice_edge(x0, slice_height, lp0, d0, 1); double x = (x_max + x_min) / 2; while (x_max - x_min > tolerance) { x = x_min + (x_max - x_min) * random_uniform_01(rng); double d; double lp = f(x, d); if (lp >= slice_height) break; else if (x > x0) x_max = x; else x_min = x; } return x; }
double LogNormalLogPdf::df_dmu(double mu, double sigma, const double* xs, size_t n) { double part = 0.0; for (size_t i = 0; i < n; ++i) { part += fastlog(xs[i]) - mu; } return part / sq(sigma); }
// thinplate spline kernel \phi(r) static inline v4sf kerneldist4(const float *x, const float *y) { const float r = sqrtf( (x[0]-y[0])*(x[0]-y[0])+ (x[1]-y[1])*(x[1]-y[1])+ (x[2]-y[2])*(x[2]-y[2])); return r*r*fastlog(MAX(1e-8f,r)); }
double LogNormalLogPdf::df_dsigma(double mu, double sigma, const double* xs, size_t n) { double part = 0.0; for (size_t i = 0; i < n; ++i) { part += sq(fastlog(xs[i]) - mu); } return part / cb(sigma) - n/sigma; }
float NormalLogPdf::f(float mu, float sigma, const float* xs, size_t n) { float part1 = n * (NEG_LOG_2_PI_DIV_2 - fastlog(sigma)); float part2 = 0.0; for (size_t i = 0; i < n; ++i) { part2 += sq(xs[i] - mu) / (2 * sq(sigma)); } return part1 - part2; }
static inline float kernel(const float *x, const float *y) { // return r*r*logf(MAX(1e-8f,r)); // well damnit, this speedup thing unfortunately shows severe artifacts. // return r*r*fasterlog(MAX(1e-8f,r)); // this one seems to be a lot better, let's see how it goes: const float r2 = (x[0]-y[0])*(x[0]-y[0])+ (x[1]-y[1])*(x[1]-y[1])+ (x[2]-y[2])*(x[2]-y[2]); return .5f * r2*fastlog(MAX(1e-8f,r2)); }
float StudentsTLogPdf::f(float nu, float mu, float sigma, const float* xs, size_t n) { float part1 = n * (lgammaf((nu + 1) / 2) - lgamma(nu / 2) - fastlog(sqrt(nu * M_PI) * sigma)); float part2 = 0.0; for (size_t i = 0; i < n; ++i) { part2 += log1p(sq((xs[i] - mu) / sigma) / nu); } return part1 - ((nu + 1) / 2) * part2; }
double StudentsTLogPdf::f(double nu, double mu, double sigma, const double* xs, size_t n) { double part1 = n * (lgamma((nu + 1) / 2) - lgamma(nu / 2) - fastlog(sqrt(nu * M_PI) * sigma)); double part2 = 0.0; for (size_t i = 0; i < n; ++i) { part2 += log1p(sq((xs[i] - mu) / sigma) / nu); } return part1 - ((nu + 1) / 2) * part2; }
double DirichletLogPdf::df_dalpha(double alpha, const boost::numeric::ublas::matrix<double>* mean, const boost::numeric::ublas::matrix<double>* data, size_t n, size_t m) { double part = 0.0; for (size_t i = 0; i < n; ++i) { for (size_t j = 0; j < m; ++j) { part += (*mean)(i, j) * (fastlog((*data)(i, j)) - boost::math::digamma(alpha * (*mean)(i, j))); } } return n * boost::math::digamma(alpha) + part; }
double DirichletLogPdf::f(double alpha, const boost::numeric::ublas::matrix<double>* mean, const boost::numeric::ublas::matrix<double>* data, size_t n, size_t m) { double part = 0.0; for (size_t i = 0; i < n; ++i) { for (size_t j = 0; j < m; ++j) { double am = alpha * (*mean)(i, j); part += (am - 1) * fastlog((*data)(i, j)) - lgamma(am); } } return n * lgamma(alpha) + part; }
double LogisticNormalLogPdf::df_dx(double mu, double sigma, double x) { double y = fastlog(x / (1 - x)); return (1/(1-x)) - (1/x) - (mu - y) / (sq(sigma) * (x - 1) * x); }
double LogisticNormalLogPdf::f(double mu, double sigma, double x) { return -fastlog(sigma) - fastlog(sqrt(2*M_PI)) - sq(fastlog(x / (1 - x)) - mu) / (2 * sq(sigma)) - fastlog(x) - fastlog(1-x); }
double BetaLogPdf::df_dgamma(double gamma, double c, double x) { return c * (fastlog(x / (1 - x)) - boost::math::digamma(gamma * c) + boost::math::digamma((1 - gamma) * c)); }
double BetaLogPdf::f(double alpha, double beta, double x) { return (alpha - 1) * fastlog(x) + (beta - 1) * fastlog(1 - x) - lbeta(alpha, beta); }
float LogSum(float log_a, float log_b) { return (log_a < log_b) ? log_b + fastlog(1 + fastexp(log_a - log_b)) : log_a + fastlog(1 + fastexp(log_b-log_a)); }
double PoissonLogPdf::f(float lambda, unsigned int k) { return k * fastlog(lambda) - lgammaf(k + 1) - lambda; }
float AltGammaLogPdf::df_dshape(float mean, float shape, float x) { float scale = mean / shape; float part = fastlog(x) - x / mean; return (-boost::math::digamma(shape) + fastlog(scale) * (mean/sq(shape))) + part; }
double NormalLogPdf::f(double mu, double sigma, const double x) { double part1 = NEG_LOG_2_PI_DIV_2 - fastlog(sigma); double part2 = sq(x - mu) / (2 * sq(sigma)); return part1 - part2; }
double LogNormalLogPdf::df_dx(double mu, double sigma, double x) { return (mu - fastlog(x)) / (x * sq(sigma)) - 1.0 / x; }
float AltGammaLogPdf::f(float mean, float shape, float x) { float scale = mean / shape; float part = (shape - 1.0) * fastlog(x) - x / scale; return -(lgammaf(shape) + shape * fastlog(scale)) + part; }
double AltGammaLogPdf::f(double mean, double shape, double x) { double scale = mean / shape; double part = (shape - 1.0) * fastlog(x) - x / scale; return -(lgamma(shape) + shape * fastlog(scale)) + part; }