Modular C
+ Collaboration diagram for C◼mod◼stats:

Modules

 Specific macros to deal with the structure of module C◼mod◼stats.
 

Files

file  C-mod-stats.c
 Collect some simple statistics online as we go.
 

Data Structures

struct  C◼mod◼stats
 A simple data structure to collect the 0th to 3rd moment of a statistic. More...
 

Functions

double C◼mod◼stats◼samples (C◼mod◼stats *c)
 Return the number of samples that had been entered into the statistic c. More...
 
double C◼mod◼stats◼mean (C◼mod◼stats *c)
 Return the mean value of the samples that had been entered into the statistic c. More...
 
double C◼mod◼stats◼var (C◼mod◼stats *c)
 Return the variance of the samples that had been entered into the statistic c. More...
 
double C◼mod◼stats◼sdev (C◼mod◼stats *c)
 Return the standard deviation of the samples that had been entered into the statistic c. More...
 
double C◼mod◼stats◼rsdev (C◼mod◼stats *c)
 Return the relative standard deviation of the samples that had been entered into the statistic c. More...
 
double C◼mod◼stats◼skew (C◼mod◼stats *c)
 Return the normalized skew of the samples that had been entered into the statistic c. More...
 
double C◼mod◼stats◼var—unbiased (C◼mod◼stats *c)
 Return the unbiased variance of the samples that had been entered into the statistic c. More...
 
double C◼mod◼stats◼sdev—unbiased (C◼mod◼stats *c)
 Return the unbiased standard deviation of the samples that had been entered into the statistic c. More...
 
double C◼mod◼stats◼rsdev—unbiased (C◼mod◼stats *c)
 Return the unbiased relative standard deviation of the samples that had been entered into the statistic c. More...
 
void C◼mod◼stats◼collect (C◼mod◼stats *c, double val, unsigned moments)
 Add value val to the statistic c. More...
 
void C◼mod◼stats◼collect0 (C◼mod◼stats *c, double val)
 
void C◼mod◼stats◼collect1 (C◼mod◼stats *c, double val)
 
void C◼mod◼stats◼collect2 (C◼mod◼stats *c, double val)
 
void C◼mod◼stats◼collect3 (C◼mod◼stats *c, double val)
 
C◼mod◼statsC◼mod◼stats◼insert (C◼mod◼stats *s)
 
void C◼mod◼stats◼print (C◼io *out)
 
void C◼mod◼stats◼cleanup (void)
 A function called at exit. More...
 
C◼mod◼statsC◼mod◼stats◼init (C◼mod◼stats *s, char const *desc)
 

Variables

static C◼mod◼stats *_Atomic list = C◼atomic◼INIT(0)
 

Detailed Description


Data Structure Documentation

§ C◼mod◼stats

struct C◼mod◼stats

A simple data structure to collect the 0th to 3rd moment of a statistic.

Warning
Since this also uses a double for the number of samples, the validity of all this is restricted to about $2^{50}$ samples.

Definition at line 124 of file C-mod-stats.c.

+ Collaboration diagram for C◼mod◼stats:

Data Fields

double moment [4]
 
C◼mod◼statsnext
 
char const * description
 

Field Documentation

§ description

char const* description

Definition at line 128 of file C-mod-stats.c.

Referenced by C◼mod◼stats◼init().

§ moment

double moment[4]

Definition at line 126 of file C-mod-stats.c.

§ next

Definition at line 127 of file C-mod-stats.c.

Function Documentation

§ C◼mod◼stats◼cleanup()

void C◼mod◼stats◼cleanup ( void  )

A function called at exit.

Definition at line 357 of file C-mod-stats.c.

References C◼atomic◼exchange, C◼lib◼free(), and list.

357  {
358 #line 233 "/home/gustedt/build/cmod/C/C-mod-stats.X"
359  C◼mod◼stats* p;
360 #ifdef C◼atomics◼NO_ATOMICS
361  p = list;
362  list = 0;
363 #else
364  p = C◼atomic◼exchange(&list, 0);
365 #endif
366  list = 0;
367  while (p) {
368 #line 242 "/home/gustedt/build/cmod/C/C-mod-stats.X"
369  C◼mod◼stats* prev = p→next;
370  C◼lib◼free(p);
371  p = prev;
372  }
373 }
A simple data structure to collect the 0th to 3rd moment of a statistic.
Definition: C-mod-stats.c:124
void C◼lib◼free(void *)
#define C◼atomic◼exchange(PTR, VAL)
Definition: C-atomic.c:68
static C◼mod◼stats *_Atomic list
Definition: C-mod-stats.c:300
+ Here is the call graph for this function:

