In [1]:
import MVG as MVG
import SuperMatExpr as SME
from sympy import *
import utils as utils
from IPython.display import display, Math, Latex
In [2]:
# Define some symbols
D, N, Ns = symbols('D N Ns')
sig_y = symbols('\u03c3_y')
In [12]:
# Prior
w = SME.SuperMatSymbol(D,1,name='w',mat_type='var')
mu_w = SME.SuperMatSymbol(D,1,mat_type='mean',dep_vars=[w],expanded=ZeroMatrix(D,1))
S_ww = SME.SuperMatSymbol(D,D,mat_type='covar',dep_vars=[w],expanded=Identity(D))
p_w = MVG.MVG([w],moments=[mu_w,S_ww,utils.getZ(S_ww)])
print("p_w:")
display(Latex(utils.matLatex(p_w)))
In [13]:
# Likelihood of w given X
X = SME.SuperMatSymbol(D,N,name='X',mat_type='var')
y = SME.SuperMatSymbol(N,1,name='y',mat_type='var')
mu_y = SME.SuperMatSymbol(N,1,mat_type='mean',dep_vars=[y],cond_vars=[w,X],expanded=X.T*w)
S_yy = SME.SuperMatSymbol(N,N,mat_type='covar',dep_vars=[y],cond_vars=[w,X],expanded=sig_y**2*Identity(N))
p_y = MVG.MVG([y],moments=[mu_y,S_yy,utils.getZ(S_yy)],conditioned_vars=[w,X])
print("p_y:")
display(Latex(utils.matLatex(p_y)))
In [14]:
# Joint of w and y
p_w_y = p_w*p_y
print("p_w_y:")
display(Latex(utils.matLatex(p_w_y)))
In [15]:
# Inference: posterior over w
p_w_post = p_w_y.condition([y])
print("p_w_post:")
display(Latex(utils.matLatex(p_w_post)))
In [16]:
#Prediction
# Likelihood of w given Xs
Xs = SME.SuperMatSymbol(D,Ns,name='X_s',mat_type='var')
ys = SME.SuperMatSymbol(Ns,1,name='y_s',mat_type='var')
mu_ys = SME.SuperMatSymbol(Ns,1,mat_type='mean',dep_vars=[ys],cond_vars=[w,Xs],expanded=Xs.T*w)
S_ysys = SME.SuperMatSymbol(Ns,Ns,mat_type='covar',dep_vars=[ys],cond_vars=[w,Xs],expanded=sig_y**2*Identity(Ns))
p_ys = MVG.MVG([ys],moments=[mu_ys,S_ysys,utils.getZ(S_ysys)],conditioned_vars=[w,Xs])
print("p_ys:")
display(Latex(utils.matLatex(p_ys)))
In [17]:
# Joint of w and ys
p_w_ys = p_w_post*p_ys
print("p_w_ys:")
display(Latex(utils.matLatex(p_w_ys)))
In [18]:
# Predictive distribution of ys
p_ys_post = p_w_ys.marginalize([w])
print("p_ys_post:")
display(Latex(utils.matLatex(p_ys_post)))
In [ ]: