Exemplo n.º 1
0
def update_hU_poisson_gamma_hierarchical(ap, bp, a, U):
    """ Update hU (vector) for Poisson + Gamma + hierarchical model. """
    I, K = U.shape
    hU = numpy.zeros(I)
    for i in range(I):
        (a_s, b_s) = gamma_hierarchical_hUi_a_b(ap=ap, bp=bp, a=a, Ui=U[i,:])
        hU[i] = gamma_draw(alpha=a_s, beta=b_s)
    return hU
Exemplo n.º 2
0
def update_U_poisson_gamma(a, b, M, V, Z):
    """ Update U for Poisson + Gamma model. """
    I, J, K = Z.shape
    assert V.shape == (J,K) and M.shape == (I,J)
    U = numpy.zeros((I,K))
    for i,k in itertools.product(range(I),range(K)):
        (a_s, b_s) = poisson_gamma_a_b(a=a, b=b, Mi=M[i,:], Vk=V[:,k], Zik=Z[i,:,k]) 
        U[i,k] = gamma_draw(alpha=a_s, beta=b_s)
    return U
Exemplo n.º 3
0
def update_tauU_gaussian_truncatednormal_hierarchical(a, b, U, muU):
    """ Update tauU (matrix) for Gaussian + Truncated Normal + hierarchical model. """
    I, K = U.shape
    assert U.shape == muU.shape
    (a_s, b_s) = tn_hierarchical_tau_a_b(a=a, b=b, U=U, muU=muU)
    new_tauU = numpy.zeros((I,K))
    for i,k in itertools.product(range(I),range(K)):
        new_tauU[i,k] = gamma_draw(alpha=a_s[i,k], beta=b_s[i,k])
    return new_tauU
Exemplo n.º 4
0
def update_lambda_gaussian_exponential_ard(alpha0, beta0, U, V):
    """ Update lambda (vector) for Gaussian + Exponential + ARD model. """
    K = U.shape[1]
    new_lambda = numpy.zeros(K)
    for k in range(K):
        alpha_s, beta_s = exponential_ard_alpha_beta(
            alpha0=alpha0, beta0=beta0, Uk=U[:,k], Vk=V[:,k])
        new_lambda[k] = gamma_draw(alpha=alpha_s, beta=beta_s)
    return new_lambda
Exemplo n.º 5
0
def update_U_poisson_gamma_hierarchical(a, hU, M, V, Z):
    """ Update U for Poisson + Gamma + hierarchical model. """
    I, J, K = Z.shape
    assert hU.shape == (I,) and V.shape == (J,K)
    U = numpy.zeros((I,K))
    for i,k in itertools.product(range(I),range(K)):
        (a_s, b_s) = poisson_gamma_hierarchical_a_b(
            a=a, hUi=hU[i], Mi=M[i,:], Vk=V[:,k], Zik=Z[i,:,k])
        U[i,k] = gamma_draw(alpha=a_s, beta=b_s)
    return U
Exemplo n.º 6
0
 def run(self,iterations):
     self.all_F = numpy.zeros((iterations,self.I,self.K))  
     self.all_S = numpy.zeros((iterations,self.K,self.L))   
     self.all_G = numpy.zeros((iterations,self.J,self.L))  
     self.all_tau = numpy.zeros(iterations)
     self.all_times = [] # to plot performance against time
     
     metrics = ['MSE','R^2','Rp']
     self.all_performances = {} # for plotting convergence of metrics
     for metric in metrics:
         self.all_performances[metric] = []
     
     time_start = time.time()
     for it in range(0,iterations):            
         for k in range(0,self.K):
             tauFk = self.tauF(k)
             muFk = self.muF(tauFk,k)
             self.F[:,k] = TN_vector_draw(muFk,tauFk)
             
         for k,l in itertools.product(xrange(0,self.K),xrange(0,self.L)):
             tauSkl = self.tauS(k,l)
             muSkl = self.muS(tauSkl,k,l)
             self.S[k,l] = TN_draw(muSkl,tauSkl)
             
         for l in range(0,self.L):
             tauGl = self.tauG(l)
             muGl = self.muG(tauGl,l)
             self.G[:,l] = TN_vector_draw(muGl,tauGl)
             
         self.tau = gamma_draw(self.alpha_s(),self.beta_s())
         
         self.all_F[it], self.all_S[it], self.all_G[it], self.all_tau[it] = numpy.copy(self.F), numpy.copy(self.S), numpy.copy(self.G), self.tau
         
         perf = self.predict_while_running()
         for metric in metrics:
             self.all_performances[metric].append(perf[metric])
             
         print "Iteration %s. MSE: %s. R^2: %s. Rp: %s." % (it+1,perf['MSE'],perf['R^2'],perf['Rp'])
     
         time_iteration = time.time()
         self.all_times.append(time_iteration-time_start)            
         
     return (self.all_F, self.all_S, self.all_G, self.all_tau)
Exemplo n.º 7
0
    def run(self, iterations):
        self.all_U = numpy.zeros((iterations, self.I, self.K))
        self.all_V = numpy.zeros((iterations, self.J, self.K))
        self.all_tau = numpy.zeros(iterations)
        self.all_times = []  # to plot performance against time

        metrics = ["MSE", "R^2", "Rp"]
        self.all_performances = {}  # for plotting convergence of metrics
        for metric in metrics:
            self.all_performances[metric] = []

        time_start = time.time()
        for it in range(0, iterations):
            for k in range(0, self.K):
                tauUk = self.tauU(k)
                muUk = self.muU(tauUk, k)
                self.U[:, k] = TN_vector_draw(muUk, tauUk)

            for k in range(0, self.K):
                tauVk = self.tauV(k)
                muVk = self.muV(tauVk, k)
                self.V[:, k] = TN_vector_draw(muVk, tauVk)

            self.tau = gamma_draw(self.alpha_s(), self.beta_s())

            self.all_U[it], self.all_V[it], self.all_tau[it] = numpy.copy(self.U), numpy.copy(self.V), self.tau

            perf = self.predict_while_running()
            for metric in metrics:
                self.all_performances[metric].append(perf[metric])

            print "Iteration %s. MSE: %s. R^2: %s. Rp: %s." % (it + 1, perf["MSE"], perf["R^2"], perf["Rp"])

            time_iteration = time.time()
            self.all_times.append(time_iteration - time_start)

        return (self.all_U, self.all_V, self.all_tau)
Exemplo n.º 8
0
def update_tau_gaussian(alpha, beta, R, M, U, V):
    """ Update tau (noise) in Gaussian models. """
    alpha_s, beta_s = gaussian_tau_alpha_beta(alpha, beta, R, M, U, V)
    new_tau = gamma_draw(alpha=alpha_s, beta=beta_s)
    return new_tau