User Priorities in PyMC

Suppose I want to put a preliminary version for two variables a and b in PyMC, for example:

p(a,b)∝(a+b)^(βˆ’5/2)

(to motivate this choice of the foregoing, see this answer )

Can this be done in PyMC? If so, how?

As an example, I would like to define such earlier in a and b in the model below.

 import pymc as pm # ... # Code that defines the prior: p(a,b)∝(a+b)^(βˆ’5/2) # ... theta = pm.Beta("prior", alpha=a, beta=b) # Binomials that share a common prior bins = dict() for i in xrange(N_cities): bins[i] = pm.Binomial('bin_{}'.format(i), p=theta,n=N_trials[i], value=N_yes[i], observed=True) mcmc = pm.MCMC([bins, ps]) 

Update

Following John Salvier’s advice, I try the following (note that I am in PyMC2, although I would be happy to switch to PyMC3), but my questions are:

  • What needs to be imported so that I can inherit from Continuous correctly?
  • In PyMC2, do I still need to stick with the Anano notation?
  • Finally, how can I tell my Beta distribution later that alpha and Beta have a previous multidimensional distribution?

    import pymc.Multivariate.Continuous

    class CustomPrior (Continuous): "" p (a, b) Ξ± (a + b) ^ (- 5/2)

     :Parameters: None :Support: 2 positive floats (parameters to a Beta distribution) """ def __init__(self, mu, tau, *args, **kwargs): super(CustomPrior, self).__init__(*args, **kwargs) def logp(self, a,b): return np.log(math.power(a+b),-5./2) 
+3
python statsmodels pymc
source share
2 answers

In PyMC2, the trick is to combine the parameters a and b :

 # Code that defines the prior: p(a,b)∝(a+b)^(βˆ’5/2) @pm.stochastic def ab(power=-2.5, value=[1,1]): if np.any(value <= 0): return -np.Inf return power * np.log(value[0]+value[1]) a = ab[0] b = ab[1] 

This laptop has a complete example.

+3
source share

Yes! It is quite possible, and actually quite simple.

If you are in PyMC 2, check out the documentation on creating stochastic variables .

 @pymc.stochastic(dtype=int) def switchpoint(value=1900, t_l=1851, t_h=1962): """The switchpoint for the rate of disaster occurrence.""" if value > t_h or value < t_l: # Invalid values return -np.inf else: # Uniform log-likelihood return -np.log(t_h - t_l + 1) 

If you are in PyMC 3, check out multivariate.py . Remember that the values ​​passed to init and logp are all anano variables, not numpy arrays. Is that enough to get you started?

For example, this is a multidimensional normal distribution.

 class MvNormal(Continuous): """ Multivariate normal :Parameters: mu : vector of means tau : precision matrix .. math:: f(x \mid \pi, T) = \frac{|T|^{1/2}}{(2\pi)^{1/2}} \exp\left\{ -\frac{1}{2} (x-\mu)^{\prime}T(x-\mu) \right\} :Support: 2 array of floats """ def __init__(self, mu, tau, *args, **kwargs): super(MvNormal, self).__init__(*args, **kwargs) self.mean = self.median = self.mode = self.mu = mu self.tau = tau def logp(self, value): mu = self.mu tau = self.tau delta = value - mu k = tau.shape[0] return 1/2. * (-k * log(2*pi) + log(det(tau)) - dot(delta.T, dot(tau, delta))) 
+4
source share

All Articles