Documentation
¶
Index ¶
- Constants
- Variables
- func ObsToF64(o Obs) ([]float64, string, string)
- func RandIntFromDist(dist []float64, r *rand.Rand) int
- func RandIntFromLogDist(dist []float64, r *rand.Rand) int
- func RandNormalVector(r *rand.Rand, mean, std []float64) []float64
- type ANode
- type Aligner
- type Alignment
- type FloatObs
- type FloatObsSequence
- func (fo *FloatObsSequence) Add(obs FloatObs, lab string)
- func (fo FloatObsSequence) Alignment() []*ANode
- func (fo FloatObsSequence) ID() string
- func (fo FloatObsSequence) Label() Labeler
- func (fo *FloatObsSequence) SetAlignment(a []*ANode)
- func (fo FloatObsSequence) Value() interface{}
- func (fo FloatObsSequence) ValueAsSlice() []interface{}
- type FloatObserver
- type IntObs
- type Labeler
- type Modeler
- type Obs
- type Observer
- type Predictor
- type Sampler
- type Scorer
- type Seq
- type SeqObserver
- type SimpleLabel
- type Trainer
Constants ¶
const (
// DefaultSeed provided for model implementation.
DefaultSeed = 33
)
Variables ¶
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.
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 RandIntFromDist ¶
RandIntFromDist randomly selects an item using a discrete PDF. TODO: This is not optimal but should work for testing.
func RandIntFromLogDist ¶
RandIntFromLogDist random selects an item using a discrete PDF. Slice dist contains log probabilities.
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 ¶
AlignLabels converts a slice of strings to an Alignment object. Consecutive elements with the same label are merged into an ANode.
func (*ANode) 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 ¶
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.
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.
type FloatObs ¶
type FloatObs struct {
// contains filtered or unexported fields
}
FloatObs implements the Obs interface. Values are slices of type float64.
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) 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.
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 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.
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 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 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.