for i in range(n): print "X{}".format(i) X[i] = BetaDistr(2, 2, sym = "X{}".format(i)) if i==0: S[i] = X[0] else: S[i] = S[i-1] + X[i] S[i].setSym("S{}".format(i)) M = Model(X, S[1:]) print M M.toGraphwiz() #M = M.inference([S[-1], S[-4]], [S[-3]], [1]) #M = M.inference([X[0], X[1]], [S[-1]], [3.5]) print "====================" M1 = M.inference(wanted_rvs =[X[0], X[1]], cond_rvs=[S[-1]], cond_X=[1]) print "====================",M1 M2 = M.inference(wanted_rvs =[S[1], S[4]]) print "====================",M2 M3 = M.inference(wanted_rvs =[S[1], S[4]], cond_rvs=[S[3]], cond_X=[2]) print "====================",M3 MC_X0 = M.inference(wanted_rvs =[X[0]], cond_rvs=[S[-1]], cond_X=[1]) print "====================" print M1 figure() M1.plot(cont_levels=10) figure() M1.plot(have_3d=True) print M2
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]: M2 = M.inference(wanted_rvs=[A, Y0], cond_rvs=[O[-1]] + U, cond_X=[yend] + ui) subplot(1, 3, i + 1) title("O_{0}={1}".format(n, yend)) M2.plot() ay0.append(M2.nddistr.mode()) # "most probable" state print "yend=", yend, ", MAP est. of A, Y0 =", ay0[i] i += 1 show() #! #! Trajectory #! ---------- figure() styles=['-', '--', '-.', ':'] for j in range(len(ay0)): ymean, ystd = [], []
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; X0 = BetaDistr(2, 2) y = X0 * exp(A) y.summary() y.plot(label="Y0*exp(A)") Y[-1].plot('r') figure() err = y.get_piecewise_pdf() - M3.as1DDistr().get_piecewise_pdf() err.plot() show() stop
Udenoised = zeros(nT) yi = 0.0 ydenoise = 0.0 ynoise = 0.0 y = 0.0 figure() for i in range(nT): t[i] = i # Deterministic simultation u[i] = 0.1 * sign(sin(4 * pi * i / nT)) y = y * K + u[i] Yorg[i] = y Ynoised[i] = y + E[0].rand() # Inference (Y[i] | O[i-n+1], ..., O[i] if i > n - 1: MY = M.inference(wanted_rvs=[Y[-1]], cond_rvs=O + U , cond_X=concatenate((Ynoised[i - n + 1:i + 1], u[i - n + 1:i + 1]))) ydenoised = MY.as1DDistr().median() Ydenoised[i] = ydenoised #MY.as1DDistr().boxplot(i, width=0.2, useci=0.1) plot(t, u, 'k-', label="U", linewidth=1.0) plot(t, Ynoised, 'k.--', label="O", linewidth=1.0) plot(t, Yorg, 'k-.', label="Y original", linewidth=3.0) plot(t, Ydenoised, 'k-', label="Y denoised", linewidth=2.0) legend(loc='lower left') #! Error of estimation using median #! -------------------------------- print "mse=", sqrt(mean((Yorg - Ynoised) ** 2)), sqrt(mean((Yorg - Ydenoised) ** 2)) print "mae=", mean(abs(Yorg - Ynoised)), mean(abs(Yorg - Ydenoised))
# MXY.plot() # plot([0.0, 1.0], [0.7, 0.7+a], "k-", linewidth=2.0) # plot(Xobs, Yobs, "ko") # plot() # show() #print ar, br print Xobs, Yobs print X + Y print concatenate((Xobs, Yobs)) #print M #MAB = M.inference([A,B]+E, X + Y, concatenate((Xobs, Yobs))) #print MAB #MAB = MAB.inference([A,B], X + Y, concatenate((Xobs, Yobs))) MW = M.inference(W, X + Y, concatenate((Xobs, Yobs))) print "-------------------" MW0 = MW.inference([W[0]], [], []) #MB = MW.inference([B],[],[]) #M = M.inference([A,B], [X[0], Y[0]], [0.2, 0.4]) print MW print MW0 figure() subplot(211) MW0.plot() #print MB #subplot(212) #MB.plot() #print "mean est. A=", MA.as1DDistr().mean(), "est. B=", MB.as1DDistr().mean()
X2 = BetaDistr(4, 4, sym="X2") Y1 = BetaDistr(4, 4, sym="Y1") Y2 = BetaDistr(4, 4, sym="Y2") C1 = FrankCopula2d(theta=5, marginals=[X1, X2]) C2 = FrankCopula2d(theta=2, marginals=[Y1, Y2]) C1.contour() C2.contour() figure() # C1 = FrankCopula2d(theta=2, marginals=[X1, X2]) # C2 = FrankCopula2d(theta=2.5, marginals=[Y1, Y2]) #C1 = GumbelCopula2d(theta=2, marginals=[X1, X2]) #C1 = PiCopula(marginals=[X1, X2]) Z1 = X1 + Y1 Z1.setSym("Z1") Z2 = X2 + Y2 Z2.setSym("Z2") M = Model([C1, C2], [Z1, Z2]) #M = Model([C1,Y1,Y2], [Z1, Z2]) #M = Model([X1,X2,Y1,Y2], [Z1, Z2]) print(M) M2 = M.inference([Z1, Z2]) print(M2) M2.plot() show()
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]: M2 = M.inference(wanted_rvs=[A, Y0], cond_rvs=[O[-1]] + U, cond_X=[yend] + ui) subplot(1, 3, i + 1) title("O_{0}={1}".format(n, yend)) M2.plot() ay0.append(M2.nddistr.mode()) # "most probable" state print "yend=", yend, ", MAP est. of A, Y0 =", ay0[i] i += 1 show() #! #! Trajectory #! ---------- figure() styles = ['-', '--', '-.', ':'] for j in range(len(ay0)):
yi = 0.0 ydenoise = 0.0 ynoise = 0.0 y = 0.0 figure() for i in range(nT): t[i] = i # Deterministic simultation u[i] = 0.1 * sign(sin(4 * pi * i / nT)) y = y * K + u[i] Yorg[i] = y Ynoised[i] = y + E[0].rand() # Inference (Y[i] | O[i-n+1], ..., O[i] if i > n - 1: MY = M.inference(wanted_rvs=[Y[-1]], cond_rvs=O + U, cond_X=concatenate( (Ynoised[i - n + 1:i + 1], u[i - n + 1:i + 1]))) ydenoised = MY.as1DDistr().median() Ydenoised[i] = ydenoised #MY.as1DDistr().boxplot(i, width=0.2, useci=0.1) plot(t, u, 'k-', label="U", linewidth=1.0) plot(t, Ynoised, 'k.--', label="O", linewidth=1.0) plot(t, Yorg, 'k-.', label="Y original", linewidth=3.0) plot(t, Ydenoised, 'k-', label="Y denoised", linewidth=2.0) legend(loc='lower left') #! Error of estimation using median #! -------------------------------- print "mse=", sqrt(mean((Yorg - Ynoised)**2)), sqrt(mean( (Yorg - Ydenoised)**2)) print "mae=", mean(abs(Yorg - Ynoised)), mean(abs(Yorg - Ydenoised))
X2 = BetaDistr(4,4, sym="X2") Y1 = BetaDistr(4,4, sym="Y1") Y2 = BetaDistr(4,4, sym="Y2") C1 = FrankCopula2d(theta=5, marginals=[X1, X2]) C2 = FrankCopula2d(theta=2, marginals=[Y1, Y2]) C1.contour() C2.contour() figure() # C1 = FrankCopula2d(theta=2, marginals=[X1, X2]) # C2 = FrankCopula2d(theta=2.5, marginals=[Y1, Y2]) #C1 = GumbelCopula2d(theta=2, marginals=[X1, X2]) #C1 = PiCopula(marginals=[X1, X2]) Z1 = X1 + Y1; Z1.setSym("Z1") Z2 = X2 + Y2; Z2.setSym("Z2") M = Model([C1,C2], [Z1, Z2]) #M = Model([C1,Y1,Y2], [Z1, Z2]) #M = Model([X1,X2,Y1,Y2], [Z1, Z2]) print(M) M2 = M.inference([Z1, Z2]) print(M2) M2.plot() show()
# MXY.plot() # plot([0.0, 1.0], [0.7, 0.7+a], "k-", linewidth=2.0) # plot(Xobs, Yobs, "ko") # plot() # show() #print ar, br print Xobs, Yobs print X + Y print concatenate((Xobs, Yobs)) #print M #MAB = M.inference([A,B]+E, X + Y, concatenate((Xobs, Yobs))) #print MAB #MAB = MAB.inference([A,B], X + Y, concatenate((Xobs, Yobs))) MW = M.inference(W, X + Y, concatenate((Xobs, Yobs))) print "-------------------" MW0 = MW.inference([W[0]],[],[]) #MB = MW.inference([B],[],[]) #M = M.inference([A,B], [X[0], Y[0]], [0.2, 0.4]) print MW print MW0 figure() subplot(211) MW0.plot() #print MB #subplot(212) #MB.plot() #print "mean est. A=", MA.as1DDistr().mean(), "est. B=", MB.as1DDistr().mean()
for i in range(n): print("X{}".format(i)) X[i] = BetaDistr(2, 2, sym = "X{}".format(i)) if i==0: S[i] = X[0] else: S[i] = S[i-1] + X[i] S[i].setSym("S{}".format(i)) M = Model(X, S[1:]) print(M) M.toGraphwiz() #M = M.inference([S[-1], S[-4]], [S[-3]], [1]) #M = M.inference([X[0], X[1]], [S[-1]], [3.5]) print("====================") M1 = M.inference(wanted_rvs =[X[0], X[1]], cond_rvs=[S[-1]], cond_X=[1]) print("====================",M1) M2 = M.inference(wanted_rvs =[S[1], S[4]]) print("====================",M2) M3 = M.inference(wanted_rvs =[S[1], S[4]], cond_rvs=[S[3]], cond_X=[2]) print("====================",M3) MC_X0 = M.inference(wanted_rvs =[X[0]], cond_rvs=[S[-1]], cond_X=[1]) print("====================") print(M1) figure() M1.plot(cont_levels=10) figure() M1.plot(have_3d=True) print(M2)
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 X0 = BetaDistr(2, 2) y = X0 * exp(A) y.summary() y.plot(label="Y0*exp(A)") Y[-1].plot('r') figure() err = y.get_piecewise_pdf() - M3.as1DDistr().get_piecewise_pdf() err.plot() show() stop
for i in range(n): print(i) if i==0: X[i] = BetaDistr(3, 3, sym = "X0") Y[i] = BetaDistr(3, 3, sym = "Y0") else: X[i] = X[i-1] + h*(A*X[i-1] - B*Y[i-1]) Y[i] = Y[i-1] + h*(-C*X[i-1] + D*Y[i-1]) X[i].setSym("X{}".format(i)) Y[i].setSym("Y{}".format(i)) M = Model([X[0], Y[0], A, B, C, D], X[1:] + Y[1:] ) print(M) M.eliminate_other([X[0], Y[0], A, B, C, D] + X[1:] + Y[1:]) print(M) print(Y[1].range()) #M1 = M.inference([X[2], Y[2]], [X[0], Y[0]], [0.5, 0.2])\ figure() for i in range(n): M1 = M.inference([X[i], Y[i]], [A, B, C, D], [0.9, 0.2, 0.3, 0.6]) print(M1) M1.plot(cont_levels=1) #figure() #M1.plot(have_3d=True) show()
h=0.1 for i in range(n): print i if i==0: X[i] = BetaDistr(3, 3, sym = "X0") Y[i] = BetaDistr(3, 3, sym = "Y0") else: X[i] = X[i-1] + h*(A*X[i-1] - B*Y[i-1]) Y[i] = Y[i-1] + h*(-C*X[i-1] + D*Y[i-1]) X[i].setSym("X{}".format(i)) Y[i].setSym("Y{}".format(i)) M = Model([X[0], Y[0], A, B, C, D], X[1:] + Y[1:] ) print M M.eliminate_other([X[0], Y[0], A, B, C, D] + X[1:] + Y[1:]) print M print Y[1].range() #M1 = M.inference([X[2], Y[2]], [X[0], Y[0]], [0.5, 0.2])\ figure() for i in range(n): M1 = M.inference([X[i], Y[i]], [A, B, C, D], [0.9, 0.2, 0.3, 0.6]) print M1 M1.plot(cont_levels=1) #figure() #M1.plot(have_3d=True) show()