View Raw SPL
/*****************************************************************************
*                                                                            *
*   CUMSUM.SPL   Copyright (C) 2004 DSP Development Corporation              *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Cumulative sum                                              *
*                                                                            *
*   Revisions:   16 Jan 2004  RRR  Creation                                  *
*                                                                            *
*****************************************************************************/


#if @HELP_CUMSUM

    CUMSUM

    Purpose: Calculates the cumulative sum of a series.

    Syntax:  CUMSUM(series)

             series        - Any series, multi-series table, or expression
                          resulting in a series or table.

    Returns: A series or table.

    Example:
             cumsum({20, 15, 30, 10, 25})

             returns a new series containing the cumulative
             sums {20, 35, 65, 75, 100}.

    Example:
             W1: integ(gnorm(1000, 1))
             W2: cumsum(W1)

             sum(W1) == W2[end] 
       
             The last point of CUMSUM is the total sum of the input data.

    Remarks:
             The nth value of the output series is equal to the sum of
             the first n points of the input series.

             CUMSUM calculates the cumulative sum of a series.

             CUMSUM differs from integration in that the deltax
             information is not incorporated into the calculation.

             CUMSUM is identical to PARTSUM.

             See CUMAVG for the cumulative average.

    See Also:
             CUMAVG
             INTEG
             PARTSUM
             SUM

#endif


/* cumulative sum */
cumsum(s, dim = 1, nanmode = "", method = "")
{
        local dof, ignorenan;

        /* args */
        (s, dim, dof, nanmode, method) = cumfun_parse_args(s, dim, 1, nanmode, method);

        if (dim > 1) 
        {
                if (dim > 2) 
                {
                        return(s);
                }

                s = s';
        }

        /* nan handling - default to includenan        */
        ignorenan = (strlen(nanmode) > 0) && not(nanmode == "includenan");

        /* partsum computation */
        s = partsum(s, ignorenan, method);

        if (dim > 1)
        {
                s = s';
        }

        return(s);
}