Documentation ¶
Overview ¶
Package mcmc provides MCMC based implementation of online clustering (cf https://hal.inria.fr/hal-01264233).
Example ¶
package main import ( "fmt" "math" "github.com/wearelumenai/distclus/core" "github.com/wearelumenai/distclus/euclid" "github.com/wearelumenai/distclus/kmeans" "github.com/wearelumenai/distclus/mcmc" "golang.org/x/exp/rand" ) var conf = mcmc.Conf{ InitK: 1, Amp: .5, B: 1, CtrlConf: core.CtrlConf{Iter: 20}, } var tConf = mcmc.MultivTConf{ Dim: 2, Nu: 3, } func main() { var centers, observations = Sample(1000) var train, test = observations[:800], observations[800:] var algo, space = Build(conf, tConf) defer algo.Stop() var errRun = RunAndFeed(algo, train) if errRun == nil { var result, rmse, errEval = Eval(algo, centers, test, space) fmt.Printf("%v %v %v\n", len(result) < 4, rmse < 1, errEval) } } func Build(conf mcmc.Conf, tConf mcmc.MultivTConf) (algo *core.Algo, space core.Space) { space = euclid.NewSpace() var distrib = mcmc.NewMultivT(tConf) // the alteration distribution algo = mcmc.NewAlgo(conf, space, nil, kmeans.PPInitializer, distrib) return } func RunAndFeed(algo *core.Algo, observations []core.Elemt) (err error) { for i := 0; i < len(observations) && err == nil; i++ { err = algo.Push(observations[i]) } err = algo.Batch() return } func Eval(algo *core.Algo, centers core.Clust, observations []core.Elemt, space core.Space) (result core.Clust, rmse float64, err error) { var output = getOutput(centers, observations, space) rmse = RMSE(algo, observations, output, space) result = algo.Centroids() return } func getOutput(centers core.Clust, observations []core.Elemt, space core.Space) (output []core.Elemt) { var labels, _ = centers.MapLabel(observations, space) output = make([]core.Elemt, len(labels)) for i := range labels { output[i] = centers[labels[i]] } return } func RMSE(algo *core.Algo, observations []core.Elemt, output []core.Elemt, space core.Space) float64 { var mse = 0. for i := range observations { var _, _, dist = algo.Predict(observations[i]) mse += dist * dist / float64(len(observations)) } return math.Sqrt(mse) } func Sample(n int) (centers core.Clust, observations []core.Elemt) { centers = core.Clust( []core.Elemt{ []float64{1.4, 0.7}, []float64{7.6, 7.6}, }) observations = make([]core.Elemt, n) for i := range observations { var obs = make([]float64, 2) if rand.Intn(2) == 1 { copy(obs, centers[0].([]float64)) } else { copy(obs, centers[1].([]float64)) } for j := range obs { obs[j] += rand.Float64() - 1 } observations[i] = obs } return }
Output: true true <nil>
Index ¶
- Constants
- func NewAlgo(conf Conf, space core.Space, data []core.Elemt, initializer core.Initializer, ...) *core.Algo
- type CenterStore
- type Conf
- type Dirac
- type Distrib
- type DistribBuilder
- type Impl
- func (impl *Impl) Copy(model core.OCModel) (core.Impl, error)
- func (impl *Impl) Init(model core.OCModel) (centroids core.Clust, err error)
- func (impl *Impl) Iterate(model core.OCModel) (clust core.Clust, runtimeFigures core.RuntimeFigures, err error)
- func (impl *Impl) Push(elemt core.Elemt, model core.OCModel) error
- type LateDistrib
- type MultivT
- type MultivTConf
- type ParStrategy
- type SeqStrategy
- type Strategy
Examples ¶
Constants ¶
const ( // Acceptations is the number of acceptations of mcmc Acceptations = "acceptations" // Lambda is lambda of mcmc Lambda = "lambda" // Rho is rho of mcmc Rho = "rho" // RGibbs is rGibbs of mcmc RGibbs = "rGibbs" // Time is time of mcmc Time = "time" )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type CenterStore ¶
type CenterStore struct {
// contains filtered or unexported fields
}
CenterStore structure
func NewCenterStore ¶
func NewCenterStore(rgen *rand.Rand) CenterStore
NewCenterStore returns a new center store
func (*CenterStore) GetCenters ¶
func (store *CenterStore) GetCenters(data []core.Elemt, space core.Space, k int, clust core.Clust) (core.Clust, error)
GetCenters returns input centroids centers
func (*CenterStore) SetCenters ¶
func (store *CenterStore) SetCenters(clust core.Clust)
SetCenters set centers to input centroids
type Conf ¶
type Conf struct { core.CtrlConf Par bool InitK int // number of initial number of clusters RGen *rand.Rand B, Amp, R float64 Norm float64 MaxK int ProbaK []float64 FrameSize int NumCPU int // maximal number of CPU to use // contains filtered or unexported fields }
Conf is the mcmc configuration object
func (*Conf) SetDefaultValues ¶ added in v0.2.0
func (conf *Conf) SetDefaultValues()
SetDefaultValues initializes nil parameter values
type Distrib ¶
type Distrib interface { Sample(mu core.Elemt, time int) core.Elemt Pdf(x, mu core.Elemt, time int) float64 }
Distrib defines distribution methods
type DistribBuilder ¶
DistribBuilder represents functor that build a Distrib from data
type Impl ¶
type Impl struct {
// contains filtered or unexported fields
}
Impl of MCMC
func NewParImpl ¶
func NewParImpl(conf Conf, initializer core.Initializer, data []core.Elemt, distrib Distrib) (impl Impl)
NewParImpl returns a new parallelized algorithm implementation
func NewSeqImpl ¶
NewSeqImpl returns a sequantial mcmc implementation
type LateDistrib ¶
type LateDistrib struct {
// contains filtered or unexported fields
}
LateDistrib initializes a wrapped Distrib when the first data is known
func NewLateDistrib ¶
func NewLateDistrib(initializer DistribBuilder) *LateDistrib
NewLateDistrib creates a new LateDistrib instance
type MultivT ¶
type MultivT struct { MultivTConf // contains filtered or unexported fields }
MultivT Vectors(float64[]) distribution wrapping StudentsT of Gonum
func NewMultivT ¶
func NewMultivT(conf MultivTConf) MultivT
NewMultivT Constructor for multivT distribution
type MultivTConf ¶
MultivTConf Configuration for multivariateT distribution
type ParStrategy ¶
type ParStrategy struct {
Degree int
}
ParStrategy defines a parallelized strategy