]> git.sesse.net Git - bcachefs-tools-debian/blob - linux/mean_and_variance.c
Update bcachefs sources to 171da96d76 bcachefs: Drop some anonymous structs, unions
[bcachefs-tools-debian] / linux / mean_and_variance.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Functions for incremental mean and variance.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published by
7  * the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * Copyright © 2022 Daniel B. Hill
15  *
16  * Author: Daniel B. Hill <daniel@gluo.nz>
17  *
18  * Description:
19  *
20  * This is includes some incremental algorithms for mean and variance calculation
21  *
22  * Derived from the paper: https://fanf2.user.srcf.net/hermes/doc/antiforgery/stats.pdf
23  *
24  * Create a struct and if it's the weighted variant set the w field (weight = 2^k).
25  *
26  * Use mean_and_variance[_weighted]_update() on the struct to update it's state.
27  *
28  * Use the mean_and_variance[_weighted]_get_* functions to calculate the mean and variance, some computation
29  * is deferred to these functions for performance reasons.
30  *
31  * see lib/math/mean_and_variance_test.c for examples of usage.
32  *
33  * DO NOT access the mean and variance fields of the weighted variants directly.
34  * DO NOT change the weight after calling update.
35  */
36
37 #include <linux/bug.h>
38 #include <linux/compiler.h>
39 #include <linux/export.h>
40 #include <linux/limits.h>
41 #include <linux/math.h>
42 #include <linux/math64.h>
43 #include <linux/mean_and_variance.h>
44 #include <linux/module.h>
45
46 /**
47  * fast_divpow2() - fast approximation for n / (1 << d)
48  * @n: numerator
49  * @d: the power of 2 denominator.
50  *
51  * note: this rounds towards 0.
52  */
53 s64 fast_divpow2(s64 n, u8 d)
54 {
55         return (n + ((n < 0) ? ((1 << d) - 1) : 0)) >> d;
56 }
57
58 /**
59  * mean_and_variance_update() - update a mean_and_variance struct @s1 with a new sample @v1
60  * and return it.
61  * @s1: the mean_and_variance to update.
62  * @v1: the new sample.
63  *
64  * see linked pdf equation 12.
65  */
66 struct mean_and_variance mean_and_variance_update(struct mean_and_variance s1, s64 v1)
67 {
68         return mean_and_variance_update_inlined(s1, v1);
69 }
70 EXPORT_SYMBOL_GPL(mean_and_variance_update);
71
72 /**
73  * mean_and_variance_get_mean() - get mean from @s
74  */
75 s64 mean_and_variance_get_mean(struct mean_and_variance s)
76 {
77         return div64_u64(s.sum, s.n);
78 }
79 EXPORT_SYMBOL_GPL(mean_and_variance_get_mean);
80
81 /**
82  * mean_and_variance_get_variance() -  get variance from @s1
83  *
84  * see linked pdf equation 12.
85  */
86 u64 mean_and_variance_get_variance(struct mean_and_variance s1)
87 {
88         u128 s2 = u128_div(s1.sum_squares, s1.n);
89         u64  s3 = abs(mean_and_variance_get_mean(s1));
90
91         return u128_to_u64(u128_sub(s2, u128_square(s3)));
92 }
93 EXPORT_SYMBOL_GPL(mean_and_variance_get_variance);
94
95 /**
96  * mean_and_variance_get_stddev() - get standard deviation from @s
97  */
98 u32 mean_and_variance_get_stddev(struct mean_and_variance s)
99 {
100         return int_sqrt64(mean_and_variance_get_variance(s));
101 }
102 EXPORT_SYMBOL_GPL(mean_and_variance_get_stddev);
103
104 /**
105  * mean_and_variance_weighted_update() - exponentially weighted variant of mean_and_variance_update()
106  * @s1: ..
107  * @s2: ..
108  *
109  * see linked pdf: function derived from equations 140-143 where alpha = 2^w.
110  * values are stored bitshifted for performance and added precision.
111  */
112 struct mean_and_variance_weighted mean_and_variance_weighted_update(struct mean_and_variance_weighted s1,
113                                                                     s64 x)
114 {
115         return mean_and_variance_weighted_update_inlined(s1, x);
116 }
117 EXPORT_SYMBOL_GPL(mean_and_variance_weighted_update);
118
119 /**
120  * mean_and_variance_weighted_get_mean() - get mean from @s
121  */
122 s64 mean_and_variance_weighted_get_mean(struct mean_and_variance_weighted s)
123 {
124         return fast_divpow2(s.mean, s.w);
125 }
126 EXPORT_SYMBOL_GPL(mean_and_variance_weighted_get_mean);
127
128 /**
129  * mean_and_variance_weighted_get_variance() -- get variance from @s
130  */
131 u64 mean_and_variance_weighted_get_variance(struct mean_and_variance_weighted s)
132 {
133         // always positive don't need fast divpow2
134         return s.variance >> s.w;
135 }
136 EXPORT_SYMBOL_GPL(mean_and_variance_weighted_get_variance);
137
138 /**
139  * mean_and_variance_weighted_get_stddev() - get standard deviation from @s
140  */
141 u32 mean_and_variance_weighted_get_stddev(struct mean_and_variance_weighted s)
142 {
143         return int_sqrt64(mean_and_variance_weighted_get_variance(s));
144 }
145 EXPORT_SYMBOL_GPL(mean_and_variance_weighted_get_stddev);
146
147 MODULE_AUTHOR("Daniel B. Hill");
148 MODULE_LICENSE("GPL");