I’ve just released the linear-regression shard, for linear least-squares regression.
Watch out, Python… Crystal now supports the most popular machine learning algorithm in the world
require "linear-regression"
linreg = LinearRegression.new([-1.0, 0.0, 1.0], [4.0, 4.0, 5.0])
# Examine properties of the fit:
linreg.slope # 0.5
linreg.intercept # 4.33333333
linreg.pearson_r # 0.8660254038
linreg.pearson_r_squared # 0.75
# Evaluate the regression line at a given x coordinate:
linreg.at(0.0) # 4.33333333
linreg.at(10.0) # 9.33333333
# Serialize and deserialize the fit: (does not serialize raw data points)
j = linreg.to_json # '{"n":3,...}'
linreg2 = LinearRegression.from_json(j)
linreg2.slope # 0.5
I’ve been using this in production on Total Real Returns and finally extracted it as its own well-tested shard.
I came across other implementations like @EvanHahn’s simple_statistics, but I particularly needed goodness-of-fit metrics .pearson_r
and .pearson_r_squared
, as well as the ability to serialize and deserialize the fit .to_json
and LinearRegression.from_json
. Enjoy.