Permalink
Browse files

simplified process of creating variables and added default values to …

…size and dtype
  • Loading branch information...
1 parent 2d162dd commit 6c0bf0e6ddd7740948a23a51d8174193a99f2afc @jsalvatier committed May 2, 2012
Showing with 24 additions and 39 deletions.
  1. +12 −31 examples/hierarchial.py
  2. +2 −4 examples/simpletest.py
  3. +8 −3 mcex/core.py
  4. +2 −1 mcex/num_derivatives.py
View
@@ -29,60 +29,41 @@
model = Model()
#m_g ~ N(0, .1)
-group_effects = FreeVariable("group_effects", (1, n_group_predictors, n_predictors), 'float64')
-AddVar(model, group_effects, Normal(0, .1))
+group_effects = AddVar(model, "group_effects", Normal(0, .1), (1, n_group_predictors, n_predictors))
-# sg ~
-sg = FreeVariable("sg", 1, 'float64')
-AddVar(model, sg, Uniform(.05, 10))
-#sg = 1
+# sg ~ Uniform(.05, 10)
+sg = AddVar(model, "sg", Uniform(.05, 10))
#m ~ N(mg * pg, sg)
-effects = FreeVariable("effects", (n_groups, n_predictors), 'float64')
-AddVar(model, effects, Normal( sum(group_predictors[:, :, newaxis] * group_effects ,1) ,sg**-2))
+effects = AddVar(model, "effects",
+ Normal( sum(group_predictors[:, :, newaxis] * group_effects ,1) ,sg**-2),
+ (n_groups, n_predictors))
#s ~
-s = FreeVariable("s", n_groups, 'float64')
-AddVar(model, s, Uniform(.01, 10))
+s = AddVar(model, "s", Uniform(.01, 10), n_groups)
g = T.constant(group)
#y ~ Normal(m[g] * p, s)
AddData(model, T.constant(y), Normal( sum(effects[g] * predictors, 1),s[g]**-2))
-
-
-view = ModelView(model, 'all')
chain = {'sg' : np.array([2.]),
's' : np.ones(n_groups) * 2.,
'group_effects' : np.zeros((1,) + group_effects_a.shape),
'effects' : np.zeros(effects_a.shape ) }
-chain2, v = find_MAP(view, chain, retall = True)
-
-def project(chain, name, slc, value):
- c = chain.copy()
- c[name] = c[name].copy()
- c[name][slc] = value
- return c
-
-def get_like(chain, name, slc):
- def like(x):
- return view.evaluate(project(chain2, name, slc, x))[0]
- return like
-
-
-hmc_cov = approx_cov( view, chain2) #find a good orientation using the hessian at the MAP
+map_x, v = find_MAP(model, chain, retall = True)
+map_cov = approx_cov(model, map_x) #find a good orientation using the hessian at the MAP
-step_method = CompoundStep([hmc.HMCStep(view, hmc_cov, step_size_scaling = .25)])
+step_method = HMCStep(model, model.vars, map_cov, step_size_scaling = .25)
ndraw = 3e3
-history = NpHistory(view, ndraw) # an object that keeps track
-print "took :", sample(ndraw, step_method, chain2, history)
+history = NpHistory(model.vars, ndraw) # an object that keeps track
+print "took :", sample(ndraw, step_method, map_x, history)
View
@@ -12,12 +12,10 @@
model = Model()
-x = FreeVariable('x', (3,1), 'float64')
-AddVar(model, x, Normal(mu = .5, tau = 2.**-2))
+x = AddVar(model, 'x', Normal(mu = .5, tau = 2.**-2), (3,1))
-z = FreeVariable('z', (1,), 'float64')
-AddVar(model, z, Beta(alpha = 10, beta =5.5))
+z = AddVar(model, 'z', Beta(alpha = 10, beta =5.5))
AddData(model, data, Normal(mu = x, tau = .75**-2))
View
@@ -26,14 +26,19 @@ def __init__(self):
def AddData(model, data, distribution):
model.factors.append(distribution(data))
-def AddVar(model, var, distribution):
+def AddVar(model, name, distribution, shape = 1, dtype = 'float64'):
+ var = FreeVariable(name, shape, dtype)
model.vars.append(var)
model.factors.append(distribution(var))
+ return var
-def AddVarIndirect(model, var,proximate_variable, distribution):
+def AddVarIndirect(model, name,proximate_calc, distribution, shape = 1):
+ var = FreeVariable(name, shape)
model.vars.append(var)
- model.factors.append(distribution(proximate_variable) * grad(proximate_variable, var))
+ prox_var = proximate_calc(var)
+ model.factors.append(distribution(prox_var) * grad(prox_var, var))
+ return var
def continuous_vars(model):
return [ var for var in model.vars if var.dtype in continuous_types]
View
@@ -7,10 +7,11 @@
import numpy as np
from core import *
-def approx_cov(model, vars, chain_state):
+def approx_cov(model,chain_state):
"""
returns an approximation of the hessian at the current chain location
"""
+ vars = model.vars
dlogp = model_dlogp(model, vars)
mapping = DASpaceMap(vars)

0 comments on commit 6c0bf0e

Please sign in to comment.