Skip to content

Commit bf56421

Browse files
committed
Prevent EWMA decimals from blowing out [#13]
1 parent 193677e commit bf56421

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

sql/02-spc-intermediates-schema.sql

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -403,20 +403,21 @@ create function spc_intermediates.ewma_step(
403403
, measurement decimal
404404
, weighting decimal
405405
, target_mean decimal
406+
, scale integer
406407
) returns decimal immutable language plpgsql as
407408
$$
408409
begin
409410
if last_avg is null then
410-
return weighting * measurement + (1.0 - weighting) * target_mean;
411+
return trunc((weighting * measurement + (1.0 - weighting) * target_mean), scale);
411412
else
412-
return weighting * measurement + (1.0 - weighting) * last_avg;
413+
return trunc((weighting * measurement + (1.0 - weighting) * last_avg), scale);
413414
end if;
414415
end;
415416
$$;
416417

417418
-- The `ewma` aggregate is what wraps up `ewma_step` into an iterative loop. This means it can be used as an aggregate
418419
-- in the same way as inbuilt aggregates like `sum` or `avg`.
419-
create aggregate spc_intermediates.ewma(measurement decimal, weighting decimal, target_mean decimal) (
420+
create aggregate spc_intermediates.ewma(measurement decimal, weighting decimal, target_mean decimal, scale integer) (
420421
sfunc = spc_intermediates.ewma_step,
421422
stype = decimal
422423
);
@@ -460,7 +461,8 @@ create function spc_intermediates.ewma_individual_measurements(
460461
p_weighting decimal,
461462
p_limits_width decimal,
462463
p_target_mean decimal default null,
463-
p_target_std_dev decimal default null
464+
p_target_std_dev decimal default null,
465+
scale integer default 8
464466
) returns table (
465467
window_id bigint,
466468
sample_id bigint,
@@ -489,7 +491,7 @@ begin
489491
, wms.period
490492
, wms.performed_at
491493
, wms.measured_value
492-
, spc_intermediates.ewma(wms.measured_value, p_weighting, coalesce(p_target_mean, mean_measured_value))
494+
, spc_intermediates.ewma(wms.measured_value, p_weighting, coalesce(p_target_mean, mean_measured_value), scale)
493495
over (partition by wms.window_id order by wms.measurement_id) as ewma
494496
, coalesce(p_target_mean, mean_measured_value) + (p_limits_width * coalesce(p_target_std_dev, std_dev_measured_value)) *
495497
sqrt(((p_weighting / (2 - p_weighting)) *

0 commit comments

Comments
 (0)