def __init__(self, nddistr, rvs=[]): if not isinstance(nddistr, NDFun): nddistr = NDProductDistr(nddistr) self.nddistr = nddistr self._segint = nddistr._segint free_rvs = list(self.nddistr.Vars) # fetch all necessary RVs dep_rvs = set(rvs) for rv in rvs: dep_rvs.update(rv.getParentsDep()) self.free_rvs = free_rvs self.dep_rvs = list(sorted(dep_rvs, key=_get_str_sym_name)) self.all_vars = list( sorted(set(free_rvs) | dep_rvs, key=_get_str_sym_name)) self.sym_to_rv = {} for rv in self.all_vars: self.sym_to_rv[rv.getSymname()] = rv self.rv_to_equation = {} for rv in self.dep_rvs: self.rv_to_equation[rv] = rv.getSym()
U.append(UniformDistr(-0.1, 0.1, sym="U{0}".format(i))) # U will be conditioned on, so in effect constant if i == 0: Y.append(Y0 * K + h * U[i]) else: Y.append(Y[i - 1] * K + h * U[i]) Y[-1].setSym("Y" + str(i + 1)) ei = NormalDistr(0.0, 0.1) | Between(-0.4, 0.4) ei.setSym("E{0}".format(i)) E.append(ei) O.append(Y[-1] + E[-1]) O[-1].setSym("O{0}".format(i)) #! #! Model #! ----- P = NDProductDistr([A, Y0] + E + U) M = Model(P, O) print M M.eliminate_other(E + Y + O + [A, Y0] + U) print M M.toGraphwiz(f=open('bn.dot', mode="w+")) #! #! Joint distribution of initial condition and parameter of equation #! ----------------------------------------------------------------- i = 0 ay0 = [] ui = [0.0] * n figure() for yend in [0.25, 1.25, 2.25]:
A = BetaDistr(3, 3, sym="A") B = BetaDistr(2, 4, sym="A") Y0 = BetaDistr(2, 2, sym="Y0") n = 5 h = 1.0 / n K = (1 + h * A) K.setSym("K") Y = [Y0] * (n + 1) for i in xrange(n + 1): if i == 0: pass else: Y[i] = Y[i - 1] * K Y[i].setSym("Y" + str(i)) P = NDProductDistr([Factor1DDistr(A), Factor1DDistr(Y[0])]) M = Model(P, Y[1:]) M.eliminate_other([K] + Y) #M2 = M.inference2([Y[0], A], [Y[n]], [1]) #M2.plot(); print M2; show() #M2 = M.inference2([Y[0]], [Y[n]], [0.5]) #figure() #M2.plot(); print M2; figure() Y[-1].plot(color='r', linewidth=5) M3 = M.inference([Y[-1]], [], []) M3.plot() print M3
#M2 = M.inference(wanted_rvs = [X], cond_rvs = [S], cond_X = [2.5]) M2 = M.inference(wanted_rvs=[X, Y], cond_rvs=[S], cond_X=[2.5]).plot() print "===", M2 #M.plot() show() 0 / 0 figure() N = X * Y N.setSym("N") D = X + Y D.setSym("D") R = N / D R.setSym("R") P = NDProductDistr([X, Y]) M = Model(P, [N, D, R]) print M M2 = M.inference(wanted_rvs=[R]) M2.plot() 0 / 0 M.varschange(X, N) M.eliminate(X) print M M.plot() show() 0 / 0 X1 = UniformDistr(1.5, 2.5, sym="x1") X2 = UniformDistr(1.5, 2.5, sym="x2") X3 = UniformDistr(1.5, 2.5, sym="x3")