2#include <cgv/math/functions.h>
3#include <cgv/math/vec.h>
10T norm_pdf(
const T x,
const T mu=0,
const T sig=1)
12 return (T) (0.398942280401432678/sig)*exp(-0.5*sqr((x-mu)/sig));
17T norm_cdf(
const T x,
const T mu=0,
const T sig=1)
19 return T(0.5)*erfc(T(-0.707106781186547524*(x-mu)/sig));
24T norm_inv(
const T& p,
const T mu=0,
const T sig=1)
26 assert(p >= 0 && p <= 1);
27 return -1.41421356237309505*sig*erfc_inv(2.*p)+mu;
32T logn_pdf(
const T x,
const T mu=0,
const T sig=1)
35 if (x == 0.)
return 0.;
36 return (0.398942280401432678/(sig*x))*exp(-0.5*sqr((log(x)-mu)/sig));
41T logn_cdf(
const T x,
const T mu=0,
const T sig=1)
44 if (x == 0.)
return 0.;
45 return (T)(0.5*erfc(-0.707106781186547524*(log(x)-mu)/sig));
50T logn_inv(
const T p,
const T mu=0,
const T sig=1)
52 assert (p > 0. && p < 1.);
53 return exp(-1.41421356237309505*sig*erfc_inv(2.*p)+mu);
59T uniform_pdf(
const T x,
const T a,
const T b)
70T uniform_cdf(
const T x,
const T a,
const T b)
84 if (z == 0.)
return 0.;
87 double y = exp(-1.23370055013616983/sqr(z));
88 return (T)(2.25675833419102515*sqrt(-log(y))
89 *(y + pow(y,9) + pow(y,25) + pow(y,49)));
92 double x = exp(-2.*sqr(z));
93 return (T)(1. - 2.*(x - pow(x,4) + pow(x,9)));
102 if (z == 0.)
return 1.;
103 if (z < 1.18)
return 1.-ks_cdf(z);
104 double x = exp(-2.*sqr(z));
105 return (T)(2.*(x - pow(x,4) + pow(x,9)));
113 const double ooe = 0.367879441171442322;
115 assert(y < 0. && y > -ooe);
116 if (y < -0.2) u = log(ooe-sqrt(2*ooe*(y+ooe)));
119 u += (t=(log(y/u)-u)*(u/(1.+u)));
120 if (t < 1.e-8 && abs(t+to)<0.01*abs(t))
break;
122 }
while (abs(t/u) > 1.e-15);
130 double y,logy,yp,x,xp,f,ff,u,t;
131 assert (q > 0. && q <= 1.)
132 if (q == 1.) return 0.;
134 f = -0.392699081698724155*SQR(1.-q);
139 ff = f/sqr(1.+ pow(y,4)+ pow(y,12));
140 u = (y*logy-ff)/(1.+logy);
141 y = y - (t=u/std::max(0.5,1.-0.5*u/(y*(1.+logy))));
142 }
while (abs(t/y)>1.e-15);
143 return (T)(1.57079632679489662/sqrt(-log(y)));
148 x = 0.5*q+pow(x,4)-pow(x,9);
149 if (x > 0.06) x += pow(x,16)-pow(x,25);
150 }
while (abs((xp-x)/x)>1.e-15);
151 return (T)sqrt(-0.5*log(x));
159 return ksc_inv(1.-p);
167T norm_ks_test(
const T mu,
const T sig,vec<T> data)
170 unsigned n = data.size();
174 for(
unsigned i = 0; i < n; i++)
177 ff = norm_cdf(data(i),mu,sig);
178 dt = std::max(std::abs(fo-ff),std::abs(fn-ff));
184 return ksc_cdf((en+0.12+0.11/en)*d);
190T ks_test(vec<T> data1, vec<T> data2)
192 int j1=0,j2=0,n1=data1.size(),n2=data2.size();
193 T d1,d2,dt,en1,en2,en,fn1=0.0,fn2=0.0;
200 while (j1 < n1 && j2 < n2) {
201 if ((d1=data1(j1)) <= (d2=data2(j2)))
204 while (j1 < n1 && d1 == data1(j1));
208 while (j2 < n2 && d2 == data2(j2));
209 if ((dt=abs(fn2-fn1)) > d) d=dt;
211 en=sqrt(en1*en2/(en1+en2));
212 return ksc_cdf((en+0.12+0.11/en)*d);