KamelK
KamelK

Reputation: 71

how to save the calculations in the same variable in a for loop in java

I am trying to calculate the loglikelihood.

I order to do so I have to get the probability 26 times and add them together. Then I take the log and that will be the loglikelyhood for that category (I have 3 categories),but in my case I am not able to add the probabilty 26 times,the program only save the last probability and takes the log and thats it...so I was thinking that the probability tmp should be static but it gives me an error... so what is the right way to add and save the calulations in the same variable?Thanks

Here is the method

static int position;
static double logLikelihoods=0.0;
public static String processExcel(double[] mfccArrayDouble) {
    String[] categories={"babycrying","doorbell","doorknock"};
    readExcel3Categories readexcel=new readExcel3Categories();

        double[] probabilityArray=new double[3];
            for (int category = 0; category < categories.length; category++) {
                double tmp = 0.0;
                //double logLikelihoods=0.0;  
                for (int ngauss = 0; ngauss < 26; ngauss++) {
                    double prob1=MathUtils.getGaussianPdfValue(mfccArrayDouble,readexcel.getMuArray1d(category,ngauss),readexcel.getArrayDeterminantSigmaMatrices(category,ngauss),readexcel.getArrayInverseSigmaMatrices(category,ngauss));
                    //double prob2=MathUtils.getGaussianPdfValue(mfccArrayDouble,readexcel.getMuArray1d(category,ngauss), readexcel.getArrayInverseSigmaMatrices(category,ngauss),MathUtils.getGaussianPdfValueConstantTerm(mfccArrayDouble.length, readexcel.getArrayDeterminantSigmaMatrices(category,ngauss)));
                    //System.out.println("prooop is :" + prob1+" category:"+categories[category]);
                    double[] weights=readexcel.getComponentProportionElement(category);
                    if(prob1==Double.NaN || prob1==Double.POSITIVE_INFINITY || prob1==Double.NEGATIVE_INFINITY ||prob1==0.0) {
                        tmp += 0.0;
                    }else {
                        tmp += weights[ngauss]*prob1;
                    }
                    if(tmp==Double.NaN || tmp==Double.POSITIVE_INFINITY || prob1==Double.NEGATIVE_INFINITY || tmp==0.0) 
                    {   
                        logLikelihoods+=0.0;
                    }
                    else
                    {
                        System.out.println("temp-------> "+tmp);
                        logLikelihoods+=Math.log(tmp);
                    }
                }//end of ngauss loop
                //probabilityArray[category]=Math.abs(logLikelihoods);
                probabilityArray[category]=logLikelihoods;
                System.out.println("teeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeemp"+tmp);
                System.out.println("log of the category"+logLikelihoods);
                logLikelihoods=0.0;

            }//end of the category loop
            double max=probabilityArray[0];
            for(int i=0;i<probabilityArray.length;i++) {
                System.out.println("end loop is :" + probabilityArray[i]);
                if(max<=probabilityArray[i]) {
                    position=i;
                    max=probabilityArray[i];
                }
            }
            if (max > 0.1) {
                System.out.println("prop of "+categories[position] +" is :" +probabilityArray[position]);
            }else {
                System.out.println("-------------------------------------------------  NADA   --------------------------------------");
            }
    //return null;
    return categories[position];

and here is the output(saving only the last value not the addition of all of them)

temp-------> 9.408678556341451E-209
temp-------> 9.408678556341451E-209
temp-------> 8.816667344841378E-140
temp-------> 8.816667344841378E-140
temp-------> 8.816667344841378E-140
temp-------> 8.816667344841378E-140
temp-------> 8.816667344841378E-140
temp-------> 8.816667344841378E-140
temp-------> 8.816667344841378E-140
temp-------> 8.816667344841378E-140
temp-------> 1.1319396856996872E-100
temp-------> 1.1319396856996872E-100
temp-------> 1.3557607254104733E-94
temp-------> 2.7823701133363326E-79
temp-------> 2.7823701133363326E-79
temp-------> 2.7823701133363326E-79
temp-------> 6.227382698808317E-41
temp-------> 6.227382698808317E-41
temp-------> 6.227382698808317E-41
temp-------> 6.227382698808317E-41
temp-------> 6.227382698808317E-41
temp-------> 6.227382698808317E-41
temp-------> 6.227382698808317E-41
temp-------> 6.227382698808317E-41
temp-------> 6.227382698808317E-41
temp-------> 5.690790280394833E-31
teeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeemp5.690790280394833E-31
log of the category-5641.364586204369

Upvotes: 0

Views: 81

Answers (2)

Bartors
Bartors

Reputation: 190

As Tomasz pointed out it looks like the temp is getting bigger with iterations, therefore it might be that probabilities are added

I would suggest this:

    if(prob1==Double.NaN || prob1==Double.POSITIVE_INFINITY || prob1==Double.NEGATIVE_INFINITY ||prob1==0.0) {
                    tmp += 0.0;
                }else {
                    tmp += weights[ngauss]*prob1;
System.out.println(weights[ngauss]*prob1);
                }

That way you will se each of the probabilities calculated and see if they are not so small that you don't catch them in your print.out

Upvotes: 1

Tomasz Wida
Tomasz Wida

Reputation: 359

this piece of code

                if(prob1==Double.NaN || prob1==Double.POSITIVE_INFINITY || prob1==Double.NEGATIVE_INFINITY ||prob1==0.0) {
                    tmp += 0.0;
                }else {
                    tmp += weights[ngauss]*prob1;
                }
                if(tmp==Double.NaN || tmp==Double.POSITIVE_INFINITY || prob1==Double.NEGATIVE_INFINITY || tmp==0.0) 
                {   
                    logLikelihoods+=0.0;
                }
                else
                {
                    System.out.println("temp-------> "+tmp);
                    logLikelihoods+=Math.log(tmp);
                }

that System.out.println("temp-------> "+tmp); is only print out, only if it does not match second set of if condition, if you notice the second if else statement does not add temp to temp but print out the value that was summed up from first of if-else condition. You should move that print out statement where temp is actually being summed up, with is in this if-else, and see if it sums up the values

                if(prob1==Double.NaN || prob1==Double.POSITIVE_INFINITY || prob1==Double.NEGATIVE_INFINITY ||prob1==0.0) {
                    tmp += 0.0;
                    System.out.println("temp Adds 0.0-------> "+tmp);

                }else {
                    tmp += weights[ngauss]*prob1;
                    System.out.println("temp add some value-------> "+tmp);

                }

also from what its looks like the last temp is bigger than the previous one, so it looks like it does sum them up with something

Upvotes: 2

Related Questions