ThatAIguyA world of ideas

THE CONSCIOUSNESS ATLAS
Read. Experiment. Follow the evidence.

ConsciousnessLearn · Explore · Joseph Anady

Janady13/missionmatch / internal/platform/learn/logistic.go

Source identity, revision, and recovered contents.

GitHub sourceSource text recovered

Source identity

FieldRecorded value
Source typeGitHub source
Original recordJanady13/missionmatch / internal/platform/learn/logistic.go
DateNot specified in source
RepositoryJanady13/missionmatch
Revision720d47fae476b0d38477ae4401697d372b1ea2bc
Branchfeature/opportunity-to-outcome
Path or archive memberinternal/platform/learn/logistic.go
Original SHA-2567686b3288a451cae885283fd62015737f5d36ea822465b37c3a2ce76e6b0912f
Git blob SHA6f627ef9bb977eb817575fca59bab0ebd6a416ff
Reading-copy SHA-2567686b3288a451cae885283fd62015737f5d36ea822465b37c3a2ce76e6b0912f

Original source ↗

Every recorded occurrence

These 10 records contain identical original source bytes. Their paths and revisions remain part of the archive.

Original recordBranch or archiveRevision / identifierOriginal location
Janady13/missionmatch / internal/platform/learn/logistic.gofeature/opportunity-to-outcome720d47fae476b0d38477ae4401697d372b1ea2bcOpen original
Janady13/missionmatch / internal/platform/learn/logistic.gocollision-deconfliction4ee5a09972608ed89f8377c6665542d5f450f0a6Open original
Janady13/missionmatch / internal/platform/learn/logistic.gomaster1e9249840a4302ec82e123b9c9cb6f75badea3e4Open original
Janady13/missionmatch / internal/platform/learn/logistic.gomag-substrateb82c7100a492dec67dfd1eda6b3f53def859be35Open original
Janady13/missionmatch / internal/platform/learn/logistic.gotime-to-fill-survivalb66e1e505863d4f17741a4b9ede73509e45bd6c6Open original
Janady13/missionmatch / internal/platform/learn/logistic.gopresentation-readinesseb574acaa29d71ce4d0f852572e87c275c79fdb7Open original
Janady13/missionmatch / internal/platform/learn/logistic.govendor-intelligencefe9796a19c261951d3f2565206c0c1b5ac294465Open original
Janady13/missionmatch / internal/platform/learn/logistic.gophase2-connectorse038d4c687aec16f8bd9bc4d907eea14d91f8704Open original
Janady13/missionmatch / internal/platform/learn/logistic.gofeature/aioe-upgrade2178e7298d0e5d9af83afa12402fd587f0ba6ed8Open original
Janady13/missionmatch / internal/platform/learn/logistic.gocapture-readinessbae6af7cb2997e819174a9e9cfff5e16067a1975Open original

Recovered source transcript

