
Patrick B. answered 09/14/20
Math and computer tutor/teacher
class Probability
{
public static final double EPSILON = 0.0000001; //sets the precision: currently 6 digits
//TRUE --> rows; FALSE--> Columns;
public void marginalProbabilityDistribution( double A[][], double k[], boolean rowColumnFlag )
{
if (rowColumnFlag)
{
int N = A.length;
System.out.println(" row totals ");
for (int iLoop=0; iLoop<N; iLoop++)
{
double rowSum=0;
for (int jLoop=0; jLoop<A[iLoop].length; jLoop++)
{
rowSum += A[iLoop][jLoop];
}
k[iLoop]=rowSum;
System.out.println( (iLoop+1) + "th row sum is " + k[iLoop]);
} //for
}
else
{
int N = A[0].length;
System.out.println(" column totals ");
for (int iLoop=0; iLoop<N; iLoop++)
{
double columnSum = 0;
for (int jLoop=0; jLoop<A.length; jLoop++)
{
columnSum += A[jLoop][iLoop];
}
k[iLoop]=columnSum;
System.out.println( (iLoop+1) + "th column sum is " + k[iLoop]);
} //for
} //main if
} //method
public boolean IsIndependant( double A[][])
{
boolean boolReturn = true;
int numRows = A.length;
int numCols = A[0].length;
double xMarginal[] = new double[numRows];
double yMarginal[] = new double[numCols];
marginalProbabilityDistribution(A,xMarginal,true);
marginalProbabilityDistribution(A,yMarginal,false);
for (int iLoop=0; iLoop<numRows; iLoop++)
{
for (int jLoop=0; jLoop<numCols; jLoop++)
{
if ((A[iLoop][jLoop]-xMarginal[iLoop]*yMarginal[jLoop])> this.EPSILON)
{
boolReturn=false;
break;
}
}
}
return(boolReturn);
}
public static void main(String args[])
{
double A[][] = new double[6][6];
for (int iLoop=0; iLoop<6; iLoop++)
{
for (int jLoop=0; jLoop<6; jLoop++)
{
A[iLoop][jLoop] = (double) (1.0000000f/36);
}
}
double k[] = new double[6];
Probability P = new Probability();
P.marginalProbabilityDistribution(A,k,false);
for (int iLoop=0; iLoop<6; iLoop++)
{
System.out.println(k[iLoop]);
}
System.out.println(P.IsIndependant(A));
}
}