mcmc

package
v0.2.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 27, 2020 License: MIT Imports: 11 Imported by: 1

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

Examples

Constants

View Source
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

func NewAlgo

func NewAlgo(conf Conf, space core.Space, data []core.Elemt, initializer core.Initializer, distrib Distrib) *core.Algo

NewAlgo creates a new kmeans algo

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

func (*Conf) Verify

func (conf *Conf) Verify() (err error)

Verify configuration parameters

type Dirac

type Dirac struct {
}

Dirac represents a Distrib that always returns the conditional.

func NewDirac

func NewDirac() Dirac

NewDirac creates a new Dirac instance.

func (Dirac) Pdf

func (Dirac) Pdf(x, mu core.Elemt, time int) float64

Pdf returns 1 if mu == x

func (Dirac) Sample

func (Dirac) Sample(mu core.Elemt, time int) core.Elemt

Sample always returns mu

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

type DistribBuilder func(elemt core.Elemt) Distrib

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

func NewSeqImpl(conf Conf, initializer core.Initializer, data []core.Elemt, distrib Distrib) Impl

NewSeqImpl returns a sequantial mcmc implementation

func (*Impl) Copy

func (impl *Impl) Copy(model core.OCModel) (core.Impl, error)

Copy impl

func (*Impl) Init

func (impl *Impl) Init(model core.OCModel) (centroids core.Clust, err error)

Init initializes the algorithm

func (*Impl) Iterate

func (impl *Impl) Iterate(model core.OCModel) (clust core.Clust, runtimeFigures core.RuntimeFigures, err error)

Iterate executes the algorithm

func (*Impl) Push

func (impl *Impl) Push(elemt core.Elemt, model core.OCModel) error

Push input element in the buffer

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

func (*LateDistrib) Pdf

func (d *LateDistrib) Pdf(x, mu core.Elemt, time int) float64

Pdf initializes the wrapped Distrib if necessary then forward to its Pdf method

func (*LateDistrib) Sample

func (d *LateDistrib) Sample(mu core.Elemt, time int) core.Elemt

Sample initializes the wrapped Distrib if necessary then forward to its Sample method

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

func (MultivT) Pdf

func (m MultivT) Pdf(mu, x core.Elemt, time int) float64

Pdf Density of a (uncorrelated) multivariate t distribution

func (MultivT) Sample

func (m MultivT) Sample(mu core.Elemt, time int) core.Elemt

Sample from a (uncorrelated) multivariate t distribution

type MultivTConf

type MultivTConf struct {
	Dim  int
	Nu   float64
	RGen *rand.Rand
}

MultivTConf Configuration for multivariateT distribution

type ParStrategy

type ParStrategy struct {
	Degree int
}

ParStrategy defines a parallelized strategy

func (*ParStrategy) Iterate

func (strategy *ParStrategy) Iterate(conf Conf, space core.Space, centroids core.Clust, data []core.Elemt, iter int) (result core.Clust)

Iterate is the iterative execution

func (*ParStrategy) Loss

func (strategy *ParStrategy) Loss(conf Conf, space core.Space, centroids core.Clust, data []core.Elemt) float64

Loss calculates loss for the given proposal and data in parallel

type SeqStrategy

type SeqStrategy struct {
}

SeqStrategy strategy structure

func (*SeqStrategy) Iterate

func (strategy *SeqStrategy) Iterate(conf Conf, space core.Space, centroids core.Clust, data []core.Elemt, iter int) (result core.Clust)

Iterate execute the algorithm

func (*SeqStrategy) Loss

func (strategy *SeqStrategy) Loss(conf Conf, space core.Space, proposal core.Clust, data []core.Elemt) float64

Loss calculates loss for the given proposal and data

type Strategy

type Strategy interface {
	Iterate(Conf, core.Space, core.Clust, []core.Elemt, int) core.Clust
	Loss(Conf, core.Space, core.Clust, []core.Elemt) float64
}

Strategy specifies strategy methods

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL