18 const double LOG2 = log(2.0);
21 constexpr
double NaN = std::numeric_limits<double>::quiet_NaN();
22 constexpr
double pi = M_PI;
31 template<
typename T,
int N=1>
37 operator T()
const {
return value; }
50 if(std::isnan(x))
return false;
51 if(std::isnan(y))
return true;
115 assert(f(lower) > precision or f(upper) > precision);
117 const T m = lower + (upper-lower)/2.0;
118 if (upper-lower < 1e-3) {
121 else if (f(m) < precision) {
122 return get_log1pexp_breakout_bound<T>(precision, f, m, upper);
125 return get_log1pexp_breakout_bound<T>(precision, f, lower, m);
139 T mx = std::max(a,b);
145 static const float breakout = get_log1pexp_breakout_bound<T>(1e-6, +[](T x) -> T {
return log1p(exp(x)); });
147 T z = std::min(a,b)-mx;
152 return mx + log1p(exp(z));
164 T mx = std::max(a,b);
165 return mx + log1p(exp(std::min(a,b)-mx));
185 double logsumexp(
const std::vector<t>& v,
double f(
const t&) ) {
198 template<
typename T>
int sgn(T val) {
199 return (T(0) < val) - (val < T(0));
208 return sgn(val)*log(std::abs(val));
219 auto ret = lgamma_r(v, &sgn);
226 auto ret = lgamma_r(v, &sgn);
240 return 1.0 / (1.0 + exp(-z));
245 return log(p)-log(1-p);
253 bool approx_eq(
const T x,
const T y,
double prec=0.00001) {
254 return abs(x-y)<prec;
267 std::sort(v.begin(), v.end());
274 for(
auto [x,lp] : v) {
276 if(exp(cumulative-z) >= q) {
295 T sum(std::vector<T>& v){
297 for(
auto& x : v) s += x;
307 T mean(std::vector<T>& v){
308 return sum(v)/v.size();
318 T sd(std::vector<T>& v) {
319 assert(v.size() > 1);
325 return sqrt(s / (v.size()-1));
335 T MAD(std::vector<T>& v) {
340 for(
size_t i=0;i<n;i++) {
341 d[i] = std::abs(v.at(i) - m);
354 T quantile(std::vector<T>& v,
double q) {
355 const size_t n = v.size();
370 T median(std::vector<T>& v) {
371 const size_t n = v.size();
372 if(n == 0)
return NaN;
373 if(n == 1)
return v.at(0);
377 return (v[n/2] + v[n/2-1]) / 2;
394 T trimmed_mean(std::vector<T>& v,
float a,
float b) {
395 const size_t n = v.size();
396 if(n == 0)
return NaN;
397 if(n == 1)
return v.at(0);
400 assert(a >= 0.0 and b <= 1.0 and a<b &&
"*** Bad range in trimmed_mean");
404 for(
int i=a*n;i<b*n;i++) {
418 T trimmed_mean(std::vector<T>& v,
float pct) {
419 return trimmed_mean(v,pct,pct);
429 T mymax(
const std::vector<T>& v) {
430 const size_t n = v.size();
431 if(n == 0)
return NaN;
432 if(n == 1)
return v.at(0);
435 for(
size_t i=0;i<n;i++)
436 m = std::max(v.at(i), m);
447 template<
typename T,
typename K>
448 K mymax(std::map<T,K>& v) {
449 auto m = v.begin()->second;
450 for(
const auto& x : v) {
451 m = std::max(x.second, m);
458 std::strong_ordering fp_ordering(T& x, T& y) {
466 if( std::isnan(x) and std::isnan(y)) {
467 return std::strong_ordering::equivalent;
469 else if( std::isnan(x) ) {
470 return std::strong_ordering::less;
472 else if (std::isnan(y)) {
473 return std::strong_ordering::greater;
478 if (v == std::partial_ordering::less)
return std::strong_ordering::less;
479 else if(v == std::partial_ordering::greater)
return std::strong_ordering::greater;
480 else if(v == std::partial_ordering::equivalent)
return std::strong_ordering::equivalent;
InheritedIntegral()
Definition: Numerics.h:35
constexpr double pi
Definition: Numerics.h:22
const double ROOT2
Definition: Numerics.h:19
double mylgamma(double v)
Definition: Numerics.h:216
This allows sorting of things that contain NaN.
Definition: Numerics.h:48
T inv_logit(const T z)
logic / inverse logit are pretty useful
Definition: Numerics.h:239
T logplusexp_full(const T a, const T b)
logsumexp with no shortcuts for precision
Definition: Numerics.h:163
Definition: Numerics.h:32
constexpr double infinity
Definition: Numerics.h:20
bool operator()(const T &x, const T &y) const
Definition: Numerics.h:49
T logplusexp(const T a, const T b)
Definition: Numerics.h:131
double logsumexp(const t &v)
Compute log(sum(exp(v)). For now, this just unrolls logplusexp, but we might consider the faster (sta...
Definition: Numerics.h:176
const double LOG2
Definition: Numerics.h:18
constexpr double tau
Definition: Numerics.h:23
double lfactorial(double x)
Definition: Numerics.h:230
T get_log1pexp_breakout_bound(const double precision, T f(T), const T lower=-1e6, const T upper=1e6)
Tis takes a function f(x) and finds a bound B so that f(x) < precision for all x < B...
Definition: Numerics.h:113
T sgnlog(T val)
The log of |val| but with sign preserved.
Definition: Numerics.h:207
int sgn(T val)
Sign function.
Definition: Numerics.h:198
constexpr double NaN
Definition: Numerics.h:21
T value
Definition: Numerics.h:33
T round(T v, int n)
Definition: Numerics.h:62
T logit(const T p)
Definition: Numerics.h:244
T weighted_quantile(std::vector< std::pair< T, double >> &v, double q)
Definition: Numerics.h:263
double mygamma(double v)
Definition: Numerics.h:223
InheritedIntegral(T v)
Definition: Numerics.h:34