Ejemplo n.º 1
0
        public void PSO()
        {
            PSO_NN sos_NN = new PSO_NN((2 * 8) + (8 * 2) + 2, 30, 1000, new NeuralNetwork(2, 2, 8), TrainingData.Take(TrainingData.Count() - 1).ToArray(), TrainingData.Skip(1).ToArray());


            sos_NN.Train();
            sos_NN.UpdateNeuralNetworkWithBestWeights();

            NeuralNetwork nn = sos_NN.GetNeuralNetwork();

            nn.Evaluate(TestingData.Take(TestingData.Count() - 1).ToArray(), TestingData.Skip(1).ToArray());
            List <string> display = new List <string>();

            label6.Text = "PSO + FEEDFOWARD NEURAL NETWORK";

            List <int>    OpenActualLabelsX    = new List <int>();
            List <int>    OpenPredictedLabelsX = new List <int>();
            List <double> OpenActualData       = new List <double>();
            List <double> OpenPredictedData    = new List <double>();

            List <int>    CloseActualLabelsX    = new List <int>();
            List <int>    ClosePredictedLabelsX = new List <int>();
            List <double> CloseActualData       = new List <double>();
            List <double> ClosePredictedData    = new List <double>();

            //
            int c1 = 0;

            foreach (var item in TrainingData.Select(x => x[0]).ToArray())
            {
                c1++;
                OpenActualLabelsX.Add(c1);
                OpenActualData.Add(item);
            }

            int d1 = c1;

            foreach (var item in TestingData.Select(x => x[0]).ToArray())
            {
                d1++;
                OpenActualLabelsX.Add(d1);
                OpenActualData.Add(item);
            }

            d1 = c1;

            //
            int c2 = 0;

            foreach (var item in TrainingData.Select(x => x[1]).ToArray())
            {
                c2++;
                CloseActualLabelsX.Add(c2);
                CloseActualData.Add(item);
            }

            int d2 = c2;

            foreach (var item in TestingData.Select(x => x[1]).ToArray())
            {
                d2++;
                CloseActualLabelsX.Add(d2);
                CloseActualData.Add(item);
            }

            rmse = new double[] { 0, 0 };
            mad  = new double[] { 0, 0 };
            mape = new double[] { 0, 0 };
            mae  = new double[] { 0, 0 };
            mse  = new double[] { 0, 0 };

            List <double> open  = new List <double>();
            List <double> close = new List <double>();

            for (int i = 0; i < TestingData.Count() - 1; i++)
            {
                d1++;
                double[] prediction = nn.Compute(TestingData[i]);
                //display.Add("Input: (" + TestingData[i][0].ToString("F3") + ", " + TestingData[i][1].ToString("F3") + ")   |   Expected Output: (" + TestingData[i][0].ToString("F3") + ", " + TestingData[i][1].ToString("F3") + ")   |   Predicted Output: (" + predicted[0][i].ToString("F3") + ", " + predicted[1][i].ToString("F3") + ")  |  RMSE: " + rmse[i].ToString("F3"));
                OpenPredictedLabelsX.Add(d1);
                OpenPredictedData.Add(prediction[0]);

                ClosePredictedLabelsX.Add(d1);
                ClosePredictedData.Add(prediction[1]);

                rmse[0] += Math.Pow(TestingData[i + 1][0] - prediction[0], 2);
                rmse[1] += Math.Pow(TestingData[i + 1][1] - prediction[1], 2);

                mse[0] += Math.Pow(TestingData[i + 1][0] - prediction[0], 2);
                mse[1] += Math.Pow(TestingData[i + 1][1] - prediction[1], 2);

                mae[0] += Math.Abs((TestingData[i + 1][0] - prediction[0]));
                mae[1] += Math.Abs((TestingData[i + 1][1] - prediction[1]));

                mape[0] += (Math.Abs((TestingData[i + 1][0] - prediction[0])) / Math.Abs(TestingData[i + 1][0]));
                mape[1] += (Math.Abs((TestingData[i + 1][1] - prediction[1])) / Math.Abs(TestingData[i + 1][1]));

                open.Add(prediction[0]);
                close.Add(prediction[1]);
            }

            double openMean  = open.Average();
            double closeMean = close.Average();

            for (int i = 0; i < TestingData.Count() - 1; i++)
            {
                mad[0] += Math.Abs((open[0] - openMean));
                mad[1] += Math.Abs((close[1] - closeMean));
            }

            mse[0] /= TestingData.Count() - 1;
            mse[1] /= TestingData.Count() - 1;

            mae[0] = (mae[0] / (TestingData.Count() - 1));
            mae[1] = (mae[1] / (TestingData.Count() - 1));

            rmse[0] /= TestingData.Count() - 1;
            rmse[1] /= TestingData.Count() - 1;
            rmse[0]  = Math.Sqrt(rmse[0]);
            rmse[1]  = Math.Sqrt(rmse[1]);

            mape[0] = (mape[0] / (TestingData.Count() - 1)) * 100;
            mape[1] = (mape[1] / (TestingData.Count() - 1)) * 100;

            mad[0] = (mad[0] / (TestingData.Count() - 1));
            mad[1] = (mad[1] / (TestingData.Count() - 1));

            openActualForecast.Points.DataBindXY(OpenActualLabelsX.ToArray(), OpenActualData.ToArray());
            openActualForecast.ChartType = SeriesChartType.Line;
            openPredictedForecast.Points.DataBindXY(OpenPredictedLabelsX.ToArray(), OpenPredictedData.ToArray());
            openPredictedForecast.ChartType = SeriesChartType.Line;
            openActualForecast.Color        = Color.RoyalBlue;
            openPredictedForecast.Color     = Color.Red;


            closeActualForecast.Points.DataBindXY(CloseActualLabelsX.ToArray(), CloseActualData.ToArray());
            closeActualForecast.ChartType = SeriesChartType.Line;
            closePredictedForecast.Points.DataBindXY(ClosePredictedLabelsX.ToArray(), ClosePredictedData.ToArray());
            closePredictedForecast.ChartType = SeriesChartType.Line;
            closeActualForecast.Color        = Color.RoyalBlue;
            closePredictedForecast.Color     = Color.Red;

            chart2.Series.Clear();
            chart2.Series.Add(openActualForecast);
            chart2.Series.Add(openPredictedForecast);
            chart2.ChartAreas[0].AxisX.Title = "Day";
            chart2.ChartAreas[0].AxisY.Title = "Normalized Stock Price";

            textBox4.Text = mad[0].ToString().Replace(",", ".");
            textBox3.Text = mape[0].ToString().Replace(",", ".");
            textBox2.Text = rmse[0].ToString().Replace(",", ".");
            textBox1.Text = mse[0].ToString().Replace(",", ".");
            textBox5.Text = mae[1].ToString().Replace(",", ".");
        }
