Back to godoc.org

Package optimize

v0.0.0-...-ff28c19
Latest Go to latest

The highest tagged major version is .

Published: Jun 13, 2020 | License: GPL2 | Module: bitbucket.org/Davydov/godon

Overview

Package optimize is a collection of optimizers and MCMC samplers.

Index

Constants

const (
	// Constrained Optimization BY Linear Approximations
	// (local, gradient-free)
	NLOPT_COBYLA = iota
	// BOBYQA optimizer (local, gradient-free)
	NLOPT_BOBYQA
	// Downhill simplex (local, gradient-free)
	NLOPT_SIMPLEX
	// LBFGS: local Broyden–Fletcher–Goldfarb–Shanno
	// (local, gradient-based)
	NLOPT_LBFGS
	// SQP: sequential quadratic programming
	// (local, gradient-based)
	NLOPT_SQP
	// DIviding RECTangles algorithm (global)
	NLOPT_DIRECT
	// Controlled Random Search (global)
	NLOPT_CRS
	// Multi-Level Single-Linkage (global)
	NLOPT_MLSL
)

NLopt optimization methods.

const (
	// RandomizeMin is the minimum value for randomization.
	RandomizeMin = -1e9
	// RandomizeMax is the maximum value for randomization.
	RandomizeMax = +1e9
)
const (
	// TINY is ftol for the downhill simplex.
	TINY = 1e-10
	// SMALL is atol for downhill simplex.
	SMALL = 1e-6
)

func DiscreteProposal

func DiscreteProposal(state int, nstates int) (newstate int)

DiscreteProposal returns function returning a random integer converted to float64.

func DiscreteUniformPrior

func DiscreteUniformPrior(nstates int) func(float64) float64

DiscreteUniformPrior returns function returning 1/nstates.

func ExponentialPrior

func ExponentialPrior(rate float64, inczero bool) func(float64) float64

ExponentialPrior returns a function returning a exponential distribution.

func GammaPrior

func GammaPrior(shape, scale float64, inczero bool) func(float64) float64

GammaPrior returns a function returning a gamma distribution.

func NormalProposal

func NormalProposal(sd float64) func(float64) float64

NormalProposal returns normal proposal function.

func ProductPrior

func ProductPrior(f, g func(float64) float64) func(float64) float64

ProductPrior returns a function which a multiplication of two functions.

func Rand

func Rand() float64

Rand returns a random value in the range [0, 1], including 1.

func ReadFloats

func ReadFloats(s string) ([]float64, error)

ReadFloats converts string of floats into slice of float64.

func UniformGlobalProposal

func UniformGlobalProposal(min, max float64) func(float64) float64

UniformGlobalProposal returns uniform proposal function given max and min.

func UniformPrior

func UniformPrior(min, max float64, incmin, incmax bool) func(float64) float64

UniformPrior returns a uniform prior function.

func UniformProposal

func UniformProposal(width float64) func(float64) float64

UniformProposal returns uniform proposal function.

type AdaptiveParameter

type AdaptiveParameter struct {
	*BasicFloatParameter

	*AdaptiveSettings
	// contains filtered or unexported fields
}

AdaptiveParameter is an adaptive parameter for adaptive MCMC.

func NewAdaptiveParameter

func NewAdaptiveParameter(par *float64, name string, ap *AdaptiveSettings) (a *AdaptiveParameter)

NewAdaptiveParameter creates a new adaptive MCMC parameter.

func (*AdaptiveParameter) Accept

func (a *AdaptiveParameter) Accept(iter int)

Accept is called if value is accepted.

func (*AdaptiveParameter) AdaptiveProposal

func (a *AdaptiveParameter) AdaptiveProposal() func(float64) float64

AdaptiveProposal proposes a new point using adaptive MCMC.

func (*AdaptiveParameter) CheckConvergenceMu

func (a *AdaptiveParameter) CheckConvergenceMu()

CheckConvergenceMu checks if Mu converged.

func (*AdaptiveParameter) RobbinsMonro

func (a *AdaptiveParameter) RobbinsMonro() (gamma float64)

RobbinsMonro implements Robbins-Monro algorithm for learning mean and variance.

func (*AdaptiveParameter) UpdateMu

func (a *AdaptiveParameter) UpdateMu()

UpdateMu updates Mu value.

type AdaptiveSettings

