Zephyr Scientific Library (zscilib)
statistics.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2020 Kevin Townsend (KTOWN)
3  * Copyright (c) 2021 Marti Riba Pons
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
23 #ifndef ZEPHYR_INCLUDE_ZSL_STATISTICS_H_
24 #define ZEPHYR_INCLUDE_ZSL_STATISTICS_H_
25 
26 #include <zsl/zsl.h>
27 #include <zsl/vectors.h>
28 #include <zsl/matrices.h>
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
48 };
49 
59 int zsl_sta_mean(struct zsl_vec *v, zsl_real_t *m);
60 
79 int zsl_sta_trim_mean(struct zsl_vec *v, zsl_real_t p, zsl_real_t *m);
80 
94 int zsl_sta_weighted_mean(struct zsl_vec *v, struct zsl_vec *w, zsl_real_t *m);
95 
112 int zsl_sta_time_weighted_mean(struct zsl_vec *v, struct zsl_vec *t,
113  zsl_real_t *m);
114 
125 int zsl_sta_demean(struct zsl_vec *v, struct zsl_vec *w);
126 
137 int zsl_sta_percentile(struct zsl_vec *v, zsl_real_t p, zsl_real_t *val);
138 
149 int zsl_sta_median(struct zsl_vec *v, zsl_real_t *m);
150 
164 int zsl_sta_weighted_median(struct zsl_vec *v, struct zsl_vec *w, zsl_real_t *m);
165 
177 int zsl_sta_quart(struct zsl_vec *v, zsl_real_t *q1, zsl_real_t *q2,
178  zsl_real_t *q3);
179 
190 int zsl_sta_quart_range(struct zsl_vec *v, zsl_real_t *r);
191 
202 int zsl_sta_mode(struct zsl_vec *v, struct zsl_vec *w);
203 
214 int zsl_sta_data_range(struct zsl_vec *v, zsl_real_t *r);
215 
230 int zsl_sta_mean_abs_dev(struct zsl_vec *v, zsl_real_t *m);
231 
245 int zsl_sta_median_abs_dev(struct zsl_vec *v, zsl_real_t *m);
246 
257 int zsl_sta_var(struct zsl_vec *v, zsl_real_t *var);
258 
273 int zsl_sta_std_dev(struct zsl_vec *v, zsl_real_t *s);
274 
284 int zsl_sta_covar(struct zsl_vec *v, struct zsl_vec *w, zsl_real_t *c);
285 
296 int zsl_sta_covar_mtx(struct zsl_mtx *m, struct zsl_mtx *mc);
297 
328 int zsl_sta_linear_reg(struct zsl_vec *x, struct zsl_vec *y,
329  struct zsl_sta_linreg *c);
330 
331 #ifndef CONFIG_ZSL_SINGLE_PRECISION
332 
346 int zsl_sta_mult_linear_reg(struct zsl_mtx *x, struct zsl_vec *y,
347  struct zsl_vec *b, zsl_real_t *r);
348 #endif
349 
350 #ifndef CONFIG_ZSL_SINGLE_PRECISION
351 
367 int zsl_sta_weighted_mult_linear_reg(struct zsl_mtx *x, struct zsl_vec *y,
368  struct zsl_vec *w, struct zsl_vec *b, zsl_real_t *r);
369 #endif
370 
371 #ifndef CONFIG_ZSL_SINGLE_PRECISION
372 
389 int zsl_sta_quad_fit(struct zsl_mtx *m, struct zsl_vec *b);
390 #endif
391 
402 int zsl_sta_abs_err(zsl_real_t *val, zsl_real_t *exp_val, zsl_real_t *err);
403 
414 int zsl_sta_rel_err(zsl_real_t *val, zsl_real_t *exp_val, zsl_real_t *err);
415 
428 int zsl_sta_sta_err(struct zsl_vec *v, zsl_real_t *err);
429 
430 #ifdef __cplusplus
431 }
432 #endif
433 
434 #endif /* ZEPHYR_INCLUDE_ZSL_STATISTICS_H_ */
435  /* End of statistics group */
zsl_sta_weighted_mult_linear_reg
int zsl_sta_weighted_mult_linear_reg(struct zsl_mtx *x, struct zsl_vec *y, struct zsl_vec *w, struct zsl_vec *b, zsl_real_t *r)
Calculates the coefficients (vector 'b') of the weighted multiple linear regression of the x_i values...
Definition: statistics.c:535
zsl_sta_quart_range
int zsl_sta_quart_range(struct zsl_vec *v, zsl_real_t *r)
Calculates the numeric difference between the third and the first quartiles of a vector v.
Definition: statistics.c:245
zsl_sta_abs_err
int zsl_sta_abs_err(zsl_real_t *val, zsl_real_t *exp_val, zsl_real_t *err)
Calculates the absolute error given a value and its expected value.
Definition: statistics.c:665
zsl_sta_mode
int zsl_sta_mode(struct zsl_vec *v, struct zsl_vec *w)
Computes the mode or modes of a vector v.
Definition: statistics.c:257
zsl_mtx
Represents a m x n matrix, with data stored in row-major order.
Definition: matrices.h:46
zsl_sta_median
int zsl_sta_median(struct zsl_vec *v, zsl_real_t *m)
Computes the median of a vector (the value separating the higher half from the lower half of a data s...
Definition: statistics.c:184
zsl_sta_mean
int zsl_sta_mean(struct zsl_vec *v, zsl_real_t *m)
Computes the arithmetic mean (average) of a vector.
Definition: statistics.c:14
zsl_sta_rel_err
int zsl_sta_rel_err(zsl_real_t *val, zsl_real_t *exp_val, zsl_real_t *err)
Calculates the relative error given a value and its expected value.
Definition: statistics.c:672
zsl_sta_linreg::correlation
zsl_real_t correlation
The correlation coefficient, where closer to 1.0 is better.
Definition: statistics.h:47
zsl_sta_time_weighted_mean
int zsl_sta_time_weighted_mean(struct zsl_vec *v, struct zsl_vec *t, zsl_real_t *m)
Computes the time-weighted arithmetic mean (average) of a positive data vector (v) and its time vecto...
Definition: statistics.c:93
zsl_sta_median_abs_dev
int zsl_sta_median_abs_dev(struct zsl_vec *v, zsl_real_t *m)
Computes the median absolute deviation of a data vector v.
Definition: statistics.c:341
zsl_sta_linreg::slope
zsl_real_t slope
The estimated slope.
Definition: statistics.h:39
zsl_sta_quart
int zsl_sta_quart(struct zsl_vec *v, zsl_real_t *q1, zsl_real_t *q2, zsl_real_t *q3)
Calculates the first, second and third quartiles of a vector v.
Definition: statistics.c:235
zsl_sta_data_range
int zsl_sta_data_range(struct zsl_vec *v, zsl_real_t *r)
Computes the difference between the greatest value and the lowest in a vector v.
Definition: statistics.c:309
zsl_sta_weighted_mean
int zsl_sta_weighted_mean(struct zsl_vec *v, struct zsl_vec *w, zsl_real_t *m)
Computes the weighted arithmetic mean (average) of a data vector (v) and a weight vector (w).
Definition: statistics.c:57
zsl_sta_trim_mean
int zsl_sta_trim_mean(struct zsl_vec *v, zsl_real_t p, zsl_real_t *m)
Computes the trimmed arithmetic mean (average) of a vector.
Definition: statistics.c:21
zsl_sta_linear_reg
int zsl_sta_linear_reg(struct zsl_vec *x, struct zsl_vec *y, struct zsl_sta_linreg *c)
Calculates the slope, intercept and correlation coefficient of the linear regression of two vectors,...
Definition: statistics.c:434
vectors.h
API header file for vectors in zscilib.
zsl_sta_covar
int zsl_sta_covar(struct zsl_vec *v, struct zsl_vec *w, zsl_real_t *c)
Computes the variance of two sets of data: v and w.
Definition: statistics.c:383
zsl_sta_mult_linear_reg
int zsl_sta_mult_linear_reg(struct zsl_mtx *x, struct zsl_vec *y, struct zsl_vec *b, zsl_real_t *r)
Calculates the coefficients (vector 'b') of the multiple linear regression of the x_i values (columns...
Definition: statistics.c:466
zsl_sta_linreg
Simple linear regression coefficients.
Definition: statistics.h:35
zsl_sta_mean_abs_dev
int zsl_sta_mean_abs_dev(struct zsl_vec *v, zsl_real_t *m)
Computes the mean absolute deviation of a data vector v.
Definition: statistics.c:320
matrices.h
API header file for matrices in zscilib.
zsl_sta_demean
int zsl_sta_demean(struct zsl_vec *v, struct zsl_vec *w)
Subtracts the mean of vector v from every component of the vector. The output vector w then has a zer...
Definition: statistics.c:140
zsl.h
API header file for zscilib.
zsl_sta_weighted_median
int zsl_sta_weighted_median(struct zsl_vec *v, struct zsl_vec *w, zsl_real_t *m)
Computes the weighted median of a data vector (v) and a weight vector (w).
Definition: statistics.c:191
zsl_sta_percentile
int zsl_sta_percentile(struct zsl_vec *v, zsl_real_t p, zsl_real_t *val)
Computes the given percentile of a vector.
Definition: statistics.c:158
zsl_vec
Represents a vector.
Definition: vectors.h:40
zsl_real_t
double zsl_real_t
Definition: zsl.h:51
zsl_sta_std_dev
int zsl_sta_std_dev(struct zsl_vec *v, zsl_real_t *s)
Computes the standard deviation of vector v.
Definition: statistics.c:372
zsl_sta_sta_err
int zsl_sta_sta_err(struct zsl_vec *v, zsl_real_t *err)
Calculates the standard error of the mean of a sample (vector v).
Definition: statistics.c:679
zsl_sta_linreg::intercept
zsl_real_t intercept
The estimated intercept.
Definition: statistics.h:43
zsl_sta_covar_mtx
int zsl_sta_covar_mtx(struct zsl_mtx *m, struct zsl_mtx *mc)
Calculates the nxn covariance matrix of a set of n vectors of the same length.
Definition: statistics.c:408
zsl_sta_var
int zsl_sta_var(struct zsl_vec *v, zsl_real_t *var)
Computes the variance of a vector v (the average of the squared differences from the mean).
Definition: statistics.c:356
zsl_sta_quad_fit
int zsl_sta_quad_fit(struct zsl_mtx *m, struct zsl_vec *b)
This function uses the least squares fitting method to compute the coefficients of a quadric surface ...
Definition: statistics.c:618