Example #1
0
        public string generateNNGraph(string matrixRef, int NumberInputsNodes, int NumberHiddenNodes, int NumberOutputNodes,
                                      int NumberOfEpochs, double LearningRate_eta, string title, int oneCol, string oneLab, int twoCol, string twoLab, string fileName)
        {
            var matrixComp = Value(matrixRef);

            if (matrixComp.GetType() == typeof(string))
            {
                return("Could not find variable " + matrixRef);
            }
            MatrixData _graphData = (MatrixData)matrixComp;

            double[] x  = _graphData.GetColumnCopy <double>(0);
            double[] y  = _graphData.GetColumnCopy <double>(1);
            double[] y1 = _graphData.GetColumnCopy <double>(2);

            double xMax = _graphData.Max(0);

            var plot = new PLStream();

            plot.width(1);
            plot.sdev("svg");
            plot.sfnam(fileName + ".svg");
            plot.scolbg(255, 255, 255);
            plot.init();

            plot.env(0, xMax, 0, 105, AxesScale.Independent, AxisBox.BoxTicksLabelsAxes);

            Dictionary <int, string> cols = new Dictionary <int, string>();

            cols.Add(0, "black");
            cols.Add(1, "red");
            cols.Add(2, "yellow");
            cols.Add(3, "green");
            cols.Add(4, "aquamarine");
            cols.Add(5, "pink");
            cols.Add(6, "wheat");
            cols.Add(7, "grey");
            cols.Add(8, "brown");
            cols.Add(9, "blue");
            cols.Add(10, "BlueViolet");
            cols.Add(11, "cyan");
            cols.Add(12, "turquoise");
            cols.Add(13, "magenta");
            cols.Add(14, "salmon");
            cols.Add(15, "white");


            plot.col0(1);
            plot.lab("Epoch", "Accuracy %", title);
            plot.ptex(xMax - 10, 25, 1.0, 0, 1, "Input Nodes: " + NumberInputsNodes);
            plot.ptex(xMax - 10, 20, 1.0, 0, 1, "Hidden Nodes: " + NumberHiddenNodes);
            plot.ptex(xMax - 10, 15, 1.0, 0, 1, "Output Nodes: " + NumberOutputNodes);
            plot.ptex(xMax - 10, 10, 1.0, 0, 1, "Epochs: " + NumberOfEpochs);
            plot.ptex(xMax - 10, 5, 1.0, 0, 1, "Learning Rate: " + LearningRate_eta);
            plot.col0(1);

            plot.col0(oneCol);
            plot.line(x, y);
            plot.ptex(xMax - 10, 35, 1.0, 0, 1, cols[oneCol] + ": " + oneLab);
            plot.col0(oneCol);

            plot.col0(twoCol);
            plot.line(x, y1);
            plot.ptex(xMax - 10, 30, 1.0, 0, 1, cols[twoCol] + ": " + twoLab);
            plot.col0(twoCol);

            plot.eop();

            return(Directory.GetCurrentDirectory() + "\\" + fileName + ".svg");
        }
        public static void PlotRegressionChart(PlotChartGeneratorModel generationModel)
        {
            using (var pl = new PLStream())
            {
                pl.sdev("pngcairo");
                pl.sfnam(generationModel.ImageName);

                // use white background with black foreground
                pl.spal0("cmap0_alternate.pal");

                // Initialize plplot
                pl.init();

                // set axis limits
                pl.env(generationModel.MinLimitX, generationModel.MaxLimitX, generationModel.MinLimitY, generationModel.MaxLimitY,
                       AxesScale.Independent, AxisBox.CustomXYBoxTicksLabels);

                // Set scaling for mail title text 125% size of default
                //pl.schr(0, 1.25);

                // The main title
                pl.lab(generationModel.LabelX, generationModel.LabelY, generationModel.Title);

                // plot using different colors
                // see http://plplot.sourceforge.net/examples.php?demo=02 for palette indices
                pl.col0(1);

                // This code is the symbol to paint
                char code = (char)9;

                double yTotal       = 0;
                double xTotal       = 0;
                double xyMultiTotal = 0;
                double xSquareTotal = 0;

                var totalNumber = 0;

                generationModel.PointsList.ForEach(pointsList =>
                {
                    double y0    = 0;
                    double x0    = 0;
                    totalNumber += pointsList.Points.Count();

                    // plot using other color
                    pl.col0(pointsList.Color);
                    pointsList.Points.ForEach(point =>
                    {
                        var x = new double[1];
                        var y = new double[1];

                        x[0] = point.X;
                        y[0] = point.Y;

                        if (pointsList.PaintDots)
                        {
                            // Paint a dot
                            pl.poin(x, y, code);
                        }
                        else
                        {
                            if (!generationModel.DrawRegressionLine)
                            {
                                // Draw lines between points
                                pl.join(x0, y0, point.X, point.Y);

                                x0 = point.X;
                                y0 = point.Y;
                            }
                        }


                        xTotal       += point.X;
                        yTotal       += point.Y;
                        xyMultiTotal += point.X * point.Y;
                        xSquareTotal += point.X * point.X;
                    });
                });

                // Regression Line calculation explanation:
                // https://www.khanacademy.org/math/statistics-probability/describing-relationships-quantitative-data/more-on-regression/v/regression-line-example

                if (generationModel.DrawRegressionLine)
                {
                    if (generationModel.RegressionPointsList != null)
                    {
                        var xArray = generationModel.RegressionPointsList.Points.Select(dp => dp.X).ToArray();
                        var yArray = generationModel.RegressionPointsList.Points.Select(dp => dp.Y).ToArray();

                        pl.col0(generationModel.RegressionPointsList.Color);
                        pl.line(xArray, yArray);
                    }
                    else
                    {
                        double minY       = yTotal / totalNumber;
                        double minX       = xTotal / totalNumber;
                        double minXY      = xyMultiTotal / totalNumber;
                        double minXsquare = xSquareTotal / totalNumber;

                        double m  = ((minX * minY) - minXY) / ((minX * minX) - minXsquare);
                        double b  = minY - (m * minX);
                        double x1 = generationModel.MinLimitX;
                        //Function for Y1 in the line
                        double y1 = (m * x1) + b;

                        double x2 = generationModel.MaxLimitX;
                        //Function for Y2 in the line
                        double y2 = (m * x2) + b;

                        var xArray = new double[2];
                        var yArray = new double[2];
                        xArray[0] = x1;
                        yArray[0] = y1;
                        xArray[1] = x2;
                        yArray[1] = y2;

                        pl.col0(4);
                        pl.line(xArray, yArray);
                    }
                }

                if (generationModel.DashedPoint != null)
                {
                    pl.col0(CommonConstants.PPLplotColorGreen);
                    pl.width(2);
                    // Horizontal Line should go from (0,DashedPoint.Y) to (DashedPoint.X, DashedPoint.Y)
                    DrawDashedLined(pl, 0, generationModel.DashedPoint.Y, generationModel.DashedPoint.X, generationModel.DashedPoint.Y);
                    // Vertical Line should go from (DashedPoint.X,0) to (DashedPoint.X, DashedPoint.Y)
                    DrawDashedLined(pl, generationModel.DashedPoint.X, 0, generationModel.DashedPoint.X, generationModel.DashedPoint.Y);
                }

                // end page (writes output to disk)
                pl.eop();

                // output version of PLplot
                pl.gver(out var verText);
            } // the pl object is disposed here

            // Open Chart File In Microsoft Photos App (Or default app, like browser for .svg)
            Console.WriteLine("Showing chart...");
            var    p = new Process();
            string chartFileNamePath = @".\" + generationModel.ImageName;

            p.StartInfo = new ProcessStartInfo(chartFileNamePath)
            {
                UseShellExecute = true
            };
            p.Start();
        }