type AdaptiveSettings struct {
	// WSize window size to compute mean and variance.
	WSize int
	// K specifies how often Mu should be updated.
	K int
	// Skip is the number of iterations to skip before starting
	// adaptation.
	Skip int
	// MaxAdapt is the number of iterations to adapt.
	MaxAdapt int
	// MaxUpdate maximum number of update for a parameter.
	MaxUpdate int
	// Epsilon is part of stopping criteria for stopping
	// adaptation.
	Epsilon float64
	// C is a Robbins-Monro algorithm parameter
	C float64
	// Nu is a Robbins-Monro algorithm parameter
	Nu float64
	// Lambda is the proposal multiplier.
	Lambda float64
	// SD is initial standard deviation.
	SD float64
}

AdaptiveSettings are settings for an adaptive MCMC.

func NewAdaptiveSettings

func NewAdaptiveSettings() *AdaptiveSettings

NewAdaptiveSettings creates new settings for adaptive MCMC.

func (*AdaptiveSettings) ParameterGenerator

func (as *AdaptiveSettings) ParameterGenerator(par *float64, name string) FloatParameter

ParameterGenerator generates an adaptive MCMC parameter.

type BaseOptimizer

type BaseOptimizer struct {
	Optimizable

	// Quiet controls whether output should be printed.
	Quiet bool
	// contains filtered or unexported fields
}

BaseOptimizer contains basic data for an optimizer.

func (*BaseOptimizer) GetMaxL

func (o *BaseOptimizer) GetMaxL() float64

GetMaxL returns the maximum likelihood value. Can me larger than current likelihood for MCMC, simulated annealing, etc.

func (*BaseOptimizer) GetMaxLParameters

func (o *BaseOptimizer) GetMaxLParameters() (s string)

GetMaxLParameters returns parameter values for the maximum likelihood value as a string.

func (*BaseOptimizer) GetNCalls

func (o *BaseOptimizer) GetNCalls() int

GetNCalls return total number of likelihood function calls.

func (*BaseOptimizer) GetNIter

func (o *BaseOptimizer) GetNIter() int

GetNIter returns total number of iterations. Can be smaller than number of likelihood function calls for the gradient-based methods.

func (*BaseOptimizer) GetOptimizable

func (o *BaseOptimizer) GetOptimizable() Optimizable

GetOptimizable get the model.

func (*BaseOptimizer) GetParametersMap

func (o *BaseOptimizer) GetParametersMap(par []float64) (m map[string]float64)

GetParametersMap returns parameter values as a map.

func (*BaseOptimizer) LoadFromOptimizer

func (o *BaseOptimizer) LoadFromOptimizer(opt Optimizer)

LoadFromOptimizer can load a model from another optimizer.

func (*BaseOptimizer) PrintHeader

func (o *BaseOptimizer) PrintHeader()

PrintHeader prints the report header.

func (*BaseOptimizer) PrintLine

func (o *BaseOptimizer) PrintLine(par FloatParameters, l float64, repPeriod int)

PrintLine prints one line of report (iteration, parameter values, likelihood).

func (*BaseOptimizer) PrintResults

func (o *BaseOptimizer) PrintResults(quiet bool)

PrintResults prints the optimization results.

func (*BaseOptimizer) SaveCheckpoint

func (o *BaseOptimizer) SaveCheckpoint(final bool)

SaveCheckpoint saves current optimization point.

func (*BaseOptimizer) SaveStart

func (o *BaseOptimizer) SaveStart()

SaveStart saves starting point and likelihood before optimization.

func (*BaseOptimizer) SetCheckpointIO

func (o *BaseOptimizer) SetCheckpointIO(cio *checkpoint.CheckpointIO)

SetCheckpointIO sets object for checkpoint I/O.

func (*BaseOptimizer) SetOptimizable

func (o *BaseOptimizer) SetOptimizable(opt Optimizable)

SetOptimizable sets a model for the optimization.

func (*BaseOptimizer) SetReportPeriod

func (o *BaseOptimizer) SetReportPeriod(period int)

SetReportPeriod specifies how often report line should be printed.

func (*BaseOptimizer) SetTrajectoryOutput

func (o *BaseOptimizer) SetTrajectoryOutput(output io.Writer)

SetTrajectoryOutput specifies an output writer for the trajectory.

func (*BaseOptimizer) Summary

func (o *BaseOptimizer) Summary() Summary

Summary returns optimization summary.

func (*BaseOptimizer) WatchSignals

