utils

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: MIT Imports: 3 Imported by: 0

README

utils

Package utils provides shared utilities for datascience including combinatorics, convergence checking, state name helpers, and validation functions.

Import path: github.com/asymmetric-effort/datascience/lib/pgm/utils

Combinatorics

Function Description
Combinations(items, k) Generate all k-element combinations of items
Permutations(items) Generate all permutations using Heap's algorithm
import "github.com/asymmetric-effort/datascience/lib/pgm/utils"

combos := utils.Combinations([]string{"A", "B", "C"}, 2)
// [["A","B"], ["A","C"], ["B","C"]]

perms := utils.Permutations([]string{"A", "B"})
// [["A","B"], ["B","A"]]

Convergence Checking

Type/Function Description
ConvergenceChecker Tracks a scalar across iterations, reports convergence when change < tolerance
NewConvergenceChecker(tolerance, maxIter) Create a checker with given tolerance and max iterations
cc := utils.NewConvergenceChecker(1e-6, 1000)

for {
    value := computeObjective()
    if cc.Update(value) {
        break // converged or max iterations reached
    }
}

fmt.Println(cc.Converged())  // true if change < tolerance
fmt.Println(cc.Iteration())  // number of iterations run

State Name Helpers

Function Description
GenerateStateNames(cardinality) Produce default names: ["s0", "s1", ..., "s{n-1}"]
ValidateStateNames(names, cardinality) Check length matches and no duplicates
StateIndex(names, name) Find the index of a state name
names := utils.GenerateStateNames(3) // ["s0", "s1", "s2"]

err := utils.ValidateStateNames(names, 3) // nil

idx, err := utils.StateIndex(names, "s1") // 1, nil

Validation Helpers

Function Description
ValidatePositiveInt(name, value) Error if value <= 0
ValidateNonNegativeFloat(name, value) Error if value < 0
ValidateProbability(name, value) Error if value not in [0, 1]
ValidateProbabilityDistribution(name, values, tolerance) Error if values don't sum to 1
err := utils.ValidatePositiveInt("nSamples", 100) // nil
err = utils.ValidateProbability("p", 1.5)          // error

probs := []float64{0.3, 0.3, 0.4}
err = utils.ValidateProbabilityDistribution("prior", probs, 1e-9) // nil

Documentation

Overview

Package utils provides shared utilities for datascience including parsing, optimization helpers, and compatibility functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CartesianProduct

func CartesianProduct(sets [][]int) [][]int

CartesianProduct computes the Cartesian product of integer sets. For example, CartesianProduct([][]int{{0,1},{2,3}}) returns [[0,2],[0,3],[1,2],[1,3]].

func Combinations

func Combinations(items []string, k int) [][]string

Combinations generates all k-element combinations of items. The order of elements within each combination matches their order in items.

func GenerateStateNames

func GenerateStateNames(cardinality int) []string

GenerateStateNames produces default state names ["s0", "s1", ..., "s{n-1}"].

func Permutations

func Permutations(items []string) [][]string

Permutations generates all permutations of items using Heap's algorithm.

func SafePath added in v0.1.1

func SafePath(path string) (string, error)

SafePath validates that a file path does not contain directory traversal sequences or other dangerous patterns.

func StateIndex

func StateIndex(names []string, name string) (int, error)

StateIndex returns the index of name within names, or an error if not found.

func StateNameMap

func StateNameMap(variables []string, stateNames map[string][]string) map[string]map[string]int

StateNameMap builds a mapping from variable name to state name to index. variables lists the variable names; stateNames maps each variable to its ordered slice of state names.

func ValidateNoInf

func ValidateNoInf(name string, values []float64) error

ValidateNoInf returns an error if any value is infinite.

func ValidateNoNaN

func ValidateNoNaN(name string, values []float64) error

ValidateNoNaN returns an error if any value is NaN.

func ValidateNonNegativeFloat

func ValidateNonNegativeFloat(name string, value float64) error

ValidateNonNegativeFloat returns an error if value is negative.

func ValidatePositiveInt

func ValidatePositiveInt(name string, value int) error

ValidatePositiveInt returns an error if value is not strictly positive.

func ValidateProbability

func ValidateProbability(name string, value float64) error

ValidateProbability returns an error if value is not in [0, 1].

func ValidateProbabilityDistribution

func ValidateProbabilityDistribution(name string, values []float64, tolerance float64) error

ValidateProbabilityDistribution returns an error if the values do not sum to 1 within the given tolerance.

func ValidateStateNames

func ValidateStateNames(names []string, cardinality int) error

ValidateStateNames checks that names has the expected length and contains no duplicates.

Types

type ConvergenceChecker

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

ConvergenceChecker tracks a scalar value across iterations and determines whether it has converged (i.e., the absolute change falls below a tolerance).

func NewConvergenceChecker

func NewConvergenceChecker(tolerance float64, maxIter int) *ConvergenceChecker

NewConvergenceChecker creates a ConvergenceChecker with the given tolerance and maximum iteration count.

func (*ConvergenceChecker) Converged

func (c *ConvergenceChecker) Converged() bool

Converged returns whether convergence has been detected.

func (*ConvergenceChecker) Iterations

func (c *ConvergenceChecker) Iterations() int

Iterations returns the number of Update calls so far.

func (*ConvergenceChecker) Reset

func (c *ConvergenceChecker) Reset()

Reset clears the checker state so it can be reused.

func (*ConvergenceChecker) Update

func (c *ConvergenceChecker) Update(value float64) bool

Update records a new value and returns true if the checker has converged (the absolute change from the previous value is less than tolerance) or the maximum number of iterations has been reached.

Jump to

Keyboard shortcuts

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