View Javadoc
1   package nl.tudelft.simulation.jstats.distributions;
2   
3   import org.djutils.exceptions.Throw;
4   
5   import nl.tudelft.simulation.jstats.distributions.empirical.DiscreteEmpiricalDistribution;
6   import nl.tudelft.simulation.jstats.distributions.empirical.DistributionEntry;
7   import nl.tudelft.simulation.jstats.streams.StreamInterface;
8   
9   /**
10   * A discrete empirical distribution as defined on page 326 of Law & Kelton, based on an EmpiricalDistribution object.
11   * <p>
12   * Copyright (c) 2002-2025 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
13   * for project information <a href="https://simulation.tudelft.nl/dsol/manual/" target="_blank">DSOL Manual</a>. The DSOL
14   * project is distributed under a three-clause BSD-style license, which can be found at
15   * <a href="https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">DSOL License</a>.
16   * </p>
17   * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
18   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
19   */
20  public class DistEmpiricalDiscreteLong extends DistDiscrete
21  {
22      /** */
23      private static final long serialVersionUID = 20210403L;
24  
25      /** the empirical distribution. */
26      private final DiscreteEmpiricalDistribution empiricalDistribution;
27  
28      /**
29       * constructs a new DistEmpirical distribution.
30       * @param stream StreamInterface; the stream to use
31       * @param empiricalDistribution EmpiricalDistributionInterface; the cumulative distribution to use
32       * @throws IllegalArgumentException when the empirical distribution has non-integer values
33       */
34      public DistEmpiricalDiscreteLong(final StreamInterface stream, final DiscreteEmpiricalDistribution empiricalDistribution)
35      {
36          super(stream);
37          // check that the values in the distribution are integer valued and we do not interpolate
38          for (Number n : empiricalDistribution.getValues())
39          {
40              Throw.when(n instanceof Double || n instanceof Float, IllegalArgumentException.class,
41                      "empirical distribution can only contain integer or long values");
42          }
43          this.empiricalDistribution = empiricalDistribution;
44      }
45  
46      @Override
47      public long draw()
48      {
49          double u = this.stream.nextDouble();
50          return this.empiricalDistribution.getCeilingEntry(u).getValue().longValue();
51      }
52  
53      @Override
54      public double probability(final long observation)
55      {
56          DistributionEntry entry1 = this.empiricalDistribution.getFloorEntryForValue(observation);
57          if (entry1 == null || entry1.getValue().longValue() != observation)
58          {
59              return 0.0;
60          }
61          double c1 = entry1.getCumulativeProbability();
62          DistributionEntry entry0 = this.empiricalDistribution.getPrevEntry(c1);
63          return (entry0 == null) ? c1 : c1 - entry0.getCumulativeProbability();
64      }
65  
66  }