model

package
v0.0.0-...-6b453b2 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2022 License: MIT Imports: 8 Imported by: 2

Documentation

Index

Constants

View Source
const (
	BAYES  = "BAYES"
	MARKOV = "MARKOV"
)

Model type constant string - matches UAI formats

Variables

This section is empty.

Functions

func HellingerDiff

func HellingerDiff(v1 *Variable, v2 *Variable) float64

HellingerDiff returns the Hellinger error between the model's current marginal estimate and this solution. Like AbsError, the result is the average over the variables, the solution's marginals are assumed normalized (sum=1.0), while the model's marginals are assumed non-normalized (but positive)

func JSDivergence

func JSDivergence(v1 *Variable, v2 *Variable) float64

JSDivergence returns the Jensen-Shannon divergence, which is a symmetric gneralization of the KL divergence

func MaxAbsDiff

func MaxAbsDiff(v1 *Variable, v2 *Variable) float64

MaxAbsDiff returns the maximum difference found between the two prob dists

func MeanAbsDiff

func MeanAbsDiff(v1 *Variable, v2 *Variable) float64

MeanAbsDiff returns the mean of the differenced found between the two prob dists

Types

type ErrorSuite

type ErrorSuite struct {
	MeanMeanAbsError float64
	MeanMaxAbsError  float64
	MeanHellinger    float64
	MeanJSDiverge    float64

	MaxMeanAbsError float64
	MaxMaxAbsError  float64
	MaxHellinger    float64
	MaxJSDiverge    float64
}

ErrorSuite represents all the loss/error functions we use to judge progress across joint dist. Errors beginning with Mean are the mean across all the variables in the joint distribution while Max is the maximum value for all the variables. So MeanMaxAbsError is the MEAN of the Maximum Absoulte Error for each of the marginal variables. Likewise, MaxMeanAbsError represents the maximum value of the mean difference between two random variables.

func NewErrorSuite

func NewErrorSuite(vars1 []*Variable, vars2 []*Variable) (*ErrorSuite, error)

NewErrorSuite returns an ErrorSuite with all calculated error functions

type FieldReader

type FieldReader struct {
	Pos    int
	Fields []string
}

FieldReader is just a simple reader for basic file formats.

func NewFieldReader

func NewFieldReader(data string) *FieldReader

NewFieldReader constructs a new field reader around the given data

func (*FieldReader) Read

func (fr *FieldReader) Read() (string, error)

Read returns the next space-delimited field/token

func (*FieldReader) ReadFloat

func (fr *FieldReader) ReadFloat() (float64, error)

ReadFloat reads the next token as a float

func (*FieldReader) ReadInt

func (fr *FieldReader) ReadInt() (int, error)

ReadInt reads the next token as an int

type Function

type Function struct {
	Name  string      // Name for function (or just a 0-based index in UAI formats)
	Vars  []*Variable // Vars in function
	Table []float64   // CPT - len is product of variables' Card
	IsLog bool        // True if values are log(v) - default is false
}

A Function represents a function of Variables (which may be a CPT or a more general factor). In a Markov network, this is defined on a clique. Note that factors in a Markox network are assumed to return NON-normalized probabilities. You need Z (the partition function) to normalize to "real" probabilities.

The actual ordering of the Table values matches the order of the variables, where the variables are ordered from "most" to "least" significant. (This is the same order used in UAI data files). As a example, let's assume 3 boolean variables in the order [A, B, C]. Let's further assume that this join probability distribution is completely uniform: The CPT would look like:

ABC  P(A,B,C)
---  --------
000   0.125
001   0.125
010   0.125
011   0.125
100   0.125
101   0.125
110   0.125
111   0.125

And our Table array would be in the same order. Since we assume that a variable's domain is [0, C-1] where C is cardinality (e.g. a boolean var has C=2 with values {0,1}), we can map directory from an ordered list of values (in the same order as the variables) to an index in the table (see Eval).

func NewFunction

func NewFunction(index int, vars []*Variable) (*Function, error)

NewFunction creates a function from an index and a list of variables

func (*Function) AddValue

func (f *Function) AddValue(values []int, inc float64) error

AddValue allows value adding based on the current input setting in values. This is used for building new functions (i.e see collapsed-gibbs)

func (*Function) Check

func (f *Function) Check() error

Check returns an error if any problem is found

func (*Function) Clone

func (f *Function) Clone() *Function

Clone returns a deep copy of the Function

func (*Function) Eval

func (f *Function) Eval(values []int) (float64, error)

Eval returns the result of the function, assuming that the values is in the same order as f.Vars.

func (*Function) UseLogSpace

func (f *Function) UseLogSpace() error

UseLogSpace converts the current factor to Log (base-e) space IFF it has not already been done

type Model

