Re: Problem applying generics to my code. Is there a better solution?




Anytime that you find yourself using a chain of "instanceof" checks on
classes which you control, its time to look into using polymorphism
instead... You're particular problem may be best solved through a
Visitor pattern.

Yet another reason for me to get my own copy of the GoF.

Here's an example, I'm removing the generics, as they're not necessary
for my point.

I'll comment on your suggestion below, but I wanted to re-emphasize
the point that I really would like to use generics for the their type-
safety in this case. Recall the function prototype I mentioned in the
OP

public static <T, P> Distribution<P> conjugate( List<T> data,
Distribution<T> likelihood, Distribution<P> prior )

Fro, the perspective of enforcing mathematical correctness, it is
important to constrain the return type to be the same as the prior
parameters and force the data to be compatible with the likelihood.
Now, on to your (slightly edited) suggestion....

abstract class Distribution {
Distribution posterior(List list, Distribution prior) {
prior.visit(this);
}
protected Distribution posterior(List list, Normal prior) {
throw new IllegalArgumentException("Not valid");
}
protected Distribution posterior(List list, Gamma prior) {
throw new IllegalArgumentException("Not valid");
}
public abstract Distribution visit(List list, Distribution d);}

Ok, so we set up a base class (abstract in this example) where we
consider every possible distribution as a potential prior. It will be
up to the implementing classes to override the conjugate cases
supported by their particular form.

class Normal {
protected Distribution posterior(List list, Normal prior) {
return normalNormaPosterior(list, this, prior);
}
protected Distribution posterior(List list, Gamma prior) {
return normalGammaPosterior(list, this, prior);
}
public Distribution visit(List list, Distribution d) {
return d.posterior(list, this);
}
}

Now, when we invoke .posterior(data, prior) for a distribution x, it
will visit the prior, passing itself along as a parameters. Since we
invoke .visit() on the prior object, it will call the correct method,
i.e. normal or gamma or whatever. Now, the prior calls back to the
likelihood object, passing itself. This will give the type
specificity to actually call one of the polymorphic posterior()
methods. Very nice!! This is a double-dispatch pattern, correct?

I actually don't know much about the problem domain your solving. I'm
guessing statistics? (The one math I haven't studied much) :-)

Correct. The particular bit of statistics I'm implementing here is
described at

http://en.wikipedia.org/wiki/Conjugate_prior

Anyway, I hope I've been able to help.

Very much! I'll give your suggestion a whirl and see how it plays
out.

Many thanks.

.



Relevant Pages

  • Re: different priors (flat, uniform, etc)
    ... as is probably obvious should you look at his history ... John Uebersax PhD ... JU> So I think we should just dispense with the word "prior" ... RF> simply ASSUME anything has a normal distribution or any other ...
    (sci.stat.math)
  • Re: different priors (flat, uniform, etc)
    ... that such nuisance parameter is allowed to take. ... A "prior distribution" is a Bayesian term, and means, basically, ...
    (sci.stat.math)
  • Re: different priors (flat, uniform, etc)
    ... that such nuisance parameter is allowed to take. ... A "prior distribution" is a Bayesian term, and means, basically, ...
    (sci.stat.math)
  • Re: different priors (flat, uniform, etc)
    ... distribution or a Bernoulli Process, ... member of the conjugate prior family -- meaning both the prior AND ... The uniform distribution on is a Beta distribution with ... It's in every Freshman textbook in Bayesian statistics. ...
    (sci.stat.math)
  • Re: Problem applying generics to my code. Is there a better solution?
    ... but "Visitor pattern" is an application of the broadly useful ... important to constrain the return type to be the same as the prior ... parameters and force the data to be compatible with the likelihood. ... conjugate sounds like a verb. ...
    (comp.lang.java.programmer)