示例#1
0
        private void ShowResults()
        {
            if (rbBestNetwork.Checked)
            {
                mSelectedActivationNetwork = mBestActivationNetwork;
            }
            if (rbBestTestNetwork.Checked)
            {
                mSelectedActivationNetwork = mBestTestActivationNetwork;
            }
            if (rbGenetic.Checked)
            {
                mSelectedActivationNetwork = mBestGeneticActivationNetwork;
            }

            try
            {
                TVec dy = null;
                TVec dx = null;
                fittedChart.Series.Clear();
                fittedChart.Series.Add(new Steema.TeeChart.Styles.FastLine());
                fittedChart.Series.Add(new Steema.TeeChart.Styles.FastLine());
                fittedChart.Series[0].Title = "наблюдения";
                fittedChart.Series[1].Title = "прогнози";

                fittedChart.Axes.Left.Title.Text = "Y";
                fittedChart.Axes.Bottom.Title.Text = "";
                fittedChart.Axes.Left.Automatic = true;

                Vector timeseries = new Vector((TestEnd-ExtractBegin) + 1);
                Vector abs = new Vector((TestEnd - ExtractBegin) + 1);

                int? dependent_variable_id = 0;
                foreach (int? dep in this.DependentVariables.Keys)
                {
                    dependent_variable_id = dep;
                }

                MainDataSetTableAdapters.VARIABLESTableAdapter adptVariables = new Plig.TimeSeries.Client.MainDataSetTableAdapters.VARIABLESTableAdapter();
                mindep = adptVariables.MinObservationValue(ExtractBegin, TestEnd, dependent_variable_id.Value).Value;
                factordep = 1.7 / (adptVariables.MaxObservationValue(ExtractBegin, TestEnd, dependent_variable_id.Value).Value - adptVariables.MinObservationValue(ExtractBegin, TestEnd, dependent_variable_id.Value).Value);

                int cnt = ExtractBegin;
                foreach (KeyValuePair<int, double?> d in this.VariableObservations(dependent_variable_id.Value))
                {
                    if (d.Key >= ExtractBegin && d.Key <= TestEnd)
                    {
                        try
                        {
                            timeseries.Values[cnt - ExtractBegin] = d.Value.Value;
                        }
                        catch
                        {
                            timeseries.Values[cnt - ExtractBegin] = 0.0;
                        }
                        abs.Values[cnt - ExtractBegin] = cnt;
                        cnt++;
                    }
                }

                dy = timeseries;
                dx = abs;
                Dew.Math.Tee.TeeChart.DrawValues(dx, dy, fittedChart.Series[0], false);

                // Извличаме стойностите на независимите променливи
                TVec dy1 = null;
                Vector timeseries1 = new Vector(TestEnd - ExtractBegin + 1);

                int independent_vars = this.IndependentVariables.Count;
                double[] tuple = new double[independent_vars];

                for (int i = ExtractBegin; i <= TestEnd; i++)
                {
                    int cnt1 = 0;
                    foreach (int variable_id in this.IndependentVariables.Keys)
                    {
                        double factor = 1.7 / ( (this.SeriesInfo[variable_id]).Max - (this.SeriesInfo[variable_id]).Min);

                        tuple[cnt1] = (this.SeriesInfo[variable_id].Values[i].Value - (this.SeriesInfo[variable_id]).Min) * factor - 0.85;
                        cnt1++;
                    }
                    double value = (mSelectedActivationNetwork.Compute(tuple)[0] + 0.85) / factordep + mindep;
                    timeseries1.Values[i - ExtractBegin] = value;
                }

                dy1 = timeseries1;
                Dew.Math.Tee.TeeChart.DrawValues(dx, dy1, fittedChart.Series[1], false);

                residuals = new TVec();
                residuals.Length = timeseries1.Length;
                for (int i = 0; i < residuals.Length; i++)
                {
                    residuals.Values[i] = timeseries1.Values[i] - timeseries.Values[i];
                }
                Vector Bins = new Vector(0);
                Vector Freq = new Vector(0);
                chartResiduals.Series[0].Title = "остатъци";
                chartResiduals.Series[1].Title = "Нормално разпределение";
                chartResiduals.Axes.Left.Title.Text = "Y";
                chartResiduals.Axes.Bottom.Title.Text = "";
                chartResiduals.Axes.Left.Automatic = true;

                Statistics.Histogram(residuals, 20, Freq, Bins, true);

                Dew.Math.Tee.TeeChart.DrawValues(Bins, Freq, chartResiduals.Series[0], false);

                Vector xdense = new Vector(0);
                Matrix tmpmtx = new Matrix(0, 0);
                Vector x = new Vector(0);
                Vector bell = new Vector(0);
                // get values for Bell curve
                double mean = residuals.Mean();
                double stddev = residuals.StdDev(mean);
                // 500 calc points
                xdense.Length = 500;
                xdense.Ramp(Bins.Min() , (Bins.Max() - Bins.Min()) * 0.005);
                Dew.Math.Units.Probabilities.NormalPDF(xdense, mean, stddev, bell);
                chartResiduals.Series[1].GetHorizAxis.Automatic = false;
                chartResiduals.Series[1].GetHorizAxis.SetMinMax(residuals.Min(), residuals.Max());
                Dew.Math.Tee.TeeChart.DrawValues(xdense, bell, chartResiduals.Series[1], false);

                double R2 = CalculateR2(dy, dy1);

                rchTests.Clear();
                rchTests.SelectionColor = Color.Red;
                rchTests.AppendText("Тестове");
                rchTests.SelectionColor = SystemColors.InfoText; ;

                rchTests.AppendText("\nНевронна мрежа с параметри: ");
                rchTests.AppendText(String.Format("\nВходящ слой: {0} неврона", mSelectedActivationNetwork.InputsCount));
                rchTests.AppendText(String.Format("\nСкрит слой: {0} неврона", mSelectedActivationNetwork[0].NeuronsCount));

                rchTests.SelectionColor = Color.Red;
                rchTests.AppendText("\nСтатистически параметри");
                rchTests.SelectionColor = SystemColors.InfoText; ;

                rchTests.AppendText(String.Format("\nCalculated R2: {0:F6}", R2));

                rchTests.AppendText("\n\nАнализ на независимите променливи");
                rchTests.AppendText("\nЗначимост");

                double max_spread = 0.0;
                foreach (int var_id in IndependentVariables.Keys)
                {
                    Vector vec = IndepVar(var_id);
                    if (max_spread < vec.Max() - vec.Min())
                    {
                        max_spread = vec.Max() - vec.Min();
                    }
                }

                int current = 1;
                foreach (int var_id in IndependentVariables.Keys)
                {
                    Vector vec = IndepVar(var_id);
                    double spread = vec.Max() - vec.Min();
                    String result = String.Format("{0,-3}. {1,25}: {2,10} | {3}", current, IndependentVariables[var_id], spread, Stars( (int)Math.Floor((10*spread)/ max_spread)));
                    rchTests.AppendText("\n" + result);
                    current++;
                }

                rchTests.AppendText("\n\nDurbin-Watson тест за наличие на автоколинеарност в остатъците");
                rchTests.AppendText(String.Format("\nDW: {0}", Utils.DurbinWatson((Vector)residuals)));
            }
            catch { };
        }
