This exercise is related to random number generation:
- Implement a class that implements the interface
DoubleSupplier
of the packagejava.util.function.DoubleSupplier
that generates a sequence of random drawings of an inhomogeneous exponential distribution with piece-wise constant intensities (see below how we provide the piece-wise constant intensities).
@FunctionalInterface
public interface DoubleSupplier {
/**
* Gets a result.
*
* @return a result
*/
double getAsDouble();
}
- When done with 1) implement the method
createRandomNumberGeneratorInhomogenousExponential
of the classInhomogenousExponentialAssignmentSolution
, such that it allows to create an object of the class you have implemented in 1).
public DoubleSupplier createRandomNumberGeneratorInhomogenousExponential(RandomNumberGenerator1D uniformSequence, double[] times, double[] intensities)
Note: The arguments times
and intensities
that are provided to you have the following interpretation:
- The length of the array
intensities
istimes.length + 1
. intensities[0]
is the intensity for the time period from0
totimes[0]
.intensities[i]
is the intensity for the time period fromtimes[i-1]
totimes[i]
.intensities[n]
is the intensity for the time period fromtimes[n-1]
toDouble.INFINITY
.
where n = intensities.length-1
, i.e., n = times.length
.
A class like InhomogenousExponentialAssignmentSolution
is called a Factory, because it allows to create objects. It allows us to create an object of your class, without knowing the name of your class. We will use this method to test your implementation.
Hint: This is a trivial task. If your class in 1) is named RandomNumberGeneratorInhomogenousExponential
and if
it has a constructor RandomNumberGeneratorInhomogenousExponential(RandomNumberGenerator1D uniformSequence, double[] times, double[] intensities)
, then the body of the implementation of the factory class
is just
public DoubleSupplier createRandomNumberGeneratorInhomogenousExponential(RandomNumberGenerator1D uniformSequence, double[] times, double[] intensities) {
return new RandomNumberGeneratorInhomogenousExponential(uniformSequence, times, intensities);
}
You can test your implementation by running the unit test in src/test/java
.
The main task of this exercise is to provide the inversion of the distribution function of an inhomogeneous exponential distribution with a piece-wise constant intensity.
The most involved step is the inversion of the function t ↦ Λ(t), where
- Λ(t) = λ0 · t for t < t0
- Λ(ti) = λ0 · (t0-0) + λ1 · (t1-t0) + ... + λi · (ti-ti-1)
- Λ(t) = Λ(ti-1) + λi · (t-ti-1) for ti-1 < t < ti