/**
   * Cars Per Hour model works by creating 24 normal distributions (1 for each hour of the day).
   * After the model is generated, you can sample it with the sampleCarsPerHourModel function
   */
  public void generateCarsPerHourModel() {
    // Get statistical data for counts at each hour
    Map<Integer, SummaryStatistics> summaries = new HashMap<>();
    for (Map.Entry<String, Integer> count : hourlyTrafficCountData.entrySet()) {
      // Java stores times in milliseconds since epoch, hence the *1000.
      String key = count.getKey();
      long timestamp = Long.parseLong(key) * 1000;
      int cars_per_hour = count.getValue();

      DateTime time = new DateTime(timestamp, DateTimeZone.UTC);
      int hour = time.getHourOfDay();

      SummaryStatistics stats;
      if (summaries.containsKey(hour)) {
        // Started gathering statistics, add to it
        stats = summaries.get(hour);
      } else {
        // Will now start gathering statistics
        stats = new SummaryStatistics();
        summaries.put(hour, stats);
      }

      stats.addValue(cars_per_hour);
    }

    // Make model based on statistics gathered
    carsPerHour = new HashMap<>();
    for (Map.Entry<Integer, SummaryStatistics> per_hour_stats : summaries.entrySet()) {
      int hour = per_hour_stats.getKey();
      SummaryStatistics stats = per_hour_stats.getValue();

      // Create distribution based on stats
      NormalDistribution distribution;
      try {
        distribution = new NormalDistribution(stats.getMean(), stats.getStandardDeviation());
      } catch (Exception ignored) {
        // No distribution made, as all counts had the same number (no deviation).
        // We'll assume our counts are pretty close to historical values.
        distribution = new NormalDistribution(stats.getMean(), 10 / stats.getN());
      }
      carsPerHour.put(hour, distribution);
    }
  }
Exemplo n.º 2
0
  /** @return Standard deviation for peak values for all TACs in cluster. */
  public double getPeakStdev() {

    long n = peak_stats.getN();
    if (n == 1) return getPeakMean();
    else return peak_stats.getStandardDeviation();
  }