public static double PrRatioChiPrExPrAllDF(double[][] d){
int df=(d.length-1)*(d[0].length-1);
System.out.println("df=\t"+df+"\t");
//(ExPr/ChiPr)
double ret = 1;
double chi=Chi.chi(d);
if(chi!=0){
double chiPr=0;
double expr=0;
double chiPr0=0;
double exPr0=0;
double[] lmarg=new double[d.length];
double[] cmarg=new double[d[0].length];
double sum=0;
for(int i=0;i<d.length;i++){
for(int j=0;j<d[i].length;j++){
lmarg[i]+=d[i][j];
cmarg[j]+=d[i][j];
}
}
for(int i=0;i<lmarg.length;i++){
sum+=lmarg[i];
//ret += org.apache.commons.math.special.Gamma.logGamma(lmarg[i]+1);
}
int maxcolumn=0;
double maxcolval=cmarg[0];
int maxline=0;
double maxlinval=lmarg[0];
for(int i=1;i<cmarg.length;i++){
if(cmarg[i]>maxcolval){
maxcolumn=i;
maxcolval=cmarg[i];
}
}
for(int i=1;i<lmarg.length;i++){
if(lmarg[i]>maxlinval){
maxline=i;
maxlinval=lmarg[i];
}
}
double[][] expTable=new double[d.length][d[0].length];
for(int i=0;i<expTable.length-1;i++){
for(int j=0;j<expTable[i].length-1;j++){
expTable[i][j]=(double)((int)(lmarg[i]*cmarg[j]/sum));
//if(i==maxline && j==maxcolumn){
//expTable[i][j]-=0.5;
//}
}
}
for(int i=0;i<expTable[0].length-1;i++){
double tmp=0;
for(int j=0;j<expTable.length-1;j++){
tmp+=expTable[j][i];
}
expTable[expTable.length-1][i]=cmarg[i]-tmp;
}
for(int i=0;i<expTable.length-1;i++){
double tmp=0;
for(int j=0;j<expTable[0].length-1;j++){
tmp+=expTable[i][j];
}
expTable[i][expTable[0].length-1]=lmarg[i]-tmp;
}
double tmpsum=0;
for(int i=0;i<expTable.length;i++){
for(int j=0;j<expTable[i].length;j++){
tmpsum+=expTable[i][j];
}
}
expTable[expTable.length-1][expTable[0].length-1]=sum-tmpsum;
double chiExp=Chi.chi(expTable);
System.out.print("chi\t"+chi+"\t");
System.out.print("chiExp\t"+chiExp+"\t");
//double chiP=chiprob(chi,df);
//double chiExpP=chiprob(chiExp,df);
//System.out.print("chiP\t"+chiP);
//System.out.print("chiExpP\t"+chiExpP);
//double chiPratio=chiP/chiExpP;
double chiPratio=Math.exp(-(chi-chiExp)/2.0);
System.out.print("chiP/chiExpP\t"+chiPratio+"\t");
expr=StatUtilsZ.Fisher.lnprViaGamma(d);
exPr0=StatUtilsZ.Fisher.lnprViaGamma(expTable);
expr*=Math.pow(chi,(double)df/2.0-1);
exPr0*=Math.pow(chiExp,(double)df/2.0-1);
double exPratio=expr/exPr0;
System.out.print("expr/exPr0\t"+exPratio+"\t");
ret = exPratio/chiPratio;
for(int i=0;i<expTable.length;i++){
for(int j=0;j<expTable[i].length;j++){
System.out.print(expTable[i][j]+"\t");
}
System.out.print("\t");
}
}
System.out.print("ret=\t"+ret);
return ret;
}