• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 90
  • Last Modified:

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
0
AndyC1000
Asked:
AndyC1000
1 Solution
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now