Create average result of monte carlo

Dear all,

I have created a monte carlo simulation, the output is 1000 separate result files.  I have been asked to create an average result, the total of each field across the 1000 files / number of files ( 1000).

I thought about creating a tree map storing the long (Date time), and the double value.  

Each field in the file will have a separate hash map
<Long, Double> averageAppliances = new TreeMap<>();


For the second file out of 1000 get the value for the long, add it the current value and so on until each file and field has been processed.

I'm not sure if this will work. Whether I can getValue(), add it to the existing value and put it back (replace) in the map.  I'm storing this data whilst writing the files.

Thanks
AndyC1000Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mccarlIT Business Systems Analyst / Software DeveloperCommented:
I'm not sure if this will work. Whether I can getValue(), add it to the existing value and put it back (replace) in the map.

It probably would work, but yeah it is a little bit ugly to have to do that sought of thing. The problem being that Double is (for a good reason) immutable, ie. you can't change the value of the object once it's created.


The way I would go would be to create a small utility class, say Accumulator, which internally holds a double value, but can update that value as you go. You can then store this Accumulator in your TreeMap. Pseudo-code would look something like this...

public class Accumulator {
  private double value;
  private int count;

  public void update(double value) {
    this.value += value;
    this.count++;
  }

  public double getAverage() {
    return value / count;
  }
}




Map<Long, Accumulator> map = new TreeMap<>();

foreach file {
  foreach sample {
    getAccumulator from map based on sample date/time (long)
    if no entry yet (ie. result from above is null) create a new Accumulator and "put" it into the map

    accumulator.update(sample.value);
  }
}

foreach entry in "map" {
  print "Date/Time: " + entry.getKey() + "    Average value: " + entry.getValue().getAverage()
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.