Пример #1
0
            ///<summary>
            /// VIDYA
            ///</summary>
            ///<param name="pNav">Navigator</param>
            ///<param name="pSource">Field Source</param>
            ///<param name="periods">Periods</param>
            ///<param name="R2Scale">R2 Scale</param>
            ///<param name="Alias">Alias</param>
            ///<returns>Recordset</returns>
            public Recordset VIDYA(Navigator pNav, Field pSource, int periods, double R2Scale, string Alias)
            {
                int Record;
                LinearRegression LR = new LinearRegression();

                int RecordCount = pNav.RecordCount;

                Field Field1 = new Field(RecordCount, Alias);

                Recordset Results = LR.Regression(pNav, pSource, periods);

                const int Start = 2;

                pNav.Position = Start;
                for (Record = Start; Record < RecordCount + 1; Record++)
                {
                    pNav.MovePrevious();
                    double PreviousValue = pSource.ValueEx(pNav.Position);
                    pNav.MoveNext();
                    double R2Scaled = Results.ValueEx("RSquared", pNav.Position) * R2Scale;
                    Field1.Value(pNav.Position, R2Scaled *
                                 pSource.Value(pNav.Position) + (1 - R2Scaled) * PreviousValue);
                    pNav.MoveNext();
                }//Record

                Results.AddField(Field1);
                return(Results);
            }
Пример #2
0
        private void PlotLinearRegression(List <GPoint> points)
        {
            var ls = new OxyPlot.Series.LineSeries()
            {
                MarkerType   = ShowMarker ? MarkerType.None : MarkerType.Plus,
                MarkerStroke = OxyPlot.OxyColors.Blue
            };

            LinearFunction func;

            if (LinearRegression.Regression(points.ToArray(), out func))
            {
                int    TOTAL = 10;
                double x     = points.First().X;
                double stepX = (points.Last().X - x) / TOTAL;
                for (int i = 0; i <= TOTAL; i++)
                {
                    double y = func.Slope * x + func.Intercept;
                    ls.Points.Add(new OxyPlot.DataPoint(x, y));
                    x += stepX;
                }
                ls.Title = $"Linear Regression(R: {func.CorrelationCoeff:f5})";
            }
            else
            {
                ls.Title = "Linear Regression(Failed)";
            }

            base.Series.Add(ls);
        }
Пример #3
0
        /// <summary>
        /// Action to be executd for calculating indicator
        /// </summary>
        /// <returns>for future usage. Must be ignored at this time.</returns>
        protected override bool TrueAction()
        {
// Validate
            int iSize = _chartPanel._chartX.RecordCount;

            if (iSize == 0)
            {
                return(false);
            }

            int paramInt1 = ParamInt(1);

            if (paramInt1 < 2 || paramInt1 > iSize / 2)
            {
                ProcessError("Invalid Periods (min 2) for indicator " + FullName, IndicatorErrorType.ShowErrorMessage);
                return(false);
            }


            // Get the data
            string paramStr0 = ParamStr(0);
            Field  pSource   = SeriesToField("Source", paramStr0, iSize);

            if (!EnsureField(pSource, paramStr0))
            {
                return(false);
            }

            Navigator pNav = new Navigator();
            Recordset pRS  = new Recordset();

            pRS.AddField(pSource);

            pNav.Recordset_ = pRS;


            // Calculate the indicator
            LinearRegression ta   = new LinearRegression();
            Recordset        pInd = ta.Regression(pNav, pSource, paramInt1);


            // Output the indicator values
            Clear();
            for (int n = 0; n < iSize; ++n)
            {
                AppendValue(DM.TS(n), n < paramInt1 ? null : pInd.Value("RSquared", n + 1));
            }

            return(_calculateResult = PostCalculate());
        }
Пример #4
0
        public double Learn(Vector x, Vector y)
        {
            //

            Random();
            aver = y.AverageValue();
            var y_ = new Vector(y.Length);

            {
                y_ = y - aver; disp = Math.Sqrt(y_.Dispersion()); y_ /= disp;
            }

            if (y_.IsNaN())
            {
                return(double.NaN);
            }

            double m = 0.001;

            {
                var lin = linear.Regression(x, y_); k = linear.k; b = linear.b;
                // y_ = y_ - lin;
            }
            //Calcf(x, y_);
            for (int ep = 0; ep < 50; ep++)
            {
                for (int i = 0; i < 2 * x.Length; i += 1)
                {
                    int ind = (int)(matlib.random.NextDouble() * x.Length);
                    Train(x[ind], y_[ind], m, m, m);
                }

                if (m > 5e-5)
                {
                    m /= step;
                }
            }
            double err = 0.0;

            for (int i = 0; i < x.Length; i++)
            {
                err += Math.Abs(G(x[i]) - y_[i]);
            }
            err /= (double)x.Length;
            return(err);
        }
Пример #5
0
            ///<summary>
            /// Time Series Moving Average
            ///</summary>
            ///<param name="pNav">Navigator</param>
            ///<param name="pSource">Field Source</param>
            ///<param name="periods">Periods</param>
            ///<param name="Alias">Alias</param>
            ///<returns>Recordset</returns>
            public Recordset TimeSeriesMovingAverage(Navigator pNav, Field pSource, int periods, string Alias)
            {
                LinearRegression LR = new LinearRegression();
                int Record;

                int RecordCount = pNav.RecordCount;

                Field Field1 = new Field(RecordCount, Alias);

                Recordset Results = LR.Regression(pNav, pSource, periods);

                pNav.MoveFirst();
                for (Record = pNav.Position; Record < RecordCount + 1; Record++)
                {
                    double Value = Results.ValueEx("Forecast", pNav.Position);
                    Field1.Value(pNav.Position, Value);
                    pNav.MoveNext();
                }

                pNav.MoveFirst();
                Results.AddField(Field1);

                return(Results);
            }