Posted By

fonnesbeck on 10/14/09


Tagged

model statistics MCMC hierarchical PyMC Bayesian


Versions (?)

Bayesian hierarchical model in PyMC


 / Published in: Python
 

URL: http://pymc.googlecode.com

Simple hierarchical linear model of price as a function of age. Implemented in PyMC.

  1. """
  2. Simple hierarchical linear model of price as a function of age.
  3. """
  4.  
  5. from pymc import *
  6. from numpy import array
  7.  
  8. # Data
  9. age = array([13, 14, 14,12, 9, 15, 10, 14, 9, 14, 13, 12, 9, 10, 15, 11, 15, 11, 7, 13, 13, 10, 9, 6, 11, 15, 13, 10, 9, 9, 15, 14, 14, 10, 14, 11, 13, 14, 10])
  10. price = array([2950, 2300, 3900, 2800, 5000, 2999, 3950, 2995, 4500, 2800, 1990, 3500, 5100, 3900, 2900, 4950, 2000, 3400, 8999, 4000, 2950, 3250, 3950, 4600, 4500, 1600, 3900, 4200, 6500, 3500, 2999, 2600, 3250, 2500, 2400, 3990, 4600, 450,4700])/1000.
  11.  
  12. """Original WinBUGS model:
  13.  
  14. model
  15. {
  16. for( i in 1 : N ) {
  17. mu[i] <- a+b*age[i]
  18. price[i] ~ dnorm(mu[i], tau)
  19. }
  20. tau ~ dgamma(0.001, 0.001)
  21. sigma <- 1 / sqrt(tau)
  22. a ~ dnorm(0, 1.0E-12)
  23. b ~ dnorm(0, 1.0E-12)
  24. }"""
  25.  
  26. # Constant priors for parameters
  27. a = Normal('a', 0, 0.0001)
  28. b = Normal('b', 0, 0.0001)
  29.  
  30. # Precision of normal distribution of prices
  31. tau = Gamma('tau', alpha=0.1, beta=0.1)
  32.  
  33. @deterministic
  34. def mu(x=age, a=a, b=b):
  35. # Linear age-price model
  36. return a + b*x
  37.  
  38.  
  39. # Sampling distribution of prices
  40. p = Normal('p', mu, tau, value=price, observed=True)

Report this snippet  

You need to login to post a comment.