wls-zig
is the implementation of weighted linear regression in pure Zig w/o any 3d party dependencies or frameworks.
- Run the following command inside your project:
zig fetch --save git+https://github.com/vspaz/wls-zig.git#main
it should add the following dependency to your project build.zig.zon file, e.g.
.dependencies = .{
.wls = .{
.url = "git+https://github.com/vspaz/wls-zig.git?ref=main#f4dfc9a98f8f538d6d25d7d3e1e431fe77d54744",
.hash = "wls-0.1.0-NK6PrjMoAABYyveo97dzY5AugQxqcGgExHsaDVMxx1wm",
},
}
- Navigate to build.zig file located in the root directory and add the following 2 lines as shown below:
const exe = b.addExecutable(.{
.name = "your project name",
.root_module = exe_mod,
});
// add these 2 lines!
const wlszig = b.dependency("wls", .{});
exe.root_module.addImport("wls", wlszig.module("wls"));
-
Test the project build with
zig build
There should be no error! -
mport
wls-zig
lib in your code as follows:
const wls = @import("wls");
and you're good to go! 🚀
pub const Wls = @import("wls").Wls;
pub const asserts = @import("wls").asserts;
pub fn main() !void {
const x_points = [_]f64{ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 };
const y_points = [_]f64{ 1.0, 3.0, 4.0, 5.0, 2.0, 3.0, 4.0 };
const weights = [_]f64{ 1.0, 2.0, 3.0, 1.0, 8.0, 1.0, 5.0 };
const wls_model = Wls.init(&x_points, &y_points, &weights);
const fitted_model = wls_model.fit_linear_regression();
if (fitted_model) |line| {
asserts.assert_almost_equal(2.14285714, line.intercept, 1.0e-6);
asserts.assert_almost_equal(0.150862, line.slope, 1.0e-6);
}
}
WLS is based on the OLS method and help solve problems of model inadequacy or violations of the basic regression assumptions.
Estimating a linear regression with WLS is can be hard w/o special stats packages, such as Python statsmodels or Pandas.