コード例 #1
0
ファイル: tdfModel.py プロジェクト: RazvanRanca/PP_Models
def runTimedCont(v, ys, maxTime, burn, lag):
  v.assume("d", "(uniform_continuous 2 100)")
  v.assume("y", "(lambda () (student_t d))")

  [v.observe("(y)", str(ys[i])) for i in range(len(ys))]
  samples = pu.posterior_samples_timed(v, "d", maxTime=maxTime, no_burns=burn, int_mh=lag)

  vals = map(lambda x:x[1], samples)
  print "Sample mean: ", np.mean(vals), " Sample Stdev: ", np.std(vals)
  pu.save_samples(samples, os.getcwd(), "cont600")
コード例 #2
0
ファイル: tdfModel.py プロジェクト: RazvanRanca/PP_Models
def runModel(v, ys, mType, sample, burn, lag, fnAdd = "", timeTest = False, silentSamp = False):
  timeStart = time.time()
  if mType == "cont":
    v.assume("d", "(uniform_continuous 2 100)")
    v.assume("y", "(lambda () (student_t d))")
  elif mType == "cont5var":
    v.assume("d1", "(uniform_continuous 2 100)")
    v.assume("d2", "(uniform_continuous 0 0.001)")
    v.assume("d3", "(uniform_continuous 0 0.001)")
    v.assume("d4", "(uniform_continuous 0 0.0001)")
    v.assume("d5", "(uniform_continuous 0 0.0001)")
    v.assume("d", "(+ d1 d2 d3 d4 d5)") #(uniform_continuous 0 5) (uniform_continuous 0 1))")
    v.assume("y", "(lambda () (student_t d))")
  elif mType == "contBin":
    v.assume("f1", "(* 0.5 (categorical 0.5 0.5))")
    v.assume("r", "(uniform_continuous 0 0.5)")
    v.assume("d", "(+ f1 r)")
    v.assume("y", "(lambda () (student_t d))")
  elif mType == "contBin3":
    v.assume("f0", "(* 0.5 (uniform_discrete 0 2))")
    v.assume("f1", "(* 0.25 (uniform_discrete 0 2))")
    v.assume("f2", "(* 0.125 (uniform_discrete 0 2))")
    v.assume("r", "(uniform_continuous 0 0.125)")
    v.assume("d", "(+ 2 (* 98 (+ f0 f1 f2 r)))")
    v.assume("y", "(lambda () (student_t d))")
  elif mType == "contBin7":
    v.assume("f0", "(* 0.5 (uniform_discrete 0 2))")
    v.assume("f1", "(* 0.25 (uniform_discrete 0 2))")
    v.assume("f2", "(* 0.125 (uniform_discrete 0 2))")
    v.assume("f3", "(* 0.0625 (uniform_discrete 0 2))")
    v.assume("f4", "(* 0.03125 (uniform_discrete 0 2))")
    v.assume("f5", "(* 0.015625 (uniform_discrete 0 2))")
    v.assume("f6", "(* 0.0078125 (uniform_discrete 0 2))")
    v.assume("r", "(uniform_continuous 0 0.0078125)")
    v.assume("d", "(+ 2 (* 98 (+ f0 f1 f2 f3 f4 f5 f6 r)))")
    v.assume("y", "(lambda () (student_t d))")
  elif mType == "contMix":
    v.assume("d1", "(uniform_continuous 2 97)")
    v.assume("d2", "(uniform_continuous 0 1)")
    v.assume("d3", "(uniform_continuous 0 2)") 
    v.assume("d", "(+ d1 d2 d3)") #(uniform_continuous 0 5) (uniform_continuous 0 1))")
    v.assume("y", "(lambda () (student_t d))")
  elif mType == "flip":
    v.assume("d0", "(uniform_continuous 0 1)")
    v.assume("f0", "(uniform_discrete 0 2)")
    v.assume("f1", "(* 2 (uniform_discrete 0 2))")
    v.assume("f2", "(* 4 (uniform_discrete 0 2))")
    v.assume("f3", "(* 8 (uniform_discrete 0 2))")
    v.assume("f4", "(* 16 (uniform_discrete 0 2))")
    v.assume("f5", "(* 32 (uniform_discrete 0 2))")
    v.assume("f6", "(* 64 (uniform_discrete 0 2))")
    v.assume("d", "(+ (+ (+ d0 f0) (+ f1 f2)) (+ (+ f3 f4) (+ f5 f6)))") #(uniform_continuous 0 5) (uniform_continuous 0 1))")
    v.assume("y", "(lambda () (student_t d))")
  elif mType == "disc1":
    v.assume("d", "(uniform_discrete 2 50)")
    v.assume("y", "(lambda () (student_t d))")
  elif mType == "disc2":
    v.assume("d", "(/ (uniform_discrete 20 60) 10)")
    v.assume("y", "(lambda () (student_t d))")
  elif mType == "disc21":
    v.assume("d", "(uniform_discrete 20 60)")
    v.assume("y", "(lambda () (student_t (/ d 10)))")
  else:
    raise Exception("Unknown model type: " + mType)

  [v.observe("(y)", str(ys[i])) for i in range(len(ys))]
  samples = pu.posterior_samples(v, "d", no_samples=sample, no_burns=burn, int_mh=lag, silent = silentSamp)

  if timeTest:
    return time.time() - timeStart
  print '\n'.join([str(samples[i][1]) for i in range(len(samples)) if i%10==0])
  #v.infer(11000000)
  vals = map(lambda x:x[1], samples)
  print "Sample mean: ", np.mean(vals), " Sample Stdev: ", np.std(vals)
  pu.save_samples(samples, os.getcwd(), fnAdd+mType)