Documentation ¶
Overview ¶
Package optimize is a collection of optimizers and MCMC samplers.
Index ¶
- Constants
- func DiscreteProposal(state int, nstates int) (newstate int)
- func DiscreteUniformPrior(nstates int) func(float64) float64
- func ExponentialPrior(rate float64, inczero bool) func(float64) float64
- func GammaPrior(shape, scale float64, inczero bool) func(float64) float64
- func NormalProposal(sd float64) func(float64) float64
- func ProductPrior(f, g func(float64) float64) func(float64) float64
- func Rand() float64
- func ReadFloats(s string) ([]float64, error)
- func UniformGlobalProposal(min, max float64) func(float64) float64
- func UniformPrior(min, max float64, incmin, incmax bool) func(float64) float64
- func UniformProposal(width float64) func(float64) float64
- type AdaptiveParameter
- type AdaptiveSettings
- type BaseOptimizer
- func (o *BaseOptimizer) GetMaxL() float64
- func (o *BaseOptimizer) GetMaxLParameters() (s string)
- func (o *BaseOptimizer) GetNCalls() int
- func (o *BaseOptimizer) GetNIter() int
- func (o *BaseOptimizer) GetOptimizable() Optimizable
- func (o *BaseOptimizer) GetParametersMap(par []float64) (m map[string]float64)
- func (o *BaseOptimizer) LoadFromOptimizer(opt Optimizer)
- func (o *BaseOptimizer) PrintHeader()
- func (o *BaseOptimizer) PrintLine(par FloatParameters, l float64, repPeriod int)
- func (o *BaseOptimizer) PrintResults(quiet bool)
- func (o *BaseOptimizer) SaveCheckpoint(final bool)
- func (o *BaseOptimizer) SaveStart()
- func (o *BaseOptimizer) SetCheckpointIO(cio *checkpoint.CheckpointIO)
- func (o *BaseOptimizer) SetOptimizable(opt Optimizable)
- func (o *BaseOptimizer) SetReportPeriod(period int)
- func (o *BaseOptimizer) SetTrajectoryOutput(output io.Writer)
- func (o *BaseOptimizer) Summary() Summary
- func (o *BaseOptimizer) WatchSignals(sigs ...os.Signal)
- type BasicFloatParameter
- func (p *BasicFloatParameter) Accept(iter int)
- func (p *BasicFloatParameter) Get() float64
- func (p *BasicFloatParameter) GetMax() float64
- func (p *BasicFloatParameter) GetMin() float64
- func (p *BasicFloatParameter) InRange() bool
- func (p *BasicFloatParameter) Name() string
- func (p *BasicFloatParameter) OldPrior() float64
- func (p *BasicFloatParameter) Prior() float64
- func (p *BasicFloatParameter) Propose()
- func (p *BasicFloatParameter) Randomize()
- func (p *BasicFloatParameter) Reject()
- func (p *BasicFloatParameter) Set(v float64)
- func (p *BasicFloatParameter) SetMax(max float64)
- func (p *BasicFloatParameter) SetMin(min float64)
- func (p *BasicFloatParameter) SetOnChange(f func())
- func (p *BasicFloatParameter) SetPriorFunc(f func(float64) float64)
- func (p *BasicFloatParameter) SetProposalFunc(f func(float64) float64)
- func (p *BasicFloatParameter) String() string
- func (p *BasicFloatParameter) ValueInRange(v float64) bool
- type DS
- type DiscreteParameter
- type FloatParameter
- type FloatParameterGenerator
- type FloatParameters
- func (p *FloatParameters) Append(par FloatParameter)
- func (p FloatParameters) GetMap() map[string]float64
- func (p *FloatParameters) InRange() bool
- func (p FloatParameters) MarshalJSON() ([]byte, error)
- func (p *FloatParameters) Names(is []string) (s []string)
- func (p *FloatParameters) NamesString() (s string)
- func (p *FloatParameters) Randomize()
- func (p *FloatParameters) ReadFromJSON(filename string) error
- func (p *FloatParameters) ReadLine(l string) error
- func (p *FloatParameters) SetByName(name string, v float64) error
- func (p *FloatParameters) SetFromMap(m map[string]float64) error
- func (p *FloatParameters) SetValues(v []float64) error
- func (p *FloatParameters) UnmarshalJSON(b []byte) error
- func (p *FloatParameters) Update(pSrc *FloatParameters)
- func (p *FloatParameters) Values(iv []float64) (v []float64)
- func (p *FloatParameters) ValuesInRange(vals []float64) bool
- func (p *FloatParameters) ValuesString() (s string)
- type LBFGSB
- type MH
- type NLOPT
- type None
- type Optimizable
- type Optimizer
- type Summary
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 )
Variables ¶
This section is empty.
Functions ¶
func DiscreteProposal ¶
DiscreteProposal returns function returning a random integer converted to float64.
func DiscreteUniformPrior ¶
DiscreteUniformPrior returns function returning 1/nstates.
func ExponentialPrior ¶
ExponentialPrior returns a function returning a exponential distribution.
func GammaPrior ¶
GammaPrior returns a function returning a gamma distribution.
func NormalProposal ¶
NormalProposal returns normal proposal function.
func ProductPrior ¶
ProductPrior returns a function which a multiplication of two functions.
func ReadFloats ¶
ReadFloats converts string of floats into slice of float64.
func UniformGlobalProposal ¶
UniformGlobalProposal returns uniform proposal function given max and min.
func UniformPrior ¶
UniformPrior returns a uniform prior function.
func UniformProposal ¶
UniformProposal returns uniform proposal function.
Types ¶
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.
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 (*LBFGSB) EvaluateFunction ¶
EvaluateFunction evaluates likelihood function for point x.
func (*LBFGSB) EvaluateGradient ¶
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.
type MH ¶
type MH struct { BaseOptimizer AccPeriod int SD float64 // contains filtered or unexported fields }
MH is a Metropolis-Hastings sampler.
type NLOPT ¶
type NLOPT struct { BaseOptimizer // contains filtered or unexported fields }
NLOPT is nlopt optimizer.
type None ¶
type None struct {
BaseOptimizer
}
None is an optimizer which computes initial value and exits.
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.