0

I am trying to keep a running average inside of a SharedPreference. Here is my code:

//Get the number of captures
int numberOfCaptures = prefs.getInt(CaptureActivity.NUMBER_OF_CAPTURES, 0);
numberOfCaptures++;

//Calculate the average of all of the captures
int runningAverage = prefs.getInt(CaptureActivity.AVERAGE_BLAST_SCORE, 0);
System.out.println("Running Average: " + runningAverage);

int averageBlastScore = (runningAverage + result.getBlastScore())/numberOfCaptures;

System.out.println("Blast Score: "  + result.getBlastScore());
System.out.println("Number of Captures: " + numberOfCaptures);
System.out.println("Average Blast Score: " + averageBlastScore);

//Save it, so we can get it again if the user captures another swing
prefs.edit().putInt(CaptureActivity.AVERAGE_BLAST_SCORE, averageBlastScore).commit();
prefs.edit().putInt(CaptureActivity.NUMBER_OF_CAPTURES, numberOfCaptures).commit();

It seems like my running average is not getting added properly.

Here it is for 3 runs:

10-28 02:53:13.690: I/System.out(1162): Running Average: 0
10-28 02:53:13.690: I/System.out(1162): Blast Score: 96
10-28 02:53:13.690: I/System.out(1162): Number of Captures: 1
10-28 02:53:13.690: I/System.out(1162): Average Blast Score: 96

10-28 02:53:25.550: I/System.out(1162): Running Average: 96
10-28 02:53:25.550: I/System.out(1162): Blast Score: 99
10-28 02:53:25.550: I/System.out(1162): Number of Captures: 2
10-28 02:53:25.550: I/System.out(1162): Average Blast Score: 97

10-28 02:54:04.720: I/System.out(1162): Running Average: 97
10-28 02:54:04.720: I/System.out(1162): Blast Score: 100
10-28 02:54:04.720: I/System.out(1162): Number of Captures: 3
10-28 02:54:04.720: I/System.out(1162): Average Blast Score: 65

By the third run I should have:

Running Average: 295
Average Blast Score: 98.3

I'm not exactly sure what I'm doing wrong.

4
  • What, mathematically, do you mean by a running average? If you want a moving average, then you need to retain the last N data points so that you can sum them and divide them by N. If you want an exponential moving average, you can do what you are doing now, but divide by k, the decay factor. Oct 28, 2011 at 3:05
  • I just want to take the average. I guess I might be using the wrong terminology. As my app continues to run, I want to take the average of my data points. Oct 28, 2011 at 3:07
  • Then you will need to retain the last N values, not just the prior average. Oct 28, 2011 at 3:10
  • @SheehanAlam store sum of scores instead of avg and compute avg before showing by dividing sum of scores with numbers of capture
    – Selvin
    Oct 28, 2011 at 3:18

3 Answers 3

1

Look at this line:

int averageBlastScore = (runningAverage + result.getBlastScore())/numberOfCaptures;

What do you expect to happen after, say, the 100th iteration?

You should find the average by adding up scores and dividing by the number of captures:

int sumBlastScore = prefs.getInt(CaptureActivity.SUM_BLAST_SCORE, 0) + result.getBlastScore();
int averageBlastScore = sumBlastScore/numberOfCaptures;

System.out.println("Running Average: " + averageBlastScore);
0

I have noticed a potential flaw in your code - the following line is the culprit:

int averageBlastScore = (runningAverage + result.getBlastScore())/numberOfCaptures;

Let's consider a scenario where you have 3 scores: 99,98,90

Since you are doing an iterative average operation, the results would look as follows:

  • (99+0)/1= 99 ---- this is fine
  • (99+98)/2 = 98.5 --- this is fine
  • (98.5+90)/3 = 62.83 --- Here is the problem

Instead, after the second iteration you should be dividing by 2 each time.

Another way to tackle this would be to wait until you have received all the scores and then divide the total by the number of captures.

0

To keep a running average you don't keep the actual average, you keep the running total and the number of samples, then you calculate the average the usual way, which is total/samples. Therefore for this set you should be keeping these values at each stage..

Samples: 96, 99, 100

Total : 96, Samples : 1 => AVG = 96/1 = 96

Total : 195, Samples : 2 => AVG = 195/2 = 97.5

Total : 295, Samples : 3 => AVG = 295/3 = 98.333

A wrong way stated elsewhere is to always divide by two after the first sample, and simply add the previous average to the new sample. This would result in 99 samples of 100, and a sample of 0 having an average of 50, which is obviously wrong.

I would change your code to something like this

            int numberOfCaptures = prefs.getInt(CaptureActivity.NUMBER_OF_CAPTURES, 0);
            numberOfCaptures++;

            int runningTotal = prefs.getInt(CaptureActivity.RUNNING_TOTAL, 0);
            runningTotal += result.getBlastScore();

            //Calculate the average of all of the captures
            int averageBlastScore = runningTotal / numberOfCaptures;

            System.out.println("Blast Score: "  + result.getBlastScore());
            System.out.println("Number of Captures: " + numberOfCaptures);
            System.out.println("Average Blast Score: " + averageBlastScore);

            //Save it, so we can get it again if the user captures another swing
            prefs.edit().putInt(CaptureActivity.RUNNING_TOTAL, runningTotal).commit();
            prefs.edit().putInt(CaptureActivity.NUMBER_OF_CAPTURES, numberOfCaptures).commit();

If you insist on storing the running average instead of running total, you are going to have to multiple that average out by samples BEFORE incrementing samples to obtain the previous total, and then proceed from there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.