Example #1
0
 def _help_accumulate_sufficient_statistics(self, obs, stats, posteriors):
     posteriors = _valid_posteriors(posteriors, obs)
     for t, symbol in enumerate(obs):
         stats['post'] += posteriors[t]
         stats['post_emission'][0] += (posteriors[t] * symbol[0])
         stats['post_emission'][1] += (posteriors[t] * symbol[1])
     #stats['obs'] = np.copy(obs)
     stats['posterior'] = np.copy(posteriors)
Example #2
0
 def _help_accumulate_sufficient_statistics(self, obs, stats, posteriors):
     posteriors = _valid_posteriors(posteriors, obs)
     for t, symbol in enumerate(obs):
         stats['post'] += posteriors[t]
         stats['post_emission'][0] += (posteriors[t] * symbol[0])
         stats['post_emission'][1] += (posteriors[t] * symbol[1])
     #stats['obs'] = np.copy(obs)
     stats['posterior'] = np.copy(posteriors)
Example #3
0
    def _help_accumulate_sufficient_statistics(self, obs, stats, posteriors):
        posteriors = _valid_posteriors(posteriors, obs)
        i = 0
        print("run...! start at " + str(time()), file=sys.stderr)
        for t, symbol in enumerate(obs):
            stats['post'] += posteriors[t]
            for dim in range(self.n_features):
                for comp in range(self.distr_magnitude):
                    #lookup
                    index = (symbol[dim],
                             tuple([
                                 self.p[dim][comp][state]
                                 for state in range(self.n_components)
                             ]))
                    if index not in lookup_poisson_state:
                        tmp = np.array([
                            self._get_poisson(symbol[dim],
                                              self.p[dim][comp][state])
                            for state in range(self.n_components)
                        ])
                        lookup_poisson_state[index] = tmp
                    h = lookup_poisson_state[index]
                    enum = self.c[dim][comp] * h
                    denum = np.array([
                        self._get_value(state, symbol, dim)
                        for state in range(self.n_components)
                    ])

                    i += 1
                    try:
                        help = posteriors[t] * enum / _add_pseudo_counts(denum)
                    except:
                        print("%s \n" % i, file=sys.stderr)
                        print("%s %s %s \n" % (denum, symbol, dim),
                              file=sys.stderr)
                        print("%s \n" % (self.c), file=sys.stderr)
                        print("%s \n" % (self.p), file=sys.stderr)
                        print("%s \n" % (posteriors[t]), file=sys.stderr)
                        print("%s \n" % (enum), file=sys.stderr)
                        help = np.array([
                            1.0 / self.distr_magnitude,
                            1.0 / self.distr_magnitude,
                            1.0 / self.distr_magnitude
                        ])
                    stats['post_sum_l'][dim][comp] += help
                    stats['post_sum_l_emisson'][dim][
                        comp] += help * symbol[dim]
                    stats['post_sum_l_factor'][dim][
                        comp] += help * self.factors[comp]

                    if posteriors[t][1] > 0.5 or posteriors[t][2] > 0.5:
                        if posteriors[t][1] >= posteriors[t][2]:
                            stats['weights'][dim][state][0] += 1
                        if posteriors[t][2] > posteriors[t][1]:
                            stats['weights'][dim][state][1] += 1

        #print(self.p)
        stats['posterior'] = np.copy(posteriors)
Example #4
0
 def _accumulate_sufficient_statistics(self, stats, obs, framelogprob,
                                   posteriors, fwdlattice, bwdlattice
                                   ):
     super(BinomialHMM, self)._accumulate_sufficient_statistics(
         stats, obs, framelogprob, posteriors, fwdlattice, bwdlattice
         )
     
     posteriors = _valid_posteriors(posteriors, obs, self.dim)
     self._help_accumulate_sufficient_statistics(obs, stats, posteriors)