func (o *BaseOptimizer) WatchSignals(sigs ...os.Signal)

WatchSignals installs OS hooks to react to signals.

type BasicFloatParameter

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

BasicFloatParameter implementation of FloatParameter interface.

func NewBasicFloatParameter

func NewBasicFloatParameter(par *float64, name string) *BasicFloatParameter

NewBasicFloatParameter creates a new BasicFloatParameter.

func (*BasicFloatParameter) Accept

func (p *BasicFloatParameter) Accept(iter int)

Accept accepts the proposal.

func (*BasicFloatParameter) Get

func (p *BasicFloatParameter) Get() float64

Get returns the parameter value.

func (*BasicFloatParameter) GetMax

func (p *BasicFloatParameter) GetMax() float64

GetMax returns a maximum value.

func (*BasicFloatParameter) GetMin

func (p *BasicFloatParameter) GetMin() float64

GetMin returns a minimal value.

func (*BasicFloatParameter) InRange

func (p *BasicFloatParameter) InRange() bool

InRange returns true if parameter value is between min and max.

func (*BasicFloatParameter) Name

func (p *BasicFloatParameter) Name() string

Name returns the parameter name.

func (*BasicFloatParameter) OldPrior

func (p *BasicFloatParameter) OldPrior() float64

OldPrior returns prior value for the old x.

func (*BasicFloatParameter) Prior

func (p *BasicFloatParameter) Prior() float64

Prior returns prior value for the current x.

func (*BasicFloatParameter) Propose

func (p *BasicFloatParameter) Propose()

Propose set x to a new proposed value

func (*BasicFloatParameter) Randomize

func (p *BasicFloatParameter) Randomize()

Randomize initializes parameter with a random value. The random value is uniformly distributed in the range [max(RMIN, min);min(RMAX, max)].

func (*BasicFloatParameter) Reject

func (p *BasicFloatParameter) Reject()

Reject rejects the proposal (and set x to an old value).

func (*BasicFloatParameter) Set

func (p *BasicFloatParameter) Set(v float64)

Set sets the parameter.

func (*BasicFloatParameter) SetMax

func (p *BasicFloatParameter) SetMax(max float64)

SetMax sets a maximal value.

func (*BasicFloatParameter) SetMin

func (p *BasicFloatParameter) SetMin(min float64)

SetMin sets a minimal value.

func (*BasicFloatParameter) SetOnChange

func (p *BasicFloatParameter) SetOnChange(f func())

SetOnChange sets a callback which should be called when the value is changed.

func (*BasicFloatParameter) SetPriorFunc

func (p *BasicFloatParameter) SetPriorFunc(f func(float64) float64)

SetPriorFunc sets a prior for the parameter.

func (*BasicFloatParameter) SetProposalFunc

func (p *BasicFloatParameter) SetProposalFunc(f func(float64) float64)

SetProposalFunc sets a proposal for the parameter.

func (*BasicFloatParameter) String

func (p *BasicFloatParameter) String() string

String returns a string representation of a value.

func (*BasicFloatParameter) ValueInRange

func (p *BasicFloatParameter) ValueInRange(v float64) bool

ValueInRange returns true if value is between min and max.

type DS

type DS struct {
	BaseOptimizer
	// contains filtered or unexported fields
}

DS is a downhill simplex optimizer.

func NewDS

func NewDS() (ds *DS)

NewDS creates a new DH optimizer.

func (*DS) Run

func (ds *DS) Run(iterations int)

Run starts the optimization.

type DiscreteParameter

type DiscreteParameter struct {
	*BasicFloatParameter
	// NStates number of discrete states of the parameter.
	NStates int
}

DiscreteParameter is a discrete parameter. It is internally based on a BasicFloatParameter. The state is stored as an integer number stored converted to float (e.g. 3.0 instead of 3).

func NewDiscreteParameter

func NewDiscreteParameter(par *float64, name string, nstates int) (dpar *DiscreteParameter)

NewDiscreteParameter creates a new DiscreteParameter.

func (*DiscreteParameter) Propose

func (p *DiscreteParameter) Propose()

Propose set x to a new proposed value

func (*DiscreteParameter) String

func (p *DiscreteParameter) String() string

String returns a string representation of a value.

type FloatParameter

