コード例 #1
0
    def expectation(self, data, model):
        """Compute probabilities
            store in self.model
        """
        mus = model.mu
        sigma = model.sigma
        weight = model.weight
        probabilities = []

        rho = np.zeros(len(data))   # rho is probabilities
        for r in range(len(data)):
            row = data[r]
            x_less = np.array(row) - np.array(mus)
            # the expectation <Zim> * P(x|Zim)

            rho[r] = multivariate_normal(sigma, x_less, .001) * weight
            #rho[r] = scipy.stats.multivariate_normal.pdf(data[r], mean=mus, cov=sigma) * weight
        #model.probabilities = hw3.univariate_normal(data, np.std(data), model.mu, model.weight)
        print 'RHO'
        print rho
        # log likelihood
        # llh = np.log(rho[i]/sum(rho))
        sum_rho = sum(rho)
        new_rho = []
        #return [float(rho[r])/sum_rho * weight for r in range(len(rho))]
        for r in range(len(rho)):
            new_rho.append(rho[r]/sum_rho) # * weight)
        return new_rho
コード例 #2
0
 def multivariate_normal(self, covar_matrix, x_less, alpha=1, mus=[]):
     """
     :param d: number of rows in X
     :param covar_matrix:
     :param x_less: X - u , u is a vector of mu
     :return:
     """
     return multivariate_normal(covar_matrix, x_less, alpha=1)