Standard Deviation challenge

Discussions related to mathematics, numerical methods, graph plotting etc.
Hated Moron

Re: Standard Deviation challenge

Post by Hated Moron »

DDRM wrote: Fri 06 Jan 2023, 09:04 One other comment I'd make about that solution is that it destroys the contents of the array, which may be undesireable. It might be better to accept the "extra cost" of one more line (or a slightly more complicated formula) to avoid that in a working version...
The challenge wasn't to create some reusable library code, or to demonstrate good programming practice, but to calculate the Standard Deviation in the shortest possible way in BBC BASIC. Like all similar 'minimum code' challenges anything goes unless explicitly ruled out.

If you don't want to destroy the contents of the array then simply make a copy of the original array first:

Code: Select all

      copy() = array()
There's no way I would accept that it should be part of the solution to the challenge.
This solution assumes that the array is being used 0-based, and has no "empty" entries ,e.g. at the top
All of the whole-array operations in BBC BASIC make the same assumptions, it's a feature of the language; it's no more necessary to state that explicitly for this example than it is for the built-in operations like SUM, MOD and the dot-product operator, or for that matter the routines in the arraylib library.

I expect your preferred programming language allows you to specify a subset of the array on which 'whole array' operations are performed. Clearly that is a valuable extension, and one which I wish Sophie had implemented when the whole-array operations were added in 1986. But it's not one which BBC BASIC supports, and the best you can do is to adopt the technique described in the Wiki here.

I will leave it as an exercise to extend the code in the Wiki to specify both the starting element and the length of the partial array, and to support MOD as well as SUM.
Edja
Posts: 64
Joined: Tue 03 Apr 2018, 12:07
Location: Belgium

Re: Standard Deviation challenge

Post by Edja »

Your original is correct (the +1 is required). Should I now take back the (non-existent) prize?!
Oh no, the horror ... :o I've never won a prize in my life. I would hate to lose it, even if non-existent
But seriously, I think I've entered the correct solution
My code was aiming for the total population standard deviation (11 values in total) in which case the +1 is required.
The point I tried to make, in respons to DDRM's comment, is that in Excel you have two functions STDEV.S (sample standard deviation) and STDEV.P (entire population standard deviation). I compared the result of my correct code with the wrong Excel function and then explained if I removed the +1 (not suggesting that this would be the right code) the two results match. I admit this may have created some confusion.

To summarize :

Code: Select all

array() -= SUM(array())/(DIM (array(),1)+1)
stdev=MOD(array())/SQR(DIM (array(),1)+1)
was the right solution (including your nice tweak) for the entire population standard deviation (Excel's STDEV.P)

But your comment to modify the DIM(10) statement to DIM array(9) escapes me (incidently as a result the code you've listed produces a "Syntax error"). The challenge, as you've formulated it,
list the code to calculate the Standard Deviation of all the values in the array
asked for the stdev of all values. That made me include array(0). And this matches with Excel.
Hated Moron

Re: Standard Deviation challenge

Post by Hated Moron »

Edja wrote: Fri 06 Jan 2023, 12:16 The point I tried to make, in response to DDRM's comment, is that in Excel you have two functions STDEV.S (sample standard deviation) and STDEV.P (entire population standard deviation).
I don't know anything about Excel, nor have I ever heard of there being two alternative definitions of Standard Deviation - I don't think there were when I was taught it at school!
But your comment to modify the DIM(10) statement to DIM array(9) escapes me
It was my clumsy way of illustrating that in BBC BASIC's DIM statement you specify the maximum index, not the total number of elements, so that to have 10 elements you need to dimension the array as (9). I forgot to change the initialisation code to omit the initial zero, hence the 'Syntax error' (when testing any code which purports to work on an 'entire array' you shouldn't initialise either the first or the last element to zero, since that may lead to undetected errors).

My code was supposed to have been:

Code: Select all

      DIM array(9)
      array() = 1,2,3,4,5,6,7,8,9,10
      array() -= SUM(array())/(DIM (array(),1)+1)
      stdev = MOD(array())/SQR(DIM (array(),1)+1)
Hated Moron

Re: Standard Deviation challenge

Post by Hated Moron »

Hated Moron wrote: Fri 06 Jan 2023, 10:41 I will leave it as an exercise to extend the code in the Wiki to specify both the starting element and the length of the partial array, and to support MOD as well as SUM.
It was so straightforward I did it myself.