streaming_algorithms  0.0.5
A collection of streaming data algorithms
running_stats.h
Go to the documentation of this file.
1 /* -*- Mode: C; tab_width: 8; indent_tabs_mode: nil; c_basic_offset: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 /** Calculates the running count, mean, variance, and standard deviation
8  * @file */
9 
10 #ifndef sa_running_stats_h_
11 #define sa_running_stats_h_
12 
13 #include <stddef.h>
14 
15 typedef struct sa_running_stats
16 {
17  double count;
18  double mean;
19  double sum;
21 
22 #ifdef __cplusplus
23 extern "C"
24 {
25 #endif
26 
27 /**
28  * Zeros out the stats counters.
29  *
30  * @param s Stat structure to zero out
31  */
33 
34 /**
35  * Value to add to the running stats.
36  *
37  * @param s Stat structure
38  * @param d Value to add
39  */
40 void sa_add_running_stats(sa_running_stats *s, double d);
41 
42 /**
43  * Returns the variance of the stats.
44  *
45  * @param s Stat structure
46  *
47  * @return double Variance of the stats up to this point
48  */
50 
51 /**
52  * Returns the corrected sample standard deviation of the stats.
53  *
54  * @param s Stat structure
55  *
56  * @return double Standard deviation of the stats up to this point
57  */
59 
60 /**
61  * Returns the uncorrected sample standard deviation of the stats.
62  *
63  * @param s Stat structure
64  *
65  * @return double Uncorrected standard deviation of the stats up to this point
66  */
68 
69 /**
70  * Serialize the internal state to a buffer.
71  *
72  * @param s Stat structure
73  * @param len Length of the returned buffer
74  *
75  * @return char* Serialized representation MUST be freed by the caller
76  */
77 char* sa_serialize_running_stats(sa_running_stats *s, size_t *len);
78 
79 /**
80  * Restores the internal state from the serialized output.
81  *
82  * @param s Stat structure
83  * @param buf Buffer containing the output of serialize_running_stats
84  * @param len Length of the buffer
85  *
86  * @return 0 = success
87  * 1 = invalid buffer length
88  * 2 = invalid count value
89  *
90  */
91 int
92 sa_deserialize_running_stats(sa_running_stats *s, const char *buf, size_t len);
93 
94 #ifdef __cplusplus
95 }
96 #endif
97 
98 #endif
void sa_init_running_stats(sa_running_stats *s)
Zeros out the stats counters.
double sa_sd_running_stats(sa_running_stats *s)
Returns the corrected sample standard deviation of the stats.
int sa_deserialize_running_stats(sa_running_stats *s, const char *buf, size_t len)
Restores the internal state from the serialized output.
struct sa_running_stats sa_running_stats
char * sa_serialize_running_stats(sa_running_stats *s, size_t *len)
Serialize the internal state to a buffer.
double sa_variance_running_stats(sa_running_stats *s)
Returns the variance of the stats.
double sa_usd_running_stats(sa_running_stats *s)
Returns the uncorrected sample standard deviation of the stats.
void sa_add_running_stats(sa_running_stats *s, double d)
Value to add to the running stats.