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