bayesian

package
v0.0.0-...-d9d2994 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultPriors

func DefaultPriors() map[PriorKey]models.BetaParams

DefaultPriors returns the informative prior Beta parameters per (task type, context category) pair. These encode domain knowledge about which context categories matter for which task types.

The prior "sample size" (alpha + beta) controls resistance to updating. Higher values = more data needed to shift the belief.

func PosteriorEntropy

func PosteriorEntropy(params models.BetaParams) float64

PosteriorEntropy returns the differential entropy of the Beta distribution. Lower entropy = more concentrated/confident posterior.

func PosteriorHDI

func PosteriorHDI(params models.BetaParams) (float64, float64)

PosteriorHDI returns the approximate 95% highest density interval for a Beta distribution. Uses quantiles as an approximation (exact HDI requires numerical optimization, but for Beta distributions the quantile-based interval is very close).

func ThompsonSample

func ThompsonSample(params models.BetaParams) float64

ThompsonSample draws a sample from the Beta posterior for the given (task type, category) pair. This implements the exploration/exploitation trade-off: wide posteriors produce highly variable draws (exploration), while tight posteriors produce draws near the mean (exploitation).

Types

type Engine

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

Engine manages Beta-Bernoulli posteriors for all (task type, category) pairs. It provides Thompson Sampling for decisions and conjugate updates for learning.

func NewEngine

func NewEngine(store Store) (*Engine, error)

func (*Engine) AllPosteriors

func (e *Engine) AllPosteriors() map[PriorKey]models.BetaParams

AllPosteriors returns a snapshot of all current posteriors.

func (*Engine) BatchUpdate

func (e *Engine) BatchUpdate(taskType models.TaskType, categories []models.ChunkCategory, success bool)

BatchUpdate updates posteriors for multiple categories at once.

func (*Engine) GetPosterior

func (e *Engine) GetPosterior(taskType models.TaskType, category models.ChunkCategory) models.BetaParams

GetPosterior returns the current posterior for a (task, category) pair.

func (*Engine) PosteriorMean

func (e *Engine) PosteriorMean(taskType models.TaskType, category models.ChunkCategory) float64

PosteriorMean returns the posterior mean for a (task, category) pair.

func (*Engine) Sample

func (e *Engine) Sample(taskType models.TaskType, category models.ChunkCategory) float64

Sample draws from the posterior Beta distribution for Thompson Sampling.

func (*Engine) Update

func (e *Engine) Update(taskType models.TaskType, category models.ChunkCategory, success bool)

Update performs a conjugate Bayesian update on the posterior. success=true increments alpha (evidence the chunk was useful); success=false increments beta (evidence it was not).

type PriorKey

type PriorKey struct {
	TaskType models.TaskType      `json:"task_type"`
	Category models.ChunkCategory `json:"category"`
}

PriorKey uniquely identifies a (task type, chunk category) pair.

type SQLiteStore

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

SQLiteStore persists posteriors to a SQLite database.

func NewSQLiteStore

func NewSQLiteStore(dbPath string) (*SQLiteStore, error)

func (*SQLiteStore) Close

func (s *SQLiteStore) Close() error

func (*SQLiteStore) LoadAll

func (s *SQLiteStore) LoadAll() (map[PriorKey]models.BetaParams, error)

func (*SQLiteStore) Save

func (s *SQLiteStore) Save(key PriorKey, params models.BetaParams) error

type Store

type Store interface {
	LoadAll() (map[PriorKey]models.BetaParams, error)
	Save(key PriorKey, params models.BetaParams) error
}

Store is the persistence interface for posteriors.

Jump to

Keyboard shortcuts

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