cgv
Loading...
Searching...
No Matches
statistics.h
1#pragma once
2#include <cgv/math/functions.h>
3#include <cgv/math/vec.h>
4
5namespace cgv {
6 namespace math {
7
9template <typename T>
10T norm_pdf(const T x,const T mu=0,const T sig=1)
11{
12 return (T) (0.398942280401432678/sig)*exp(-0.5*sqr((x-mu)/sig));
13}
14
16template <typename T>
17T norm_cdf(const T x,const T mu=0,const T sig=1)
18{
19 return T(0.5)*erfc(T(-0.707106781186547524*(x-mu)/sig));
20}
21
23template <typename T>
24T norm_inv(const T& p,const T mu=0,const T sig=1)
25{
26 assert(p >= 0 && p <= 1);
27 return -1.41421356237309505*sig*erfc_inv(2.*p)+mu;
28}
29
31template <typename T>
32T logn_pdf(const T x,const T mu=0,const T sig=1)
33{
34 assert (x >= 0.);
35 if (x == 0.) return 0.;
36 return (0.398942280401432678/(sig*x))*exp(-0.5*sqr((log(x)-mu)/sig));
37}
38
40template <typename T>
41T logn_cdf(const T x, const T mu=0,const T sig=1)
42{
43 assert(x >= 0.);
44 if (x == 0.) return 0.;
45 return (T)(0.5*erfc(-0.707106781186547524*(log(x)-mu)/sig));
46}
47
49template <typename T>
50T logn_inv(const T p, const T mu=0,const T sig=1)
51{
52 assert (p > 0. && p < 1.);
53 return exp(-1.41421356237309505*sig*erfc_inv(2.*p)+mu);
54}
55
56
58template <typename T>
59T uniform_pdf(const T x,const T a,const T b)
60{
61 if(x < a)
62 return 0;
63 if(x > b)
64 return 0;
65 return 1.0/(b-a);
66}
67
69template <typename T>
70T uniform_cdf(const T x,const T a,const T b)
71{
72 if(x < a)
73 return 0;
74 if(x >=b)
75 return 1;
76 return (x-a)/(b-a);
77}
78
79//cumulative kolmogorov-smirnov distribution function
80template <typename T>
81T ks_cdf(const T z)
82{
83 assert(z >= 0.);
84 if (z == 0.) return 0.;
85 if (z < 1.18)
86 {
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)));
90 } else
91 {
92 double x = exp(-2.*sqr(z));
93 return (T)(1. - 2.*(x - pow(x,4) + pow(x,9)));
94 }
95}
96
98template <typename T>
99T ksc_cdf(const T z)
100{
101 assert (z >= 0.) ;
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)));
106}
107
108
110template <typename T>
111T invxlogx(const T y)
112{
113 const double ooe = 0.367879441171442322;
114 double t,u,to=0.;
115 assert(y < 0. && y > -ooe);
116 if (y < -0.2) u = log(ooe-sqrt(2*ooe*(y+ooe)));
117 else u = -10.;
118 do {
119 u += (t=(log(y/u)-u)*(u/(1.+u)));
120 if (t < 1.e-8 && abs(t+to)<0.01*abs(t)) break;
121 to = t;
122 } while (abs(t/u) > 1.e-15);
123 return(T) exp(u);
124}
125
127template <typename T>
128T ksc_inv(const T q)
129{
130 double y,logy,yp,x,xp,f,ff,u,t;
131 assert (q > 0. && q <= 1.)
132 if (q == 1.) return 0.;
133 if (q > 0.3) {
134 f = -0.392699081698724155*SQR(1.-q);
135 y = invxlogx(f);
136 do {
137 yp = y;
138 logy = log(y);
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)));
144 } else {
145 x = 0.03;
146 do {
147 xp = x;
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));
152 }
153}
154
156template <typename T>
157T ks_inv(const T p)
158{
159 return ksc_inv(1.-p);
160}
161
162
163
166template <typename T>
167T norm_ks_test(const T mu,const T sig,vec<T> data)
168{
169 sort_values(data);
170 unsigned n = data.size();
171 T fo=0;
172 T dt,ff,fn,en,d=0.0;
173 en=(T)n;
174 for(unsigned i = 0; i < n; i++)
175 {
176 fn = (T)(i+1.0)/en;
177 ff = norm_cdf(data(i),mu,sig);
178 dt = std::max(std::abs(fo-ff),std::abs(fn-ff));
179 if(dt > d)
180 d=dt;
181 fo=fn;
182 }
183 en=sqrt(en);
184 return ksc_cdf((en+0.12+0.11/en)*d);
185}
186
189template <typename T>
190T ks_test(vec<T> data1, vec<T> data2)
191{
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;
194 KSdist ks;
195 sort_values(data1);
196 sort_values(data2);
197 en1=n1;
198 en2=n2;
199 T d=0.0;
200 while (j1 < n1 && j2 < n2) {
201 if ((d1=data1(j1)) <= (d2=data2(j2)))
202 do
203 fn1=++j1/en1;
204 while (j1 < n1 && d1 == data1(j1));
205 if (d2 <= d1)
206 do
207 fn2=++j2/en2;
208 while (j2 < n2 && d2 == data2(j2));
209 if ((dt=abs(fn2-fn1)) > d) d=dt;
210 }
211 en=sqrt(en1*en2/(en1+en2));
212 return ksc_cdf((en+0.12+0.11/en)*d);
213}
214
215
216 }
217}
the cgv namespace
Definition print.h:11