// Package learn is the pure Go numerical core of the MAG learning loop: it fits interpretable win // probability coefficients from labeled outcomes and calibrates them, with NO database and NO Python. // float64 lives ONLY inside these offline solvers (an offline numerical optimization, never a request path // score); every coefficient the caller stores and every number the platform surfaces is re quantized to // shopspring/decimal by the caller (cmd/train-model), so the served path stays decimal exact. package learn import ( "errors" "math" ) // ErrNoRows is returned when a fit is asked over an empty dataset. var ErrNoRows = errors.New("learn: no rows") // LogisticModel is a fitted ridge penalized logistic regression on STANDARDIZED features. Serving computes // sigmoid(Intercept + sum_j Coeffs[j] * (x_j - Means[j]) / Scales[j]). Storing the means and scales lets the // serve path standardize a new feature vector exactly as training did. type LogisticModel struct { Intercept float64 Coeffs []float64 // one per feature, on the standardized scale Means []float64 // feature means used to standardize Scales []float64 // feature standard deviations used to standardize Iterations int // Newton iterations to convergence } // FitLogistic fits a ridge penalized logistic regression by iteratively reweighted least squares (Newton // Raphson on the penalized log likelihood). x is n rows by p features, y is n labels in {0,1}, l2 is the // ridge strength (a zero mean Gaussian prior on the non intercept coefficients, so a thin dataset shrinks // toward zero rather than diverging; the intercept is never penalized). Features are standardized internally. // Deterministic: the same input always yields the same coefficients. func FitLogistic(x [][]float64, y []int, l2 float64) (LogisticModel, error) { n := len(x) if n == 0 { return LogisticModel{}, ErrNoRows } if len(y) != n { return LogisticModel{}, errors.New("learn: x and y length mismatch") } p := len(x[0]) if p == 0 { return LogisticModel{}, errors.New("learn: no features") } means := make([]float64, p) scales := make([]float64, p) for j := 0; j < p; j++ { var s float64 for i := 0; i < n; i++ { s += x[i][j] } means[j] = s / float64(n) } for j := 0; j < p; j++ { var v float64 for i := 0; i < n; i++ { d := x[i][j] - means[j] v += d * d } sd := math.Sqrt(v / float64(n)) if sd < 1e-12 { sd = 1 // a constant feature gets scale 1, so its standardized value is exactly 0 and it contributes nothing } scales[j] = sd } z := make([][]float64, n) for i := 0; i < n; i++ { z[i] = make([]float64, p) for j := 0; j < p; j++ { z[i][j] = (x[i][j] - means[j]) / scales[j] } } // A tiny ridge floor keeps the Hessian positive definite even when a feature is constant (a zero // standardized column) or the data is perfectly separable (coefficients would otherwise diverge). It is // small enough not to bias a normal fit and is standard numerical hygiene. effL2 := l2 if effL2 < 1e-8 { effL2 = 1e-8 } // beta index 0 is the intercept, index j+1 is the coefficient of standardized feature j. dim := p + 1 beta := make([]float64, dim) const maxIter = 100 const tol = 1e-9 iters := 0 for it := 0; it < maxIter; it++ { iters = it + 1 g := make([]float64, dim) // gradient h := make([][]float64, dim) // Hessian for a := range h { h[a] = make([]float64, dim) } for i := 0; i < n; i++ { eta := beta[0] for j := 0; j < p; j++ { eta += beta[j+1] * z[i][j] } pi := sigmoid(eta) w := pi * (1 - pi) if w < 1e-10 { w = 1e-10 // keep the weight matrix positive definite when a point is near certain } r := float64(y[i]) - pi // the design row is xi = [1, z_i0, z_i1, ...]; g += xi*r and h += w * xi xi^T g[0] += r h[0][0] += w for a := 0; a < p; a++ { g[a+1] += z[i][a] * r h[0][a+1] += w * z[i][a] h[a+1][0] += w * z[i][a] for b := 0; b < p; b++ { h[a+1][b+1] += w * z[i][a] * z[i][b] } } } // ridge on the non intercept coefficients only for a := 1; a < dim; a++ { g[a] -= effL2 * beta[a] h[a][a] += effL2 } delta, err := solveLinear(h, g) if err != nil { return LogisticModel{}, err } var maxStep float64 for a := 0; a < dim; a++ { beta[a] += delta[a] if s := math.Abs(delta[a]); s > maxStep { maxStep = s } } if maxStep < tol { break } } return LogisticModel{Intercept: beta[0], Coeffs: beta[1:], Means: means, Scales: scales, Iterations: iters}, nil } // Predict returns the model probability in [0,1] for a RAW (unstandardized) feature vector. func (m LogisticModel) Predict(features []float64) float64 { eta := m.Intercept for j := range m.Coeffs { x := 0.0 if j < len(features) && j < len(m.Means) { x = (features[j] - m.Means[j]) / m.Scales[j] } eta += m.Coeffs[j] * x } return sigmoid(eta) } // sigmoid is the numerically stable logistic function. func sigmoid(x float64) float64 { if x >= 0 { return 1 / (1 + math.Exp(-x)) } e := math.Exp(x) return e / (1 + e) } // solveLinear solves h x = g for a small dense system by Gaussian elimination with partial pivoting. h is // copied, not mutated. It errors on a singular system. func solveLinear(h [][]float64, g []float64) ([]float64, error) { n := len(g) m := make([][]float64, n) for i := 0; i < n; i++ { m[i] = make([]float64, n+1) copy(m[i], h[i]) m[i][n] = g[i] } for col := 0; col < n; col++ { piv := col for r := col + 1; r < n; r++ { if math.Abs(m[r][col]) > math.Abs(m[piv][col]) { piv = r } } if math.Abs(m[piv][col]) < 1e-14 { return nil, errors.New("learn: singular system") } m[col], m[piv] = m[piv], m[col] for r := 0; r < n; r++ { if r == col { continue } f := m[r][col] / m[col][col] for c := col; c <= n; c++ { m[r][c] -= f * m[col][c] } } } x := make([]float64, n) for i := 0; i < n; i++ { x[i] = m[i][n] / m[i][i] } return x, nil } // FitPlatt fits Platt scaling: a one dimensional logistic map from raw model scores to calibrated // probabilities, returning a and b such that calibrated = sigmoid(a*score + b). It reuses FitLogistic with // the score as the single feature and recovers a and b on the raw score scale. Platt is the cold start // calibrator: parametric, robust when the label set is thin, later replaced by isotonic past ~1000 labels. func FitPlatt(scores []float64, y []int) (a, b float64, err error) { x := make([][]float64, len(scores)) for i := range scores { x[i] = []float64{scores[i]} } m, e := FitLogistic(x, y, 0) if e != nil { return 0, 0, e } // standardized eta = Intercept + Coeff*(s-mean)/scale = (Coeff/scale)*s + (Intercept - Coeff*mean/scale) a = m.Coeffs[0] / m.Scales[0] b = m.Intercept - m.Coeffs[0]*m.Means[0]/m.Scales[0] return a, b, nil }

Mathematical records from this source

Search equations, builds, manuscript text, and recovered sources.