Dan,

        A weighted average is computed as follows.

   wtavg =: *%&(+/)[  NB. wts left, values right

        Your case is more specific where all weights are
unity except the first which is wmr. So your computation
would be as follows.

   dbwtavg =: ([EMAIL PROTECTED])wtavg]
   6 dbwtavg 1 2 3 NB. for example wmr is 6
1.375

        Your person would have to supply the new average and
the number of values in the series, say newvalue =: 8, N =:
10 and newaverage =: 7, in your example. You would then make
the following calculation.

   wmr dbwtavg newvalue 0}N#newaverage
7.25



On Tue, 8 Apr 2008, Dan Bron wrote:

I need to "undo" an average.  The problem is I don't have the whole series of 
data; I have only the initial average and, periodically, a new value to be 
averaged in.

So, let's say a user hands me a pre-computed average.  I want to take that 
average, and whenever I get a new value of the series, I want to "expand" the 
precomputed average, drop off its leading (oldest) value, append the new value, 
and redo the average.  Then I would use this new average as my "precomputed" 
average.

One wrinkle is that the average is weighted.  In particular, the newest value 
is always weighted by some user-specified factor (all other weights are 1).  
The initial user-provided precomputed average has already taken this into 
account for the first iteration, and my code is responsible for taking it into 
account for any new values.


Concretely, I would like to emulate this J verb:

        L         =:  ?.~10        NB.  Original list -- I won't have access to 
this.
        wmr       =:  3            NB. Weight Most Recent value by a factor of 3

        movingAvg =: dyad define

                L =:  y,}:L

                (+/%#) (x*{.L) , }.L

        )

           wmr movingAvg 10
        7
           wmr movingAvg 5
        5.8
           wmr movingAvg 8
        6.4
           wmr movingAvg 9
        7.2


except that I do not have access to the original series of data  L  .  In my 
scenario, my initial inputs would be  wmr  and  (+/%#) (wmr*{.L) , }.L   and, 
later, the values  10 5 8 9  (spread out over time).   Ideally, I would like to 
avoid keeping any kind of list (e.g., of the new values as they come in).

Is this possible?

-Dan

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to