Skip to content

Instantly share code, notes, and snippets.

View kyledacones's full-sized avatar

Kyle Dacones kyledacones

  • Seattle, WA
View GitHub Profile
@kyledacones
kyledacones / linear_regression.sql
Last active December 29, 2021 05:54 — forked from patrickpissurno/linear_regression.sql
Linear Regression Functions for Gathering Inputs/ Implementing a Forecasting Model to PostgreSQL
-- This code is based on my other Gist "Simple linear regression in Javascript" (https://gist.github.com/patrickpissurno/ea0dc4039f075fbaf398619761bd9044)
-- There might be a more efficient way to do this in SQL
-- This function is resposible for computing the weights for the ax + b equation
CREATE OR REPLACE FUNCTION linear_regression(x_array decimal(15,2)[], y_array decimal(15,2)[]) RETURNS decimal(15,2)[] AS $$
DECLARE
slope decimal(15,2);
intercept decimal(15,2);
n integer;