type FloatParameter interface {
	// Name returns the parameter name.
	Name() string
	// Prior returns prior value for the current x.
	Prior() float64
	// OldPrior returns prior value for the old x.
	OldPrior() float64
	// Propose set x to a new proposed value
	Propose()
	// Accept accepts the proposal.
	Accept(int)
	// Reject rejects the proposal (and set x to an old value).
	Reject()
	// String returns a string representation of a value.
	String() string
	// SetMin sets a minimal value.
	SetMin(float64)
	// SetMax sets a maximal value.
	SetMax(float64)
	// GetMin returns a minimal value.
	GetMin() float64
	// GetMax returns a maximal value.
	GetMax() float64
	// SetOnChange sets a callback which should be called when the
	// value is changed.
	SetOnChange(func())
	// SetProposalFunc sets a proposal for the parameter.
	SetProposalFunc(func(float64) float64)
	// SetPriorFunc sets a prior for the parameter.
	SetPriorFunc(func(float64) float64)
	// Get returns the parameter value.
	Get() float64
	// Set sets the parameter.
	Set(float64)
	// Randomize initializes a parameter with a random value.
	// The random value is uniformly distributed in the range
	// [max(RadnomizeMin, min);min(RandomizeMax, max)].
	Randomize()
	// InRange returns true if parameter value is between min and
	// max.
	InRange() bool
	// ValueInRange returns true if value is between min and max.
	ValueInRange(float64) bool
}

FloatParameter is a floating point parameter.

func BasicFloatParameterGenerator

func BasicFloatParameterGenerator(par *float64, name string) FloatParameter

BasicFloatParameterGenerator is a function which returns a FloatParameter which is a BasicFloatParameter.

type FloatParameterGenerator

type FloatParameterGenerator func(*float64, string) FloatParameter

FloatParameterGenerator is a function which returns a FloatParameter.

type FloatParameters

type FloatParameters []FloatParameter

FloatParameters is an array (slice) of FloatParameters.

func (*FloatParameters) Append

func (p *FloatParameters) Append(par FloatParameter)

Append appends a new float parameter.

func (FloatParameters) GetMap

func (p FloatParameters) GetMap() map[string]float64

GetMap get parameters from FloatParameters in a map.

func (*FloatParameters) InRange

func (p *FloatParameters) InRange() bool

InRange returns true if parameter value is between min and max.

func (FloatParameters) MarshalJSON

func (p FloatParameters) MarshalJSON() ([]byte, error)

MarshalJSON creates a JSON representation of FlatParameters.

func (*FloatParameters) Names

func (p *FloatParameters) Names(is []string) (s []string)

Names returns a slice of parameter names.

func (*FloatParameters) NamesString

func (p *FloatParameters) NamesString() (s string)

NamesString returns a string with tab-separated parameter names.

func (*FloatParameters) Randomize

func (p *FloatParameters) Randomize()

Randomize sets random values to all the parameters. If no minimum and maximum are specified, RandomizeMin and RandomizeMax are used.

func (*FloatParameters) ReadFromJSON

func (p *FloatParameters) ReadFromJSON(filename string) error

ReadFromJSON sets parameter values from JSON map.

func (*FloatParameters) ReadLine

func (p *FloatParameters) ReadLine(l string) error

ReadLine sets values from a string.

func (*FloatParameters) SetByName

func (p *FloatParameters) SetByName(name string, v float64) error

SetByName sets parameter value by its' name.

func (*FloatParameters) SetFromMap

func (p *FloatParameters) SetFromMap(m map[string]float64) error

SetFromMap sets parameters from a map. This map is produced by JSON parser usually.

func (*FloatParameters) SetValues

func (p *FloatParameters) SetValues(v []float64) error

SetValues sets values from a slice.

func (*FloatParameters) UnmarshalJSON

func (p *FloatParameters) UnmarshalJSON(b []byte) error

UnmarshalJSON sets parameters using JSON bytes.

func (*FloatParameters) Update

func (p *FloatParameters) Update(pSrc *FloatParameters)

Update updates values from another FloatParameters object.

func (*FloatParameters) Values

func (p *FloatParameters) Values(iv []float64) (v []float64)

Values returns a slice of parameter values.

func (*FloatParameters) ValuesInRange

func (p *FloatParameters) ValuesInRange(vals []float64) bool

ValuesInRange returns true if all parameters are in range.

func (*FloatParameters) ValuesString

func (p *FloatParameters) ValuesString() (s string)

ValuesString returns a tab-separated string of values.