示例#2
0
        private void btnResult_Click(object sender, EventArgs e)
        {
            try
            {
                TVec dy = null;
                TVec dx = null;
                fittedChart.Series.Clear();
                fittedChart.Series.Add(new Steema.TeeChart.Styles.FastLine());
                fittedChart.Series.Add(new Steema.TeeChart.Styles.FastLine());
                fittedChart.Series[0].Title = "наблюдения";
                fittedChart.Series[1].Title = "прогнози";

                fittedChart.Axes.Left.Title.Text = "Y";
                fittedChart.Axes.Bottom.Title.Text = "";
                fittedChart.Axes.Left.Automatic = true;

                Vector timeseries = new Vector((TestEnd - ExtractBegin) + 1);
                Vector abs = new Vector((TestEnd - ExtractBegin) + 1);

                int? dependent_variable_id = 0;
                foreach (int? dep in this.DependentVariables.Keys)
                {
                    dependent_variable_id = dep;
                }

                MainDataSetTableAdapters.VARIABLESTableAdapter adptVariables = new Plig.TimeSeries.Client.MainDataSetTableAdapters.VARIABLESTableAdapter();
                double mindep = adptVariables.MinObservationValue(ExtractBegin, TestEnd, dependent_variable_id.Value).Value;
                double factordep = 1.7 / (adptVariables.MaxObservationValue(ExtractBegin, TestEnd, dependent_variable_id.Value).Value - adptVariables.MinObservationValue(ExtractBegin, TestEnd, dependent_variable_id.Value).Value);

                int cnt = ExtractBegin;
                foreach (KeyValuePair<int, double?> d in this.VariableObservations(dependent_variable_id.Value))
                {
                    if (d.Key >= ExtractBegin && d.Key <= TestEnd)
                    {
                        try
                        {
                            timeseries.Values[cnt - ExtractBegin] = d.Value.Value;
                        }
                        catch
                        {
                            timeseries.Values[cnt - ExtractBegin] = 0.0;
                        }
                        abs.Values[cnt - ExtractBegin] = cnt;
                        cnt++;
                    }
                }

                dy = timeseries;
                dx = abs;
                Dew.Math.Tee.TeeChart.DrawValues(dx, dy, fittedChart.Series[0], false);

                // Извличаме стойностите на независимите променливи
                TVec dy1 = null;
                Vector timeseries1 = new Vector(TestEnd - ExtractBegin + 1);

                int independent_vars = this.IndependentVariables.Count;
                double[] tuple = new double[independent_vars];

                Dew.Math.Tee.TeeChart.DrawValues(dx, multiLinReg.RegressResult.YCalc, fittedChart.Series[1], false);
            }
            catch (Exception ex)
            {
            }
        }
