示例#1
0
    public void learnNormal(Timeseries ts)
    {
        anomaly_detection_util             adu   = new anomaly_detection_util();
        Dictionary <string, List <float> > tsMap = ts.table;
        List <string> tsFeaturesVector           = ts.features;
        int           featuresVectorSize         = tsFeaturesVector.Count();
        int           valueVectorSize            = tsMap[tsFeaturesVector[0]].Count();

        for (int i = 0; i < featuresVectorSize; ++i)
        {
            string             fiName     = tsFeaturesVector[i];
            float[]            fiData     = tsMap[fiName].ToArray();
            float              maxPearson = 0;
            correlatedFeatures currentCF  = new correlatedFeatures();
            currentCF.feature1 = fiName;
            for (int j = i + 1; j < featuresVectorSize; ++j)
            {
                string  fjName         = tsFeaturesVector[j];
                float[] fjData         = tsMap[fjName].ToArray();
                float   currentPearson = Math.Abs(adu.pearson(fiData, fjData, valueVectorSize));
                if (currentPearson > maxPearson)
                {
                    maxPearson         = currentPearson;
                    currentCF.feature2 = fjName;
                }
            }
            if (maxPearson > this.threshold)
            {
                currentCF.corrlation = maxPearson;
                Point[] points = new Point[valueVectorSize];
                for (int j = 0; j < valueVectorSize; ++j)
                {
                    points[j] = new Point(tsMap[currentCF.feature1][j], tsMap[currentCF.feature2][j]);
                }
                currentCF.lin_reg   = adu.linear_reg(points, valueVectorSize);
                currentCF.threshold = 0;
                for (int j = 0; j < valueVectorSize; ++j)
                {
                    currentCF.threshold = Math.Max(currentCF.threshold, Math.Abs(adu.dev(points[j], currentCF.lin_reg)));
                }
                currentCF.threshold *= (float)1.1;
                cf.Add(currentCF);
            }
        }
    }
        private void initRegressionLinesList()
        {
            for (int i = 0; i < xmlDict.Count; i++)
            {
                regressionLinesDict[i] = new List <DataPoint>();
            }

            List <DataPoint> tempList = new List <DataPoint>();

            for (int i = 0; i < this.csvLinesNumber; i++)
            {
                tempList.Add(new DataPoint(0, 0));
            }

            for (int i = 0; i < xmlDict.Count; i++)
            {
                if (findCorrelativeAttribute(xmlDict[i]) == "")
                {
                    regressionLinesPointsDict[i] = tempList;
                    continue;
                }

                // for each attribute we need to find two points of the line.
                List <DataPoint> currentList = new List <DataPoint>();

                List <DataPoint> currentRegressionLinesPointsList =
                    regressionLinesPointsDict[findIndexByAttribute(xmlDict[i])];

                AnomalyDetector.Line    pointsLine;
                anomaly_detection_util  adu        = new anomaly_detection_util();
                AnomalyDetector.Point[] pointsList = new AnomalyDetector.Point[csvLinesNumber];

                List <double> xs = new List <double>();

                for (int j = 0; j < csvLinesNumber; j++)
                {
                    AnomalyDetector.Point point
                        = new AnomalyDetector.Point(
                              (float)currentRegressionLinesPointsList[j].X, (float)currentRegressionLinesPointsList[j].Y);
                    pointsList[j] = point;
                    xs.Add(currentRegressionLinesPointsList[j].X);
                }

                double minX = double.MaxValue;
                double maxX = double.MinValue;
                foreach (double x in xs)
                {
                    if (x < minX)
                    {
                        minX = x;
                    }

                    if (x > maxX)
                    {
                        maxX = x;
                    }
                }


                pointsLine = adu.linear_reg(pointsList, csvLinesNumber);

                currentList.Add(new DataPoint(minX, minX * pointsLine.a + pointsLine.b));

                currentList.Add(new DataPoint(maxX, maxX * pointsLine.a + pointsLine.b));

                regressionLinesDict[i] = currentList;
            }
        }