Search code examples
google-sheetsarray-formulassampling

How to calculate sample variance for "moving" sample using Google Sheets and arrayformula


I have a large set of time-based data and wish to sample the variance for small incremental subsets of the data using a "moving window".

Time Data VAR
03/14 11:16 32.0
03/14 11:22 32.0
03/14 11:28 32.0
03/14 11:33 33.0
03/14 11:39 33.0
03/14 11:44 33.0
03/14 11:50 33.0
03/14 11:56 33.0
03/14 12:01 33.0
03/14 12:07 34.0 0.40
03/14 12:13 34.0 0.49
03/14 12:18 35.0 0.76
03/14 12:24 35.0 0.87
03/14 12:35 35.0 0.82
03/14 12:41 35.0 0.89

Data Sample

I can get the required variance using either row numbers:

VAR(INDIRECT("B"&TEXT(ROW()-9,"0")&":B"&ROW())) //Variance for the previous 10 rows

or times (there being approximately 10 rows of data per hour)

VAR(QUERY(A:B,"select B where A > datetime '"&TEXT(A11-60/(24*60),"yyyy-mm-dd HH:mm:ss")&"' and A <= datetime '"&TEXT(A11,"yyyy-mm-dd HH:mm:ss")&"'",0)) //Variance for data accumulated during the past hour

but I have not been able to wrap an ARRAYFORMULA around either of these formulae which, I believe, is the desired solution.


Solution

  • You can try with BYROW and OFFSET:

    =BYROW(B11:B,LAMBDA(d,VAR(OFFSET(d,-10,0):d)))
    

    enter image description here