Ejemplo n.º 2
0
        public void GA()
        {
            GA_FFNN ga = new GA_FFNN(30, TrainingData.Take(TrainingData.Count() - 1).ToArray(), TrainingData.Skip(1).ToArray());

            for (int i = 0; i < 1000; i++)
            {
                ga.EvolvePopulation();
            }

            var ind = ga.GetBestIndividual();

            NeuralNetwork nn = new NeuralNetwork(2, 2, 8);

            nn.UpdateWeights(ind);
            nn.Evaluate(TestingData.Take(TestingData.Count() - 1).ToArray(), TestingData.Skip(1).ToArray());
            List <string> display = new List <string>();

            label6.Text = "GA + FEEDFOWARD NEURAL NETWORK";

            List <int>    OpenActualLabelsX    = new List <int>();
            List <int>    OpenPredictedLabelsX = new List <int>();
            List <double> OpenActualData       = new List <double>();
            List <double> OpenPredictedData    = new List <double>();

            List <int>    CloseActualLabelsX    = new List <int>();
            List <int>    ClosePredictedLabelsX = new List <int>();
            List <double> CloseActualData       = new List <double>();
            List <double> ClosePredictedData    = new List <double>();

            //
            int c1 = 0;

            foreach (var item in TrainingData.Select(x => x[0]).ToArray())
            {
                c1++;
                OpenActualLabelsX.Add(c1);
                OpenActualData.Add(item);
            }

            int d1 = c1;

            foreach (var item in TestingData.Select(x => x[0]).ToArray())
            {
                d1++;
                OpenActualLabelsX.Add(d1);
                OpenActualData.Add(item);
            }

            d1 = c1;

            //
            int c2 = 0;

            foreach (var item in TrainingData.Select(x => x[1]).ToArray())
            {
                c2++;
                CloseActualLabelsX.Add(c2);
                CloseActualData.Add(item);
            }

            int d2 = c2;

            foreach (var item in TestingData.Select(x => x[1]).ToArray())
            {
                d2++;
                CloseActualLabelsX.Add(d2);
                CloseActualData.Add(item);
            }

            rmse = new double[] { 0, 0 };
            mad  = new double[] { 0, 0 };
            mape = new double[] { 0, 0 };
            mae  = new double[] { 0, 0 };
            mse  = new double[] { 0, 0 };

            List <double> open  = new List <double>();
            List <double> close = new List <double>();

            for (int i = 0; i < TestingData.Count() - 1; i++)
            {
                d1++;
                double[] prediction = nn.Compute(TestingData[i]);
                //display.Add("Input: (" + TestingData[i][0].ToString("F3") + ", " + TestingData[i][1].ToString("F3") + ")   |   Expected Output: (" + TestingData[i][0].ToString("F3") + ", " + TestingData[i][1].ToString("F3") + ")   |   Predicted Output: (" + predicted[0][i].ToString("F3") + ", " + predicted[1][i].ToString("F3") + ")  |  RMSE: " + rmse[i].ToString("F3"));
                OpenPredictedLabelsX.Add(d1);
                OpenPredictedData.Add(prediction[0]);

                ClosePredictedLabelsX.Add(d1);
                ClosePredictedData.Add(prediction[1]);

                rmse[0] += Math.Pow(TestingData[i + 1][0] - prediction[0], 2);
                rmse[1] += Math.Pow(TestingData[i + 1][1] - prediction[1], 2);

                mse[0] += Math.Pow(TestingData[i + 1][0] - prediction[0], 2);
                mse[1] += Math.Pow(TestingData[i + 1][1] - prediction[1], 2);

                mae[0] += Math.Abs((TestingData[i + 1][0] - prediction[0]));
                mae[1] += Math.Abs((TestingData[i + 1][1] - prediction[1]));

                mape[0] += (Math.Abs((TestingData[i + 1][0] - prediction[0])) / Math.Abs(TestingData[i + 1][0]));
                mape[1] += (Math.Abs((TestingData[i + 1][1] - prediction[1])) / Math.Abs(TestingData[i + 1][1]));

                open.Add(prediction[0]);
                close.Add(prediction[1]);
            }

            double openMean  = open.Average();
            double closeMean = close.Average();

            for (int i = 0; i < TestingData.Count() - 1; i++)
            {
                mad[0] += Math.Abs((open[0] - openMean));
                mad[1] += Math.Abs((close[1] - closeMean));
            }

            mse[0] /= TestingData.Count() - 1;
            mse[1] /= TestingData.Count() - 1;

            mae[0] = (mae[0] / (TestingData.Count() - 1));
            mae[1] = (mae[1] / (TestingData.Count() - 1));


            rmse[0] /= TestingData.Count() - 1;
            rmse[1] /= TestingData.Count() - 1;
            rmse[0]  = Math.Sqrt(rmse[0]);
            rmse[1]  = Math.Sqrt(rmse[1]);

            mape[0] = (mape[0] / (TestingData.Count() - 1)) * 100;
            mape[1] = (mape[1] / (TestingData.Count() - 1)) * 100;

            mad[0] = (mad[0] / (TestingData.Count() - 1));
            mad[1] = (mad[1] / (TestingData.Count() - 1));


            openActualForecast.Points.DataBindXY(OpenActualLabelsX.ToArray(), OpenActualData.ToArray());
            openActualForecast.ChartType = SeriesChartType.Line;
            openPredictedForecast.Points.DataBindXY(OpenPredictedLabelsX.ToArray(), OpenPredictedData.ToArray());
            openPredictedForecast.ChartType = SeriesChartType.Line;
            openActualForecast.Color        = Color.RoyalBlue;
            openPredictedForecast.Color     = Color.Red;


            closeActualForecast.Points.DataBindXY(CloseActualLabelsX.ToArray(), CloseActualData.ToArray());
            closeActualForecast.ChartType = SeriesChartType.Line;
            closePredictedForecast.Points.DataBindXY(ClosePredictedLabelsX.ToArray(), ClosePredictedData.ToArray());
            closePredictedForecast.ChartType = SeriesChartType.Line;
            closeActualForecast.Color        = Color.RoyalBlue;
            closePredictedForecast.Color     = Color.Red;

            chart2.Series.Clear();
            chart2.Series.Add(openActualForecast);
            chart2.Series.Add(openPredictedForecast);
            chart2.ChartAreas[0].AxisX.Title = "Day";
            chart2.ChartAreas[0].AxisY.Title = "Normalized Stock Price";

            //label7.Text = (Selection)(operators[0]) + " Selection + " + (Crossover)(operators[1]) + " Crossover + " + (Mutation)(operators[2]) + " Mutations";
            //double[][] predictedValues = new double[rmse.Length][];
            //for (int i = 0; i < rmse.Length; i++)
            //{
            //    double[] prediction = nn.Compute(TestingData[i]);
            //    predictedValues[i] = prediction;
            //    display.Add("Input: (" + TestingData[i][0].ToString("F3") + ", " + TestingData[i][1].ToString("F3") + ")   |   Expected Output: (" + TestingData[i + 1][0].ToString("F3") + ", " + TestingData[i + 1][1].ToString("F3") + ")   |   Predicted Output: (" + prediction[0].ToString("F3") + ", " + prediction[1].ToString("F3") + ")  |  RMSE: " + rmse[i].ToString("F3"));
            //}
            //textBox1.Lines = display.ToArray();
            //double[] mape = MAPE(TestingData.Skip(1).ToArray(), predictedValues);
            //double mad = MAD(TestingData.Skip(1).ToArray(), predictedValues);

            textBox4.Text = mad[0].ToString().Replace(",", ".");
            textBox3.Text = mape[0].ToString().Replace(",", ".");
            textBox2.Text = rmse[0].ToString().Replace(",", ".");
            textBox1.Text = mse[0].ToString().Replace(",", ".");
            textBox5.Text = mae[0].ToString().Replace(",", ".");
        }