def P(self,x):
""" calculate vector of P_a for each action a """
indx = np.arange(len(x))
ps = self.pX[x,indx] #probability of P(X_i = x_i) for each i given do()
joint = ps.prod() # probability of x given do()
pi = np.true_divide(joint,ps) # will be nan for elements for which ps is 0
for j in np.where(np.isnan(pi))[0]:
pi[j] = np.prod(ps[indx != j])
pij = np.vstack((pi,pi))
pij[1-x,indx] = 0 # now this is the probability of x given do(x_i=j)
pij = pij.reshape((len(x)*2,)) #flatten first N-1 will be px=0,2nd px=1
result = np.hstack((pij,joint))
return result
评论列表
文章目录