@beenotung/tslib
Version:
utils library in Typescript
54 lines (53 loc) • 1.34 kB
JavaScript
;
/**
* calculate mean square error
* */
Object.defineProperty(exports, "__esModule", { value: true });
exports.mse_2arr = mse_2arr;
exports.mse_2d = mse_2d;
exports.mse_object = mse_object;
exports.mse_by = mse_by;
function mse_2arr(inputs, outputs) {
if (inputs.length !== outputs.length) {
throw new Error('length mismatch');
}
const n = inputs.length;
let acc = 0;
for (let i = 0; i < n; i++) {
const e = inputs[i] - outputs[i];
acc += e * e;
}
return acc / n;
}
function mse_2d(input_outputs) {
const n = input_outputs.length;
let acc = 0;
for (let i = 0; i < n; i++) {
const input_output = input_outputs[i];
const e = input_output[0] - input_output[1];
acc += e * e;
}
return acc / n;
}
function mse_object(data, options) {
const { input, output } = options;
const n = data.length;
let acc = 0;
for (let i = 0; i < n; i++) {
const o = data[i];
const e = o[input] - o[output];
acc += e * e;
}
return acc / n;
}
function mse_by(data, options) {
const { input, output } = options;
const n = data.length;
let acc = 0;
for (let i = 0; i < n; i++) {
const o = data[i];
const e = input(o) - output(o);
acc += e * e;
}
return acc / n;
}