def anlPrior1(): pois = ss.poisson pois4 = {} fib3 = {} for i in range(101): pois4[i] = 0 fib3[i] = 0 for i in range(101): prob = pois.pmf(i,4) pois4[i] = prob if i < 5: fib3[fib1(3*i)] = prob fib3 = stocPy.norm(fib3) p = pois.cdf(3,4) print sum(pois4.values()) print sorted(fib3.items()) print sum(fib3.values()) l2 = {} for v1, p1 in fib3.items(): poisCur = {} for i in range(101): prob = pois.pmf(i,v1) if math.isnan(prob): prob = 0 print v1, i poisCur[i] = prob #print v1, v2, p1, p2 for v2, p2 in poisCur.items(): try: l2[v1+v2] += p1*p2 except: l2[v1+v2] = p1*p2 l2 = stocPy.norm(l2) print sorted(l2.items()) print sum(l2.values()) l = {} for k,v in l2.items(): l[k] = v * (1-p) try: l[6] += p except: l[6] = p print sum(l.values()) plotDict(l, 2000)
def anlLik(plot=True, trans=True, twoDim = True): rng = 15 pois = ss.poisson pois4 = {} for i in range(rng): pois4[i] = pois.pmf(i, 4) lik = {} for r, pr in pois4.items(): for p, pp in pois4.items(): if r > 4: l = 6 else: l = fib0(3*r) + p pl = pois.pmf(6, l) if r > 4 and trans: #2nd poisson not evaluated prob = pr*pl else: prob = pr*pp*pl if r == 0 and p == 0: prob = 0 if twoDim: lik[(r,p)] = prob else: try: lik[r] += prob except: lik[r] = prob if plot: if twoDim: Xs2, Ys2 = np.meshgrid(range(rng),range(rng)) print len(Xs2), len(Ys2) Zs2 = [] for i in range(len(Xs2)): Zs2.append([]) for j in range(len(Ys2)): Zs2[i].append(lik[Xs2[i][j], Ys2[i][j]]) fig = plt.figure() ax = fig.gca(projection='3d') ax.plot_surface(Xs2, Ys2, Zs2, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=0) plt.xlabel("pois1", size=20) plt.ylabel("pois2", size=20) ax.set_zlabel("Trace likelihood", size=20) plt.title("Trace likelihood when ignoring trans-dimensionality", size=20) else: Xs = [] Ys = [] lik = stocPy.norm(lik) [(Xs.append(x), Ys.append(y)) for x,y in sorted(lik.items())] plt.plot(Xs, Ys, 'D') plt.xlabel("pois1", size=20) plt.ylabel("Probability", size=20) plt.title("Posterior implied by ignoring trans-dimensionality", size=22) plt.show() return lik
def getMixExpMovement(d, depths, weights = None): if not weights: weights = [1.0 for i in range(len(depths))] weights = stocPy.norm(weights) mix = 0 for i in range(len(depths)): mix += weights[i] * getExpMovement(d, depths[i]) return mix
def anlPost(data, ps, prior = ss.uniform(0,20).pdf): probs = {} for p in ps: val = prior(p) if val > 0: ep = math.exp(-float(p)) for d in data: val *= float(p)**float(d) * ep / math.factorial(float(d)) probs[p] = val probs = stocPy.norm(probs) return probs
def getPost(model, start, end, inc, aprox=True, show = True, fn=None, rfn = None, data=None): xs = [] ys = [] if rfn: with open(rfn,'r') as f: xs, ys = cPickle.load(f) else: for m in np.arange(start, end+inc, inc): xs.append(m) if aprox: ys.append(getLikAprox(model, m, data=data)) else: ys.append(getLikExact(model, m)) if aprox and (model < 5 or model == 9): ys = stocPy.norm(ys) else: print ys corr = 1500 print [math.e**(y + corr) for y in ys] ys = stocPy.norm([math.e**(y + corr) for y in ys])
def anlPrior(): pois = ss.poisson pois4 = {} fib3 = {} for i in range(101): pois4[i] = 0 fib3[i] = 0 for i in range(101): prob = pois.pmf(i,4) pois4[i] = prob if i < 5: fib3[fib0(3*i)] = prob fib3 = stocPy.norm(fib3) p = pois.cdf(3,4) print sum(pois4.values()) print sorted(fib3.items()) print sum(fib3.values()) l2 = {} for v1, p1 in pois4.items(): for v2, p2 in fib3.items(): try: l2[v1+v2] += p1*p2 except: l2[v1+v2] = p1*p2 print sum(l2.values()) l = {} for k,v in l2.items(): l[k] = v * (1-p) try: l[6] += p except: l[6] = p print sum(l.values()) plotDict(l, 2000)
def getPost(start, end, inc, show = True, fn=None, rfn = None): xs = [] ys = [] if rfn: with open(rfn,'r') as f: xs, ys = cPickle.load(f) else: for m in np.arange(start, end+inc, inc): xs.append(m) ys.append(ss.norm.pdf(9, m, math.sqrt(2)) * ss.norm.pdf(8, m, math.sqrt(2)) * ss.norm.pdf(m, 1, math.sqrt(5))) ys = stocPy.norm(ys) if show: plt.plot(xs,ys, linewidth=3) plt.ylabel("Probability", size=20) plt.xlabel("x", size=20) plt.title("True Posterior for MarsagliaMean model", size=30) plt.show() if fn: with open(fn,'w') as f: cPickle.dump((xs,ys),f) return dict(zip(xs, ys))
def anlPost(disp=True, rep=1, cond=6, size=21): pois = ss.poisson pois4 = {} for i in range(size): prob = pois.pmf(i,4) pois4[i] = prob rs = {} for r in range(size): pr = pois4[r] l = {} if r > 4: l[6] = 1 else: f = fib0(3*r) for k,v in pois4.items(): l[k+f] = v assert(abs(sum(l.values()) - 1) < 0.0001) for val,prob in l.items(): #print r, val, pois.pmf(6,val), prob condProb = 1 for i in range(rep): condProb *= pois.pmf(cond,val) pl = condProb * prob if math.isnan(pl): pl = 0 try: rs[r] += pl*pr except: rs[r] = pl*pr rs = stocPy.norm(rs) if disp: print sorted(rs.items()) print sum(rs.values()) plotDict(rs) return rs
def getPost(ds, a, fn=None): crp = stocPy.CRP(a) parts = allParts(ds) post = {} for p in range(len(parts)): part = parts[p] ll = crp.logpmf(part) if p % 1000 == 0: print p, part, ll, sys.stdout.flush() for cl in part: ll += getLL(cl, fn) try: post[len(part)].append(ll) except: post[len(part)] = [ll] if p % 1000 == 0: print ll sys.stdout.flush() post = dict([(k, sum(map(lambda x: math.e**x, v))) for (k, v) in post.items()]) print post post = stocPy.norm(post) return post