Calculating mean and standard deviation with std.algorithm.reduce

2013-02-13 Thread Joseph Rushton Wakeling
Hello all, A little challenge that's been bothering me today. The docs for std.algorithm give an illustration of its use to calculate mean and standard deviation in a single pass: // Compute sum and sum of squares in one pass r = reduce!(a + b, a + b * b)(tuple(0.0, 0.0), a);

Re: Calculating mean and standard deviation with std.algorithm.reduce

2013-02-13 Thread jerro
... where k represents the index count 1, 2, 3, ... However, it's not evident to me how you could get reduce() to know this counting value. You would use zip and sequence to add indices to x, like this: reduce!reducer(initial, zip(x, sequence!n)) Where calculating Q[k] is concerned, you

Re: Calculating mean and standard deviation with std.algorithm.reduce

2013-02-13 Thread jerro
reduce!reducer(MQ(x.front, 0), zip(x, sequence!n)) A small correction : you would need to use x.drop(1) instead of x, because the first element of x is only used to compute the initial value of 1. If you wanted k to have the same meaning as the one in your formula, you would need to use

Re: Calculating mean and standard deviation with std.algorithm.reduce

2013-02-13 Thread FG
On 2013-02-13 14:44, Joseph Rushton Wakeling wrote: The docs for std.algorithm give an illustration of its use to calculate mean and standard deviation in a single pass: [...] However, this formula for standard deviation is one that is well known for being subject to potentially fatal rounding

Re: Calculating mean and standard deviation with std.algorithm.reduce

2013-02-13 Thread bearophile
jerro: reduce!reducer(MQ(x.front, 0), zip(x, sequence!n)) A small correction : you would need to use x.drop(1) instead of x, because the first element of x is only used to compute the initial value of 1. If you wanted k to have the same meaning as the one in your formula, you would need to

Re: Calculating mean and standard deviation with std.algorithm.reduce

2013-02-13 Thread Joseph Rushton Wakeling
On 02/13/2013 03:48 PM, FG wrote: You can use reduce and put the division and subtraction into the reduce itself to prevent overflows. You also won't end up with jaw-dropping tuples, sorry. :) float[] a = [10_000.0f, 10_001.0f, 10_002.0f]; auto n = a.length; auto avg =

Re: Calculating mean and standard deviation with std.algorithm.reduce

2013-02-13 Thread Joseph Rushton Wakeling
On 02/13/2013 03:48 PM, FG wrote: Typical thing with examples - they try to be terse and show off a mechanism like reduce, without going into too much details and hence are unusable IRL. My favourite -- in the tutorial for a really serious piece of scientific code written in C: int n =

Re: Calculating mean and standard deviation with std.algorithm.reduce

2013-02-13 Thread jerro
See enumerate(): http://d.puremagic.com/issues/show_bug.cgi?id=5550 I like this enumerate() thing. Is there any particular reason why it isn't in phobos, or is it just that no one has added it yet? I think with enumerate it becomes: MQ(x.front, 0).enumerate(1).reduce!reducer() I think the