Example #3
0
        private static void PlotTrades(string name, int totalBars, RangeBarModel[] prices, TradeModel[] orders, bool byTime,
                                       PLStream pl)
        {
            var priceCount     = prices.Length;
            var priceX         = new double[priceCount];
            var priceY         = new double[priceCount];
            var priceTimestamp = new double[priceCount];

            for (var j = 0; j < priceCount; j++)
            {
                var price = prices[j];

                priceTimestamp[j] = price.Timestamp;
                priceX[j]         = byTime ? price.Timestamp : j;
                priceY[j]         = price.CurrentPrice;
            }

            var buys = orders
                       .Where(x => x.Amount > 0)
                       .OrderBy(x => x.Timestamp)
                       .ToArray();
            var sells = orders
                        .Where(x => x.Amount < 0)
                        .OrderBy(x => x.Timestamp)
                        .ToArray();

            var buysCount = buys.Length;
            var buyX      = new double[buysCount];
            var buyY      = new double[buysCount];

            for (var j = 0; j < buysCount; j++)
            {
                var order      = buys[j];
                var priceIndex = Array.IndexOf(priceTimestamp, order.Timestamp);

                buyX[j] = byTime ? order.Timestamp : priceIndex;
                buyY[j] = Math.Abs(order.Price);
            }

            var sellsCount = sells.Length;
            var sellX      = new double[sellsCount];
            var sellY      = new double[sellsCount];

            for (var j = 0; j < sellsCount; j++)
            {
                var order      = sells[j];
                var priceIndex = Array.IndexOf(priceTimestamp, order.Timestamp);

                sellX[j] = byTime ? order.Timestamp : priceIndex;
                sellY[j] = Math.Abs(order.Price);
            }


            // set axis limits
            var xMin = byTime ? prices.First().Timestamp - 10000 : 0;
            var xMax = byTime ? prices.Last().Timestamp + 10000 : prices.Length + 1;
            var yMin = prices.Min(x => x.CurrentPrice);
            var yMax = prices.Max(x => x.CurrentPrice);

            yMin = yMin - (yMin * 0.001);
            yMax = yMax + (yMax * 0.001);

            pl.col0(8);

            // Set scaling for mail title text 125% size of default
            //pl.schr(0, 1.25);

            pl.env(xMin, xMax, yMin, yMax, AxesScale.Independent, AxisBox.BoxTicksLabelsAxes);

            // The main title
            if (byTime)
            {
                pl.lab($"Timestamp (to {prices.LastOrDefault()?.TimestampDate:d})", "Price", name);
            }
            else
            {
                pl.lab($"Bars (first {prices.Length} of {totalBars})", "Price", name);
            }
            //pl.timefmt("%s");

            // plot using different colors
            // see http://plplot.sourceforge.net/examples.php?demo=02 for palette indices

            pl.col0(3);
            pl.width(0.4);

            //pl.poin(buyX, buyY, (char)8);
            for (int i = 0; i < buyX.Length; i++)
            {
                pl.line(new[]
                {
                    buyX[i], buyX[i],
                }, new[]
                {
                    yMin, buyY[i]
                });
            }

            pl.col0(14);
            //pl.poin(sellX, sellY, (char)9);
            for (int j = 0; j < sellX.Length; j++)
            {
                pl.line(new[]
                {
                    sellX[j], sellX[j],
                }, new[]
                {
                    sellY[j], yMax,
                });
            }


            pl.col0(7);
            pl.width(1);
            pl.line(priceX, priceY);
        }