Example #5
0
    def _accumulate_sufficient_statistics(self, stats, obs, framelogprob,
                                          posteriors, fwdlattice, bwdlattice):
        super(BinomialHMM,
              self)._accumulate_sufficient_statistics(stats, obs, framelogprob,
                                                      posteriors, fwdlattice,
                                                      bwdlattice)

        posteriors = _valid_posteriors(posteriors, obs, self.dim)
        self._help_accumulate_sufficient_statistics(obs, stats, posteriors)
 def _help_accumulate_sufficient_statistics(self, obs, stats, posteriors):
     posteriors = _valid_posteriors(posteriors, obs)
     for t, symbol in enumerate(obs):
         stats['post'] += posteriors[t]
         
         for dim in range(self.n_features):
             stats['post_emission'][dim] += (posteriors[t] * symbol[dim])
         
         for dim in range(self.n_features):
             for state in range(self.n_components):
                 for comp in range(self.distr_magnitude):
                     enum = self.c[dim][state][comp] * binom.pmf(symbol[dim], self.n[dim], self.p[dim][state][comp])
                     denum = self._get_value(state, symbol, dim, stats)
                     stats['post_sum_l'][dim][state][comp] += posteriors[t][state] * enum / denum
                     stats['post_sum_l_emisson'][dim][state][comp] += posteriors[t][state] * enum / denum * symbol[dim]
         
     stats['posterior'] = np.copy(posteriors)
Example #7
0
 def _help_accumulate_sufficient_statistics(self, obs, stats, posteriors):
     posteriors = _valid_posteriors(posteriors, obs)
     for t, symbol in enumerate(obs):
         stats['post'] += posteriors[t]
         
         for dim in range(self.n_features):
             stats['post_emission'][dim] += (posteriors[t] * symbol[dim])
         
         for dim in range(self.n_features):
             for state in range(self.n_components):
                 for comp in range(self.distr_magnitude):
                     enum = self.c[dim][state][comp] * binom.pmf(symbol[dim], self.n[dim], self.p[dim][state][comp])
                     denum = self._get_value(state, symbol, dim, stats)
                     stats['post_sum_l'][dim][state][comp] += posteriors[t][state] * enum / denum
                     stats['post_sum_l_emisson'][dim][state][comp] += posteriors[t][state] * enum / denum * symbol[dim]
         
     stats['posterior'] = np.copy(posteriors)
 def _help_accumulate_sufficient_statistics(self, obs, stats, posteriors):
     posteriors = _valid_posteriors(posteriors, obs)
     i = 0
     print("run...! start at " + str(time()), file=sys.stderr)
     for t, symbol in enumerate(obs):
         stats['post'] += posteriors[t]
         for dim in range(self.n_features):
             for comp in range(self.distr_magnitude):
                 #lookup
                 index = (symbol[dim], tuple([self.p[dim][comp][state] for state in range(self.n_components)]))
                 if index not in lookup_poisson_state: 
                     tmp = np.array([self._get_poisson(symbol[dim], self.p[dim][comp][state]) for state in range(self.n_components)])
                     lookup_poisson_state[index] = tmp
                 h = lookup_poisson_state[index]
                 enum = self.c[dim][comp] * h
                 denum = np.array([self._get_value(state, symbol, dim) for state in range(self.n_components)])
                 
                 i += 1
                 try:
                     help = posteriors[t] * enum / _add_pseudo_counts(denum)
                 except:
                     print("%s \n" %i, file=sys.stderr)
                     print("%s %s %s \n" %(denum, symbol, dim), file=sys.stderr)
                     print("%s \n" %(self.c), file=sys.stderr)
                     print("%s \n" %(self.p), file=sys.stderr)
                     print("%s \n" %(posteriors[t]), file=sys.stderr)
                     print("%s \n" %(enum), file=sys.stderr)
                     help = np.array([1.0/self.distr_magnitude, 1.0/self.distr_magnitude, 1.0/self.distr_magnitude])
                 stats['post_sum_l'][dim][comp] += help
                 stats['post_sum_l_emisson'][dim][comp] += help * symbol[dim]
                 stats['post_sum_l_factor'][dim][comp] += help * self.factors[comp]
                 
                 if posteriors[t][1] > 0.5 or posteriors[t][2] > 0.5:
                     if posteriors[t][1] >= posteriors[t][2]:
                         stats['weights'][dim][state][0] += 1
                     if posteriors[t][2] > posteriors[t][1]:
                         stats['weights'][dim][state][1] += 1
                     
     #print(self.p)        
     stats['posterior'] = np.copy(posteriors)