def approx_pred_mean_var(self, i, j): mean = self.mean cov = self.cov us = self.u[:, i] vs = self.v[:, j] mn = (mean[us] * mean[vs] + cov[us, vs]).sum() var = exp_dotprod_sq(self.u, self.v, mean, cov, i, j) - mn**2 return mn, var
def pred_variance(self, ij): ''' The variance in our prediction for R_ij, according to the current approximation. ''' i, j = ij mean = self.mean cov = self.cov us = self.u[:, i] vs = self.v[:, j] return ( # E[ (U_i^T V_j)^2 ] exp_dotprod_sq(self.u, self.v, mean, cov, i, j) # - E[U_i^T V_j] ^ 2 - (mean[us] * mean[vs] + cov[us, vs]).sum()**2)
def test_exp_dotprod_sq(): n = 1 m = 1 d = 3 k = (n + m) * d u = np.arange(0, n * d).reshape(n, d).T v = np.arange(n * d, (n+m) * d).reshape(m, d).T for i in range(NUM_TIMES): mn = np.random.normal(0, 10, (k,)) cov = project_psd(np.random.normal(0, 5, (k,k)), 1e-5) samps = np.random.multivariate_normal(mn, cov, NUM_SAMPS) monte = _mean(np.dot(x[u[:,0]], x[v[:,0]])**2 for x in samps) exp1 = pure.exp_dotprod_sq(u, v, mn, cov, 0, 0) exp2 = cyth.exp_dotprod_sq(u, v, mn, cov, 0, 0) assert exp1 - exp2 < 1e-7 assert abs(monte - exp1) / exp1 < .02, "%r - %r" % (monte, exp1)
def pred_variance(self, ij): ''' The variance in our prediction for R_ij, according to the current approximation. ''' i, j = ij mean = self.mean cov = self.cov us = self.u[:, i] vs = self.v[:, j] return ( # E[ (U_i^T V_j)^2 ] exp_dotprod_sq(self.u, self.v, mean, cov, i, j) # - E[U_i^T V_j] ^ 2 - (mean[us] * mean[vs] + cov[us, vs]).sum() ** 2 )