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-2024 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/" target="_blank"> https://simulation.tudelft.nl</a>. The DSOL
14   * project is distributed under a three-clause BSD-style license, which can be found at
15   * <a href="https://https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">
16   * https://https://simulation.tudelft.nl/dsol/docs/latest/license.html</a>.
17   * </p>
18   * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
19   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
20   */
21  public class DistEmpiricalDiscreteLong extends DistDiscrete
22  {
23      /** */
24      private static final long serialVersionUID = 20210403L;
25  
26      /** the empirical distribution. */
27      private final DiscreteEmpiricalDistribution empiricalDistribution;
28  
29      /**
30       * constructs a new DistEmpirical distribution.
31       * @param stream StreamInterface; the stream to use
32       * @param empiricalDistribution EmpiricalDistributionInterface; the cumulative distribution to use
33       * @throws IllegalArgumentException when the empirical distribution has non-integer values
34       */
35      public DistEmpiricalDiscreteLong(final StreamInterface stream, final DiscreteEmpiricalDistribution empiricalDistribution)
36      {
37          super(stream);
38          // check that the values in the distribution are integer valued and we do not interpolate
39          for (Number n : empiricalDistribution.getValues())
40          {
41              Throw.when(n instanceof Double || n instanceof Float, IllegalArgumentException.class,
42                      "empirical distribution can only contain integer or long values");
43          }
44          this.empiricalDistribution = empiricalDistribution;
45      }
46  
47      /** {@inheritDoc} */
48      @Override
49      public long draw()
50      {
51          double u = this.stream.nextDouble();
52          return this.empiricalDistribution.getCeilingEntry(u).getValue().longValue();
53      }
54  
55      /** {@inheritDoc} */
56      @Override
57      public double probability(final long observation)
58      {
59          DistributionEntry entry1 = this.empiricalDistribution.getFloorEntryForValue(observation);
60          if (entry1 == null || entry1.getValue().longValue() != observation)
61          {
62              return 0.0;
63          }
64          double c1 = entry1.getCumulativeProbability();
65          DistributionEntry entry0 = this.empiricalDistribution.getPrevEntry(c1);
66          return (entry0 == null) ? c1 : c1 - entry0.getCumulativeProbability();
67      }
68  
69  }