§ C◼mod◼stats◼collect()

void C◼mod◼stats◼collect ( C◼mod◼stats c,
double  val,
unsigned  moments 
)
inline

Add value val to the statistic c.

Definition at line 233 of file C-mod-stats.c.

References C◼mod◼stats◼mean(), and C◼mod◼stats◼samples().

Referenced by C◼mod◼stats◼collect0(), C◼mod◼stats◼collect1(), C◼mod◼stats◼collect2(), and C◼mod◼stats◼collect3().

233  {
234 #line 130 "/home/gustedt/build/cmod/C/C-mod-stats.X"
235  double n = C◼mod◼stats◼samples(c);
236  double n0 = n-1;
237  double n1 = n+1;
238  double delta0 = 1;
239  double delta = val - C◼mod◼stats◼mean(c);
240  double delta1 = delta/n1;
241  double delta2 = delta1*delta*n;
242  switch (moments) {
243 #line 138 "/home/gustedt/build/cmod/C/C-mod-stats.X"
244  default:
245  c→moment[3] += (delta2*n0 - 3*c→moment[2])*delta1;
246  case 2:
247  c→moment[2] += delta2;
248  case 1:
249  c→moment[1] += delta1;
250  case 0:
251  c→moment[0] += delta0;
252  }
253 }
double C◼mod◼stats◼samples(C◼mod◼stats *c)
Return the number of samples that had been entered into the statistic c.
Definition: C-mod-stats.c:137
double C◼mod◼stats◼mean(C◼mod◼stats *c)
Return the mean value of the samples that had been entered into the statistic c.
Definition: C-mod-stats.c:147
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

§ C◼mod◼stats◼collect0()

void C◼mod◼stats◼collect0 ( C◼mod◼stats c,
double  val 
)
inline

Definition at line 256 of file C-mod-stats.c.

References C◼mod◼stats◼collect().

256  {
257 #line 151 "/home/gustedt/build/cmod/C/C-mod-stats.X"
258  C◼mod◼stats◼collect(c, val, 0);
259 }
void C◼mod◼stats◼collect(C◼mod◼stats *c, double val, unsigned moments)
Add value val to the statistic c.
Definition: C-mod-stats.c:233
+ Here is the call graph for this function:

§ C◼mod◼stats◼collect1()

void C◼mod◼stats◼collect1 ( C◼mod◼stats c,
double  val 
)
inline

Definition at line 262 of file C-mod-stats.c.

References C◼mod◼stats◼collect().

262  {
263 #line 156 "/home/gustedt/build/cmod/C/C-mod-stats.X"
264  C◼mod◼stats◼collect(c, val, 1);
265 }
void C◼mod◼stats◼collect(C◼mod◼stats *c, double val, unsigned moments)
Add value val to the statistic c.
Definition: C-mod-stats.c:233
+ Here is the call graph for this function:

§ C◼mod◼stats◼collect2()

void C◼mod◼stats◼collect2 ( C◼mod◼stats c,
double  val 
)
inline

Definition at line 268 of file C-mod-stats.c.

References C◼mod◼stats◼collect().

268  {
269 #line 161 "/home/gustedt/build/cmod/C/C-mod-stats.X"
270  C◼mod◼stats◼collect(c, val, 2);
271 }
void C◼mod◼stats◼collect(C◼mod◼stats *c, double val, unsigned moments)
Add value val to the statistic c.
Definition: C-mod-stats.c:233
+ Here is the call graph for this function:

§ C◼mod◼stats◼collect3()

void C◼mod◼stats◼collect3 ( C◼mod◼stats c,
double  val 
)
inline

Definition at line 274 of file C-mod-stats.c.

References C◼mod◼stats◼collect(), C◼mod◼stats◼insert(), and list.

Referenced by C◼mod◼timer◼sample().

274  {
275 #line 166 "/home/gustedt/build/cmod/C/C-mod-stats.X"
276  C◼mod◼stats◼collect(c, val, 3);
277 }
void C◼mod◼stats◼collect(C◼mod◼stats *c, double val, unsigned moments)
Add value val to the statistic c.
Definition: C-mod-stats.c:233
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

§ C◼mod◼stats◼init()

C◼mod◼stats* C◼mod◼stats◼init ( C◼mod◼stats s,
char const *  desc 
)

