tests/testthat/test_anomalylikelihood_scorer.R

library(otsad)
context("Anomaly Likelihood Scorer")

test_that("AnomalyLikelihoodScorer gives the correct result", {

  a <- c(-0.590, -0.157, 0.052, 1.027, -0.274, 0.357, 1.464, 1.797, 0.546, 0.749, 0.028, 1.449,
         -0.008, 2.520, -0.780, -1.180, -0.985, 1.919, 1.678, 1.046, -0.517, 0.375, 0.832, -0.376,
         -0.709, 0.046, 0.771, 0.286, 0.343, -0.378, 1.321, 1.404, 1.054, -0.662, -0.357, -0.283,
         -0.586, 2.577, 0.262, 0.208, -0.115, -1.387, -0.016, 1.649, -0.613, -0.362, -0.439, 0.202,
         -2.671, -0.334, 0.653, -0.248, -0.149, -0.808, 0.216, -0.319, -0.042, -3.356, -0.151,
         1.901, 0.625, 0.592, -0.777, 1.742, -2.011, 0.957, 1.002, -1.311, 0.255, 1.043, 0.369,
         1.177, 1.023, 0.118, 0.861, 1.176, 2.120, 1.332, -0.720, -0.052, -0.636, 1.173, -0.278,
         0.037, 1.753, -0.493, -0.124, -0.064, -1.242, -0.196, 0.297, 0.471, 1.234, 1.544, 1.918,
         -0.716, 0.665, 1.496, -0.491, -0.816)

  res <- c(0.500, 0.500, 0.500, 0.500, 0.500, 0.500, 0.500, 0.500, 0.500, 0.500, 0.500, 0.500,
           0.500, 0.500, 0.500, 0.500, 0.500, 0.500, 0.500, 0.500, 0.783, 0.936, 0.828, 0.998,
           0.998, 0.980, 0.798, 0.975, 0.998, 1.000, 0.750, 0.589, 0.551, 0.600, 0.540, 0.596,
           0.799, 0.565, 0.551, 0.650, 0.765, 0.674, 0.816, 0.528, 0.514, 0.528, 0.503, 0.843,
           0.987, 0.993, 0.932, 0.895, 0.900, 0.963, 0.947, 0.946, 0.937, 0.988, 0.959, 0.900,
           0.910, 0.820, 0.892, 0.527, 0.858, 0.662, 0.546, 0.877, 0.914, 0.823, 0.723, 0.792,
           0.933, 0.811, 0.976, 0.980, 0.993, 1.000, 0.999, 0.997, 0.800, 0.799, 0.636, 0.625,
           0.743, 0.510, 0.795, 0.911, 0.939, 0.945, 0.945, 0.967, 0.908, 0.795, 0.779, 0.801,
           0.719, 0.524, 0.576, 0.506)

  scorer <- AnomalyLikelihoodScorer$new(
     learningPeriod = 10,
     estimationSamples = 10,
     historicWindowSize = 20,
     reestimationPeriod = 10
  )

  scores <- round(sapply(a, function(x) {scorer$computeScore(x, x)}), 3)

  expect_equal(scores, res)

})
alaineiturria/otsad documentation built on Jan. 12, 2023, 12:26 p.m.