model

package
v0.0.0-...-0953495 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2015 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultSeed provided for model implementation.
	DefaultSeed = 33
)

Variables

View Source
var NoWeight = func(o Obs) float64 { return 1.0 }

NoWeight is a parameter for the Update() method. Applies a weight of one to the observations.

View Source
var Weight = func(w float64) func(o Obs) float64 {
	return func(o Obs) float64 {
		return w
	}
}

Weight is a parameter for the Update() method. Applies a weight to the observations.

Functions

func ObsToF64

func ObsToF64(o Obs) ([]float64, string, string)

ObsToF64 converts an Obs to a tuple: []float64, label, id.

func RandIntFromDist

func RandIntFromDist(dist []float64, r *rand.Rand) int

RandIntFromDist randomly selects an item using a discrete PDF. TODO: This is not optimal but should work for testing.

func RandIntFromLogDist

func RandIntFromLogDist(dist []float64, r *rand.Rand) int

RandIntFromLogDist random selects an item using a discrete PDF. Slice dist contains log probabilities.

func RandNormalVector

func RandNormalVector(r *rand.Rand, mean, std []float64) []float64

RandNormalVector returns a random observation.

Types

type ANode

type ANode struct {
	// Start index (inclusive)
	Start int `json:"s"`
	// End index (exclusive)
	End int `json:"e"`
	// Name of unit being aligned.
	Name string `json:"n"`
	// Value is an arbitrary object associated to an interval.
	Value interface{} `json:"v,omitempty"`
	// Pointers to child alignments one level down.
	Children []*ANode `json:"-"`
}

ANode is an alignment node. Assumptions:

  • Root node (no parent) covers the full interval.
  • A child node interval is included in the parent interval.
  • Concatenation of children intervals must match exactly the parent interval.
  • Tree must be balanced.

func AlignLabels

func AlignLabels(labels []string) []*ANode

AlignLabels converts a slice of strings to an Alignment object. Consecutive elements with the same label are merged into an ANode.

func NewANode

func NewANode(start, end int, name string, value interface{}) *ANode

NewANode creates a new ANode.

func (*ANode) Alignment

func (a *ANode) Alignment() Alignment

Alignment returns alignments by level. The first slice index corresponds to a level from 0 to max depth. The second index is the nth contiguous ANode at that level. The ANodes are NOT copied. Make explicit copies if you need an independent set of ANodes.

func (*ANode) AppendChild

func (a *ANode) AppendChild(end int, name string) *ANode

AppendChild creates a new ANode and appends to the receiver node. The start index of the new node equals the end index of the last child. Will panic if end < start OR end > parent's end.

func (*ANode) Copy

func (a *ANode) Copy() *ANode

Copy copies an ANode. Children and Value fields are not copied.

func (*ANode) IsValid

func (a *ANode) IsValid() bool

IsValid returns true if the alignment subtree is valid.

func (*ANode) Level

func (a *ANode) Level() int

Level retrurns the level of the ANode. Level zero corresponds to the leaves.

func (*ANode) String

func (a *ANode) String() string

String prints an ANode.

func (*ANode) ToJSON

func (a *ANode) ToJSON() (string, error)

ToJSON returns a json string.

type Aligner

type Aligner interface {

	// Alignment info for all available levels.
	Alignment() []*ANode
}

The Aligner interface provides access to alignment information at various levels.

type Alignment

type Alignment [][]*ANode

Alignment represents alignments as a sequence of ANodes by level.

func (Alignment) Level

func (a Alignment) Level(level int) []*ANode

Level returns the list of intervals for a specific level.

func (Alignment) String

func (a Alignment) String() string

String prints alignments.

func (Alignment) ToJSON

func (a Alignment) ToJSON() (string, error)

ToJSON returns a json string.

func (Alignment) Tree

func (a Alignment) Tree() *ANode

Tree converts an Alignment object to an alignment tree. ANodes are NOT copied from the alignment data, instead the Children fields are set to point to the children nodes.

type FloatObs

type FloatObs struct {
	// contains filtered or unexported fields
}

FloatObs implements the Obs interface. Values are slices of type float64.

func (FloatObs) ID

func (fo FloatObs) ID() string

ID returns the observation id.

func (FloatObs) Label

func (fo FloatObs) Label() Labeler

Label returns the label for the observation.

func (FloatObs) Value

func (fo FloatObs) Value() interface{}

Value method returns the observed value.

type FloatObsSequence

type FloatObsSequence struct {
	// contains filtered or unexported fields
}

FloatObsSequence implements the Obs interface using a slice of float64 slices.

func (*FloatObsSequence) Add

func (fo *FloatObsSequence) Add(obs FloatObs, lab string)

Add adds a FloatObs to the sequence.

func (FloatObsSequence) Alignment

func (fo FloatObsSequence) Alignment() []*ANode

Alignment returns the alignment object.

func (FloatObsSequence) ID

func (fo FloatObsSequence) ID() string

ID returns the observation id.

func (FloatObsSequence) Label

func (fo FloatObsSequence) Label() Labeler

Label returns the label for the observation.

func (*FloatObsSequence) SetAlignment

func (fo *FloatObsSequence) SetAlignment(a []*ANode)

SetAlignment sets the alignment object.

func (FloatObsSequence) Value

func (fo FloatObsSequence) Value() interface{}

Value method returns the observed value.

func (FloatObsSequence) ValueAsSlice

func (fo FloatObsSequence) ValueAsSlice() []interface{}

ValueAsSlice returns the observed value as a slice of interfaces.

type FloatObserver

type FloatObserver struct {
	Values [][]float64
	Labels []SimpleLabel
	// contains filtered or unexported fields
}

