openlang

weighted rms wrong in Matlab, only when weights not 1

Fortran and spreadsheet give 0.0300. Matlab gives bigger number and it changes with weights. All ones is fine. cfg.weights looks right. Why?

% config.m
cfg.weights = [1 1 2 2 4 4];

% run_calibration.m
data = load_readings(cfg.data_file);
pred = model_predict(data.t, cfg);
score = weighted_rms(data.v, pred, cfg.weights);

% weighted_rms.m
function s = weighted_rms(obs, pred, w)
res = (obs - pred) .* w;
wss = sum(res.^2);
% mean() in case we pass several channels later
s = sqrt(mean(wss) / sum(w));
end

obs and pred checked separately, both match reference. No error, no warning. In Fortran this is one loop.

The conversation continues 2

Follow the reasoning
  1. Permalink

    Weight is applied twice. Look at this line:

    res = (obs - pred) .* w;

    Then you square res, so numerator gets w^2, but denominator is sum(w) only. With all ones no difference, so it looks fine. Your Fortran loop surely does w(i)*r(i)**2.

    Fix: square first, then weight.

    res = obs - pred;
    s = sqrt(sum(w .* res.^2) / sum(w));

    Check shapes too. If obs is a column and w is a row, .* and - broadcast to a matrix, and no error is shown. Make all three the same orientation (w(:), obs(:), pred(:)).

    mean(wss) of a scalar does nothing. For several channels, do sum(..., 1) per column first, then decide how to average.

    Docs for sum with dimension argument: https://www.mathworks.com/help/matlab/ref/sum.html

Other sides of the same question

  1. Ported a short MATLAB script to R and one result came back as a matrix

    Hi all. I'm porting a short MATLAB script from a colleague (Priyesh, 2023) into R, because the rest of our pipeline is R and…

    Replies 2Latest activity rmatlab
  2. Freezer with zero readings reports status "ok", what am I missing

    My nightly audit script (Rscript, base R) flags freezers whose peak temp is above a limit. Last night U-03's logger was unplu…

    Replies 2Latest activity r
  3. A loop that allocated a column at a time

    Slicing in Julia copies, which I knew and then forgot inside a loop. Take a function that walks the columns of a matrix and d…

    Replies 2Latest activity julia