Пример #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            StringBuilder Result = new StringBuilder();
            int           ColNum = 0;

            if (Int32.TryParse(texbox_dep.Text, out ColNum))
            {
                string[]     NumberSeries  = Tabulation.ReadVector(MainForm.MainDT, ColNum - 1).ToArray();
                int          length        = 0;
                int          lag           = 4; //自己给定lag的值
                string[][]   NumberCombine = new string[lag + 1][];
                BigDecimal[] corr          = new BigDecimal[lag];
                BigDecimal   sum_Qtest     = 0;
                BigDecimal   sum_LBtest    = 0;
                foreach (string Num in NumberSeries)
                {
                    if (Num != "" && Num != null)
                    {
                        length++;
                    }
                }
                if (length < 8)
                {
                    MessageBox.Show("数据量过少,建议使用灰色预测");
                }
                else
                {
                    for (int i = 0; i < lag + 1; i++)
                    {
                        NumberCombine[i] = new string[length - lag];
                        for (int j = 0; j < length - lag; j++)
                        {
                            NumberCombine[i][j] = NumberSeries[j + lag - i];
                        }
                        try
                        {
                            corr[i - 1] = Stat.Corr(NumberCombine[i], NumberCombine[i - 1]);
                            sum_Qtest  += corr[i - 1] * corr[i - 1];
                            sum_LBtest += corr[i - 1] * corr[i - 1] / (BigDecimal)(length - i);
                        }
                        catch (Exception ex) { }
                    }

                    string[] output = Stat.TimeseriesTest(length, sum_Qtest, sum_LBtest, lag, corr);
                    Result.Append(StrManipulation.PadLeftX("时间序列平稳性检验", ' ', 12));
                    Result.Append("\r\n");
                    Result.Append(StrManipulation.PadRightX("Q检验:", ' ', 12));
                    Result.Append("\r\n");
                    Result.Append(StrManipulation.PadLeftX("Q      =", ' ', 12));
                    Result.Append("\t");
                    Result.Append(StrManipulation.PadLeftX(MathV.NumberPolish(output[lag]), ' ', 12));
                    Result.Append("\r\n");
                    Result.Append(StrManipulation.PadLeftX("Prob > Q =", ' ', 12));
                    Result.Append("\t");
                    Result.Append(StrManipulation.PadLeftX(MathV.NumberPolish(output[lag + 1]), ' ', 12));
                    Result.Append("\r\n");

                    Result.Append(StrManipulation.PadRightX("LB检验:", ' ', 12));
                    Result.Append("\r\n");
                    Result.Append(StrManipulation.PadLeftX("LB      =", ' ', 12));
                    Result.Append("\t");
                    Result.Append(StrManipulation.PadLeftX(MathV.NumberPolish(output[lag + 2]), ' ', 12));
                    Result.Append("\r\n");
                    Result.Append(StrManipulation.PadLeftX("Prob > LB =", ' ', 12));
                    Result.Append("\t");
                    Result.Append(StrManipulation.PadLeftX(MathV.NumberPolish(output[lag + 3]), ' ', 12));
                    Result.Append("\r\n");


                    if (Convert.ToDouble(output[lag + 1]) <= 0.05 || Convert.ToDouble(output[lag + 3]) <= 0.05)
                    {
                        Result.Append(StrManipulation.PadRightX("在95%的显著性水平下认为序列非白噪声,观测值间显著相关。", ' ', 50));
                        Result.Append("\r\n");
                    }
                    else
                    {
                        Result.Append(StrManipulation.PadRightX("在95%的显著性水平下认为序列为白噪声,观测值间相互独立。", ' ', 50));
                        Result.Append("\r\n");
                    }
                    Result.Append("\r\n");
                    int period = 0;
                    if (Int32.TryParse(textBox_period.Text, out period))
                    {
                        BigDecimal[] AR1           = Stat.AR1(NumberSeries, length, corr, period);
                        BigDecimal[] AR2           = Stat.AR2(NumberSeries, length, corr, period);
                        BigDecimal[] plot_forecast = new BigDecimal[period];
                        if (AR1[period] <= AR2[period])
                        {
                            Result.Append(StrManipulation.PadRightX("最佳预测模型:AR1", ' ', 12));
                            Result.Append("\r\n");
                            Result.Append(StrManipulation.PadLeftX("序号(预测)", ' ', 12));
                            Result.Append("\t");
                            for (int i = 0; i < period; i++)
                            {
                                Result.Append(StrManipulation.PadLeftX(i.ToString(), ' ', 12));
                                plot_forecast[i] = AR1[i];
                                Result.Append("\t");
                            }
                            Result.Append("\r\n");
                            Result.Append(StrManipulation.PadLeftX("预测值", ' ', 12));
                            Result.Append("\t");

                            for (int i = 0; i < period; i++)
                            {
                                Result.Append(StrManipulation.PadLeftX(MathV.NumberPolish(AR1[i].ToString()), ' ', 12));
                                Result.Append("\t");
                            }
                        }
                        else
                        {
                            Result.Append(StrManipulation.PadRightX("最佳预测模型:AR2", ' ', 12));
                            Result.Append("\r\n");
                            Result.Append(StrManipulation.PadLeftX("序号(预测)", ' ', 12));
                            Result.Append("\t");
                            for (int i = 0; i < period; i++)
                            {
                                Result.Append(StrManipulation.PadLeftX(i.ToString(), ' ', 12));
                                plot_forecast[i] = AR2[i];
                                Result.Append("\t");
                            }
                            Result.Append("\r\n");
                            Result.Append(StrManipulation.PadLeftX("预测值", ' ', 12));
                            Result.Append("\t");

                            for (int i = 0; i < period; i++)
                            {
                                Result.Append(StrManipulation.PadLeftX(MathV.NumberPolish(AR2[i].ToString()), ' ', 12));
                                Result.Append("\t");
                            }
                        }
                        Result.Append("\r\n");
                        Result.Append("\r\n");
                        MainForm.S.richTextBox1.AppendText(Result.ToString());
                        //MainForm.S.richTextBox1.Select();//让RichTextBox获得焦点

                        chart_timeseries.Series.Clear();
                        Series series  = new Series("原数据");
                        Series series2 = new Series("预测数据");
                        //ChartArea area = chart_timeseries.ChartAreas.Add("chartArea");
                        //area.AxisX.MajorGrid.LineWidth = 0;
                        series.Color  = Color.MidnightBlue;
                        series2.Color = Color.Maroon;
                        if (length <= 50)
                        {
                            series.BorderWidth  = 2;
                            series2.BorderWidth = 2;
                        }
                        series.ChartType  = SeriesChartType.Line;
                        series2.ChartType = SeriesChartType.Line;
                        for (int i = 0; i < length; i++)
                        {
                            series.Points.AddXY(i + 1, Convert.ToDouble(NumberSeries[i].ToString()));
                        }
                        series2.Points.AddXY(length, Convert.ToDouble(NumberSeries[length - 1].ToString()));
                        for (int i = 0; i < period; i++)
                        {
                            series2.Points.AddXY(i + 1 + length, Convert.ToDouble(plot_forecast[i].ToString()));
                        }
                        chart_timeseries.Series.Add(series);
                        chart_timeseries.Series.Add(series2);
                    }
                    else
                    {
                        MessageBox.Show("请输入预测期数");
                    }
                }
            }
            else
            {
                MessageBox.Show("无此列");
            }
        }