FloatObserver implements an observer to stream FloatObs objects. Not safe to use with multiple goroutines.

func NewFloatObserver

func NewFloatObserver(v [][]float64, lab []SimpleLabel) (*FloatObserver, error)

NewFloatObserver creates a new FloatObserver.

func (FloatObserver) ObsChan

func (fo FloatObserver) ObsChan() (<-chan Obs, error)

ObsChan implements the ObsChan method for the observer interface.

type IntObs

type IntObs struct {
	// contains filtered or unexported fields
}

IntObs implements Obs for integer values.

func (IntObs) ID

func (io IntObs) ID() string

ID returns the observation id.

func (IntObs) Label

func (io IntObs) Label() Labeler

Label returns the label for the observation.

func (IntObs) Value

func (io IntObs) Value() interface{}

Value method returns the observed value.

type Labeler

type Labeler interface {

	// Human-readable name.
	String() string

	// Compare labels.
	IsEqual(label Labeler) bool
}

The Labeler interface manages data labels.

type Modeler

type Modeler interface {

	// The model name.
	Name() string

	// Dimensionality of the observation vector.
	Dim() int

	Trainer
	Predictor
	Scorer
	Sampler
}

A Modeler type is a complete implementation of a statistical model in gjoa.

type Obs

type Obs interface {

	// The observation's id.
	ID() string
	// The observation's value.
	Value() interface{}
	// The observation's label.
	Label() Labeler
}

Obs is a generic interface to handle observed data. Each observation may have a value and a label.

func F64ToObs

func F64ToObs(v []float64, label string) Obs

F64ToObs converts a []float64 to Obs.

func JoinFloatObsSequence

func JoinFloatObsSequence(id string, inputs ...*FloatObsSequence) Obs

JoinFloatObsSequence joins various FloatObsSequence objects into a new sequence. id is the new id of the joined sequence.

func NewFloatObs

func NewFloatObs(val []float64, lab SimpleLabel) Obs

NewFloatObs creates new FloatObs objects.

func NewFloatObsSequence

func NewFloatObsSequence(val [][]float64, lab SimpleLabel, id string) Obs

NewFloatObsSequence creates new FloatObsSequence objects.

func NewIntObs

func NewIntObs(val int, lab SimpleLabel, id string) Obs

NewIntObs creates new IntObs objects.

type Observer

type Observer interface {

	// Returns channel of observations.
	// The sequence ends when the channel closes.
	ObsChan() (<-chan Obs, error)
}

The Observer provides streams of observations.

type Predictor

type Predictor interface {
	Predict(x Observer) ([]Labeler, error)
}

Predictor returns a label with a hypothesis given the observations.

type Sampler

type Sampler interface {
	// Returns a sample drawn from the underlying distribution.
	Sample(*rand.Rand) Obs

	// Returns a sample of size "size" drawn from the underlying distribution.
	// The sequence ends when the channel closes.
	SampleChan(r *rand.Rand, size int) <-chan Obs
}

The Sampler type generates random data using the model.

type Scorer

type Scorer interface {
	LogProb(x Obs) float64
}

Scorer computes log probabilities.

type Seq

type Seq struct {
	Vectors    [][]float64 `json:"vectors"`
	Labels     []string    `json:"labels"`
	ID         string      `json:"id"`
	Alignments []*ANode    `json:"alignments,omitempty"`
}

Seq is a data format to represent a sequence of observation vectors. We use it to read json data.

type SeqObserver

type SeqObserver struct {
	// contains filtered or unexported fields
}

SeqObserver implements an observer whose undelying values are of type FloatObsSequence.

func NewSeqObserver

func NewSeqObserver(reader io.Reader) (*SeqObserver, error)

NewSeqObserver creates a new SeqObserver. The data is read as a stream of JSON objects accessed from an io.Reader. Each JSON object must be separated by a newline.

Example to create an SeqObserver from a file (error handling ignored for brevity). The data must be a stream of JSON-encoded Seq values.

 r, _ = os.Open(fn)              // Open file.
 obs, _ = NewSeqObserver(r)      // Create observer that reads from file.
 c, _ = obs.ObsChan()            // Get channel. (See model.Observer interface.)
                                 // Obs type is model.FloatObsSequence.
_ = obs.Close()                  // Closes the underlying file reader.

func (*SeqObserver) Close

func (so *SeqObserver) Close() error

Close underlying reader if reader implements the io.Closer interface.

func (*SeqObserver) ObsChan

func (so *SeqObserver) ObsChan() (<-chan Obs, error)

ObsChan implements the ObsChan method for the observer interface. Each observation is a sequence of type model.FloatObsSequence.

type SimpleLabel

type SimpleLabel string

SimpleLabel implements a basic Labeler interface.

func (SimpleLabel) IsEqual

func (lab SimpleLabel) IsEqual(lab2 Labeler) bool

IsEqual compares two labels.

func (SimpleLabel) String

func (lab SimpleLabel) String() string

String returns the label as a string. Multiple labels must be separated using a comma.

type Trainer

type Trainer interface {

	// Updates model using weighted samples: x[i] * w(x[i]).
	Update(x Observer, w func(Obs) float64) error

	// Updates model using a single weighted sample.
	UpdateOne(o Obs, w float64)

	// Estimates model parameters.
	Estimate() error

	// Clears all model parameters.
	Clear()
}

A Trainer type can do statictical learning.

Directories

Path Synopsis
Package hmm provides an implementation of hidden Markov models.
Package hmm provides an implementation of hidden Markov models.

Jump to

Keyboard shortcuts

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