func (i *ITM) Threshold(cutoff float64) { for _, d := range rand.Perm(i.M) { for n := 0; n < i.N[d]; n++ { i.unsetZ(d, n) counts := i.conditional(d, n) util.Normalize(counts) bestZ := -1 bestP := math.Inf(-1) useBest := true for z, count := range counts { if count > bestP { bestZ = z bestP = count } if count < cutoff { counts[z] = 0 } else { useBest = false } } if useBest { i.setZ(d, n, bestZ) } else { i.setZ(d, n, util.SampleCounts(counts)) } } } }
func (i *ITM) Gibbs() { for _, d := range rand.Perm(i.M) { for n := 0; n < i.N[d]; n++ { i.unsetZ(d, n) z := util.SampleCounts(i.conditional(d, n)) i.setZ(d, n, z) } } }
func (l *LDA) Gibbs() { for _, d := range rand.Perm(l.M) { for n := 0; n < l.N[d]; n++ { l.unsetZ(d, n) z := util.SampleCounts(l.conditional(d, n)) l.setZ(d, n, z) } } }
func (i *ITM) AnnealedGibbs(temp float64) { tempInv := 1 / temp for _, d := range rand.Perm(i.M) { for n := 0; n < i.N[d]; n++ { i.unsetZ(d, n) counts := i.conditional(d, n) for z := 0; z < i.T; z++ { counts[z] = math.Pow(counts[z], tempInv) } z := util.SampleCounts(counts) i.setZ(d, n, z) } } }
func (i *ITM) Coinflip(pMax float64) { for _, d := range rand.Perm(i.M) { for n := 0; n < i.N[d]; n++ { i.unsetZ(d, n) var z int if rand.Float64() < pMax { z = util.Argmax(i.conditional(d, n)) } else { z = util.SampleCounts(i.conditional(d, n)) } i.setZ(d, n, z) } } }
func (i *ITM) SampleTopN(n int) { for _, d := range rand.Perm(i.M) { for w := 0; w < i.N[d]; w++ { i.unsetZ(d, w) allCounts := i.conditional(d, w) topCounts := make([]float64, i.T) for _, z := range util.TopN(util.FloatCounts(allCounts), n) { topCounts[z] = allCounts[z] } z := util.SampleCounts(topCounts) i.setZ(d, w, z) } } }