public static BigNumber[,] MatPlus(BigNumber[,] mat1, BigNumber[,] mat2) {//矩阵加法 int len11 = mat1.GetLength(0); int len12 = mat1.GetLength(1); int len21 = mat2.GetLength(0); int len22 = mat2.GetLength(1); if (len11 == len21 && len12 == len22) { BigNumber[,] a = new BigNumber[len11, len12]; for (int i = 0; i < len11; i++) { for (int j = 0; j < len12; j++) { a[i, j] = mat1[i, j] + mat2[i, j]; } } return a; } else { return null; } }
public static BigNumber[,] MatTrans(BigNumber[,] mat) { //矩阵转置 int len1 = mat.GetLength(0); int len2 = mat.GetLength(1); BigNumber[,] a = new BigNumber[len2, len1]; for (int i = 0; i < len1; i++) { for (int j = 0; j < len2; j++) { a[j, i] = mat[i, j]; } } return a; }
public static BigNumber[,] MatCorr(BigNumber[,] matrix) { int len1 = matrix.GetLength(0); int len2 = matrix.GetLength(1); BigNumber[] a1 = new BigNumber[len1 * len2]; for (int j = 0; j < len2; j++) { for (int i = 0; i < len1; i++) { a1[j * len1 + i] = matrix[i, j]; } } BigNumber[] a2 = new BigNumber[len1]; BigNumber[] a3 = new BigNumber[len1]; BigNumber[,] corr = new BigNumber[len2, len2]; for (int c = 1; c < len2 ;c++ ) { for (int j = 0; j < len2 -1; j++) { for (int i = 0; i < len1; i++) { a2[i] = a1[j * len1 + i]; } for (int i = 0; i < len1; i++) { a3[i] = a1[(j +1)* len1 + i]; } try { corr[j, j + c] = Corr(a2, a3); corr[j + c, j] = Corr(a2, a3); } catch (Exception ex) { } } } for (int i = 0; i < len2;i ++ ) { corr[i, i] = new BigNumber("1"); } return corr; }
public static BigNumber[,] MatTimes(BigNumber[,] mat1, BigNumber[,] mat2) { //矩阵乘法 int len11 = mat1.GetLength(0); int len12 = mat1.GetLength(1); int len21 = mat2.GetLength(0); int len22 = mat2.GetLength(1); if (len12 == len21) { BigNumber[,] a = new BigNumber[len11, len22]; for (int i = 0; i < len11; i++) { for (int j = 0; j < len22; j++) { for (int u = 0; u < len12; u++) { a[i, j] += mat1[i, u] * mat2[u, j]; } } } return a; } else { return null; } }
public static string MutiRegR(BigNumber[,] x, BigNumber[,] y) { //返回多元回归拟合优度R^2 and adj_R^2 int len11 = x.GetLength(0);//x行数 int len12 = x.GetLength(1);//x列数 int len21 = y.GetLength(1);//y列数 int len22 = y.GetLength(0);//y列数 if (len21 != 1) { return null; } BigNumber ysum = new BigNumber("0"); for (int i = 0; i < len11;i++ ) { ysum += y[i,1]; } BigNumber ybar = ysum / (new BigNumber(len11.ToString())); BigNumber TSS = new BigNumber("0"); for (int i = 0; i < len11; i++) { TSS += (y[i, 1] - ybar).Power(new BigNumber("2")); } BigNumber[,] b1 = MathV.MatTrans(x); BigNumber[,] b2 = MathV.MatTimes(b1, x); BigNumber[,] b3 = Stat.MatInv(b2, len12); BigNumber[,] b4 = MathV.MatTimes(b3, b1); BigNumber[,] bhat = MathV.MatTimes(b4, y); BigNumber[,] b5 = MathV.MatTimes(x, bhat); BigNumber[,] epsilon = MathV.MatMinu(y, b5); BigNumber ESS = new BigNumber("0"); for (int i = 0; i < len11; i++) { ESS += (epsilon[i, 1]).Power(new BigNumber("2")); } BigNumber MSS = TSS - ESS; BigNumber Rsquare = MSS / TSS; BigNumber Adj_Rsquare = new BigNumber("1") - (new BigNumber("1")- Rsquare)*(new BigNumber(len11.ToString()) -new BigNumber("1") / (new BigNumber(len11.ToString()) -new BigNumber(len12.ToString()) -new BigNumber("1"))); BigNumber Fvalue = (MSS/new BigNumber(len12.ToString()))/(ESS/( new BigNumber(len11.ToString())- new BigNumber(len12.ToString()) - new BigNumber("1") )); return Rsquare.ToString() + "," + Adj_Rsquare.ToString() + ","+ Fvalue.ToString(); }
public static BigNumber[] MutiRegP(BigNumber[,] x, BigNumber[,] y) { //返回多元回归P值 int len11 = x.GetLength(0);//行数 int len12 = x.GetLength(1);//列数 int len21 = y.GetLength(1);//y列数 if (len21 != 1) { return null; } BigNumber[,] b1 = MathV.MatTrans(x); BigNumber[,] b2 = MathV.MatTimes(b1, x); BigNumber[,] b3 = Stat.MatInv(b2, len12); BigNumber[,] b4 = MathV.MatTimes(b3, b1); BigNumber[,] bhat = MathV.MatTimes(b4, y); BigNumber[,] b5 = MathV.MatTimes(x, bhat); BigNumber[,] epsilon = MathV.MatMinu(y, b5); BigNumber[] variance = new BigNumber[len12]; for (int i = 1; i < len11; i++) { variance[i] = epsilon[i, 1]; } BigNumber sigma2 = Variance(variance) * new BigNumber(((len11 - 1) / len11).ToString()); BigNumber[,] b6 = new BigNumber[len12, len12]; //sigma^2*(C^T C)^{-1} 参数方差 for (int i = 0; i < len12; i++) { for (int j = 0; j < len12; j++) { b6[i, j] = sigma2 * b3[i, j]; } } BigNumber[] std_b = new BigNumber[len12]; for (int i = 0; i < len12; i++) { std_b[i] = b6[i, i].Power(new BigNumber("0.5")); } BigNumber[] tvalue_b = new BigNumber[len12]; for (int i = 0; i < len12; i++) { tvalue_b[i] = bhat[i, 1] / std_b[i]; } BigNumber[] pvalue_b = new BigNumber[len12]; for (int i = 0; i < len12; i++) { pvalue_b[i] = new BigNumber(Stat.NORMSDIST(Convert.ToDouble(tvalue_b[i].ToString())).ToString()); } return pvalue_b; }
/* static void Main(string[] args) { BigNumber[,] dMatrix = new BigNumber[2, 2] { { new BigNumber("0"), new BigNumber("1") }, { new BigNumber("2"), new BigNumber("2") } }; BigNumber[,] dReturn = MatInv(dMatrix,2); if (dReturn != null) { for (int i = 0; i < 2; i++) Console.WriteLine(string.Format("{0} {1} ", dReturn[i, 0], dReturn[i, 1])); //输出 Console.ReadKey(); } }*/ public static BigNumber[,] MutiRegB(BigNumber[,] x,BigNumber[,] y) { //返回多元回归参数估计值**************************************注意:x第一列为1******************* int len11 = x.GetLength(0);//行数 int len12 = x.GetLength(1);//列数 BigNumber[,] b1 = MathV.MatTrans(x); BigNumber[,] b2 = MathV.MatTimes(b1, x); BigNumber[,] b3 = Stat.MatInv(b2, len12); BigNumber[,] b4 = MathV.MatTimes(b3,b1); BigNumber[,] bhat = MathV.MatTimes(b4, y); return bhat; }
public static string OneWayANOVA(BigNumber[,] Numberseies) { // 单因素方差分析 int weidu = Numberseies.Rank; int x = Numberseies.GetLength(0); int y = Numberseies.GetLength(1); int z = Numberseies.Length; BigNumber SST = new BigNumber("0"); BigNumber SSA = new BigNumber("0"); BigNumber SSW = new BigNumber("0"); BigNumber sum1 = new BigNumber("0"); BigNumber[] sum2 = new BigNumber[y]; for (int j = 0; j < y; j++) { for (int i = 0; i < x; i++) { sum1 += Numberseies[i, j]; sum2[j] += Numberseies[i, j]; } } BigNumber Txbar = sum1 / new BigNumber(z.ToString()); BigNumber[] Axbar = new BigNumber[y]; for (int j = 0; j < y; j++) { Axbar[j] = sum2[j] / new BigNumber(y.ToString()); } for (int j = 0; j < y; j++) { SSA += new BigNumber(y.ToString()) * (Axbar[j] - Txbar).Power(new BigNumber("0.5"), 30); } for (int j = 0; j < y; j++) { for (int i = 0; i < x; i++) { SST += (Numberseies[i, j] - Txbar).Power(new BigNumber("0.5"), 30); SSW += (Numberseies[i, j] - Axbar[j]).Power(new BigNumber("0.5"), 30); } } BigNumber MSA = SSA / (new BigNumber(y.ToString()) - new BigNumber("1")); BigNumber MSW = SSW / (new BigNumber(z.ToString()) - new BigNumber(y.ToString())); BigNumber MST = SST / (new BigNumber(z.ToString()) - new BigNumber("1")); BigNumber Fvalue = MSA / MSW; return Fvalue.ToString(); }
public static string contingency(BigNumber[,] table) { int len11 = table.GetLength(0);//x行数 int len12 = table.GetLength(1);//x列数 BigNumber[,] fe = new BigNumber[len11, len12];//给定单元中的频数期望值 BigNumber chisq_value = new BigNumber("0"); BigNumber sum = new BigNumber("0"); BigNumber[] sum_c = new BigNumber[len12]; BigNumber[] sum_r = new BigNumber[len11]; for (int i = 0; i < len11; i++) { for (int j = 0; j < len12; j++) { sum = sum + table[i, j]; } } for (int j = 0; j < len12;j++ ) { sum_c[j] = new BigNumber("0"); for(int i =0; i<len11; i++) { sum_c[j] += table[i,j]; } }//对每一列求和 for (int i = 0; i < len12; i++) { sum_r[i] = new BigNumber("0"); for (int j = 0; j < len11; j++) { sum_r[i] += table[i, j]; } }//对每行求和 for (int i = 0; i < len11;i++ ) { for(int j = 0; j < len12;j++) { fe[i, j] = sum_r[i] * sum_c[j] / sum; } }//计算给定单元的频数期望值 for (int i = 0; i < len11;i++ ) { for(int j = 0; j < len12;j++) { chisq_value = chisq_value+(table[i,j]-fe[i,j]).Power(new BigNumber("2"))/fe[i,j]; } } double Xsquare1; Xsquare1= Stat.chi2Ua(0.05, (len11-1)*(len12-1));//返回卡方分布临界值 BigNumber Xsquare = new BigNumber(Xsquare1.ToString()); if (CompareNumber.Compare(chisq_value, Xsquare) == 1 ) { return "拒绝原假设,行列变量之间不独立"; } else { return "不拒绝原假设,行列变量独立"; } }