type Model struct {
	Type  string      // PGM type - should match a constant
	Name  string      // Model name
	Vars  []*Variable // Variables (nodes) in the model
	Funcs []*Function `json:"-"` // Function of variables (CPT) in the model
}

Model represent a PGM

func NewModelFromBuffer

func NewModelFromBuffer(r Reader, data []byte) (*Model, error)

NewModelFromBuffer creates a model from the given pre-read data

func NewModelFromFile

func NewModelFromFile(r Reader, filename string, useEvidence bool) (*Model, error)

NewModelFromFile initializes and creates a model from the specified source.

func (*Model) ApplyEvidenceFromFile

func (m *Model) ApplyEvidenceFromFile(r Reader, eviFilename string) error

ApplyEvidenceFromFile will read, parse, and apply the evidence

func (*Model) Check

func (m *Model) Check() error

Check returns an error if there is a problem with the model

func (*Model) Clone

func (m *Model) Clone() *Model

Clone returns a copy of the current model. Note that marginal state will be copied as well.

type Reader

type Reader interface {
	ReadModel(data []byte) (*Model, error)
	ApplyEvidence(data []byte, m *Model) error
}

Reader implementors instantiate a model from a byte stream and optionally applies evidence from a second byte stream.

type SolReader

type SolReader interface {
	ReadMargSolution(data []byte) (*Solution, error)
}

SolReader implementors read a solution (currently we only support marginal solutions)

type Solution

type Solution struct {
	Vars []*Variable // Variables with their marginals
}

Solution to a marginal estimation problem specified on a Model. It also provides evaluation metrics to evaluate vs the solution.

func NewSolutionFromBuffer

func NewSolutionFromBuffer(r SolReader, data []byte) (*Solution, error)

NewSolutionFromBuffer reads a UAI MAR solution file from the specified buffer

func NewSolutionFromFile

func NewSolutionFromFile(r SolReader, filename string) (*Solution, error)

NewSolutionFromFile reads a UAI MAR solution file

func (*Solution) Check

func (s *Solution) Check(m *Model) error

Check insures that the solution is as correct as can be checked given a model

func (*Solution) Error

func (s *Solution) Error(vars []*Variable) (*ErrorSuite, error)

Error is a helper method to return the entire error suite we offer for the current solution against the given model

type UAIReader

type UAIReader struct {
}

UAIReader reads the UAI inference data set format. This format has also been used at competitions like PIC2001 at PASCAL2. In fact, a very good description of the format is available at http://www.cs.huji.ac.il/project/PASCAL/fileFormat.php

func (UAIReader) ApplyEvidence

func (r UAIReader) ApplyEvidence(data []byte, m *Model) error

ApplyEvidence is part of the reader interface - read the evidence file and apply to the model.

func (UAIReader) ReadMargSolution

func (r UAIReader) ReadMargSolution(data []byte) (*Solution, error)

ReadMargSolution implements the model.SolReader interface

func (UAIReader) ReadModel

func (r UAIReader) ReadModel(data []byte) (*Model, error)

ReadModel implements the model.Reader interface

type Variable

type Variable struct {
	ID        int                // A numeric ID for tracking a variable
	Name      string             // Variable name (just a zero-based index in UAI formats)
	Card      int                // Cardinality - values are assume to be 0 to Card-1
	FixedVal  int                // Current fixed value (fixed by evidence): -1 is no evidence, else if 0 to Card-1
	Marginal  []float64          // Current best estimate for marginal distribution: len should equal Card
	State     map[string]float64 // State/stats a sampler can track - mainly for JSON tracking
	Collapsed bool               // For Collapsed == True, you should just sample from Marginal (default is False)
}

Variable represents a single node in a PGM, a random variable, or a marginal distribution.

func NewVariable

func NewVariable(index int, card int) (*Variable, error)

NewVariable is our standard way to create a variable from an index and a cardinality. The marginal will be set to uniform.

func (*Variable) Check

func (v *Variable) Check() error

Check returns an error if any problem is found

func (*Variable) Clone

func (v *Variable) Clone() *Variable

Clone returns a deep copy of the variable. Marginal is normalize, and the state dict is copied.

func (*Variable) CreateName

func (v *Variable) CreateName(i int) error

CreateName just gives a name to variable based on a numeric index

func (*Variable) NormMarginal

func (v *Variable) NormMarginal() error

NormMarginal insures/scales the current Marginal vector to sum to 1

type VariableIter

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

VariableIter is an iterator over all possible values for a list of variables

func NewVariableIter

func NewVariableIter(src []*Variable, honorFixed bool) (*VariableIter, error)

NewVariableIter returns a new iterator over the list of variables

func (*VariableIter) Next

func (vi *VariableIter) Next() bool

Next advances to the next value and returns True if there are still values to see

func (*VariableIter) Val

func (vi *VariableIter) Val(curr []int) error

Val populates curr with the current value

Jump to

Keyboard shortcuts

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