Beispiel #1
0
        public Task <byte[]> Execute(byte[] data)
        {
            // prep the data
            Int32 questionId = BitConverter.ToInt32(data, 0);

            Console.WriteLine("Node task created with parameter: " + questionId);

            return(Task.Factory.StartNew(() =>
            {
                // To prevent any x-threaded issues
                Int32 q = questionId;

                Random rnd = new Random(q);

                // prep the regres algorithm
                LinearRegression regres = new LinearRegression();

                double[] y;
                double[,] x;
                double[] w;     // weighting

                using (SqlConnection conn = new SqlConnection(@"Data Source=kivu;Initial Catalog=StackoverflowAug2012;uid=sblackler;pwd=password"))
                {
                    conn.Open();

                    var results = conn.Query <PostResult>(@"SELECT TOP 8000 id, PostScore, ViewCount FROM Posts WHERE AcceptedAnswerId IS NULL AND AnswerCount < 10 AND PostScore > (SELECT TOP 1 PostScore FROM Posts WHERE ID = @Q) ORDER BY PostScore DESC", new { Q = rnd.Next(0, 11750761) }).ToList();

                    y = results.Select(p => p.ID).ToArray();

                    Int32 i = 0;
                    x = new double[results.Count, results.Count];
                    foreach (var el in results)
                    {
                        for (Int32 j = 1; j < results.Count; j++)
                        {
                            x[i, j - 1] = (el.PostScore < 1 ? 1 : el.PostScore / el.ViewCount < 1 ? 1 : el.ViewCount) * j / 100;
                        }
                    }
                }

                w = new double[Math.Max(y.Length, x.Length)];

                // Blank the array with the value 1
                Extensions.MemSet(w, 1);
                try
                {
                    if (regres.Regress(y, x, w))
                    {
                        return BitConverter.GetBytes((int)regres.SEC[0]);
                    }
                }
                catch { return BitConverter.GetBytes(-1); }
                // In case the regression fails.
                return BitConverter.GetBytes(rnd.Next(0, 11750761));
            }));
        }
Beispiel #2
0
        public List <double> Remove(List <double> Data, ref Stack <ITransformation> Transforms)
        {
            if (Transforms == null)
            {
                Transforms = new Stack <ITransformation>();
            }

            double[] y = new double[Data.Count];
            double[,] x = new double[2, Data.Count];
            double[] w = new double[Data.Count];

            for (int j = 0; j < Data.Count; j++)
            {
                y[j]    = GetY(Data[j]);
                x[0, j] = 1;    // constant term
                x[1, j] = GetX(j + 1);
                w[j]    = 1.0;
            }

            double a = Double.MinValue;
            double b = Double.MinValue;

            LinearRegression lr = new LinearRegression();

            if (lr.Regress(y, x, w))
            {
                a = lr.Coefficients[1];
                b = lr.Coefficients[0];
            }

            List <double> detrendedData = (List <double>)Utilities.DeepClone(Data);

            double lastX = 0.0;

            for (int i = 0; i < Data.Count; i++)
            {
                double regression = GetRegression(a, b, (i + 1));
                detrendedData[i] -= regression;
                lastX             = (i + 1);
            }

            Transforms.Push(GetTransform(a, b, lastX));

            return(detrendedData);
        }