Example #4
0
        public string GenerateGraph()
        {
            double[] x  = _graphData.GetColumnCopy <double>(0);
            double[] y  = _graphData.GetColumnCopy <double>(1);
            double[] y1 = _graphData.GetColumnCopy <double>(2);

            double xMax = _graphData.Max(0);
            //double yMax = _graphData.Max(1);
            //yMax = (_graphData.Max(2) > yMax)? _graphData.Max(2) : yMax;
            //double yMin = _graphData.Min(1);
            //yMin = (_graphData.Min(2) < yMin)? _graphData.Min(2): yMin;

            var plot = new PLStream();

            plot.width(1);
            plot.sdev("svg");
            plot.sfnam("Test.svg");
            plot.scolbg(255, 255, 255);
            plot.init();

            plot.env(0, xMax, 0, 105, AxesScale.Independent, AxisBox.BoxTicksLabelsAxes);

            //####### PLPlot colour guide:
            //0	black (default background)
            //1	red (default foreground)
            //2	yellow
            //3	green
            //4	aquamarine
            //5	pink
            //6	wheat
            //7	grey
            //8	brown
            //9	blue
            //10 BlueViolet
            //11 cyan
            //12 turquoise
            //13 magenta
            //14 salmon
            //15 white


            plot.col0(1);
            if (!(_testingData == null))
            {
                plot.lab("Epoch", "Accuracy %", "Test vs Train Accuracy");
            }
            else
            {
                plot.lab("Epoch", "Accuracy %", "Test vs Val Accuracy");
            }
            plot.ptex(xMax - 10, 25, 1.0, 0, 1, "Input Nodes: " + NumberInputsNodes);
            plot.ptex(xMax - 10, 20, 1.0, 0, 1, "Hidden Nodes: " + NumberHiddenNodes);
            plot.ptex(xMax - 10, 15, 1.0, 0, 1, "Output Nodes: " + NumberOutputNodes);
            plot.ptex(xMax - 10, 10, 1.0, 0, 1, "Epochs: " + NumberOfEpochs);
            plot.ptex(xMax - 10, 5, 1.0, 0, 1, "Learning Rate: " + LearningRate_eta);
            plot.col0(1);

            plot.col0(9);
            plot.line(x, y);
            plot.ptex(xMax - 10, 35, 1.0, 0, 1, "Blue: Train");
            plot.col0(9);

            plot.col0(3);
            plot.line(x, y1);
            if (!(_testingData == null))
            {
                plot.ptex(xMax - 10, 30, 1.0, 0, 1, "Green: Test");
            }
            else
            {
                plot.ptex(xMax - 10, 30, 1.0, 0, 1, "Green: Val");
            }
            plot.col0(3);

            plot.eop();

            return("Saved \"Test.svg\"");
        }