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
11
12
13
14
15
16
17
18
19
20
21 public class DistEmpiricalDiscreteLong extends DistDiscrete
22 {
23
24 private static final long serialVersionUID = 20210403L;
25
26
27 private final DiscreteEmpiricalDistribution empiricalDistribution;
28
29
30
31
32
33
34
35 public DistEmpiricalDiscreteLong(final StreamInterface stream, final DiscreteEmpiricalDistribution empiricalDistribution)
36 {
37 super(stream);
38
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
48 @Override
49 public long draw()
50 {
51 double u = this.stream.nextDouble();
52 return this.empiricalDistribution.getCeilingEntry(u).getValue().longValue();
53 }
54
55
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 }