Advanced Octave A08 — Block reads and incremental statistics
Advanced GNU Octave with GX-TXT · Module 02
A series can be too big to be read all together. Calculate a block summary limits the required memory and allows you to process the samples progressively, provided the intervals do not overlap and do not skip rows.
Objectives and data
We use the octave_advanced_sensor Synthetic Dataset created in the lesson A07 : 11 volt voltage samples. We already use the basic readings of the Course Octave. We will calculate average and sample variance with incremental updates, then compare them with mean and var of Octave.
The test uses GX-TXT dev112-P011-bis and GNU Octave 9.4.0. For a different Dataset draws the length from its descriptor, check that the variable is the desired one and manage the cases with less than two observations: here we fix count = 11 because we know the didactic series.
Read and update
Import statistiche_blocchi.m to the Session used for A07, register it and run it with the Runner.
The central reading is:
for start = 0:block_size:count-1
take = min(block_size, count-start);
batch = gxtxt_api_call('CANONICAL_DATASET_API_v2', 'read_range', ...
{id, {'voltage_v'}, start, take});
values = batch.voltage_v.values(:);
assert(numel(values) == take);
for k = 1:numel(values)
n += 1;
delta = values(k) - mean_value;
mean_value += delta / n;
m2 += delta * (values(k) - mean_value);
endfor
endfor
variance = m2 / (n-1);
read_range uses an initial zero offset and the number of rows required. In this test the blocks start from 0, 4 and 8; the last one contains only three lines. The carrier is explicitly transformed into a column with (:).
For each value, the average is updated with delta/n; m2 accumulates the sum of square waste incrementally. Splitting for n-1 we obtain the sample variance, defined here only for at least two values. The complete file also performs an integral reading for the didactic comparison: in a large processing the verification should not vanify the saving of memory.
In the test the average is 1,825 V, the 0,171875 V² sample variance, and the comparison with the Octave functions returns 1, using 10⁻¹² tolerance. Tolerance does not imply exact identity of the representations in mobile comma.
Limit cases
Always check the size actually received, as assert does, and predict explicit behavior for Dataset blanks, only one value, missing data and unfinished values. If scientific policy plans to exclude observations, it records the criterion and the excluded number: do not remove them silently. Do not confuse the sample variance with that of the population, which uses a different divider.
Exercise
- With four-line blocks, how many rows does the last block of this series contain?
- Why is the variable divider here
n-1? - What limit does the final comparison have on a very large series?
Worked solution
- Three: the first two blocks cover eight of the eleven rows.
- Because the example calculates the sample variance; with less than two samples the calculation should be treated separately.
- It rereads the whole series in memory: it is a useful control on small data, not a general scalable strategy.
Other steps of this project
- Advanced Octave A75 — Dynamical systems in phase space
- Advanced Octave A81 — Geography of the waves and a 3D surface
- Advanced Octave A82 — Age, sex and demographic inequalities
- Advanced Octave A83 — All-cause mortality, certified COVID deaths and surveillance
- Advanced Octave A84 — A reproducible public account
- Advanced Octave A73 — Optimization landscapes and 3D trajectories