## The module "cumstd" of the Mastrave modelling library

Daniele de Rigo

#### Copyright and license notice of the function cumstd

The file cumstd.m is part of Mastrave.

Mastrave is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Mastrave is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with Mastrave. If not, see http://www.gnu.org/licenses/.

#### Function declaration

answer = cumstd( values      ,
dim    = [] ,
mode   = 0  )



#### Description

Utility to extend the function std(.) providing cumulative standard deviations of the elements of the array values along a given dimension dim .

To mitigate unwanted numeric cancellations, all cumulative standard deviations are first approximated by centering values elements with their corresponding non-cumulative means and then refined by correcting the centering with the correct cumulative means. This way, if N is the total number of elements of values and n is the size of the dimension dim of values , then the computation remains O( n * N ).

#### Input arguments


values            ::numeric::
Vector, matrix or multi-dimensional array of numbers.

dim               ::scalar_index|empty::
Scalar positive integer representing the dimension along
which the cumulative standard deviations have to be
computed.
If  dim  is an empty array  [] , the dimension is the
first non-singleton dimension.  In case  values  is a
vector, this definition means that the default
dimension is the one along which the elements of the
vector  values  are aligned.
If omitted, the default value is  [] .

mode              ::numstring::
Boolean or string declaring the kind of standard
deviation to be computed.
If omitted, its default value is: 0.
Valid modes are:

mode        │      meaning
─────────────────┼────────────────────────────────────
0              │ Compute the unbiased sample
'--sample'     │ standard deviation by normalizing
│ the i-th standard deviation with
│ (i-1) .  Standard deviations of
│ the first elements of  values
│ along  dim  are therefore NaN.
─────────────────┼────────────────────────────────────
1              │ Compute the population standard
'--population' │ deviation (or biased sample
│ standard deviation) by normalizing
│ the i-th standard deviation
│ with  i .  Standard deviations of
│ the first elements of  values
│ along  dim  are therefore zeros.



#### Example of usage


% Correctness check function
check = @(computed,expected)assert(                          ...
abs( (computed - expected)./expected ) < 10*eps           ...
)

% Basic usage

% Vectors:
v    = ceil( rand(1,7)* 100 )
cs   = cumstd( v )
check( cs(end), std(v) );

% Matrices:
v    = ceil( rand(5,7)* 100 )
cs   = cumstd( v )
check( cs(end,:), std(v) );

% Passing a custom dimension
cs   = cumstd( v , 2 )
check( cs(:,end), std(v,0,2) );

% Dealing with multi-dimensional arrays
v    = ceil( rand(5,7,3)* 100 )
cs   = cumstd( v )
cs   = cumstd( v , [] )
check( cs(end,:,:), std(v) );

cs   = cumstd( v , 3 )
check( cs(:,:,end), std(v,0,3) );

% Passing the kind of standard deviation to be computed
% Unbiased sample standard deviation
cs   = cumstd( v , [], '--sample' )
cs   = cumstd( v , [], 0 )
check( cs(end,:,:), std(v) );

% Population standard deviation (biased sample standard deviation)
cs   = cumstd( v , [], '--population' )
cs   = cumstd( v , [], 1 )
check( cs(end,:,:), std(v,1,1) );
cs   = cumstd( v , 2 , 1 )
check( cs(:,end,:), std(v,1,2) );
cs   = cumstd( v , 3 , 1 )
check( cs(:,:,end), std(v,1,3) );

% Complex-valued elements of  values
v    = ceil( rand(5,7)* 100 ) +1i * ceil( rand(5,7)* 100 )
cs   = cumstd( v )
check( cs(end,:), std(v) );
cs   = cumstd( v , 2 )
check( cs(:,end), std(v,0,2) );


Memory requirements:
O( numel(  values  ) )

cummean, cumvar, cumsumsq, groupfun

Keywords:
cumulative operators, scan

Version: 0.3.3

#### Support

The Mastrave modelling library is committed to provide reusable and general - but also robust and scalable - modules for research modellers dealing with computational science.  You can help the Mastrave project by providing feedbacks on unexpected behaviours of this module.  Despite all efforts, all of us - either developers or users - (should) know that errors are unavoidable.  However, the free software paradigm successfully highlights that scientific knowledge freedom also implies an impressive opportunity for collectively evolve the tools and ideas upon which our daily work is based.  Reporting a problem that you found using Mastrave may help the developer team to find a possible bug.  Please, be aware that Mastrave is entirely based on voluntary efforts: in order for your help to be as effective as possible, please read carefully the section on reporting problems.  Thank you for your collaboration.

Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Daniele de Rigo