Beispiel #1
0
    def handleRecord(self, inputData):
        """Returns a tuple (anomalyScore).
    The anomalyScore is the tail probability of the gaussian (normal) distribution
    over a sliding window of inputData values. The tail probability is based on the
    Q-function. The windowSize has been tuned to give best performance on NAB.
    """

        anomalyScore = 0.0
        inputValue = inputData["value"]
        if len(self.windowData) > 0:
            anomalyScore = 1 - anomaly_likelihood.normalProbability(
                inputValue, {
                    "mean": self.mean,
                    "stdev": self.std
                })

        if len(self.windowData) < self.windowSize:
            self.windowData.append(inputValue)
            self._updateWindow()
        else:
            self.stepBuffer.append(inputValue)
            if len(self.stepBuffer) == self.stepSize:
                # slide window forward by stepSize
                self.windowData = self.windowData[self.stepSize:]
                self.windowData.extend(self.stepBuffer)
                # reset stepBuffer
                self.stepBuffer = []
                self._updateWindow()

        return (anomalyScore, )
  def handleRecord(self, inputData):
    """Returns a tuple (anomalyScore).
    The anomalyScore is the tail probability of the gaussian (normal) distribution
    over a sliding window of inputData values. The tail probability is based on the
    Q-function. The windowSize has been tuned to give best performance on NAB.
    """

    anomalyScore = 0.0
    inputValue = inputData["value"]
    if len(self.windowData) > 0:
      anomalyScore = 1 - anomaly_likelihood.normalProbability(
        inputValue, {"mean": self.mean,"stdev": self.std})

    if len(self.windowData) < self.windowSize:
      self.windowData.append(inputValue)
      self._updateWindow()
    else:
      self.stepBuffer.append(inputValue)
      if len(self.stepBuffer) == self.stepSize:
        # slide window forward by stepSize
        self.windowData = self.windowData[self.stepSize:]
        self.windowData.extend(self.stepBuffer)
        # reset stepBuffer
        self.stepBuffer = []
        self._updateWindow()

    return (anomalyScore, )
    def testNormalProbability(self):
        """
    Test that the normalProbability function returns correct normal values
    """
        # Test a standard normal distribution
        # Values taken from http://en.wikipedia.org/wiki/Standard_normal_table
        p = {"name": "normal", "mean": 0.0, "variance": 1.0, "stdev": 1.0}
        self.assertWithinEpsilon(an.normalProbability(0.0, p), 0.5)
        self.assertWithinEpsilon(an.normalProbability(0.3, p), 0.3820885780)
        self.assertWithinEpsilon(an.normalProbability(1.0, p), 0.1587)
        self.assertWithinEpsilon(1.0 - an.normalProbability(1.0, p),
                                 an.normalProbability(-1.0, p))
        self.assertWithinEpsilon(an.normalProbability(-0.3, p),
                                 1.0 - an.normalProbability(0.3, p))

        # Non standard normal distribution
        p = {"name": "normal", "mean": 1.0, "variance": 4.0, "stdev": 2.0}
        self.assertWithinEpsilon(an.normalProbability(1.0, p), 0.5)
        self.assertWithinEpsilon(an.normalProbability(2.0, p), 0.3085)
        self.assertWithinEpsilon(an.normalProbability(3.0, p), 0.1587)
        self.assertWithinEpsilon(an.normalProbability(3.0, p),
                                 1.0 - an.normalProbability(-1.0, p))
        self.assertWithinEpsilon(an.normalProbability(0.0, p),
                                 1.0 - an.normalProbability(2.0, p))

        # Non standard normal distribution
        p = {
            "name": "normal",
            "mean": -2.0,
            "variance": 0.5,
            "stdev": math.sqrt(0.5)
        }
        self.assertWithinEpsilon(an.normalProbability(-2.0, p), 0.5)
        self.assertWithinEpsilon(an.normalProbability(-1.5, p), 0.241963652)
        self.assertWithinEpsilon(an.normalProbability(-2.5, p),
                                 1.0 - an.normalProbability(-1.5, p))
  def testNormalProbability(self):
    """
    Test that the normalProbability function returns correct normal values
    """
    # Test a standard normal distribution
    # Values taken from http://en.wikipedia.org/wiki/Standard_normal_table
    p = {"name": "normal", "mean": 0.0, "variance": 1.0, "stdev": 1.0}
    self.assertWithinEpsilon(an.normalProbability(0.0, p), 0.5)
    self.assertWithinEpsilon(an.normalProbability(0.3, p), 0.3820885780)
    self.assertWithinEpsilon(an.normalProbability(1.0, p), 0.1587)
    self.assertWithinEpsilon(1.0 - an.normalProbability(1.0, p),
                             an.normalProbability(-1.0, p))
    self.assertWithinEpsilon(an.normalProbability(-0.3, p),
                             1.0 - an.normalProbability(0.3, p))

    # Non standard normal distribution
    p = {"name": "normal", "mean": 1.0, "variance": 4.0, "stdev": 2.0}
    self.assertWithinEpsilon(an.normalProbability(1.0, p), 0.5)
    self.assertWithinEpsilon(an.normalProbability(2.0, p), 0.3085)
    self.assertWithinEpsilon(an.normalProbability(3.0, p), 0.1587)
    self.assertWithinEpsilon(an.normalProbability(3.0, p),
                             1.0 - an.normalProbability(-1.0, p))
    self.assertWithinEpsilon(an.normalProbability(0.0, p),
                             1.0 - an.normalProbability(2.0, p))

    # Non standard normal distribution
    p = {"name": "normal", "mean": -2.0, "variance": 0.5,
         "stdev": math.sqrt(0.5)}
    self.assertWithinEpsilon(an.normalProbability(-2.0, p), 0.5)
    self.assertWithinEpsilon(an.normalProbability(-1.5, p), 0.241963652)
    self.assertWithinEpsilon(an.normalProbability(-2.5, p),
                             1.0 - an.normalProbability(-1.5, p))