Definition at line 375 of file C-mod-stats.c.

References C◼MODULE_ABI, C◼MODULE_FEATURES, C◼mod◼stats◼insert(), and description.

Referenced by C◼mod◼timer◼startup().

375  {
376 #line 249 "/home/gustedt/build/cmod/C/C-mod-stats.X"
377  if (s) {
378 #line 250 "/home/gustedt/build/cmod/C/C-mod-stats.X"
379  *s = (C◼mod◼stats){ .description = desc, };
381  }
382  return s;
383 }
char const * description
Definition: C-mod-stats.c:128
C◼mod◼stats * C◼mod◼stats◼insert(C◼mod◼stats *s)
Definition: C-mod-stats.c:302
A simple data structure to collect the 0th to 3rd moment of a statistic.
Definition: C-mod-stats.c:124
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

§ C◼mod◼stats◼insert()

C◼mod◼stats* C◼mod◼stats◼insert ( C◼mod◼stats s)

Definition at line 302 of file C-mod-stats.c.

Referenced by C◼mod◼stats◼collect3(), and C◼mod◼stats◼init().

302  {
303 #line 188 "/home/gustedt/build/cmod/C/C-mod-stats.X"
304  if (s ∧ ¬s→next) {
305 #line 189 "/home/gustedt/build/cmod/C/C-mod-stats.X"
306  while (¬C◼atomic◼compare_exchange_weak(&list, &s→next, s)) {
307 #line 190 "/home/gustedt/build/cmod/C/C-mod-stats.X"
308  // empty
309  }
310  }
311  return s;
312 }
static C◼mod◼stats *_Atomic list
Definition: C-mod-stats.c:300
+ Here is the caller graph for this function:

§ C◼mod◼stats◼mean()

double C◼mod◼stats◼mean ( C◼mod◼stats c)
inline

Return the mean value of the samples that had been entered into the statistic c.

Definition at line 147 of file C-mod-stats.c.

Referenced by C◼mod◼stats◼collect(), C◼mod◼stats◼print(), and C◼mod◼stats◼rsdev().

147  {
148 #line 52 "/home/gustedt/build/cmod/C/C-mod-stats.X"
149  return c→moment[1];
150 }
+ Here is the caller graph for this function:

§ C◼mod◼stats◼print()

void C◼mod◼stats◼print ( C◼io out)

Definition at line 316 of file C-mod-stats.c.

References C◼io◼printf(), C◼lib◼free(), C◼lib◼malloc(), C◼lib◼mbstowcs(), C◼lib◼realloc(), C◼math◼max, C◼mod◼demangle, C◼mod◼stats◼mean(), C◼mod◼stats◼rsdev(), C◼mod◼stats◼samples(), C◼mod◼stats◼skew(), and C◼str◼len().

Referenced by C◼mod◼timer◼test().

