Some of the distributions precompute values to be used in the probability
computations, others do not.

IMO if you are using a distribution it is likely that you will call one of
the probability functions many times. Which one is unknown. Options are:

1. Precompute all values that can be precomputed for each probability
computation. Some values will not be necessary depending on the use case.
2. Precompute nothing. This will repeat computations for each call to the
relevant method. In the case of inverse CDF computations solved by search
of the CDF this can be expensive.
3. Dynamically compute the values on the first call.

Option 3 is possible without synchronization using a pattern:

double value = Double.NaN;

double getValue() {
    double v = value;
    if (Double.isNaN(v)) {
        value = v = computeValue();
    }
    return v;
}

This adds a lot of complexity to the classes.

I would opt for option 1 in order to optimise the distribution in use and
take the performance hit in the constructor. In the assumed use case of
multiple invocations of a probability method then the precomputation of
values for all probability methods will likely still be an optimisation for
the single method of interest invoked multiple times.

In the use case of creating a distribution to create a sampler, the
generation of random deviates will likely take far longer than any
performance hit for unnecessary precomputations.

Alex

Reply via email to