Livt packages
Livt.ML
Version 1.6.1
Fixed-size quantized inference, tensor storage, attention, and transformer building blocks for Livt.
Published files (132)
src/model/LinearRegression.lvt
namespace Livt.ML.Model
using Livt.Math.Arithmetic
/**
* Runtime division is scheduled; completion latency increases with work.
* Serialize operations and keep inputs stable until the call or handshake completes.
*
* Fits a small integer linear regression model with fixed-point slope.
*/
component LinearRegression
{
// Owned divider; calls complete before this component advances.
divider: IntegerDivision<32>
// Maximum number of samples accepted by Fit.
public const MAX_SAMPLES: int = 32
// Fixed-point scale used to store the slope coefficient.
public const SCALE: int = 1000
// Fitted slope and intercept retained for predictions.
aFixed: int
b: int
/**
* Owns the scheduled arithmetic worker; serialize public operations.
*/
new()
{
divider = new IntegerDivision<32>()
}
/**
* Fits y = a*x + b from n samples, clearing coefficients on invalid input.
*/
public fn Fit(x: int[32], y: int[32], n: int)
{
aFixed = 0
b = 0
if (n < 2) {
return
}
if (n > MAX_SAMPLES) {
return
}
var sumX = 0
var sumY = 0
var sumXX = 0
var sumXY = 0
for (var i = 0; i < n; i++) {
sumX = sumX + x[i]
sumY = sumY + y[i]
sumXX = sumXX + x[i] * x[i]
sumXY = sumXY + x[i] * y[i]
}
var denom = n * sumXX - sumX * sumX
if (denom == 0) {
return
}
aFixed = divider.Divide((n * sumXY - sumX * sumY) * SCALE, denom)
b = divider.Divide(sumY - divider.Divide(aFixed * sumX, SCALE), n)
}
/**
* Predicts y for x using the last fitted coefficients.
*/
public fn Predict(x: int) int
{
return divider.Divide(aFixed * x, SCALE) + b
}
/**
* Returns the fitted slope scaled by SCALE.
*/
public fn GetAFixed() int
{
return aFixed
}
/**
* Returns the fitted intercept.
*/
public fn GetB() int
{
return b
}
}