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(); }
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(); }
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_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"); }
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) { 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 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 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 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()); } } } }