type LBFGSB

type LBFGSB struct {
	BaseOptimizer
	// contains filtered or unexported fields
}

LBFGSB is wrapper around go-lbfgsb library. It uses L-BFGS-B algorithm of hill climbing.

func NewLBFGSB

func NewLBFGSB() (lbfgsb *LBFGSB)

NewLBFGSB creates a new LBFGSB optimizer.

func (*LBFGSB) EvaluateFunction

func (l *LBFGSB) EvaluateFunction(x []float64) float64

EvaluateFunction evaluates likelihood function for point x.

func (*LBFGSB) EvaluateGradient

func (l *LBFGSB) EvaluateGradient(x []float64) (grad []float64)

EvaluateGradient evaluates gradient for point x.

func (*LBFGSB) Logger

func (l *LBFGSB) Logger(info *lbfgsb.OptimizationIterationInformation)

Logger is a function which is called back on every iteration.

func (*LBFGSB) Run

func (l *LBFGSB) Run(iterations int)

Run starts the optimization.

func (*LBFGSB) Summary

func (l *LBFGSB) Summary() Summary

Summary returns optimization summary (i.e. success/error, etc).

type MH

type MH struct {
	BaseOptimizer
	AccPeriod int

	SD float64
	// contains filtered or unexported fields
}

MH is a Metropolis-Hastings sampler.

func NewMH

func NewMH(annealing bool, annealingSkip int) (mcmc *MH)

NewMH creates a new MH sampler.

func (*MH) Run

func (m *MH) Run(iterations int)

Run starts sampling.

type NLOPT

type NLOPT struct {
	BaseOptimizer
	// contains filtered or unexported fields
}

NLOPT is nlopt optimizer.

func NewNLOPT

func NewNLOPT(algorithm int, seed int64) (nlopt *NLOPT)

NewNLOPT creates a new NLOPT optimizer.

func (*NLOPT) Run

func (n *NLOPT) Run(iterations int)

Run runs the optimizer.

func (*NLOPT) Summary

func (n *NLOPT) Summary() Summary

Summary returns optimization summary (i.e. success/error, etc).

type None

type None struct {
	BaseOptimizer
}

None is an optimizer which computes initial value and exits.

func NewNone

func NewNone() *None

NewNone creates an optimizer which computes initial likelihood only.

func (*None) Run

func (i *None) Run(iterations int)

Run starts sampling.

type Optimizable

type Optimizable interface {
	// GetFloatParameters returns array (slice) of float
	// parameters.
	GetFloatParameters() FloatParameters
	// Copy creates a copy of optimizable.
	Copy() Optimizable
	// Likelihood returns likelihood.
	Likelihood() float64
}

Optimizable is something which can be optimized using the optimizer.

type Optimizer

type Optimizer interface {
	// SetOptimizable sets model for the optimization.
	SetOptimizable(Optimizable)
	// GetOptimizable returns the underlying model.
	GetOptimizable() Optimizable
	// WatchSignals installs OS hooks to react to signals.
	WatchSignals(...os.Signal)
	// SetReportPeriod specifies how often report line should be printed.
	SetReportPeriod(period int)
	// SetTrajectoryOutput specifies an output writer for the trajectory.
	SetTrajectoryOutput(io.Writer)
	// SetCheckpointIO sets CheckpointIO class.
	SetCheckpointIO(*checkpoint.CheckpointIO)
	// Starts the optimization or sampling.
	Run(iterations int)
	// GetMaxL returns the maximum likelihood value.
	GetMaxL() float64
	// GetMaxLParameters returns parameter values for the maximum
	// likelihood value.
	GetMaxLParameters() string
	// GetNCalls return total number of likelihood function calls.
	GetNCalls() int
	// GetNIter returns total number of iterations. Can be smaller than
	// number of likelihood function calls for the gradient-based methods.
	GetNIter() int
	// LoadFromOptimizer can load a model from another optimizer.
	LoadFromOptimizer(Optimizer)
	// PrintResults prints the results of optimization; quiet=true reduces
	// log levels of estimated parameter values.
	PrintResults(quiet bool)
	// Summary returns optimization summary for JSON output.
	Summary() Summary
}

Optimizer an optimizer interface.

type Summary

type Summary interface {
	GetMaxLikelihood() float64
	GetMaxLikelihoodParameters() map[string]float64
}

Summary allows quering of maximum likelihood estimates.

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier