How to use Empirical Bayes(EB) in pymc3

like type II maximum likelihood?Is there some method in pymc3?

@twiecki Our Dear Thomas~What shall i do?

There’s an option to do maximum a posteriori estimation which is very similar to maximum likelihood estimation. If you use this in conjunction with your data and model to estimate the model hyperparameters, that should correspond to an empirical Bayes approach. Is this what you mean by type II MLE?


s = pd.read_excel('retention.xlsx')['and_retention']
if __name__ == '__main__':
    with pm.Model() as model:
        a = pm.Beta('a', observed=s)
        trace = pm.sample(5000)

Our Dear Chris。It will be error~

It’s nice that you posted a small example of your code that failed, but it would also be helpful to see the error message. Now, since your model has only a single variable and it has observations there are no free variables and there is nothing for the sampler to do.

Our Dear Chris。Error is here:

Traceback (most recent call last):
  File "F:/Code_Work/", line 41, in <module>
    a = pm.Beta('a', alpha=None, beta=None, observed=s)
  File "D:\ProgramData\Miniconda3\envs\untitled_0\lib\site-packages\pymc3\distributions\", line 41, in __new__
    dist = cls.dist(*args, **kwargs)
  File "D:\ProgramData\Miniconda3\envs\untitled_0\lib\site-packages\pymc3\distributions\", line 52, in dist
    dist.__init__(*args, **kwargs)
  File "D:\ProgramData\Miniconda3\envs\untitled_0\lib\site-packages\pymc3\distributions\", line 1030, in __init__
    alpha, beta = self.get_alpha_beta(alpha, beta, mu, sd)
  File "D:\ProgramData\Miniconda3\envs\untitled_0\lib\site-packages\pymc3\distributions\", line 1049, in get_alpha_beta
    raise ValueError('Incompatible parameterization. Either use alpha '
ValueError: Incompatible parameterization. Either use alpha and beta, or mu and sd to specify distribution.

Could you show me a correct code about EB?

What sort of quantity are you attempting to estimate from the data? Here, you have only a single variable which is conditioned on your observations. There is nothing to be sampled.

Estimate the parameters of prior distribution from the data——Type II MLE

Type II MLE and Empirical Bayes are two different thing - are you trying to use Type II MLE to estimate the prior for EB? Either way they are no general way to do it - which means they are not easy to do it in pymc3. You usually need to have good knowledge of your model to be able to derive a solution.