private void button_Cal_Click(object sender, EventArgs e) { List <int> BlackList = new List <int>(); double [] ans = (double [])MathV.CalExpression(textBox_expression.Text.Replace("\r\n", ""), MainForm.MainDT, ref BlackList); int ColNum = 0; if (checkBox_NewCol.Checked == true) { MainForm.MainDT.Columns.Add(); ColNum = MainForm.MainDT.Columns.Count - 1; } else { ColNum = Tabulation.FindCol(MainForm.MainDT, comboBox_Output.Text); } if (ColNum == -1) { return; } int RealData = 0; int RowsCount = MainForm.MainDT.Rows.Count; for (int i = 0; i < RowsCount; i++) { if (BlackList.IndexOf(i) == -1) { MainForm.MainDT.Rows[i][ColNum] = ans[RealData].ToString(); RealData++; } } Tabulation.InitDataSet(MainForm.MainDT, ref MainForm.nMax, ref MainForm.pageCount, ref MainForm.pageCurrent, ref MainForm.nCurrent, MainForm.S.label_CurrentPage, MainForm.S.label_TotalPage, MainForm.S.dataGridView1, MainForm.S.textBox_CurrentPage, MainForm.pageSize); }
private void button_Percentile_Click(object sender, EventArgs e) { char[] separator = { ',' }; string[] Percentiles = textBox_Percentiles.Text.Split(separator); double Temp = 0; StringBuilder Str = new StringBuilder(); int len = Multinomial.Classification.Length; foreach (string EachPer in Percentiles) { if (double.TryParse(EachPer, out Temp)) { if (Temp > 0 && Temp < 1) { Str.Append(StrManipulation.PadLeftX(Temp.ToString() + "分位数:\r\n", ' ', 12)); for (int i = 0; i < len; i++) { Str.Append(StrManipulation.PadLeftX(Multinomial.Classification[i], ' ', 12)); Str.Append("\t"); Str.Append(StrManipulation.PadLeftX(MathV.NumberPolish(Stat.BetaUa(1 - Temp, Multinomial.Alphas[i], Multinomial.Alphas.Sum() - Multinomial.Alphas[i]).ToString()), ' ', 12)); Str.Append("\r\n"); } } } } MainForm.S.richTextBox1.AppendText(Str.ToString()); MainForm.S.richTextBox1.Select(); //让RichTextBox获得焦点 MainForm.S.richTextBox1.Select(MainForm.S.richTextBox1.TextLength, 0); //将插入符号置于文本结束处 MainForm.S.richTextBox1.ScrollToCaret(); }
public static string DirichletPrediction(List <int> Numbers, List <int> Alphas) { BigInteger Denominator = 1; int k = Alphas.Count; for (int i = 0; i < k; i++) { Denominator *= MathV.Factorial(Numbers[i]); } BigInteger Numerator = MathV.Factorial(Numbers.Sum()); BigNumber Factor1 = (((BigNumber)Numerator.ToString()) / ((BigNumber)Denominator.ToString())).ToString(); Denominator = 1; for (int i = 0; i < k; i++) { Denominator *= MathV.Factorial(Alphas[i] - 1); } Numerator = MathV.Factorial(Alphas.Sum() - 1); BigNumber Factor2 = (((BigNumber)Numerator.ToString()) / ((BigNumber)Denominator.ToString())).ToString(); Denominator = MathV.Factorial(Alphas.Sum() + Numbers.Sum() - 1); Numerator = 1; for (int i = 0; i < k; i++) { Numerator *= MathV.Factorial(Alphas[i] + Numbers[i] - 1); } BigNumber Factor3 = (((BigNumber)Numerator.ToString()) / ((BigNumber)Denominator.ToString())).ToString(); BigNumber Result = Factor1 * Factor2 * Factor3; return(Result.ToString()); }
private void button_predict_Click(object sender, EventArgs e) { char[] separator = { ',' }; string[] Prediction = textBox_success.Text.Split(separator); int Temp = 0; int n = Convert.ToInt32(textBox_NewSampleSize.Text); StringBuilder Str = new StringBuilder(); Str.Append(StrManipulation.PadLeftX("次数", ' ', 12)); Str.Append("\t"); Str.Append(StrManipulation.PadLeftX("预测概率值", ' ', 12)); Str.Append("\r\n"); foreach (string EachPred in Prediction) { if (int.TryParse(EachPred, out Temp)) { if (Temp >= 0 && Temp <= n) { Str.Append(StrManipulation.PadLeftX(Temp.ToString(), ' ', 12)); Str.Append("\t"); Str.Append(StrManipulation.PadLeftX(MathV.NumberPolish(Stat.BetaPrediction(Alpha, Beta, n, Temp).ToString()), ' ', 12)); Str.Append("\r\n"); } } } MainForm.S.richTextBox1.AppendText(Str.ToString()); MainForm.S.richTextBox1.Select(); //让RichTextBox获得焦点 MainForm.S.richTextBox1.Select(MainForm.S.richTextBox1.TextLength, 0); //将插入符号置于文本结束处 MainForm.S.richTextBox1.ScrollToCaret(); }
public static double BinomialPDF(double p, int n, int k) { //k为成功次数 BigNumber Combination = MathV.Combination(n, k).ToString(); return((double)Combination * Math.Pow(p, k) * Math.Pow(1 - p, n - k)); }
public static double BetaPDF(double x, int a, int b) { BigInteger Numerator = MathV.Factorial(a + b - 1); BigInteger Denominator = MathV.Factorial(a - 1) * MathV.Factorial(b - 1); BigNumber Quotient = (BigNumber)Numerator.ToString() / (BigNumber)Denominator.ToString(); double NormConst = (double)Quotient; return(NormConst * Math.Pow(x, a - 1) * Math.Pow(1 - x, b - 1)); }
public static BigDecimal[,] MultiRegBeta(BigDecimal[,] x, BigDecimal[,] y) { //返回多元回归参数估计值**************************************注意:x第一列为1******************* int len11 = x.GetLength(0); //行数 int len12 = x.GetLength(1); //列数 BigDecimal[,] b1 = MathV.MatTrans(x); BigDecimal[,] b2 = MathV.MatTimes(b1, x); BigDecimal[,] b3 = MathV.MatInv(b2, len12); BigDecimal[,] b4 = MathV.MatTimes(b3, b1); BigDecimal[,] bhat = MathV.MatTimes(b4, y); return(bhat); }
public static double BetaPrediction(int a, int b, int n, int y) { BigInteger Numerator = MathV.Factorial(a + b - 1); BigInteger Denominator = MathV.Factorial(a - 1) * MathV.Factorial(b - 1); BigNumber Quotient = (BigNumber)Numerator.ToString() / (BigNumber)Denominator.ToString(); double NormConst = (double)Quotient; Numerator = MathV.Combination(n, y) * MathV.Factorial(y + a - 1) * MathV.Factorial(n - y + b - 1); Denominator = MathV.Factorial(a + b + n - 1); Quotient = (BigNumber)Numerator.ToString() / (BigNumber)Denominator.ToString(); return(NormConst * (double)Quotient); }
public static BigDecimal Corr(string[] NumberSeries1, string[] NumberSeries2) { //相关系数计算 BigDecimal Mean1 = 0; BigDecimal Mean2 = 0; BigDecimal Variance1 = 0; BigDecimal Variance2 = 0; BigDecimal cov = Covariance(NumberSeries1, ref Mean1, ref Variance1, NumberSeries2, ref Mean2, ref Variance2); BigDecimal Rho = cov / (MathV.Sqrt((Variance1 * Variance2).ToString())).ToString(); //计算相关系数 return(Rho); }
private void button_predict_Click(object sender, EventArgs e) { //string[] ContentLines = Content.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); string[] ContentLines = textBox_successes.Text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); List <int> Numbers = new List <int>(); int Temp = 0; int Len = 0; StringBuilder Information = new StringBuilder(); Information.Append(StrManipulation.PadLeftX("组编号", ' ', 12)); Information.Append("\t"); Information.Append(StrManipulation.PadLeftX("预测概率值", ' ', 12)); Information.Append("\r\n"); int GroupTimes = 1; foreach (string EachLine in ContentLines) { Numbers.Clear(); string[] Strs = EachLine.Split(','); foreach (string EachStr in Strs) { if (Int32.TryParse(EachStr, out Temp)) { Numbers.Add(Temp); } } Len = Numbers.Count; if (Len != Multinomial.Classification.Length) { MessageBox.Show("参数个数不正确!"); } else { Information.Append("第"); Information.Append(GroupTimes.ToString()); Information.Append("组"); GroupTimes++; Information.Append("\t"); Information.Append(MathV.NumberPolish(Stat.DirichletPrediction(Numbers, Multinomial.Alphas))); Information.Append("\r\n"); } } MainForm.S.richTextBox1.AppendText(Information.ToString()); MainForm.S.richTextBox1.Select(); //让RichTextBox获得焦点 MainForm.S.richTextBox1.Select(MainForm.S.richTextBox1.TextLength, 0); //将插入符号置于文本结束处 MainForm.S.richTextBox1.ScrollToCaret(); }
public static void MultiCorr(DataTable dt, int [] ColNums, RichTextBox richTextBox1) { StringBuilder Result = new StringBuilder(); //相关系数输出是一个矩阵,因此要有行名和列名 //例如: // 列1 列2 列3 //列1 1 0 0 //列2 0 1 0 //列3 0 1 1 //自身的相关系数一定是1 Result.Append(StrManipulation.PadLeftX(" ", ' ', 12)); Result.Append("\t"); foreach (int EachCol in ColNums) { Result.Append(StrManipulation.PadLeftX(dt.Columns[EachCol].ColumnName, ' ', 12)); Result.Append("\t"); } Result.Append("\r\n"); foreach (int EachCol in ColNums) { Result.Append(StrManipulation.PadLeftX(dt.Columns[EachCol].ColumnName, ' ', 12)); Result.Append("\t"); foreach (int EveryCol in ColNums) { if (EachCol == EveryCol) { Result.Append(StrManipulation.PadLeftX("1.000", ' ', 12)); Result.Append("\t"); } else { string[] NumberSeries1 = Tabulation.ReadVector(dt, EachCol).ToArray(); string[] NumberSeries2 = Tabulation.ReadVector(dt, EveryCol).ToArray(); Result.Append(StrManipulation.PadLeftX(MathV.NumberPolish(Corr(NumberSeries1, NumberSeries2).ToString()), ' ', 12)); Result.Append("\t"); } } Result.Append("\r\n"); } richTextBox1.AppendText(Result.ToString()); MainForm.S.richTextBox1.Select(); //让RichTextBox获得焦点 MainForm.S.richTextBox1.Select(MainForm.S.richTextBox1.TextLength, 0); //将插入符号置于文本结束处 MainForm.S.richTextBox1.ScrollToCaret(); }
private void button_Generate_Click(object sender, EventArgs e) { Stat.BetaAlphaBeta(Convert.ToInt32(textBox_SampleSize.Text), Convert.ToInt32(textBox_success.Text), ref Alpha, ref Beta); if (Classification.Length != 0 && CountTimes.Length != 0) { Result.Append("先验分布: "); Result.Append("Beta("); Result.Append(Alpha.ToString()); Result.Append(", "); Result.Append(Beta.ToString()); Result.Append(")\r\n"); Stat.ConjugateBeta(ref Alpha, ref Beta, CountTimes[0] + CountTimes[1], CountTimes[0]); Series series = new Series(); series.Points.AddXY(Classification[0], CountTimes[0]); series.Points.AddXY(Classification[1], CountTimes[1]); series.ChartType = SeriesChartType.Pie; chart_Posterior.Series.Clear(); chart_Posterior.Series.Add(series); Result.Append("似然函数: "); Result.Append("Binomial("); double p = ((double)CountTimes[0]) / (double)(CountTimes[0] + CountTimes[1]); Result.Append(CountTimes[0] + CountTimes[1]); Result.Append(", "); Result.Append(MathV.NumberPolish(p.ToString()).Trim()); Result.Append(")\r\n"); Result.Append("后验分布: "); Result.Append("Beta("); Result.Append(Alpha.ToString()); Result.Append(", "); Result.Append(Beta.ToString()); Result.Append(")\r\n"); Result.Append("后验分布等效样本量: "); Result.Append((Alpha + Beta - 2).ToString()); Result.Append("\r\n"); MainForm.S.richTextBox1.AppendText(Result.ToString()); MainForm.S.richTextBox1.Select(); //让RichTextBox获得焦点 MainForm.S.richTextBox1.Select(MainForm.S.richTextBox1.TextLength, 0); //将插入符号置于文本结束处 MainForm.S.richTextBox1.ScrollToCaret(); button_PosteriorDensity.Enabled = true; } }
public static string GetColsName(DataGridView dataGridView1, string ColNums) { char [] separator = { ',' }; //string是以逗号分隔的 string[] AllNum = ColNums.Split(separator); //按照逗号分割 string AllNames = ""; foreach (string SingleNum in AllNum) { if (SingleNum != "") { if (MathV.IsStringInt(SingleNum)) { AllNames += dataGridView1.Columns[Convert.ToInt32(SingleNum) - 1].Name + ","; } } } if (AllNames != "") { AllNames = AllNames.Substring(0, AllNames.Length - 1); } return(AllNames); }
private void button_Generate_Click(object sender, EventArgs e) { Result.Clear(); string[] Str = textBox_Times.Text.Replace("\r", "").Split('\n'); List <int> Nums = new List <int>(); int Temp = 0; foreach (string EachStr in Str) { if (Int32.TryParse(EachStr, out Temp)) { Nums.Add(Temp); } } int ClassLen = Classification.Length; int CountTimesLen = CountTimes.Length; if (Stat.DirichletAlphas(Nums, ClassLen, ref Alphas)) { if (ClassLen != 0 && CountTimesLen != 0) { Result.Append("先验分布: "); Result.Append("Dirichlet("); for (int i = 0; i < ClassLen; i++) { Result.Append(Alphas[i].ToString()); if (i < ClassLen - 1) { Result.Append(", "); } } Result.Append(")\r\n"); Stat.ConjugateDirichlet(ref Alphas, CountTimes); Result.Append("似然函数: "); Result.Append("Binomial("); double p = 0; double sum = CountTimes.Sum(); Result.Append(sum.ToString()); Result.Append(", "); for (int i = 0; i < ClassLen; i++) { p = ((double)CountTimes[i]) / sum; Result.Append(MathV.NumberPolish(p.ToString()).Trim()); if (i < ClassLen - 1) { Result.Append(", "); } } Result.Append(")\r\n"); Result.Append("Dirichlet("); for (int i = 0; i < ClassLen; i++) { Result.Append(Alphas[i].ToString()); if (i < ClassLen - 1) { Result.Append(", "); } } Result.Append(")\r\n"); Result.Append("后验分布等效样本量: "); Result.Append((Alphas.Sum() - Alphas.Count).ToString()); Result.Append("\r\n"); MainForm.S.richTextBox1.AppendText(Result.ToString()); MainForm.S.richTextBox1.Select(); //让RichTextBox获得焦点 MainForm.S.richTextBox1.Select(MainForm.S.richTextBox1.TextLength, 0); //将插入符号置于文本结束处 MainForm.S.richTextBox1.ScrollToCaret(); button_PosteriorDensity.Enabled = true; } } else { MessageBox.Show("输入次数缺失!"); } }
public static string NumberPolish(string Number) { //首先要判别输入的数字是否为科学计数法 int Char_E = Number.IndexOf('E'); if (Char_E != -1) { //如果是科学计数法则先转化为常规格式 Number = (new BigNumber(Number.Substring(0, Char_E - 1)) * MathV.Pow(10, Number.Substring(Char_E + 1, Number.Length - Char_E - 1))).ToString(); } //目标为控制位数在10位 Number = Number.Trim(); int CountDecimalPoint = 0; int ScientificNumber; string ScientificNotation = ""; int digits_part; BigDecimal NumberProcess = 0; string ProcessedNumber; char[] separator = { '.' }; //用于分割小数点 string[] NumberParts = new string[2]; int IsNegative = 0; //用来判定数字是否为负 int zero_count = 0; //用于计算小数点后有多少个零 NumberParts = Number.Split(separator); if (NumberParts[0].Length + 5 > 10) //四位小数加上1个小数点 { if (Number[0] == '-') { IsNegative = 1; } foreach (char FindDecimalPoint in Number) { if (FindDecimalPoint == '.') { CountDecimalPoint = 1; break; } } if (CountDecimalPoint == 0) { //无小数点则只有整数部分 ScientificNumber = Number.Length - IsNegative - 1; //转换为科学计数法 if (ScientificNumber <= 9 && ScientificNumber > 0) { ScientificNotation = "0" + ScientificNumber.ToString(); //如果科学计数法为个位要补零 } else { ScientificNotation = ScientificNumber.ToString(); } //数字正负无需考虑 if (ScientificNotation.Length + 1 + 2 <= 9) { //例如:-3.1415E+05 //数字负号占一位,E和科学计数法的符号占两位 //大于9的情况不做考虑,数字太大。 digits_part = 9 - (ScientificNotation.Length + 1 + 2); //小数位由此算出 NumberProcess = new BigDecimal(Number.ToString()) / Math.Pow(10, ScientificNumber); ProcessedNumber = MathV.round(NumberProcess.ToString(), digits_part, 0) + "E+" + ScientificNotation; } else { //如果已经大于已经大于九位了,则不作处理 //这种情况极为罕见,不考虑 NumberProcess = new BigDecimal(Number.ToString()) / Math.Pow(10, ScientificNumber); ProcessedNumber = MathV.round(NumberProcess.ToString(), 0, 0) + "E+" + ScientificNotation; } } else { //有小数点的情况 NumberParts = Number.Split(separator); if (NumberParts[0] != "0") { if (NumberParts[0].Length + 1 + 4 >= 9) { //如果保留四位小数之后依旧总长度超过九,则进行如下操作 //判断整数部分是否为0 //如果不为0,继续科学计数法的处理,舍去小数部分 ScientificNumber = NumberParts[0].Length - IsNegative - 1; //转换为科学计数法 if (ScientificNumber <= 9 && ScientificNumber > 0) { ScientificNotation = "0" + ScientificNumber.ToString(); //如果科学计数法为个位要补零 } else { ScientificNotation = ScientificNumber.ToString(); } //数字正负无需考虑 if (ScientificNotation.Length + 1 + 2 <= 9) { //例如:-3.1415E+05 //数字负号占一位,E和科学计数法的符号占两位 //大于9的情况不做考虑,数字太大。 digits_part = 9 - (ScientificNotation.Length + 1 + 2); //小数位由此算出 NumberProcess = new BigDecimal(Number.ToString()) / Math.Pow(10, ScientificNumber); ProcessedNumber = MathV.round(NumberProcess.ToString(), digits_part, 0) + "E+" + ScientificNotation; } else { //如果已经大于已经大于九位了,则不作处理 //这种情况极为罕见,不考虑 NumberProcess = new BigDecimal(Number.ToString()) / Math.Pow(10, ScientificNumber); ProcessedNumber = MathV.round(NumberProcess.ToString(), 0, 0) + "E+" + ScientificNotation; } } else { ProcessedNumber = MathV.round(Number, 4, 0); } } else { zero_count = 0; foreach (char EachDigit in NumberParts[1]) { if (EachDigit == '0') { zero_count++; } else { break; } } ScientificNumber = zero_count + 1; //科学计数法的数字为零的个数加1 if (ScientificNumber <= 9 && ScientificNumber > 0) { ScientificNotation = "0" + ScientificNumber.ToString(); //如果科学计数法为个位要补零 } else { ScientificNotation = ScientificNumber.ToString(); } if (ScientificNotation.Length + 1 + 2 <= 9) { //例如:-2.654E-05 //数字负号占一位,E和科学计数法的符号占两位 //大于9的情况不做考虑,数字太大。 digits_part = 9 - (ScientificNotation.Length + 1 + 2); //小数位由此算出 NumberProcess = new BigDecimal(Number.ToString()) * Math.Pow(10, ScientificNumber); ProcessedNumber = MathV.round(NumberProcess.ToString(), digits_part, 0) + "E-" + ScientificNotation; } else { //如果已经大于已经大于九位了,则不作处理 //这种情况极为罕见,不考虑 NumberProcess = new BigDecimal(Number.ToString()) * Math.Pow(10, ScientificNumber); ProcessedNumber = MathV.round(NumberProcess.ToString(), 0, 0) + "E-" + ScientificNotation; } } } } else { ProcessedNumber = MathV.round(Number, 4, 0); //如果本身长度就在十个单位以内,则不作处理 //保留四位小数 } if (ProcessedNumber.Length < 11) { ProcessedNumber = ProcessedNumber.PadLeft(11, ' '); //11个单位的长度是为了预留量,以免出现连续进位的情况 } return(ProcessedNumber); }
private void button_Anova_Click(object sender, EventArgs e) { char[] separator = { ',' }; string[] AllCols = textBox_Cols.Text.Split(separator); int ColNum = 0; int Cols_count = 0; int Cols_length = AllCols.Length; //变量数 int[] Row_length = new int[Cols_length]; //每个变量长度 int Total_length = 0; //总单元数 string[][] Numbers = new string[Cols_length][]; foreach (string EachCol in AllCols) { if (Int32.TryParse(EachCol, out ColNum)) { Row_length[Cols_count] = 0; Numbers[Cols_count] = Tabulation.ReadVector(MainForm.MainDT, ColNum - 1).ToArray(); foreach (string value in Numbers[Cols_count]) { if (value != "" && value != null) { Row_length[Cols_count]++; } } Total_length += Row_length[Cols_count]; //MessageBox.Show("变量" + Cols_count.ToString()); //MessageBox.Show("变量长度" + Total_length.ToString()); } Cols_count++; } //计算开始 BigDecimal sum_all = 0; BigDecimal[] sum_col = new BigDecimal[Cols_length]; BigDecimal[] xjbar = new BigDecimal[Cols_length]; for (int i = 0; i < Cols_length; i++) { sum_col[i] = 0; foreach (BigDecimal value in Numbers[i]) { sum_all = sum_all + value; sum_col[i] = sum_col[i] + value; xjbar[i] = sum_col[i] / Row_length[i]; } } BigDecimal xbarbar = sum_all / Total_length; BigDecimal SST = 0; BigDecimal SSA = 0; BigDecimal SSW = 0; BigDecimal[] SSW_list = new BigDecimal[Cols_length]; for (int i = 0; i < Cols_length; i++) { SSW_list[i] = 0; foreach (BigDecimal var in Numbers[i]) { SST = SST + (var - xbarbar) * (var - xbarbar); SSW_list[i] = SSW_list[i] + (var - xjbar[i]) * (var - xjbar[i]); } SSA = SSA + Row_length[i] * (xjbar[i] - xbarbar) * (xjbar[i] - xbarbar); SSW = SSW + SSW_list[i]; } BigDecimal MSA = SSA / (Cols_length - 1); BigDecimal MSW = SSW / (Total_length - Cols_length); BigDecimal MST = SST / (Total_length - 1); BigDecimal Fvalue = MSA / MSW; BigDecimal Pvalue = Stat.FDIST(Convert.ToDouble(Fvalue.ToString()), Cols_length - 1, Total_length - 1); MainForm.S.richTextBox1.AppendText(StrManipulation.PadLeftX(StrManipulation.VariableNamePolish("方差分析"), ' ', 12)); MainForm.S.richTextBox1.AppendText("\r\n"); MainForm.S.richTextBox1.AppendText(StrManipulation.PadLeftX(StrManipulation.VariableNamePolish("变量名:"), ' ', 12)); MainForm.S.richTextBox1.AppendText("\t"); MainForm.S.richTextBox1.AppendText(textBox_VarNames.Text); MainForm.S.richTextBox1.AppendText("\r\n"); MainForm.S.richTextBox1.AppendText(StrManipulation.PadLeftX(StrManipulation.VariableNamePolish("F值:"), ' ', 12)); MainForm.S.richTextBox1.AppendText("\t"); MainForm.S.richTextBox1.AppendText(StrManipulation.PadLeftX(MathV.NumberPolish(Fvalue.ToString()), ' ', 12)); MainForm.S.richTextBox1.AppendText("\r\n"); MainForm.S.richTextBox1.AppendText(StrManipulation.PadLeftX(StrManipulation.VariableNamePolish("P值:"), ' ', 12)); MainForm.S.richTextBox1.AppendText("\t"); MainForm.S.richTextBox1.AppendText(StrManipulation.PadLeftX(MathV.NumberPolish(Pvalue.ToString()), ' ', 12)); MainForm.S.richTextBox1.AppendText("\r\n"); MainForm.S.richTextBox1.AppendText(Header); MainForm.S.richTextBox1.AppendText(StrManipulation.PadLeftX(StrManipulation.VariableNamePolish("组内"), ' ', 12)); MainForm.S.richTextBox1.AppendText("\t"); MainForm.S.richTextBox1.AppendText(StrManipulation.PadLeftX(MathV.NumberPolish((Cols_length - 1).ToString()), ' ', 12)); MainForm.S.richTextBox1.AppendText("\t"); MainForm.S.richTextBox1.AppendText(StrManipulation.PadLeftX(MathV.NumberPolish(SSA.ToString()), ' ', 12)); MainForm.S.richTextBox1.AppendText("\t"); MainForm.S.richTextBox1.AppendText(StrManipulation.PadLeftX(MathV.NumberPolish(MSA.ToString()), ' ', 12)); MainForm.S.richTextBox1.AppendText("\r\n"); MainForm.S.richTextBox1.AppendText(StrManipulation.PadLeftX(StrManipulation.VariableNamePolish("组间"), ' ', 12)); MainForm.S.richTextBox1.AppendText("\t"); MainForm.S.richTextBox1.AppendText(StrManipulation.PadLeftX(MathV.NumberPolish((Total_length - Cols_length).ToString()), ' ', 12)); MainForm.S.richTextBox1.AppendText("\t"); MainForm.S.richTextBox1.AppendText(StrManipulation.PadLeftX(MathV.NumberPolish(SSW.ToString()), ' ', 12)); MainForm.S.richTextBox1.AppendText("\t"); MainForm.S.richTextBox1.AppendText(StrManipulation.PadLeftX(MathV.NumberPolish(MSW.ToString()), ' ', 12)); MainForm.S.richTextBox1.AppendText("\r\n"); MainForm.S.richTextBox1.AppendText(StrManipulation.PadLeftX(StrManipulation.VariableNamePolish("总"), ' ', 12)); MainForm.S.richTextBox1.AppendText("\t"); MainForm.S.richTextBox1.AppendText(StrManipulation.PadLeftX(MathV.NumberPolish((Total_length - 1).ToString()), ' ', 12)); MainForm.S.richTextBox1.AppendText("\t"); MainForm.S.richTextBox1.AppendText(StrManipulation.PadLeftX(MathV.NumberPolish(SST.ToString()), ' ', 12)); MainForm.S.richTextBox1.AppendText("\t"); MainForm.S.richTextBox1.AppendText(StrManipulation.PadLeftX(MathV.NumberPolish(MST.ToString()), ' ', 12)); MainForm.S.richTextBox1.AppendText("\r\n"); }
private void button_Regression_Click(object sender, EventArgs e) { string ColNums = textBox_Cols.Text; char[] separator = { ',' }; //string是以逗号分隔的 string[] AllNum = ColNums.Split(separator); //按照逗号分割 List <int> Cols = new List <int>(); foreach (string SingleNum in AllNum) { if (SingleNum != "") { Cols.Add(Convert.ToInt32(SingleNum) - 1); } } int[] AllColNums = Cols.ToArray(); int yCol = Tabulation.FindCol(MainForm.MainDT, comboBox_y.Text); List <List <string> > data = new List <List <string> >(); int RowsCount = MainForm.MainDT.Rows.Count; int InputColsCount = AllColNums.Length; //计算总共要录入的列数 int count = 0; //计算实际录入数据数 List <string> Ydata = new List <string>(); for (int i = 0; i < InputColsCount; i++) { data.Add(new List <string>()); } for (int i = 0; i < RowsCount; i++) { if (Tabulation.IdentifyNARow(MainForm.MainDT, i, AllColNums)) { //确认该行无空格 if (MainForm.MainDT.Rows[i][yCol].ToString().Trim() != "") { Ydata.Add(MainForm.MainDT.Rows[i][yCol].ToString().Trim()); for (int j = 0; j < InputColsCount; j++) { data[j].Add(MainForm.MainDT.Rows[i][j].ToString()); } count++; } } } if (count > 2) { BigDecimal[,] IndependentVariables = new BigDecimal[count, InputColsCount + 1]; //第一列全是1 for (int i = 0; i < count; i++) { IndependentVariables[i, 0] = 1; } for (int i = 0; i < count; i++) { for (int j = 0; j < InputColsCount; j++) { //录入时,BigDecimal数组列数要+1,因为第一列全是1 IndependentVariables[i, j + 1] = data[j][i]; } } BigDecimal[,] DependentVariable = new BigDecimal[count, 1]; for (int i = 0; i < count; i++) { DependentVariable[i, 0] = Ydata[i]; } BigDecimal[,] bhat = Stat.MultiRegBeta(IndependentVariables, DependentVariable); StringBuilder Result = new StringBuilder(); Result.Append(comboBox_y.Text); Result.Append(" = "); int ColumnNumberCount = 0; foreach (BigDecimal EachNum in bhat) { if (ColumnNumberCount == 0) { Result.Append(MathV.NumberPolish(EachNum.ToString())); Result.Append(" + "); } else if (ColumnNumberCount == InputColsCount) { Result.Append(MathV.NumberPolish(EachNum.ToString())); Result.Append(" "); Result.Append(MainForm.MainDT.Columns[AllColNums[ColumnNumberCount - 1]].ColumnName); } else { Result.Append(MathV.NumberPolish(EachNum.ToString())); Result.Append(" "); Result.Append(MainForm.MainDT.Columns[AllColNums[ColumnNumberCount - 1]].ColumnName); Result.Append(" + "); } ColumnNumberCount++; } MainForm.S.richTextBox1.AppendText(Result.ToString()); } }
public static string HypothesisTesting(DataTable MainDT, string ColName, string Statistics, string Operation, string Tail, double Significance, double NullHypothesis) { //假设检验 //Statistics为统计量,Operation为运算(>,<,=) //Tail为单尾双尾,这里内容为双侧、左单侧、右单侧 //Significance为显著性水平 int ColNum = Tabulation.FindCol(MainDT, ColName); string[] Numbers = Tabulation.ReadVector(MainDT, ColNum).ToArray(); StringBuilder Result = new StringBuilder(); Result.Append("假设检验: "); Result.Append(Statistics); Result.Append("\r\n"); Result.Append("显著性水平: "); Result.Append(Significance.ToString()); Result.Append("\t"); Result.Append(Tail); Result.Append("\r\n原假设: "); Result.Append(Statistics); Result.Append(" "); Result.Append(Operation); Result.Append(" "); Result.Append(NullHypothesis.ToString()); Result.Append("\r\n备择假设: "); Result.Append(Statistics); Result.Append(" "); if (Operation == "=") { Result.Append("<>"); } else if (Operation == "<=") { Result.Append(">"); } else { Result.Append("<"); } Result.Append(" "); Result.Append(NullHypothesis.ToString()); Result.Append("\r\n"); BigDecimal sum = 0; BigDecimal mean = 0; BigDecimal Variance = 0; BigDecimal Sd = 0; //Sd为标准差 int count = 0; BigDecimal sum2 = 0; //sum2用于计算数字的平方,方便计算方差 Double TempNum = 0; BigDecimal BigTemp = 0; //BigTemp用于记录BigDecimal类型的临时数据 double Threshold = 0; //Threshold为临界值 double PValue = 0; //Pvalue不用多解释了吧~ foreach (string Num in Numbers) { if (Double.TryParse(Num, out TempNum)) { sum += TempNum; sum2 += (BigDecimal)TempNum * (BigDecimal)TempNum; count++; } } //遍历了该列所有数字 if (Statistics == "均值") { if (count >= 30) { Result.Append(HTHeader_Ztest); } else { Result.Append(HTHeader_Ttest); } if (count <= 1) { return("样本数量过少,无法进行假设检验!\r\n"); } else { //开始计算均值和方差 //方差要乘以调整系数 mean = sum / count; Variance = (sum2 / count - mean * mean) * count / (count - 1); Result.Append(StrManipulation.PadLeftX(StrManipulation.VariableNamePolish(ColName), ' ', 12)); Result.Append("\t"); Result.Append(StrManipulation.PadLeftX(count.ToString(), ' ', 12)); Result.Append("\t"); Result.Append(StrManipulation.PadLeftX(MathV.NumberPolish(mean.ToString()), ' ', 12)); Result.Append("\t"); Result.Append(StrManipulation.PadLeftX(MathV.NumberPolish(MathV.Sqrt(Variance.ToString()).ToString()), ' ', 12)); Result.Append("\t"); if (count >= 30) { //大样本 //z检验 Sd = MathV.Sqrt(Variance.ToString()).ToString(); BigTemp = (mean - NullHypothesis) / (Sd / Math.Sqrt(count)); //BigTemp此时的值为z检验统计量 //样本数count无需大数开方,用普通方法开方即可 if (Tail == "双侧") { Threshold = NORMSINV(1 - Significance / 2); PValue = 2 * (1 - NORMDIST(Math.Abs(Convert.ToDouble(BigTemp.ToString())))); } else if (Tail == "左单侧") { Threshold = NORMSINV(Significance); PValue = 1 - NORMDIST(Math.Abs(Convert.ToDouble(BigTemp.ToString()))); } else if (Tail == "右单侧") { Threshold = NORMSINV(1 - Significance); PValue = 1 - NORMDIST(Math.Abs(Convert.ToDouble(BigTemp.ToString()))); } Result.Append(StrManipulation.PadLeftX(MathV.NumberPolish(BigTemp.ToString()), ' ', 12)); Result.Append("\t"); Result.Append(StrManipulation.PadLeftX(MathV.NumberPolish(Threshold.ToString()), ' ', 12)); Result.Append("\t"); Result.Append(StrManipulation.PadLeftX(MathV.NumberPolish(PValue.ToString()), ' ', 12)); Result.Append("\r\n"); if (PValue < Significance) { Result.Append("在"); Result.Append(Significance.ToString()); Result.Append("的显著性水平上拒绝原假设"); } else { Result.Append("在"); Result.Append(Significance.ToString()); Result.Append("的显著性水平上不拒绝原假设"); } } else { //小样本 //t检验 Sd = MathV.Sqrt(Variance.ToString()).ToString(); BigTemp = (mean - NullHypothesis) / (Sd / Math.Sqrt(count)); //BigTemp此时的值为z检验统计量 //样本数count无需大数开方,用普通方法开方即可 if (Tail == "双侧") { Threshold = TINV(Significance / 2 / 2, count - 1); //每个放进TINV的值都要除以2,这个bug之后会进行修复 PValue = TDIST(Math.Abs(Convert.ToDouble(BigTemp.ToString()) / 2), count - 1, 2); } else if (Tail == "左单侧") { Threshold = TINV(Significance / 2, count - 1); PValue = TDIST(Math.Abs(Convert.ToDouble(BigTemp.ToString()) / 2), count - 1, 1); } else if (Tail == "右单侧") { Threshold = TINV(Significance / 2, count - 1); PValue = TDIST(Math.Abs(Convert.ToDouble(BigTemp.ToString()) / 2), count - 1, 1); } Result.Append(StrManipulation.PadLeftX(MathV.NumberPolish(BigTemp.ToString()), ' ', 12)); Result.Append("\t"); Result.Append(StrManipulation.PadLeftX(MathV.NumberPolish(Threshold.ToString()), ' ', 12)); Result.Append("\t"); Result.Append(StrManipulation.PadLeftX(MathV.NumberPolish(PValue.ToString()), ' ', 12)); Result.Append("\r\n"); if (PValue < Significance) { Result.Append("在"); Result.Append(Significance.ToString()); Result.Append("的显著性水平上拒绝原假设"); } else { Result.Append("在"); Result.Append(Significance.ToString()); Result.Append("的显著性水平上不拒绝原假设"); } } Result.Append("\r\n"); return(Result.ToString()); } } else if (Statistics == "比率") { return("hhh"); } else { //方差 return("hhh"); } }
public static string QuickSummary(DataTable MainDT, int ColNum) { string ColName = MainDT.Columns[ColNum].ColumnName; List <string> Numbers = Tabulation.ReadVector(MainDT, ColNum); StringBuilder Result = new StringBuilder(); BigDecimal sum = 0; BigDecimal mean = 0; BigDecimal Variance = 0; int count = 0; BigDecimal max = 0; BigDecimal min = 0; BigDecimal sum2 = 0; Double TempNum = 0; //sum2用于计算数字的平方,方便计算方差 foreach (string Num in Numbers) { if (Double.TryParse(Num, out TempNum)) { sum += TempNum; sum2 += (BigDecimal)TempNum * (BigDecimal)TempNum; count++; if (count == 1) { max = TempNum; min = TempNum; } else { if (max < TempNum) { max = TempNum; } if (min > TempNum) { min = TempNum; } } } } if (count == 0) { //如果没有获取任何数字 return(""); } else if (count == 1) { //如果只有一个数字,可以计算均值,但是不可以计算标准差 mean = sum / count; Result.Append(StrManipulation.PadLeftX(StrManipulation.VariableNamePolish(ColName), ' ', 12)); Result.Append("\t"); Result.Append(StrManipulation.PadLeftX(count.ToString(), ' ', 12)); Result.Append("\t"); Result.Append(StrManipulation.PadLeftX(MathV.NumberPolish(mean.ToString()), ' ', 12)); Result.Append("\t"); Result.Append(StrManipulation.PadLeftX("NA", ' ', 12)); Result.Append("\t"); Result.Append(StrManipulation.PadLeftX(MathV.NumberPolish(min.ToString()), ' ', 12)); Result.Append("\t"); Result.Append(StrManipulation.PadLeftX(MathV.NumberPolish(max.ToString()), ' ', 12)); Result.Append("\r\n"); //Temp += Result; return(Result.ToString()); } else { //开始计算均值和方差 //方差要乘以调整系数 mean = sum / count; Variance = (sum2 / count - mean * mean) * count / (count - 1); Result.Append(StrManipulation.PadLeftX(StrManipulation.VariableNamePolish(ColName), ' ', 12)); Result.Append("\t"); Result.Append(StrManipulation.PadLeftX(count.ToString(), ' ', 12)); Result.Append("\t"); Result.Append(StrManipulation.PadLeftX(MathV.NumberPolish(mean.ToString()), ' ', 12)); Result.Append("\t"); Result.Append(StrManipulation.PadLeftX(MathV.NumberPolish(MathV.Sqrt(Variance.ToString()).ToString()), ' ', 12)); Result.Append("\t"); Result.Append(StrManipulation.PadLeftX(MathV.NumberPolish(min.ToString()), ' ', 12)); Result.Append("\t"); Result.Append(StrManipulation.PadLeftX(MathV.NumberPolish(max.ToString()), ' ', 12)); Result.Append("\r\n"); return(Result.ToString()); } }
public static string CI(string VarName, string [] Numbers, string Tail, string Statistics, double Significance) { //Confidence Interval 置信区间 //Tail为单双尾,Tail = "单尾",Tail = "双尾" //Statistics为统计量 "均值" "比例" int count = 0; //count用于统计样本个数 StringBuilder result = new StringBuilder(); //result记录反馈结果 BigDecimal Sd = 0; //Sd为标准差 double Temp = 0; //Temp用于记录临时数据 BigDecimal BigTemp = 0; //BigTemp用于记录BigDecimal类型的临时数据 if (Tail == "双尾") { Significance = 1 - (1 - Significance) / 2; // 1 - (1 - 0.95)/2 } if (Statistics == "均值" || Statistics == "方差") { BigDecimal Mean = 0; BigDecimal Sum = 0; BigDecimal Sum2 = 0; foreach (string Num in Numbers) { if (double.TryParse(Num, out Temp)) { Sum += Temp; Sum2 += (BigDecimal)Temp * (BigDecimal)Temp; count++; } } if (count <= 1) { return("样本数量过少,无法进行参数估计"); } else { Mean = Sum / count; BigDecimal Variance = (Sum2 / count - Mean * Mean) * count / (count - 1); Sd = MathV.Sqrt(Variance.ToString()).ToString(); result.Append(StrManipulation.PadLeftX(StrManipulation.VariableNamePolish(VarName), ' ', 12)); result.Append("\t"); result.Append(StrManipulation.PadLeftX(count.ToString(), ' ', 12)); result.Append("\t"); result.Append(StrManipulation.PadLeftX(MathV.NumberPolish(Mean.ToString()), ' ', 12)); result.Append("\t"); result.Append(StrManipulation.PadLeftX(MathV.NumberPolish(Sd.ToString()), ' ', 12)); result.Append("\t"); if (Statistics == "均值") { if (count >= 30) { //大样本 BigTemp = NORMSINV(Significance) * Sd / MathV.Sqrt(count).ToString(); } else { //小样本 BigTemp = TINV(1 - Significance, (count - 1)) * Sd / MathV.Sqrt(count).ToString(); } result.Append(StrManipulation.PadLeftX(MathV.NumberPolish((Mean - BigTemp).ToString()), ' ', 12)); result.Append("\t"); result.Append(StrManipulation.PadLeftX(MathV.NumberPolish((Mean + BigTemp).ToString()), ' ', 12)); result.Append("\r\n"); } else { //Statistics == "方差" BigTemp = (count - 1) * Variance; result.Append(StrManipulation.PadLeftX(MathV.NumberPolish((BigTemp / Stat.chi2Ua(1 - Significance, count - 1)).ToString()), ' ', 12)); result.Append("\t"); MessageBox.Show(Significance.ToString()); MessageBox.Show(Stat.chi2Ua(Significance, count - 1).ToString()); result.Append(StrManipulation.PadLeftX(MathV.NumberPolish((BigTemp / Stat.chi2Ua(Significance, count - 1)).ToString()), ' ', 12)); result.Append("\r\n"); } return(result.ToString()); } } else if (Statistics == "比例") { int OneCounts = 0; int ZeroCounts = 0; foreach (string Num in Numbers) { if (Num.Trim() == "1") { OneCounts++; } else if (Num.Trim() == "0") { ZeroCounts++; } } if (ZeroCounts + OneCounts == 0) { return("样本不存在,无法进行参数估计\r\n"); } else { if (ZeroCounts + OneCounts < 30) { result.Append("样本数量较少,进行比例的参数估计可能会不稳定\r\n"); } BigDecimal Proportion = (BigDecimal)OneCounts / ((BigDecimal)ZeroCounts + (BigDecimal)OneCounts); count = ZeroCounts + OneCounts; result.Append(StrManipulation.PadLeftX(StrManipulation.VariableNamePolish(VarName), ' ', 12)); result.Append("\t"); result.Append(StrManipulation.PadLeftX((ZeroCounts + OneCounts).ToString(), ' ', 12)); result.Append("\t"); result.Append(StrManipulation.PadLeftX(MathV.NumberPolish(Proportion.ToString()), ' ', 12)); result.Append("\t"); BigTemp = Proportion * (1 - Proportion); result.Append(StrManipulation.PadLeftX(MathV.NumberPolish(MathV.Sqrt(BigTemp.ToString()).ToString()), ' ', 12)); result.Append("\t"); BigTemp = NORMSINV(Significance) * ((BigDecimal)MathV.Sqrt(BigTemp.ToString()).ToString() / (BigDecimal)MathV.Sqrt(count).ToString()); result.Append(StrManipulation.PadLeftX(MathV.NumberPolish((Proportion - BigTemp).ToString()).ToString(), ' ', 12)); result.Append("\t"); result.Append(StrManipulation.PadLeftX(MathV.NumberPolish((Proportion + BigTemp).ToString()).ToString(), ' ', 12)); result.Append("\r\n"); return(result.ToString()); } } else { return("未知统计量\r\n"); } }
private void button1_Click(object sender, EventArgs e) { string ColNums = textBox_Cols.Text; char[] separator = { ',' }; //string是以逗号分隔的 string[] AllNum = ColNums.Split(separator); //按照逗号分割 List <int> Cols = new List <int>(); foreach (string SingleNum in AllNum) { if (SingleNum != "") { Cols.Add(Convert.ToInt32(SingleNum) - 1); } } int[] AllColNums = Cols.ToArray(); //MessageBox.Show(AllColNums[0].ToString()); //MessageBox.Show(AllColNums[1].ToString()); int yCol = Tabulation.FindCol(MainForm.MainDT, comboBox_y.Text); List <List <string> > data = new List <List <string> >(); int RowsCount = MainForm.MainDT.Rows.Count; int InputColsCount = AllColNums.Length; //计算总共要录入的列数 int count = 0; //计算实际录入数据数 List <string> Ydata = new List <string>(); for (int i = 0; i < InputColsCount; i++) { data.Add(new List <string>()); } for (int i = 0; i < RowsCount; i++) { if (Tabulation.IdentifyNARow(MainForm.MainDT, i, AllColNums)) { //确认该行无空格 if (MainForm.MainDT.Rows[i][yCol].ToString().Trim() != "") { Ydata.Add(MainForm.MainDT.Rows[i][yCol].ToString().Trim()); for (int j = 0; j < InputColsCount; j++) { data[j].Add(MainForm.MainDT.Rows[i][AllColNums[j]].ToString()); //此处队长有bug,已改正 } count++; } } } if (count > 2) { BigDecimal[,] IndependentVariables = new BigDecimal[count, InputColsCount + 1]; StringBuilder Result = new StringBuilder(); if (count <= InputColsCount + 1) { Result.Append("样本量过少,无法估计"); } else { //第一列全是1 for (int i = 0; i < count; i++) { IndependentVariables[i, 0] = 1; } for (int i = 0; i < count; i++) { for (int j = 0; j < InputColsCount; j++) { //录入时,BigDecimal数组列数要+1,因为第一列全是1 IndependentVariables[i, j + 1] = data[j][i]; } } //MathV.ArrayPrint(IndependentVariables); BigDecimal[,] DependentVariable = new BigDecimal[count, 1]; for (int i = 0; i < count; i++) { DependentVariable[i, 0] = Ydata[i]; } int len12 = IndependentVariables.GetLength(1); //列数 BigDecimal[,] b1 = MathV.MatTrans(IndependentVariables); BigDecimal[,] b2 = MathV.MatTimes(b1, IndependentVariables); BigDecimal[,] b3 = MathV.MatInv(b2, len12); if (b3 != null) { BigDecimal[,] bhat = Stat.MultiRegBeta(b1, b3, IndependentVariables, DependentVariable); BigDecimal[,] value_beta = Stat.MultiRegP(b3, bhat, IndependentVariables, DependentVariable); string[] RandF = Stat.MultiRegR(bhat, IndependentVariables, DependentVariable).Split(separator); //回归诊断开始 //#初步判断 : 根据R^2 adjR^2 Fvalue tvalue Result.Append("1.初步诊断 \r\n"); if ((BigDecimal)RandF[1] < 0.70) { Result.Append(" 调整后R^2较小,可能存在遗漏变量\r\n"); } if ((BigDecimal)RandF[0] - (BigDecimal)RandF[1] > 0.1) { Result.Append(" 调整后R^2与R^2差值较大,可能存在冗余变量\r\n"); } if (Stat.FDIST(Convert.ToDouble(RandF[2]), InputColsCount, count - 1) > 0.01) { Result.Append(" 模型拟合情况不好,建议更改解释变量\r\n"); } else { Result.Append(" 模型拟合情况较好\r\n"); } int count_nonsignifi = 0; for (int i = 0; i < InputColsCount + 1; i++) { if (i == 0) { if (value_beta[i, 0] > 0.05) { Result.Append(" 常数项在95%置信水平下不显著\r\n"); count_nonsignifi++; } } else { if (value_beta[i, 0] > 0.05) { Result.Append(" 变量" + (i + 1).ToString() + "在95%置信水平下不显著\r\n"); count_nonsignifi++; } } } if ((BigDecimal)RandF[0] > 0.7 && count_nonsignifi > ((InputColsCount + 1) / 2)) { Result.Append(" 高拟合优度伴随着大量非显著解释变量,存在多重共线性\r\n"); } Result.Append("\r\n"); //#遗漏变量 : 拉姆齐检验 Result.Append("2.模型设定偏误检验(拉姆齐检验)\r\n"); BigDecimal[,] Y_hat = new BigDecimal[count, 1]; Y_hat = MathV.MatTimes(IndependentVariables, bhat); BigDecimal[,] Ramsey_indep = new BigDecimal[count, InputColsCount + 3]; for (int i = 0; i < count; i++) { for (int j = 0; j < InputColsCount + 3; j++) { if (j < InputColsCount + 1) { Ramsey_indep[i, j] = IndependentVariables[i, j]; } else if (j == InputColsCount + 1) { Ramsey_indep[i, j] = Y_hat[i, 0] * Y_hat[i, 0]; } else { Ramsey_indep[i, j] = Y_hat[i, 0] * Y_hat[i, 0] * Y_hat[i, 0]; } } } BigDecimal[,] b1_ramsey = MathV.MatTrans(Ramsey_indep); BigDecimal[,] b2_ramsey = MathV.MatTimes(b1_ramsey, Ramsey_indep); BigDecimal[,] b3_ramsey = MathV.MatInv(b2_ramsey, InputColsCount + 3); if (b3_ramsey != null && count > InputColsCount + 3) { BigDecimal[,] bhat_ramsey = Stat.MultiRegBeta(b1_ramsey, b3_ramsey, Ramsey_indep, DependentVariable); string[] RandF_ramsey = Stat.MultiRegR(bhat_ramsey, Ramsey_indep, DependentVariable).Split(separator); double R2_ramsey = Convert.ToDouble(RandF_ramsey[0]); double F_ramsey = (R2_ramsey - Convert.ToDouble(RandF[0])) * (count - InputColsCount - 2) / ((1 - R2_ramsey) * 2); double F_P_ramsey = Stat.FDIST(Convert.ToDouble(RandF_ramsey[2]), 2, count - InputColsCount - 2); if (F_P_ramsey <= 0.001) { Result.Append(" 模型在99.9%的置信水平下认为存在偏误\r\n"); } else { Result.Append(" 模型设定在99.9%的置信水平下认为无偏误\r\n"); } } else { Result.Append(" !样本量过少,无法进行拉姆齐检验\r\n"); } Result.Append(""); Result.Append("\r\n3.多重共线性检验:方差膨胀因子(VIF)\r\n"); //#多重共线性检验:方差膨胀因子(VIF) int count_vif; if (InputColsCount == 1) { Result.Append(" !一个自变量无法进行方差膨胀因子检验\r\n"); } else { for (int i = 0; i < InputColsCount; i++) { BigDecimal[,] Vif_dep = new BigDecimal[count, 1]; BigDecimal[,] Vif_indep = new BigDecimal[count, InputColsCount]; for (int j = 0; j < count; j++) { count_vif = 0; Vif_dep[j, 0] = IndependentVariables[j, i + 1]; for (int k = 0; k < InputColsCount + 1; k++) { if (k != i + 1) { Vif_indep[j, count_vif] = IndependentVariables[j, k]; count_vif++; } } } //MathV.ArrayPrint(Vif_indep); // MathV.ArrayPrint(Vif_dep); BigDecimal[,] b1_vif = MathV.MatTrans(Vif_indep); BigDecimal[,] b2_vif = MathV.MatTimes(b1_vif, Vif_indep); BigDecimal[,] b3_vif = MathV.MatInv(b2_vif, InputColsCount); if (b3_vif != null) { BigDecimal[,] bhat_vif = Stat.MultiRegBeta(b1_vif, b3_vif, Vif_indep, Vif_dep); string[] RandF_vif = Stat.MultiRegR(bhat_vif, Vif_indep, Vif_dep).Split(separator); //MessageBox.Show(RandF_vif[0]); BigDecimal VIF = 1 / (1 - Convert.ToDouble(RandF_vif[0])); if (VIF <= 10) { Result.Append(" 变量" + (i + 1).ToString() + "在模型中共线性较弱,可以保留\r\n"); } else if (VIF > 10 && VIF < 100) { Result.Append(" 变量" + (i + 1).ToString() + "在模型中共线性较强,建议剔除\r\n"); } else { Result.Append(" 变量" + (i + 1).ToString() + "在模型中共线性极强,强烈建议剔除\r\n"); } } } } //#异方差 : White 检验 Result.Append("\r\n4.异方差检验:怀特检验与LM统计量\r\n"); //Y_hat2 与 u^hat2 BigDecimal[,] u_hat2 = new BigDecimal[count, 1]; BigDecimal[,] u_hat = new BigDecimal[count, 1]; BigDecimal[,] Y_hat2 = new BigDecimal[count, 1]; BigDecimal[,] White_indep = new BigDecimal[count, 3]; BigDecimal[,] White_dep = new BigDecimal[count, 1]; for (int i = 0; i < count; i++) { u_hat2[i, 0] = (DependentVariable[i, 0] - Y_hat[i, 0]) * (DependentVariable[i, 0] - Y_hat[i, 0]); Y_hat2[i, 0] = Y_hat[i, 0] * Y_hat[i, 0]; u_hat[i, 0] = (DependentVariable[i, 0] - Y_hat[i, 0]); White_indep[i, 0] = 1; White_indep[i, 1] = Y_hat[i, 0]; White_indep[i, 2] = Y_hat2[i, 0]; White_dep[i, 0] = u_hat2[i, 0]; } BigDecimal[,] b1_white = MathV.MatTrans(White_indep); BigDecimal[,] b2_white = MathV.MatTimes(b1_white, White_indep); BigDecimal[,] b3_white = MathV.MatInv(b2_white, 3); if (b3_white != null) { BigDecimal[,] bhat_white = Stat.MultiRegBeta(b1_white, b3_white, White_indep, White_dep); string[] RandF_white = Stat.MultiRegR(bhat_white, White_indep, White_dep).Split(separator); BigDecimal LM_white = count * Convert.ToDouble(RandF_white[0]); //MessageBox.Show(LM_white.ToString()); BigDecimal P_white = 1 - Stat.chi2(Convert.ToDouble(LM_white.ToString()), 2); //MessageBox.Show(P_white2.ToString()); Result.Append(" LM统计量 \t"); Result.Append(" =\t"); Result.Append(StrManipulation.PadLeftX(MathV.NumberPolish(LM_white.ToString()), ' ', 12)); Result.Append("\r\n"); Result.Append(" LM > chi(2)\t"); Result.Append(" =\t"); Result.Append(StrManipulation.PadLeftX(MathV.NumberPolish(P_white.ToString()), ' ', 12)); Result.Append("\r\n"); if (P_white > 0.05) { Result.Append(" 在95%的置信度上认为样本同方差\r\n \r\n"); } else { Result.Append(" 在95%的置信度上认为样本异方差\r\n"); Result.Append(" 解决方法:变量取对数回归,结果如下:\r\n"); BigDecimal[,] ln_indep = new BigDecimal[count, InputColsCount + 1]; BigDecimal[,] ln_dep = new BigDecimal[count, 1]; for (int i = 0; i < count; i++) { for (int j = 0; j < InputColsCount; j++) { ln_indep[i, j + 1] = Math.Log(Convert.ToDouble(IndependentVariables[i, j + 1].ToString())); } ln_indep[i, 0] = 1; ln_dep[i, 0] = Math.Log(Convert.ToDouble(DependentVariable[i, 0].ToString())); } BigDecimal[,] b1_ln = MathV.MatTrans(ln_indep); BigDecimal[,] b2_ln = MathV.MatTimes(b1_ln, ln_indep); BigDecimal[,] b3_ln = MathV.MatInv(b2, len12); if (b3_ln != null) { BigDecimal[,] bhat_ln = Stat.MultiRegBeta(b1_ln, b3_ln, ln_indep, ln_dep); BigDecimal[,] value_beta_ln = Stat.MultiRegP(b3_ln, bhat_ln, ln_indep, ln_dep); string[] RandF_ln = Stat.MultiRegR(bhat_ln, ln_indep, ln_dep).Split(separator); //MathV.ArrayPrint(bhat); Result.Append(StrManipulation.PadRightX(StrManipulation.VariableNamePolish("拟合R^2:"), ' ', 12)); Result.Append(MathV.NumberPolish(RandF_ln[0])); Result.Append("\r\n"); Result.Append(StrManipulation.PadRightX(StrManipulation.VariableNamePolish("调整后R^2:"), ' ', 12)); Result.Append(MathV.NumberPolish(RandF_ln[1])); Result.Append("\r\n"); Result.Append(StrManipulation.PadRightX(StrManipulation.VariableNamePolish("回归F值:"), ' ', 12)); Result.Append(MathV.NumberPolish(RandF_ln[2])); Result.Append("\r\n"); Result.Append("\r\n"); Result.Append(comboBox_y.Text); Result.Append(" = "); int ColumnNumberCount = 0; foreach (BigDecimal EachNum in bhat_ln) { if (ColumnNumberCount == 0) { Result.Append(MathV.NumberPolish(EachNum.ToString())); Result.Append(" + "); } else if (ColumnNumberCount == InputColsCount) { Result.Append(MathV.NumberPolish(EachNum.ToString())); Result.Append(" "); Result.Append(" ln " + MainForm.MainDT.Columns[AllColNums[ColumnNumberCount - 1]].ColumnName); } else { Result.Append(MathV.NumberPolish(EachNum.ToString())); Result.Append(" ln " + MainForm.MainDT.Columns[AllColNums[ColumnNumberCount - 1]].ColumnName); Result.Append(" + "); } ColumnNumberCount++; } Result.Append("\r\n检验P值: "); for (int i = 0; i < InputColsCount + 1; i++) { Result.Append(MathV.NumberPolish(value_beta_ln[i, 0].ToString())); Result.Append(" \t"); } Result.Append("\r\n检验t值: "); for (int i = 0; i < InputColsCount + 1; i++) { Result.Append(MathV.NumberPolish(value_beta_ln[i, 1].ToString())); Result.Append(" \t"); } Result.Append("\r\n\r\n"); } else { Result.Append("矩阵不可逆,回归方程无解 \r\n"); } } } //#自相关(3阶内) : Result.Append("5.自相关检验:拉格朗日乘子(LM) \r\n"); BigDecimal[,] u_hat_Auto = new BigDecimal[count - 3, 1]; BigDecimal[,] indep_Auto = new BigDecimal[count - 3, InputColsCount + 4]; for (int i = 0; i < count - 3; i++) { u_hat_Auto[i, 0] = u_hat[i + 3, 0]; for (int j = 0; j < InputColsCount + 1; j++) { indep_Auto[i, j] = IndependentVariables[i + 3, j]; } indep_Auto[i, InputColsCount + 1] = u_hat[i + 2, 0]; indep_Auto[i, InputColsCount + 2] = u_hat[i + 1, 0]; indep_Auto[i, InputColsCount + 3] = u_hat[i, 0]; } BigDecimal[,] b1_Auto = MathV.MatTrans(indep_Auto); BigDecimal[,] b2_Auto = MathV.MatTimes(b1_Auto, indep_Auto); BigDecimal[,] b3_Auto = MathV.MatInv(b2_Auto, InputColsCount + 4); if (b3_Auto != null) { BigDecimal[,] bhat_Auto = Stat.MultiRegBeta(b1_Auto, b3_Auto, indep_Auto, u_hat_Auto); string[] RandF_Auto = Stat.MultiRegR(bhat_Auto, indep_Auto, u_hat_Auto).Split(separator); BigDecimal LM_Auto = (count - 3) * Convert.ToDouble(RandF_Auto[0].ToString()); BigDecimal P_Auto = 1 - Stat.chi2(Convert.ToDouble(LM_Auto.ToString()), 3); Result.Append(" LM统计量 \t"); Result.Append(" =\t"); Result.Append(StrManipulation.PadLeftX(MathV.NumberPolish(LM_Auto.ToString()), ' ', 12)); Result.Append("\r\n"); Result.Append(" LM > chi(2)\t"); Result.Append(" =\t"); Result.Append(StrManipulation.PadLeftX(MathV.NumberPolish(P_Auto.ToString()), ' ', 12)); Result.Append("\r\n"); if (P_Auto <= 0.05) { Result.Append(" 在95%的置信水平下拒绝原假设,认为模型序列存在自相关\r\n \r\n"); } else { Result.Append(" 在95%的置信水平下接受原假设,认为模型序列不存在自相关\r\n \r\n"); } } MainForm.S.richTextBox1.AppendText(Result.ToString()); } } } }
private void button_Regression_Click(object sender, EventArgs e) { string ColNums = textBox_Cols.Text; char[] separator = { ',' }; //string是以逗号分隔的 string[] AllNum = ColNums.Split(separator); //按照逗号分割 List <int> Cols = new List <int>(); foreach (string SingleNum in AllNum) { if (SingleNum != "") { Cols.Add(Convert.ToInt32(SingleNum) - 1); } } int[] AllColNums = Cols.ToArray(); //MessageBox.Show(AllColNums[0].ToString()); //MessageBox.Show(AllColNums[1].ToString()); int yCol = Tabulation.FindCol(MainForm.MainDT, comboBox_y.Text); List <List <string> > data = new List <List <string> >(); int RowsCount = MainForm.MainDT.Rows.Count; int InputColsCount = AllColNums.Length; //计算总共要录入的列数 int count = 0; //计算实际录入数据数 List <string> Ydata = new List <string>(); for (int i = 0; i < InputColsCount; i++) { data.Add(new List <string>()); } for (int i = 0; i < RowsCount; i++) { if (Tabulation.IdentifyNARow(MainForm.MainDT, i, AllColNums)) { //确认该行无空格 if (MainForm.MainDT.Rows[i][yCol].ToString().Trim() != "") { Ydata.Add(MainForm.MainDT.Rows[i][yCol].ToString().Trim()); for (int j = 0; j < InputColsCount; j++) { data[j].Add(MainForm.MainDT.Rows[i][AllColNums[j]].ToString()); //此处队长有bug,已改正 } count++; } } } if (count > 2) { BigDecimal[,] IndependentVariables = new BigDecimal[count, InputColsCount + 1]; StringBuilder Result = new StringBuilder(); if (count <= InputColsCount + 1) { Result.Append("样本量过少,无法估计"); } else { //第一列全是1 for (int i = 0; i < count; i++) { IndependentVariables[i, 0] = 1; } for (int i = 0; i < count; i++) { for (int j = 0; j < InputColsCount; j++) { //录入时,BigDecimal数组列数要+1,因为第一列全是1 IndependentVariables[i, j + 1] = data[j][i]; } } //MathV.ArrayPrint(IndependentVariables); BigDecimal[,] DependentVariable = new BigDecimal[count, 1]; for (int i = 0; i < count; i++) { DependentVariable[i, 0] = Ydata[i]; } int len12 = IndependentVariables.GetLength(1);//列数 BigDecimal[,] b1 = MathV.MatTrans(IndependentVariables); BigDecimal[,] b2 = MathV.MatTimes(b1, IndependentVariables); try { BigDecimal[,] b3 = MathV.MatInv(b2, len12); if (b3 != null) { BigDecimal[,] bhat = Stat.MultiRegBeta(b1, b3, IndependentVariables, DependentVariable); BigDecimal[,] value_beta = Stat.MultiRegP(b3, bhat, IndependentVariables, DependentVariable); string[] RandF = Stat.MultiRegR(bhat, IndependentVariables, DependentVariable).Split(separator); //MathV.ArrayPrint(bhat); Result.Append(StrManipulation.PadRightX(StrManipulation.VariableNamePolish("拟合R^2:"), ' ', 12)); Result.Append(MathV.NumberPolish(RandF[0])); Result.Append("\r\n"); Result.Append(StrManipulation.PadRightX(StrManipulation.VariableNamePolish("调整后R^2:"), ' ', 12)); Result.Append(MathV.NumberPolish(RandF[1])); Result.Append("\r\n"); Result.Append(StrManipulation.PadRightX(StrManipulation.VariableNamePolish("回归F值:"), ' ', 12)); Result.Append(MathV.NumberPolish(RandF[2])); Result.Append("\r\n"); Result.Append(StrManipulation.PadRightX(StrManipulation.VariableNamePolish("回归P值:"), ' ', 12)); Result.Append(MathV.NumberPolish(Stat.FINV(Convert.ToDouble(RandF[2]), count - InputColsCount - 1, InputColsCount).ToString())); Result.Append("\r\n \r\n"); Result.Append(comboBox_y.Text + " = "); int ColumnNumberCount = 0; foreach (BigDecimal EachNum in bhat) { if (ColumnNumberCount == 0) { Result.Append(MathV.NumberPolish(EachNum.ToString())); Result.Append(" + "); } else if (ColumnNumberCount == InputColsCount) { Result.Append(MathV.NumberPolish(EachNum.ToString())); Result.Append(" "); Result.Append(MainForm.MainDT.Columns[AllColNums[ColumnNumberCount - 1]].ColumnName); } else { Result.Append(MathV.NumberPolish(EachNum.ToString())); Result.Append(MainForm.MainDT.Columns[AllColNums[ColumnNumberCount - 1]].ColumnName); Result.Append(" + "); } ColumnNumberCount++; } Result.Append("\r\n检验P值: "); for (int i = 0; i < InputColsCount + 1; i++) { Result.Append(MathV.NumberPolish(value_beta[i, 0].ToString())); Result.Append(" \t"); } Result.Append("\r\n检验t值: "); for (int i = 0; i < InputColsCount + 1; i++) { Result.Append(MathV.NumberPolish(value_beta[i, 1].ToString())); Result.Append(" \t"); } Result.Append("\r\n \r\n"); } else { Result.Append("矩阵不可逆,回归方程无解"); } } catch (Exception ex) { MessageBox.Show("存在重复自变量"); } } MainForm.S.richTextBox1.AppendText(Result.ToString()); } }
private void button_chart_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; foreach (string Num in NumberSeries) { if (Num != "" && Num != null) { length++; } } int period = 0; if (Int32.TryParse(textBox_period.Text, out period)) { BigDecimal[] cumsum = new BigDecimal[length]; for (int i = 0; i < length; i++) { cumsum[i] = 0; for (int j = 0; j <= i; j++) { cumsum[i] += NumberSeries[j]; } } BigDecimal[] C = new BigDecimal[length - 1]; for (int i = 0; i < length - 1; i++) { C[i] = 0 - (cumsum[i] + cumsum[i + 1]) / 2; } BigDecimal[,] D = new BigDecimal[length - 1, 1]; for (int i = 0; i < length - 1; i++) { D[i, 0] = NumberSeries[i + 1]; } BigDecimal[,] E = new BigDecimal[2, length - 1]; for (int i = 0; i < length - 1; i++) { E[1, i] = 1; E[0, i] = C[i]; } //BigNumber[,] c = MathV.MatTimes(MathV.MatTimes(MathV.MatInv(MathV.MatTimes(E, MathV.MatTrans(E)), 2), E), MathV.MatTrans(D)); BigDecimal[,] c = MathV.MatTimes(MathV.MatTimes(MathV.MatInv(MathV.MatTimes(E, MathV.MatTrans(E)), 2), E), D); BigDecimal a = c[0, 0]; BigDecimal b = c[1, 0]; BigDecimal t = b / a; BigDecimal[] F = new BigDecimal[length + period]; for (int i = 0; i < length + period; i++) { BigDecimal e1 = 2.718281828; F[i] = (NumberSeries[0] - t) / Math.Exp((i) * Convert.ToDouble(a.ToString())) + t; } BigDecimal[] G = new BigDecimal[length + period]; G[0] = NumberSeries[0]; for (int i = 1; i < length + period; i++) { G[i] = F[i] - F[i - 1]; } Result.Append(StrManipulation.PadRightX("灰色预测GM(1,1)模型", ' ', 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)); Result.Append("\t"); } Result.Append("\r\n"); Result.Append(StrManipulation.PadLeftX("预测值", ' ', 12)); Result.Append("\t"); for (int i = 0; i < period + length; i++) { Result.Append(StrManipulation.PadLeftX(MathV.NumberPolish(G[i].ToString()), ' ', 12)); Result.Append("\t"); } Result.Append("\r\n"); Result.Append("\r\n"); chart_timeseries.Series.Clear(); Series series3 = new Series("原数据(点)"); Series series4 = new Series("灰色预测数据"); series3.MarkerStyle = MarkerStyle.Circle; series3.MarkerSize = 6; series4.BorderWidth = 2; series3.Color = Color.MidnightBlue; series4.Color = Color.Maroon; series3.ChartType = SeriesChartType.Point; series4.ChartType = SeriesChartType.Line; for (int i = 0; i < length; i++) { series3.Points.AddXY(i + 1, Convert.ToDouble(NumberSeries[i].ToString())); } for (int i = 0; i < period + length; i++) { series4.Points.AddXY(i + 1, Convert.ToDouble(G[i].ToString())); } chart_timeseries.Series.Add(series3); chart_timeseries.Series.Add(series4); } else { MessageBox.Show("请输入预测期数"); } } else { MessageBox.Show("无此列"); } }
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("无此列"); } }
private void button1_Click(object sender, EventArgs e) { int ProvinceNum = 0; int VariableNum = 0; if (Int32.TryParse(texbox_province.Text, out ProvinceNum)) { if (Int32.TryParse(textBox_variable.Text, out VariableNum)) { string[] Province_name = Tabulation.ReadVector(MainForm.MainDT, ProvinceNum - 1).ToArray(); string[] Variable_data = Tabulation.ReadVector(MainForm.MainDT, VariableNum - 1).ToArray(); double[] data1 = new double[31]; string[] match = new string[] { "黑", "内", "新", "吉", "辽", "甘", "河北", "京", "山西", "津", "陕", "夏", "青", "山东", "藏", "河南", "苏", "安", "川", "湖北", "重", "上", "浙", "湖南", "江西", "云", "贵", "福", "广西", "海南", "广东" }; for (int i = 0; i < 31; i++) { for (int j = 0; j < 31; j++) { if (Province_name[j].IndexOf(match[i]) > -1) { data1[i] = Convert.ToDouble(Variable_data[j]); break; } } } string str2 = Environment.CurrentDirectory; DataTable dt = new DataTable(); DataTable dt_fill = new DataTable(); string coor_path = str2 + "\\coordinates_tiny.csv"; string coor_fill = str2 + "\\coordinates_tinyfill.csv"; char[] separators = new char[1]; separators[0] = ','; dt = Tabulation.LoadFromCSVFile(coor_path, separators); dt_fill = Tabulation.LoadFromCSVFile(coor_fill, separators); int[] X_fill = new int[dt_fill.Rows.Count]; int[] Y_fill = new int[dt_fill.Rows.Count]; int[] Num_count = new int[31]; //31个省的填充坐标点数 Num_count[0] = 5436; Num_count[1] = 18313; Num_count[2] = 35839; Num_count[3] = 37966; Num_count[4] = 39530; Num_count[5] = 43696; Num_count[6] = 45647; Num_count[7] = 45814; Num_count[8] = 47410; Num_count[9] = 47531; Num_count[10] = 49571; Num_count[11] = 50103; Num_count[12] = 57224; Num_count[13] = 58756; Num_count[14] = 70210; Num_count[15] = 71816; Num_count[16] = 72785; Num_count[17] = 74121; Num_count[18] = 78660; Num_count[19] = 80410; Num_count[20] = 81181; Num_count[21] = 81229; Num_count[22] = 82155; Num_count[23] = 84079; Num_count[24] = 85602; Num_count[25] = 89021; Num_count[26] = 90616; Num_count[27] = 91701; Num_count[28] = 93791; Num_count[29] = 94079; Num_count[30] = 95620; for (int i = 0; i < dt_fill.Rows.Count; i++) { //读取每个省的填充坐标数据 for (int j = 0; j < 31; j++) { if (j == 0) { if (i < Num_count[j]) { X_fill[i] = Convert.ToInt32(dt_fill.Rows[i][0]); // 经度 Y_fill[i] = Convert.ToInt32(dt_fill.Rows[i][1]); //纬度 } } else { if (i < Num_count[j] && i >= Num_count[j - 1]) { X_fill[i] = Convert.ToInt32(dt_fill.Rows[i][0]); // 经度 Y_fill[i] = Convert.ToInt32(dt_fill.Rows[i][1]); //纬度 //ID_coordinates[i] = Convert.ToDouble(dt.Rows[i][0]); } } } } //double[] data1 = new double[] {0.589967025780379,2.76640767785393,2.94486656044847,5.18774795087285,9.55568524078571,7.21217446706751,4.81608668665851,5.67326916712699,9.54321203609774,6.27015178543689,0.354135803190215,8.14711495872654,2.47480472823066,3.05263290782758,2.21504588700546,6.70140475318904,6.05666406518314,2.40509705776911,2.60405493113485,1.6725398082315,5.49355137602265,2.82091154095248,9.6588283411846,5.38501889047855,2.50490878854465,0.604765223162727,1.58887244642718,1.17203933984219,2.20719788025169,2.15731627156846,9.43471799430495}; //按照黑龙江 。。。。 的顺序排好的数 double[] data = new double[31]; data1.CopyTo(data, 0); double[] datarank = MathV.Sort(31, data1);//从小到大排序 this.chart1.Series.Clear(); int count_class = 4; double[] quan_value = new double[count_class + 1]; quan_value[0] = datarank[0]; quan_value[1] = datarank[7]; quan_value[2] = datarank[15]; quan_value[3] = datarank[23]; quan_value[4] = datarank[30]; for (int i = 0; i < count_class; i++) { Series series = new Series(); series.ChartType = SeriesChartType.FastPoint; this.chart1.Series.Add(series); } for (int i = 0; i < 4; i++) { Series series = new Series(); series.Name = "(" + MathV.round(quan_value[i].ToString(), 2, 0) + "," + MathV.round(quan_value[i + 1].ToString(), 2, 0) + ")"; series.ChartType = SeriesChartType.Column; this.chart1.Series.Add(series); } //台湾不能忘 Series series_taiwan = new Series(); series_taiwan.ChartType = SeriesChartType.FastPoint; //边界 this.chart1.Series.Add(series_taiwan); series_taiwan.MarkerStyle = MarkerStyle.Circle; series_taiwan.MarkerSize = 2; series_taiwan.IsVisibleInLegend = false; series_taiwan.Color = Color.FromArgb(6, 52, 156); if (comboBox_theme.Text == "蓝色") { this.chart1.Series[0].Color = Color.FromArgb(151, 182, 251); this.chart1.Series[1].Color = Color.FromArgb(78, 131, 248); this.chart1.Series[2].Color = Color.FromArgb(9, 71, 219); this.chart1.Series[3].Color = Color.FromArgb(6, 52, 156); this.chart1.Series[4].Color = Color.FromArgb(151, 182, 251); this.chart1.Series[5].Color = Color.FromArgb(78, 131, 248); this.chart1.Series[6].Color = Color.FromArgb(9, 71, 219); this.chart1.Series[7].Color = Color.FromArgb(6, 52, 156); series_taiwan.Color = Color.FromArgb(6, 52, 156); } else if (comboBox_theme.Text == "青色") { this.chart1.Series[0].Color = Color.FromArgb(128, 234, 248); this.chart1.Series[1].Color = Color.FromArgb(34, 217, 242); this.chart1.Series[2].Color = Color.FromArgb(12, 190, 214); this.chart1.Series[3].Color = Color.FromArgb(8, 119, 134); this.chart1.Series[4].Color = Color.FromArgb(128, 234, 248); this.chart1.Series[5].Color = Color.FromArgb(34, 217, 242); this.chart1.Series[6].Color = Color.FromArgb(12, 190, 214); this.chart1.Series[7].Color = Color.FromArgb(8, 119, 134); series_taiwan.Color = Color.FromArgb(8, 119, 134); } else if (comboBox_theme.Text == "绿色") { this.chart1.Series[0].Color = Color.FromArgb(122, 250, 146); this.chart1.Series[1].Color = Color.FromArgb(55, 247, 92); this.chart1.Series[2].Color = Color.FromArgb(9, 221, 49); this.chart1.Series[3].Color = Color.FromArgb(6, 144, 32); this.chart1.Series[4].Color = Color.FromArgb(122, 250, 146); this.chart1.Series[5].Color = Color.FromArgb(55, 247, 92); this.chart1.Series[6].Color = Color.FromArgb(9, 221, 49); this.chart1.Series[7].Color = Color.FromArgb(6, 144, 32); series_taiwan.Color = Color.FromArgb(6, 144, 32); } else if (comboBox_theme.Text == "红色") { this.chart1.Series[0].Color = Color.FromArgb(253, 149, 141); this.chart1.Series[1].Color = Color.FromArgb(251, 73, 59); this.chart1.Series[2].Color = Color.FromArgb(242, 21, 4); this.chart1.Series[3].Color = Color.FromArgb(196, 0, 0); this.chart1.Series[4].Color = Color.FromArgb(253, 149, 141); this.chart1.Series[5].Color = Color.FromArgb(251, 73, 59); this.chart1.Series[6].Color = Color.FromArgb(242, 21, 4); this.chart1.Series[7].Color = Color.FromArgb(196, 0, 0); series_taiwan.Color = Color.FromArgb(196, 0, 0); } for (int i = 0; i < 4; i++) { this.chart1.Series[i].MarkerStyle = MarkerStyle.Circle; this.chart1.Series[i].MarkerSize = 2; this.chart1.Series[i].IsVisibleInLegend = false; } Series series5 = new Series(); series5.ChartType = SeriesChartType.FastPoint; //边界 this.chart1.Series.Add(series5); series5.MarkerStyle = MarkerStyle.Square; series5.MarkerSize = 2; series5.IsVisibleInLegend = false; if (comboBox_boundary.Text == "无边界") { } else if (comboBox_boundary.Text == "白色") { series5.Color = Color.White; for (int i = 0; i < dt.Rows.Count - 168; i++) { series5.Points.AddXY(Convert.ToInt32(dt.Rows[i][1]), Convert.ToInt32(dt.Rows[i][2])); } } else { series5.Color = Color.Black; for (int i = 0; i < dt.Rows.Count - 168; i++) { series5.Points.AddXY(Convert.ToInt32(dt.Rows[i][1]), Convert.ToInt32(dt.Rows[i][2])); } } for (int i = 12504; i < 12672; i++) { series_taiwan.Points.AddXY(Convert.ToInt32(dt.Rows[i][1]), Convert.ToInt32(dt.Rows[i][2])); } for (int i = 0; i < 31; i++) { if (data[i] <= quan_value[1]) { if (i == 0) { for (int j = 0; j < Num_count[i]; j++) { //MessageBox.Show(X_fill[j].ToString()); this.chart1.Series[0].Points.AddXY(X_fill[j], Y_fill[j]); //添加对应数据点 } } else { for (int j = Num_count[i - 1]; j < Num_count[i]; j++) { this.chart1.Series[0].Points.AddXY(X_fill[j], Y_fill[j]); //添加对应数据点 } } } if (data[i] > quan_value[1] && data[i] <= quan_value[2]) { if (i == 0) { for (int j = 0; j < Num_count[i]; j++) { this.chart1.Series[1].Points.AddXY(X_fill[j], Y_fill[j]); //添加对应数据点 } } else { for (int j = Num_count[i - 1]; j < Num_count[i]; j++) { this.chart1.Series[1].Points.AddXY(X_fill[j], Y_fill[j]); //添加对应数据点 } } } if (data[i] > quan_value[2] && data[i] <= quan_value[3]) { if (i == 0) { for (int j = 0; j < Num_count[i]; j++) { this.chart1.Series[2].Points.AddXY(X_fill[j], Y_fill[j]); //添加对应数据点 } } else { for (int j = Num_count[i - 1]; j < Num_count[i]; j++) { this.chart1.Series[2].Points.AddXY(X_fill[j], Y_fill[j]); //添加对应数据点 } } } if (data[i] > quan_value[3]) { if (i == 0) { for (int j = 0; j < Num_count[i]; j++) { this.chart1.Series[3].Points.AddXY(X_fill[j], Y_fill[j]); //添加对应数据点 } } else { for (int j = Num_count[i - 1]; j < Num_count[i]; j++) { this.chart1.Series[3].Points.AddXY(X_fill[j], Y_fill[j]); //添加对应数据点 } } } } } } }