Skip to content

Commit

Permalink
Implemented SDLE alogrism.
Browse files Browse the repository at this point in the history
  • Loading branch information
monochromegane committed Feb 25, 2018
1 parent 27f61b6 commit 23b6678
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
5 changes: 4 additions & 1 deletion algorism.go
Expand Up @@ -16,7 +16,10 @@ func (c NoOpContinuousAlgorism) input([]float64, bool) float64 {
}

func newCategoricalAlgorism(discount, beta float64, cellNum int) CategoricalAlgorism {
return NoOpCategoricalAlgorism{}
if cellNum == 0 {
return NoOpCategoricalAlgorism{}
}
return newSDLE(discount, beta, cellNum)
}

type CategoricalAlgorism interface {
Expand Down
17 changes: 17 additions & 0 deletions categorizer.go
@@ -0,0 +1,17 @@
package smartsifter

type Categorizer interface {
Index(x []int) int
Size(idx int) int
}

type noOpCategorizer struct {
}

func (c noOpCategorizer) Index(x []int) int {
return int(x[0])
}

func (c noOpCategorizer) Size(idx int) int {
return 1
}
54 changes: 54 additions & 0 deletions sdle.go
@@ -0,0 +1,54 @@
package smartsifter

import "math"

type SDLE struct {
t int
r float64
beta float64
cellNum int
occurrences []float64
probabilities []float64
categorizer Categorizer
}

func newSDLE(discount, beta float64, cellNum int) *SDLE {
sdle := &SDLE{
r: discount,
beta: beta,
cellNum: cellNum,
categorizer: noOpCategorizer{},
}
sdle.initialize()
return sdle
}

func (sdle *SDLE) initialize() {
sdle.t = 1
sdle.occurrences = make([]float64, sdle.cellNum)
sdle.probabilities = make([]float64, sdle.cellNum)
}

func (sdle *SDLE) input(x []int, update bool) float64 {
idx := sdle.categorizer.Index(x)
q := sdle.probabilities[idx]
if update {
sdle.update(idx)
}
return q / float64(sdle.categorizer.Size(idx))
}

func (sdle *SDLE) update(idx int) {
for i := 0; i < sdle.cellNum; i++ {
delta := 0.0
if i == idx {
delta = 1.0
}
t := (1-sdle.r)*sdle.occurrences[i] + delta
q := (t + sdle.beta) * sdle.r / ((1 - math.Pow((1-sdle.r), float64(sdle.t))) + sdle.r*float64(sdle.cellNum)*sdle.beta)

sdle.occurrences[i] = t
sdle.probabilities[i] = q
}
sdle.t += 1
}

0 comments on commit 23b6678

Please sign in to comment.