I need a function in javascript to calculate MAPE. (Mean absolute percentage error) I do not want to depend on an external library and just want a vanilla javascript version of the solution.
there are quite some sample solutions in other programming language, and it would be great if someone can convert it to javascript for the use in javascript.
Formula for MAPE:
where f_i is the forecast value and a_i is the actual value.
NOTE: that i want to achieve this w/o the use of a library, since there is one library available out there, but i feel this can be achieved without the use of a library.
Some examples of MAPE in other programming languages:
If you can write out the code in javascript, that will really help me out, since i need to use this formula.
function MAPE(f, a) {
n = a.length
if (f.length != n) throw new Error(‘f must have the same length as a!’)
sum = 0
for (i = 0; i < n; ++i) sum += Math.abs((a[i] - f[i])/a[i])
return (100/n)*sum
}