 # Linear Regression Algorithm in pl/sql

articles:

********************************************************************************
--LINEAR REGRESSION ALGORITHM IN PL/SQL
--------------------------------------------------------------------------------
Modified Mar 8th 2019
Using Linear Regression algorithm, find the weight of a studen, whose is 71 inches tall.
Given the height and weight of 8 students.

This script is for educational purpose only.
Pls make necessary changes to the script, as may be required in your environment.
I generated this script and tested it in Oracle 12C.

set serverout on size 1000000 timing on
DECLARE
type tallarray IS VARRAY(8) OF float;
type heavyarray IS VARRAY(8) OF float;
height tallarray;
weight heavyarray;
total float;
xy float:=0;
sx float:=0;
sy float:=0;
x2 float:=0;
b1 float;
b0 float;
BEGIN
height := tallarray(65,65,62,67,69,65,61,67);
weight := heavyarray(105,125,110,120,140,135,95,130);
total := height.count;
FOR i in 1 .. total LOOP
xy:=xy + round((height(i)*weight(i)),2);
sx:=sx + height(i);
sy:=sy + weight(i);
x2:=x2 + height(i)**2;
END LOOP;
b1:=round((xy - ((sx * sy)/total))/(x2 - (sx**2/total)),2);
b0:=round((sy - (b1*sx))/total,2);
dbms_output.put_line(sx||' '||sy||' '||x2||' '||xy||' '||b0||' '||b1||' '||(b0 + b1*71));
END;
/
521 960 33979 62750 -186.74 4.71 147.67

PL/SQL procedure successfully completed