示例#3
0
        protected override void StartTraining(bool aIsRunning)
        {
            if (aIsRunning)
            {
                mStopWatch = new Stopwatch();
                mStopWatch.Start();

                Stopwatch st = new Stopwatch();
                st.Start();

                min = Double.MaxValue;
                mintest = Double.MaxValue;

                // create grid application
                Settings settings = Settings.LoadSettings();
                GA = new GApplication(new GConnection(settings.AlchemiURL, settings.AlchemiPort, settings.AlchemiUser, settings.AlchemiPassword));
                GA.ApplicationName = "Plain Neural Network model";

                // add GridThread module (this executable) as a dependency
                GA.Manifest.Add(new ModuleDependency(typeof(AForge.PolishExpression).Module));
                GA.Manifest.Add(new ModuleDependency(typeof(AForge.Neuro.ActivationNetwork).Module));
                GA.Manifest.Add(new ModuleDependency(typeof(SingleNNTraining).Module));

                // subscribe to events
                GA.ThreadFinish += new GThreadFinish(ThreadFinished);
                GA.ThreadFailed += new GThreadFailed(ThreadFailed);
                GA.ApplicationFinish += new GApplicationFinish(ApplicationFinished);

                // Набор от данни използван за обучение
                mInput = new double[ExtractEnd - ExtractBegin + 1][];
                mOutput = new double[ExtractEnd - ExtractBegin + 1][];
                // Тестов набор от данни
                mTestInput = new double[TestEnd - TestBegin + 1][];
                mTestOutput = new double[TestEnd - TestBegin + 1][];
                for (int i = 0; i < ExtractEnd - ExtractBegin + 1; i++)
                {
                    mInput[i] = new double[IndependentVariables.Count];
                    mOutput[i] = new double[DependentVariables.Count];
                }

                for (int i = 0; i < TestEnd - TestBegin + 1; i++)
                {
                    mTestInput[i] = new double[IndependentVariables.Count];
                    mTestOutput[i] = new double[DependentVariables.Count];
                }

                MainDataSetTableAdapters.VARIABLESTableAdapter adptVariables = new Plig.TimeSeries.Client.MainDataSetTableAdapters.VARIABLESTableAdapter();

                mReinitializations = Convert.ToInt32(txtReinitializations.Text);
                results = new List<NNResultSet>(mReinitializations);

                mEpochs = Convert.ToInt32(txtEpochs.Text);
                ReinitsFinished = 0;

                int index_of_variable = 0;
                foreach (int variable_id in IndependentVariables.Keys)
                {
                    double minObsValue = adptVariables.MinObservationValue(ExtractBegin, ExtractEnd, variable_id).Value;
                    double factor = 1.7 / (adptVariables.MaxObservationValue(ExtractBegin, ExtractEnd, variable_id).Value - adptVariables.MinObservationValue(ExtractBegin, ExtractEnd, variable_id).Value);
                    Dictionary<int, double?> observations = VariableObservations(variable_id);
                    foreach (int counter in observations.Keys)
                    {
                        if ((counter >= ExtractBegin) && (counter <= ExtractEnd))
                        {
                            mInput[counter - ExtractBegin][index_of_variable] = (observations[counter].Value - minObsValue) * factor - 0.85;
                        }

                        if ((counter >= TestBegin) && (counter <= TestEnd))
                        {
                            mTestInput[counter - TestBegin][index_of_variable] = (observations[counter].Value - minObsValue) * factor - 0.85;
                        }

                    }
                    index_of_variable++;
                }

                index_of_variable = 0;
                foreach (int variable_id in DependentVariables.Keys)
                {
                    double minObsValue = adptVariables.MinObservationValue(ExtractBegin, ExtractEnd, variable_id).Value;
                    double factor = 1.7 / (adptVariables.MaxObservationValue(ExtractBegin, ExtractEnd, variable_id).Value - adptVariables.MinObservationValue(ExtractBegin, ExtractEnd, variable_id).Value);
                    Dictionary<int, double?> observations = VariableObservations(variable_id);
                    foreach (int counter in observations.Keys)
                    {
                        if ((counter >= ExtractBegin) && (counter <= ExtractEnd))
                        {
                            mOutput[counter - ExtractBegin][index_of_variable] = (observations[counter].Value - minObsValue) * factor - 0.85;
                        }

                        if ((counter >= TestBegin) && (counter <= TestEnd))
                        {
                            mTestOutput[counter - TestBegin][index_of_variable] = (observations[counter].Value - minObsValue) * factor - 0.85;
                        }
                    }
                    index_of_variable++;
                }

                double momentum = 0.0;
                double learning_rate = 0.1;

                learning_rate = Convert.ToDouble(txtLearningRate.Text);
                momentum = Convert.ToDouble(txtMomentum.Text);

                int min_neurons = 1;
                int max_neurons = 1;
                if (rbFixedHiddenNeurons.Checked)
                {
                    min_neurons = Convert.ToInt32(txtHiddenNeurons.Text);
                    max_neurons = min_neurons;
                }

                if (rbFlexibleHiddenNeurons.Checked)
                {
                    min_neurons = Convert.ToInt32(txtMinHiddenNeurons.Text);
                    max_neurons = Convert.ToInt32(txtMaxHiddenNeurons.Text);
                }

                int TotalNNCalculations = 0;
                for (int hidden_neurons = min_neurons; hidden_neurons <= max_neurons; hidden_neurons++)
                {

                    for (int i = 0; i < mReinitializations; i++)
                    //              Parallel.For(0, mReinitializations - 1, i =>
                    {
                        // create thread
                        SingleNNTraining single_nn = new SingleNNTraining(mInput, mOutput, mEpochs);
                        single_nn.HiddenNeurons = hidden_neurons;
                        single_nn.Iterations = mEpochs;
                        single_nn.Momentum = momentum;
                        single_nn.LearningRate = learning_rate;

                        // добавяне на набора от данни за тестване
                        single_nn.TestInput = mTestInput;
                        single_nn.TestOutput = mTestOutput;

                        // add thread to application
                        GA.Threads.Add(single_nn);
                        TotalNNCalculations++;
                    }
                    //                );
                }

                pbProgressTraining.Minimum = 0;
                pbProgressTraining.Maximum = TotalNNCalculations - 1;

                GA.Start();

                st.Stop();
                lbResults.Items.Add("Време необходимо за конструиране на данните: " + st.Elapsed.ToString());

                int populationCount = Convert.ToInt32(txtPopulationCount.Text);
                Population population = new Population(populationCount,
                    new DoubleArrayChromosome(new AForge.Math.Random.UniformOneGenerator(),
                        new AForge.Math.Random.GaussianGenerator(0, 1),new AForge.Math.Random.GaussianGenerator(0, 1),
                        (mOutput.Length + mInput.Length + 1) * 10 + mOutput.Length),
                    new NeuralNetworkFitness(mInput, mOutput, new int[] { IndependentVariables.Count, 10, DependentVariables.Count }),
                    new EliteSelection());

                int geneticEpochs = Convert.ToInt32(txtGeneticEpochs.Text);
                pbGeneticProgress.Maximum = geneticEpochs;

                for (int i = 0; i < geneticEpochs; i++)
                {
                    population.RunEpoch();
                    pbGeneticProgress.Value = i + 1;
                    Application.DoEvents();
                }

                NeuralNetworkFitness nnf = new NeuralNetworkFitness(mInput, mOutput, new int[] { IndependentVariables.Count, 5, DependentVariables.Count });
                mBestGeneticActivationNetwork = (ActivationNetwork)nnf.Translate(population.BestChromosome);

            }
            else
            {
                if (GA.Running)
                {
                    foreach (GThread thread in GA.Threads)
                    {
                        thread.Abort();
                    }
            //                     GA.Stop();
                    lbResults.Items.Add("Обучението е прекъснато.");
                }
            }
        }