public void Calculate()
        {
            double summary = 0;

            foreach (var pair in pairs)
            {
                var regression = new LinearRegression();
                regression.Compute(pair.DeltaValues.ToDecimal(), synthIndex.ToDecimal());

                pair.Weight = 1 / (1 + Math.Abs(regression.Beta.ToDouble()));

                summary += pair.Weight;
            }

            foreach (var pair in pairs)
            {
                pair.Weight      = pair.Weight / summary;
                pair.TradeVolume = Balance * pair.Weight;
            }

            foreach (var pair in pairs)
            {
                double beta   = pair.Regression.Beta.ToDouble();
                double weight = 1.0 / (1.0 + Math.Abs(beta));

                pair.X.TradeVolume = pair.TradeVolume * (weight * Math.Abs(beta));
                pair.Y.TradeVolume = pair.TradeVolume * weight;
            }
        }
        public void LinearTest()
        {
            decimal[] x = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            decimal[] y = { 8, 6, 10, 6, 10, 13, 9, 11, 15, 17 };

            var lr = new LinearRegression();

            lr.Compute(y, x);

            Assert.AreEqual(0.8141, (double)lr.RValue, 0.0001);
            Assert.AreEqual(0.6628, (double)lr.RSquared, 0.0001);
            Assert.AreEqual(5.1333, (double)lr.Alpha, 0.0001);
            Assert.AreEqual(0.9757, (double)lr.Beta, 0.0001);
        }
        public void ComputeTest()
        {
            double[] x = new double[]
            {
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10
            };

            double[] y = new double[]
            {
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10
            };

            var result = LinearRegression.Compute(x, y);

            Assert.AreEqual(result.Intercept, 0.0);
            Assert.AreEqual(result.Slope, 1.0);
            Assert.AreEqual(result.CorrelationCoefficient, 1.0);
            Assert.AreEqual(result.SquareStandardError, 0.0);
        }
 protected void SetRegression(double[] x, double[] y)
 {
     Regression = new LinearRegression();
     Regression.Compute(x.ToDecimal(), y.ToDecimal());
 }
        public double Evaluate(IChromosome chromosome)
        {
            var genes = chromosome.GetGenes();

            List <Point> getPointList = new List <Point>();

            for (int i = 0; i < genes.Length; i++)
            {
                if ((int)genes[i].Value == 1)
                {
                    getPointList.Add(pointArr[i]);
                }
            }
            if (getPointList.Count <= 1)
            {
                return(double.MinValue);
            }


            double Score = Math.Pow(10, -getPointList.Count); //KS:使得取到的点越多越好

            //Console.Write(""+ Score+" "+getPointList.Count+" ");
            decimal[] x     = new decimal[getPointList.Count];
            decimal[] y     = new decimal[getPointList.Count];
            decimal   diffx = 0;
            decimal   diffy = 0;

            for (int i = 0; i < getPointList.Count; i++)
            {
                x[i] = getPointList[i].X;
                y[i] = getPointList[i].Y;
                //Console.Write(" x:"+x[i]+" y:"+y[i] );
                if (i >= 1)
                {
                    diffx += Math.Abs(x[i] - x[i - 1]);
                    diffy += Math.Abs(y[i] - y[i - 1]);
                }
            }
            //KS:防止斜率为0或者无穷大
            if (diffx <= 1E-5m || diffy <= 1E-5m)
            {
                return(-Score * MinLimitScoreRadio);
            }

            lr = new LinearRegression();

            lr.Compute(y, x);

            var     b     = lr.Alpha;
            var     k     = lr.Beta;
            decimal total = 0;

            for (int i = 0; i < getPointList.Count; i++)
            {
                decimal temp = (getPointList[i].X * k + b) - getPointList[i].Y;
                total += temp * temp;
            }

            /* double RSquared = (double)lr.RSquared;
             * Console.WriteLine(" " + lr.RSquared + " " + lr.RValue);*/
            Score = Score * Math.Max((double)total, MinLimitScoreRadio);

            return(-Score);
        }