Here is one. The goal is to provide an over-dispersed exponential
distribution which is defined as an exponential distribution with a prior
distribution on the lambda parameter.
/**
* Sample from an exponential distribution whose parameter is distributed
according
* to a Gamma distribution.
*/
public class UncertainExponentialDistribution extends
ExponentialDistribution {
private Gamma prior;
public UncertainExponentialDistribution(double alpha, double beta) {
prior = new Gamma(alpha, beta);
}
public double getLambda() {
return prior.next();
}
}
Doing this with an immutable super class is a royal pain in the ass. Doing
this with setters on lambda isn't sooo bad, but it really obscures the real
point which is that the over-dispersed exponential distribution is *exactly*
an exponential distribution but lambda varies. The implementation above
says exactly that.
So what about the layer below? The exponential can be defined as a
specialized Gamma which benefits from the same sort of idiom (yes I know
that this distribution can be implemented as -lambda * log(1-rand()), but we
are talking principles here, not specifics).
public class ExponentialDistribution extends AbstractDistribution {
private double lambda;
private Gamma wrapped;
public ExponentialDistribution() {
}
public ExponentialDistribution(double lambda) {
this.lambda = lambda;
wrapped = new Gamma(1, 1 / lambda) {
@Override
public double getAlpha() {
return 1;
}
@Override
public double getBeta() {
return 1 / getLambda();
}
};
}
public double next() {
return wrapped.next();
}
/**
* Default implementation of this getter just returns the value we
know. Sub-classes
* might over-ride this for special effects.
* @return
*/
public double getLambda() {
return lambda;
}
}
Again, we have clarity of expression. The phrase "gamma distribution with
alpha = 1 and beta = 1/lambda" is just what is in the code here. You
*could* do this with setters, but at considerable loss of clarity and no
gain in efficiency.
On Fri, Mar 5, 2010 at 3:51 PM, Gilles Sadowski <
[email protected]> wrote:
> Sorry, I don't follow you. Could you give a code example of these
> one-liners?
>
--
Ted Dunning, CTO
DeepDyve