View Javadoc

1   /*
2    * Copyright 2013 University of Glasgow.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package broadwick.montecarlo;
17  
18  import java.io.Serializable;
19  import java.util.LinkedHashMap;
20  import java.util.Map;
21  import lombok.Getter;
22  
23  /**
24   * A representation of the current step in a Markov Chain. The step is represented by a set of coordinates in the
25   * parameter space spanning the possible set of steps.
26   */
27  public class MonteCarloStep implements Serializable {
28  
29      /**
30       * Copy constructor.
31       * @param step the step to copy.
32       */
33      public MonteCarloStep(final MonteCarloStep step) {
34          this.coordinates = new LinkedHashMap<>();
35          for (Map.Entry<String, Double> entry : step.coordinates.entrySet()) {
36              coordinates.put(entry.getKey(), entry.getValue());
37          }
38      }
39  
40      /**
41       * Create a single step in a Markov Chain.
42       * @param coordinates the coordinates of the step in the parameter space of the chain.
43       */
44      public MonteCarloStep(final Map<String, Double> coordinates) {
45          if (coordinates instanceof LinkedHashMap) {
46              this.coordinates = new LinkedHashMap<>();
47              this.coordinates.putAll(coordinates);
48          } else {
49              throw new IllegalArgumentException("The coordinates of the Monte Carlo Path step must be a LinkedHashMap");
50          }
51      }
52  
53      @Override
54      public final String toString() {
55          final StringBuilder sb = new StringBuilder();
56          for (Map.Entry<String, Double> entry : coordinates.entrySet()) {
57              sb.append(entry.getValue()).append(',');
58          }
59  
60          if (sb.length() > 0) {
61              // remove the last value separator.
62              sb.deleteCharAt(sb.length() - 1);
63          }
64          return sb.toString();
65      }
66      @Getter
67      Map<String, Double> coordinates;
68  }