]> git.sesse.net Git - bcachefs-tools-debian/blob - linux/mean_and_variance.c
fix snapshotting when dst is single component path
[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 u128_u u128_div(u128_u n, u64 d)
47 {
48         u128_u r;
49         u64 rem;
50         u64 hi = u128_hi(n);
51         u64 lo = u128_lo(n);
52         u64  h =  hi & ((u64) U32_MAX  << 32);
53         u64  l = (hi &  (u64) U32_MAX) << 32;
54
55         r =             u128_shl(u64_to_u128(div64_u64_rem(h,                d, &rem)), 64);
56         r = u128_add(r, u128_shl(u64_to_u128(div64_u64_rem(l  + (rem << 32), d, &rem)), 32));
57         r = u128_add(r,          u64_to_u128(div64_u64_rem(lo + (rem << 32), d, &rem)));
58         return r;
59 }
60 EXPORT_SYMBOL_GPL(u128_div);
61
62 /**
63  * mean_and_variance_get_mean() - get mean from @s
64  * @s: mean and variance number of samples and their sums
65  */
66 s64 mean_and_variance_get_mean(struct mean_and_variance s)
67 {
68         return s.n ? div64_u64(s.sum, s.n) : 0;
69 }
70 EXPORT_SYMBOL_GPL(mean_and_variance_get_mean);
71
72 /**
73  * mean_and_variance_get_variance() -  get variance from @s1
74  * @s1: mean and variance number of samples and sums
75  *
76  * see linked pdf equation 12.
77  */
78 u64 mean_and_variance_get_variance(struct mean_and_variance s1)
79 {
80         if (s1.n) {
81                 u128_u s2 = u128_div(s1.sum_squares, s1.n);
82                 u64  s3 = abs(mean_and_variance_get_mean(s1));
83
84                 return u128_lo(u128_sub(s2, u128_square(s3)));
85         } else {
86                 return 0;
87         }
88 }
89 EXPORT_SYMBOL_GPL(mean_and_variance_get_variance);
90
91 /**
92  * mean_and_variance_get_stddev() - get standard deviation from @s
93  * @s: mean and variance number of samples and their sums
94  */
95 u32 mean_and_variance_get_stddev(struct mean_and_variance s)
96 {
97         return int_sqrt64(mean_and_variance_get_variance(s));
98 }
99 EXPORT_SYMBOL_GPL(mean_and_variance_get_stddev);
100
101 /**
102  * mean_and_variance_weighted_update() - exponentially weighted variant of mean_and_variance_update()
103  * @s: mean and variance number of samples and their sums
104  * @x: new value to include in the &mean_and_variance_weighted
105  *
106  * see linked pdf: function derived from equations 140-143 where alpha = 2^w.
107  * values are stored bitshifted for performance and added precision.
108  */
109 void mean_and_variance_weighted_update(struct mean_and_variance_weighted *s,
110                 s64 x, bool initted, u8 weight)
111 {
112         // previous weighted variance.
113         u8 w            = weight;
114         u64 var_w0      = s->variance;
115         // new value weighted.
116         s64 x_w         = x << w;
117         s64 diff_w      = x_w - s->mean;
118         s64 diff        = fast_divpow2(diff_w, w);
119         // new mean weighted.
120         s64 u_w1        = s->mean + diff;
121
122         if (!initted) {
123                 s->mean = x_w;
124                 s->variance = 0;
125         } else {
126                 s->mean = u_w1;
127                 s->variance = ((var_w0 << w) - var_w0 + ((diff_w * (x_w - u_w1)) >> w)) >> w;
128         }
129 }
130 EXPORT_SYMBOL_GPL(mean_and_variance_weighted_update);
131
132 /**
133  * mean_and_variance_weighted_get_mean() - get mean from @s
134  * @s: mean and variance number of samples and their sums
135  */
136 s64 mean_and_variance_weighted_get_mean(struct mean_and_variance_weighted s,
137                 u8 weight)
138 {
139         return fast_divpow2(s.mean, weight);
140 }
141 EXPORT_SYMBOL_GPL(mean_and_variance_weighted_get_mean);
142
143 /**
144  * mean_and_variance_weighted_get_variance() -- get variance from @s
145  * @s: mean and variance number of samples and their sums
146  */
147 u64 mean_and_variance_weighted_get_variance(struct mean_and_variance_weighted s,
148                 u8 weight)
149 {
150         // always positive don't need fast divpow2
151         return s.variance >> weight;
152 }
153 EXPORT_SYMBOL_GPL(mean_and_variance_weighted_get_variance);
154
155 /**
156  * mean_and_variance_weighted_get_stddev() - get standard deviation from @s
157  * @s: mean and variance number of samples and their sums
158  */
159 u32 mean_and_variance_weighted_get_stddev(struct mean_and_variance_weighted s,
160                 u8 weight)
161 {
162         return int_sqrt64(mean_and_variance_weighted_get_variance(s, weight));
163 }
164 EXPORT_SYMBOL_GPL(mean_and_variance_weighted_get_stddev);
165
166 MODULE_AUTHOR("Daniel B. Hill");
167 MODULE_LICENSE("GPL");