From 7f5021e3e758a54ef6c58a69f434726579bb95be Mon Sep 17 00:00:00 2001 From: Emre Kartoglu <iemrek@gmail.com> Date: Sat, 4 May 2019 13:27:45 +0100 Subject: [PATCH] Fix RMSE calculation --- AbstractExperiment.m | 2 +- tests/AbstractExperimentTest.m | 14 +++++++++++++- tests/ItemBasedKNNTest.m | 2 +- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/AbstractExperiment.m b/AbstractExperiment.m index 289e984..01acab0 100644 --- a/AbstractExperiment.m +++ b/AbstractExperiment.m @@ -212,7 +212,7 @@ function calculatePredictiveAccuracy(obj) end obj.result.MAE = totalError / predictionCount; - obj.result.RMSE = totalSquaredError / predictionCount; + obj.result.RMSE = sqrt(totalSquaredError / predictionCount); display(obj.result); end diff --git a/tests/AbstractExperimentTest.m b/tests/AbstractExperimentTest.m index 9774ec6..1dc3f98 100644 --- a/tests/AbstractExperimentTest.m +++ b/tests/AbstractExperimentTest.m @@ -32,7 +32,19 @@ function shouldSetDefaultMinAndMaxRatingsCorrectly(testCase) testCase.verifyEqual(exp.maxRating, 5); testCase.verifyEqual(exp.minRating, 1); - end + end + + function shouldCalculateRmseAndMaeCorrectly(testCase) + baseSet = [4 0 3 5; 0 5 4 0; 5 4 2 0; 2 4 0 3; 3 4 5 0]; + testSet = [0 2 0 0; 1 0 0 0; 0 0 0 3; 0 0 1 0; 0 0 0 2]; + knnTest = ItemBasedKNN.createNewWithDatasets(baseSet, testSet); + knnTest.setSimilarityCalculatorTo(Similarity.COSINE); + knnTest.k = 2; + knnTest.calculatePredictiveAccuracy; + + testCase.verifyEqual(knnTest.result.MAE, 1.9250, 'AbsTol', 0.001); + testCase.verifyEqual(knnTest.result.RMSE, 2.1106, 'AbsTol', 0.001); + end end diff --git a/tests/ItemBasedKNNTest.m b/tests/ItemBasedKNNTest.m index 8598377..0ef87a6 100644 --- a/tests/ItemBasedKNNTest.m +++ b/tests/ItemBasedKNNTest.m @@ -54,7 +54,7 @@ function shouldCalculatePredictionCorrectlyUsingCosine(testCase) baseSet = [4 0 3 5; 0 5 4 0; 5 4 2 0; 2 4 0 3; 3 4 5 0]; testSet = zeros(5, 4); knnTest = ItemBasedKNN.createNewWithDatasets(baseSet, testSet); - knnTest.setSimilarityCalculatorTo(Similarity.COSINE) + knnTest.setSimilarityCalculatorTo(Similarity.COSINE); knnTest.k = 2; prediction = knnTest.makePrediction(3, 4);