Boost logo

Boost :

Subject: Re: [boost] Interest in algorithm for arithmetic mean of integers
From: Matt Calabrese (rivorus_at_[hidden])
Date: 2015-09-05 15:20:51


On Sat, Sep 5, 2015 at 4:07 AM, Bjorn Reese <breese_at_[hidden]>
wrote:
>
> Can you provide an example of how it works? For example, if I have an
> input array with {10, 20, 30}, how are V, N, and D updated in each
> iteration?

Without posting the code, here is the generated output during accumulation
for your example {10, 20, 30}.
"State" in the following code is the state at the start of a given
iteration. "Incremented state" is the state after the transformation of N/D
to N/(D+1) I described in an earlier reply but before adding in the value
from the current. Again, that transformation is done on a mixed number and
without the possibility of overflow. Only additive operators, division, and
modulus are used. There is no overflow with the additive operations because
I do a difference calculation from the current denominator before
performing the operation, carrying over to the whole number part when
necessary.

State : 0 0/0
Incremented State: 0 0/1

State : 10 0/1
Incremented State: 5 0/2

State : 15 0/2
Incremented State: 10 0/3

State : 20 0/3

Your sample array isn't too interesting in terms of the state changes, so
here's a more complex one for the array {11, 17, 3, 31, 27}

State : 0 0/0
Incremented State: 0 0/1

State : 11 0/1
Incremented State: 5 1/2

State : 14 0/2
Incremented State: 9 1/3

State : 10 1/3
Incremented State: 7 3/4

State : 15 2/4
Incremented State: 12 2/5

State : 17 4/5

Anyway, it seems like there's interest so I'll try to release it. What
exactly is the review process for adding algorithms, or is it still
somewhat informal?

The algorithm should be able to be adapted to introduce weights. It would
work very similarly, only when advancing the state, it would be based on
the weight rather than always effectively using (D+1). Similarly, the
denominator would no longer always exactly match the number of elements,
but instead it would always match the sum of the weights that were
encountered. I haven't implemented this weighted variation, but it
shouldn't be too much of a change.

-- 
-Matt Calabrese

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk