Skip to content

Commit c02fc1f

Browse files
RealPehaitsvinayak
andauthored
Added Mean Square Error (#417)
* Added Mean Square Error * Update MeanSquareError.js Co-authored-by: vinayak <itssvinayak@gmail.com>
1 parent a8fe5e7 commit c02fc1f

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Maths/MeanSquareError.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Wikipedia: https://en.wikipedia.org/wiki/Mean_squared_error
2+
3+
const meanSquaredError = (predicted, expected) => {
4+
if (!Array.isArray(predicted) || !Array.isArray(expected)) {
5+
throw new TypeError('Argument must be an Array')
6+
}
7+
8+
if (predicted.length !== expected.length) {
9+
throw new TypeError('The two lists must be of equal length')
10+
}
11+
12+
let err = 0
13+
14+
for (let i = 0; i < expected.length; i++) {
15+
err += (expected[i] - predicted[i]) ** 2
16+
}
17+
18+
return err / expected.length
19+
}
20+
21+
// testing
22+
(() => {
23+
console.log(meanSquaredError([1, 2, 3, 4], [1, 2, 3, 4]) === 0)
24+
console.log(meanSquaredError([4, 3, 2, 1], [1, 2, 3, 4]) === 5)
25+
console.log(meanSquaredError([2, 0, 2, 0], [0, 0, 0, 0]) === 3)
26+
})()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy