View Raw SPL
/*****************************************************************************
*                                                                            *
*   CUMAVG.SPL   Copyright (C) 2014 DSP Development Corporation              *
*                            All Rights Reserved                             *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Cumulative mean                                             *
*                                                                            *
*   Revisions:   25 Sep 2014  RRR  Creation                                  *
*                                                                            *
*****************************************************************************/


#if @HELP_CUMAVG

    CUMAVG

    Purpose: Calculates the cumulative average of a series.

    Syntax:  CUMAVG(series)

             series        - A series or table.

    Returns: A series or table.

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

             returns a new series containing the cumulative
             average {20, 17.5, 21.6667, 18.75, 20}.

    Example:
             W1: gnorm(1000, 1)
             W2: gline(length(w1), deltax(w1), 1/1000, 0)
             W3: W1 + W2
             W4: cumavg(W3)
       
             W3 contains the sum of 1000 random samples and a linear trend.
             W4 shows the linear trend developing as more samples of the
             data are averaged.

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

             mean(W1) == W2[end] 
       
             The last point of CUMAVG is the overall average of the input
             data.

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

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

             See CUMSUM for the cumulative sum.

    See Also:
             AVGS
             CUMSUM
             INTEG
             MEAN
#endif


/* cumulative average */
cumavg(s, dim = 1, nanmode = "", method = "")
{
        local c, dof;

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

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

        if (dim <= 1)
        {
                c = cumsum(s, dim, nanmode, method) / colidx(s);
        }
        else
        {
                c = cumsum(s, dim, nanmode, method) / rowidx(s);
        }

        return(c);
}