{ "LinearFit — runnable example": { "prefix": "blyx-linearfit-demo", "description": "Train a one-feature regression", "isFileTemplate": true, "body": [ "// https://github.com/Rahulchaube1/blyx-moments/blob/v0.1.0/moments.blyx", "// A paired summary stores [count, mean_x, mean_y, Sxx, Sxy, Syy].", "fn moments_finite(x: f64) -> bool { x - x == 0.0 \\}", "", "pub fn mom_empty() -> vector { [0, 0, 0, 0, 0, 0] \\}", "", "pub fn mom_valid(s: vector) -> bool {", " if len(s) != 6 { return false; \\}", " let n = at(s, 0);", " if !(n >= 0.0 && n <= 9007199254740991.0) || n % 1.0 != 0.0 { return false; \\}", " let mut i = 1;", " while i < 6 {", " if !moments_finite(at(s, i)) || (n == 0.0 && at(s, i) != 0.0) { return false; \\}", " i += 1;", " \\}", " at(s, 3) >= 0.0 && at(s, 5) >= 0.0", "\\}", "", "pub fn mom_update(s: vector, x: f64, y: f64) -> vector {", " if !mom_valid(s) || !moments_finite(x) || !moments_finite(y) { return []; \\}", " if at(s, 0) == 0.0 { return [1, x, y, 0, 0, 0]; \\}", " let n = at(s, 0) + 1.0;", " let dx = x - at(s, 1);", " let dy = y - at(s, 2);", " let mx = at(s, 1) + dx / n;", " let my = at(s, 2) + dy / n;", " let next = [n, mx, my, at(s, 3) + dx * (x - mx),", " at(s, 4) + dx * (y - my), at(s, 5) + dy * (y - my)];", " if !mom_valid(next) { return []; \\}", " next", "\\}", "", "pub fn mom_summary(x: vector, y: vector) -> vector {", " if len(x) != len(y) { return []; \\}", " let mut state = mom_empty();", " let mut i = 0;", " while i < len(x) {", " state = mom_update(state, at(x, i), at(y, i));", " if len(state) == 0 { return []; \\}", " i += 1;", " \\}", " state", "\\}", "", "pub fn mom_merge(a: vector, b: vector) -> vector {", " if !mom_valid(a) || !mom_valid(b) { return []; \\}", " if at(a, 0) == 0.0 { return b; \\}", " if at(b, 0) == 0.0 { return a; \\}", " let n = at(a, 0) + at(b, 0);", " let share = at(b, 0) / n;", " let weight = at(a, 0) * share;", " let dx = at(b, 1) - at(a, 1);", " let dy = at(b, 2) - at(a, 2);", " let next = [n, at(a, 1) + dx * share, at(a, 2) + dy * share,", " (at(a, 3) + at(b, 3)) + (dx * weight) * dx,", " (at(a, 4) + at(b, 4)) + (dx * weight) * dy,", " (at(a, 5) + at(b, 5)) + (dy * weight) * dy];", " if !mom_valid(next) { return []; \\}", " next", "\\}", "", "// Reduce to [1,4) before Newton iteration, including for subnormal variance.", "fn moments_positive_root(value: f64) -> f64 {", " if value == 0.0 { return 0.0; \\}", " let mut v = value;", " let mut factor = 1.0;", " while v >= 4.0 { v /= 4.0; factor *= 2.0; \\}", " while v < 1.0 { v *= 4.0; factor /= 2.0; \\}", " let mut root = 2.0;", " let mut i = 0;", " while i < 6 { root = 0.5 * (root + v / root); i += 1; \\}", " root * factor", "\\}", "", "pub fn mom_sample_sd_y(s: vector) -> f64 {", " if !mom_valid(s) || at(s, 0) < 2.0 { return -1.0; \\}", " moments_positive_root(at(s, 5) / (at(s, 0) - 1.0))", "\\}", "", "// https://github.com/Rahulchaube1/blyx-linearfit/blob/v0.1.1/linearfit.blyx", "fn linearfit_finite(x: f64) -> bool { x - x == 0.0 \\}", "", "pub fn lf_start() -> vector { mom_empty() \\}", "pub fn lf_update(s: vector, x: f64, y: f64) -> vector { mom_update(s, x, y) \\}", "pub fn lf_merge(a: vector, b: vector) -> vector { mom_merge(a, b) \\}", "", "// Model is centered at the sample means to preserve precision in prediction.", "// penalty multiplies slope squared in the unnormalized sum-of-squares objective.", "pub fn lf_finish(s: vector, penalty: f64) -> vector {", " if !mom_valid(s) || at(s, 0) < 2.0 || !linearfit_finite(penalty) || penalty < 0.0 { return []; \\}", " let denominator = at(s, 3) + penalty;", " if denominator <= 0.0 || !linearfit_finite(denominator) { return []; \\}", " let slope = at(s, 4) / denominator;", " if !linearfit_finite(slope) { return []; \\}", " [at(s, 1), at(s, 2), slope]", "\\}", "", "pub fn lf_fit(x: vector, y: vector, penalty: f64) -> vector {", " lf_finish(mom_summary(x, y), penalty)", "\\}", "", "pub fn lf_predict(model: vector, x: f64) -> f64 {", " if len(model) != 3 || !linearfit_finite(x) { return 0.0 / 0.0; \\}", " let y = at(model, 1) + at(model, 2) * (x - at(model, 0));", " if !linearfit_finite(y) { return 0.0 / 0.0; \\}", " y", "\\}", "", "fn main() {", " let training = lf_fit([1, 2, 3, 4], [3, 5, 7, 9], 0.0);", " println!(at(training, 2));", " println!(lf_predict(training, 5.0));", "\\}" ] }, "LinearFit — import the framework": { "prefix": "blyx-linearfit-import", "description": "Use the module from the extracted LinearFit starter project.", "isFileTemplate": true, "body": [ "use vendor::linearfit::linearfit;", "fn main() {", " let training = lf_fit([1, 2, 3, 4], [3, 5, 7, 9], 0.0);", " println!(at(training, 2));", " println!(lf_predict(training, 5.0));", "\\}" ] }, "LinearFit — lf_start": { "prefix": "blyx-linearfit-lf_start", "body": "lf_start()$0", "description": "Accumulate rows or merge disjoint training batches." }, "LinearFit — lf_update": { "prefix": "blyx-linearfit-lf_update", "body": "lf_update(${1:s}, ${2:x}, ${3:y})$0", "description": "Accumulate rows or merge disjoint training batches." }, "LinearFit — lf_merge": { "prefix": "blyx-linearfit-lf_merge", "body": "lf_merge(${1:a}, ${2:b})$0", "description": "Accumulate rows or merge disjoint training batches." }, "LinearFit — lf_finish": { "prefix": "blyx-linearfit-lf_finish", "body": "lf_finish(${1:s}, ${2:penalty})$0", "description": "Freeze a model and predict a response in the original units." }, "LinearFit — lf_fit": { "prefix": "blyx-linearfit-lf_fit", "body": "lf_fit(${1:x}, ${2:y}, ${3:penalty})$0", "description": "Train on a pair of vectors. Returns [mean_x, mean_y, slope]." }, "LinearFit — lf_predict": { "prefix": "blyx-linearfit-lf_predict", "body": "lf_predict(${1:model}, ${2:x})$0", "description": "Freeze a model and predict a response in the original units." }, "LinearFit — Train a factored matrix": { "prefix": "blyx-linearfit-scale", "isFileTemplate": true, "body": [ "use vendor::linearfit::bilinear;", "", "fn main() {", " let left = [1,0,0,1];", " let right = [1,0,0,1];", " let gram = lf_pair_gram(left,right,2);", " let alpha = lf_pair_fit(gram,[1,-1],[0,0],1,1.0);", " println!(lf_pair_predict(left,right,alpha,[2,3],[1,1]));", " println!(lf_pair_weight(left,right,alpha,1,1));", "}" ], "description": "Run inside the LinearFit starter using the pinned native compiler." } }