316  {
317 #line 199 "/home/gustedt/build/cmod/C/C-mod-stats.X"
318  int mlen = C◼str◼len("description");
319  C◼size number = 0;
320  C◼size maxn = 10;
321  C◼mod◼stats** tab = C◼lib◼malloc(sizeof(C◼mod◼stats*[maxn]));
322  for (C◼mod◼stats* p = list; p; p = p→next) {
323 #line 204 "/home/gustedt/build/cmod/C/C-mod-stats.X"
324  if (C◼mod◼stats◼samples(p)) {
325 #line 205 "/home/gustedt/build/cmod/C/C-mod-stats.X"
326  char const* pretty = C◼mod◼demangle(p→description);
327  C◼size len = C◼lib◼mbstowcs(0, pretty, 0);
328  mlen = C◼math◼max(mlen, len);
329  //C¯io¯fprintf(C¯io¯err, "%zu (%d) %s\n", len, mlen, pretty);
330  tab[number] = p;
331  number++;
332  if (number > maxn) {
333 #line 212 "/home/gustedt/build/cmod/C/C-mod-stats.X"
334  maxn *= 2;
335  tab = C◼lib◼realloc(tab, sizeof(C◼mod◼stats*[maxn]));
336  }
337  }
338  }
339  C◼io◼printf(out, "#%*s %12s%12s%12s%12s\n",
340  mlen-1, "description", "samples", "mean",
341  "rsdev", "skew");
342  for (C◼size i = number-1; i < number; --i) {
343 #line 221 "/home/gustedt/build/cmod/C/C-mod-stats.X"
344  char const* pretty = C◼mod◼demangle(tab[i]→description);
345  int len = C◼lib◼mbstowcs(0, pretty, 0);
346  C◼io◼printf(out, "%*s%s %12g%12g%12.4e%12.4e\n",
347  mlen-len, "", pretty, C◼mod◼stats◼samples(tab[i]), C◼mod◼stats◼mean(tab[i]),
349  }
350  C◼lib◼free(tab);
351 }
#define C◼mod◼demangle(...)
Demangle a mangled composed identifier into its components.
C◼lib◼size C◼lib◼mbstowcs(C◼lib◼wchar *restrict, char const *restrict, C◼lib◼size)
double C◼mod◼stats◼skew(C◼mod◼stats *c)
Return the normalized skew of the samples that had been entered into the statistic c...
Definition: C-mod-stats.c:187
double C◼mod◼stats◼samples(C◼mod◼stats *c)
Return the number of samples that had been entered into the statistic c.
Definition: C-mod-stats.c:137
void * C◼lib◼realloc(void *, C◼lib◼size)
int() C◼io◼printf(const char *restrict,...)
void * C◼lib◼malloc(C◼lib◼size)
A simple data structure to collect the 0th to 3rd moment of a statistic.
Definition: C-mod-stats.c:124
void C◼lib◼free(void *)
double C◼mod◼stats◼rsdev(C◼mod◼stats *c)
Return the relative standard deviation of the samples that had been entered into the statistic c...
Definition: C-mod-stats.c:177
#define C◼math◼max(...)
Definition: C-math.c:234
C◼size C◼size
Definition: C-sizeptr.c:94
static C◼mod◼stats *_Atomic list
Definition: C-mod-stats.c:300
C◼size() C◼str◼len(char const *)
double C◼mod◼stats◼mean(C◼mod◼stats *c)
Return the mean value of the samples that had been entered into the statistic c.
Definition: C-mod-stats.c:147
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

§ C◼mod◼stats◼rsdev()

double C◼mod◼stats◼rsdev ( C◼mod◼stats c)
inline

Return the relative standard deviation of the samples that had been entered into the statistic c.

Definition at line 177 of file C-mod-stats.c.

References C◼math◼sqrt, C◼mod◼stats◼mean(), and C◼mod◼stats◼var().

Referenced by C◼mod◼stats◼print(), and C◼mod◼stats◼rsdev—unbiased().

177  {
178 #line 79 "/home/gustedt/build/cmod/C/C-mod-stats.X"
180 }
double C◼mod◼stats◼var(C◼mod◼stats *c)
Return the variance of the samples that had been entered into the statistic c.
Definition: C-mod-stats.c:157
#define C◼math◼sqrt(X)
Definition: C-math.c:150
double C◼mod◼stats◼mean(C◼mod◼stats *c)
Return the mean value of the samples that had been entered into the statistic c.
Definition: C-mod-stats.c:147
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

§ C◼mod◼stats◼rsdev—unbiased()

double C◼mod◼stats◼rsdev—unbiased ( C◼mod◼stats c)
inline

Return the unbiased relative standard deviation of the samples that had been entered into the statistic c.

Definition at line 224 of file C-mod-stats.c.

References C◼mod◼stats◼rsdev(), and C◼mod◼stats◼samples().

224  {
225 #line 122 "/home/gustedt/build/cmod/C/C-mod-stats.X"
226  return C◼mod◼stats◼rsdev(c)*(1+1/(4*C◼mod◼stats◼samples(c)));
227 }
double C◼mod◼stats◼samples(C◼mod◼stats *c)
Return the number of samples that had been entered into the statistic c.
Definition: C-mod-stats.c:137
double C◼mod◼stats◼rsdev(C◼mod◼stats *c)
Return the relative standard deviation of the samples that had been entered into the statistic c...
Definition: C-mod-stats.c:177
+ Here is the call graph for this function:

§ C◼mod◼stats◼samples()

double C◼mod◼stats◼samples ( C◼mod◼stats c)
inline

Return the number of samples that had been entered into the statistic c.

Definition at line 137 of file C-mod-stats.c.

Referenced by C◼mod◼stats◼collect(), C◼mod◼stats◼print(), C◼mod◼stats◼rsdev—unbiased(), C◼mod◼stats◼skew(), C◼mod◼stats◼var(), and C◼mod◼stats◼var—unbiased().

137  {
138 #line 43 "/home/gustedt/build/cmod/C/C-mod-stats.X"
139  return c→moment[0];
140 }
+ Here is the caller graph for this function:

§ C◼mod◼stats◼sdev()

double C◼mod◼stats◼sdev ( C◼mod◼stats c)
inline

Return the standard deviation of the samples that had been entered into the statistic c.

Definition at line 167 of file C-mod-stats.c.

References C◼math◼sqrt, and C◼mod◼stats◼var().

167  {
168 #line 70 "/home/gustedt/build/cmod/C/C-mod-stats.X"
170 }
double C◼mod◼stats◼var(C◼mod◼stats *c)
Return the variance of the samples that had been entered into the statistic c.
Definition: C-mod-stats.c:157
#define C◼math◼sqrt(X)
Definition: C-math.c:150
+ Here is the call graph for this function:

§ C◼mod◼stats◼sdev—unbiased()

double C◼mod◼stats◼sdev—unbiased ( C◼mod◼stats c)
inline

Return the unbiased standard deviation of the samples that had been entered into the statistic c.

Use Bessel's correction to have an less biased estimation of the variance of the overall population.

Definition at line 214 of file C-mod-stats.c.

References C◼math◼sqrt, and C◼mod◼stats◼var—unbiased().

214  {
215 #line 113 "/home/gustedt/build/cmod/C/C-mod-stats.X"
217 }
double C◼mod◼stats◼var—unbiased(C◼mod◼stats *c)
Return the unbiased variance of the samples that had been entered into the statistic c...
Definition: C-mod-stats.c:201
#define C◼math◼sqrt(X)
Definition: C-math.c:150
+ Here is the call graph for this function:

§ C◼mod◼stats◼skew()

double C◼mod◼stats◼skew ( C◼mod◼stats c)
inline

Return the normalized skew of the samples that had been entered into the statistic c.

Definition at line 187 of file C-mod-stats.c.

References C◼math◼pow, C◼mod◼stats◼samples(), and C◼mod◼stats◼var().

Referenced by C◼mod◼stats◼print().

187  {
188 #line 88 "/home/gustedt/build/cmod/C/C-mod-stats.X"
189  double v = C◼mod◼stats◼var(c);
190  return (c→moment[3]/C◼math◼pow(v, 1.5))/C◼mod◼stats◼samples(c);
191 }
double C◼mod◼stats◼samples(C◼mod◼stats *c)
Return the number of samples that had been entered into the statistic c.
Definition: C-mod-stats.c:137
double C◼mod◼stats◼var(C◼mod◼stats *c)
Return the variance of the samples that had been entered into the statistic c.
Definition: C-mod-stats.c:157
#define C◼math◼pow(X, Y)
Definition: C-math.c:154
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

§ C◼mod◼stats◼var()

double C◼mod◼stats◼var ( C◼mod◼stats c)
inline

Return the variance of the samples that had been entered into the statistic c.

Definition at line 157 of file C-mod-stats.c.

References C◼mod◼stats◼samples().

Referenced by C◼mod◼stats◼rsdev(), C◼mod◼stats◼sdev(), and C◼mod◼stats◼skew().

157  {
158 #line 61 "/home/gustedt/build/cmod/C/C-mod-stats.X"
159  return c→moment[2]/C◼mod◼stats◼samples(c);
160 }
double C◼mod◼stats◼samples(C◼mod◼stats *c)
Return the number of samples that had been entered into the statistic c.
Definition: C-mod-stats.c:137
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

§ C◼mod◼stats◼var—unbiased()

double C◼mod◼stats◼var—unbiased ( C◼mod◼stats c)
inline

Return the unbiased variance of the samples that had been entered into the statistic c.

Use Bessel's correction to have an estimation of the unbiased variance of the overall population.

Definition at line 201 of file C-mod-stats.c.

References C◼mod◼stats◼samples().

Referenced by C◼mod◼stats◼sdev—unbiased().

201  {
202 #line 101 "/home/gustedt/build/cmod/C/C-mod-stats.X"
203  return c→moment[2]/(C◼mod◼stats◼samples(c)-1);
204 }
double C◼mod◼stats◼samples(C◼mod◼stats *c)
Return the number of samples that had been entered into the statistic c.
Definition: C-mod-stats.c:137
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

§ list

C◼mod◼stats* _Atomic list = C◼atomic◼INIT(0)
static

Definition at line 300 of file C-mod-stats.c.

Referenced by C◼mod◼stats◼cleanup(), and C◼mod◼stats◼collect3().