Linear Regression / best fit

Share your advanced PureBasic knowledge/code with the community.
jesperbrannmark
Enthusiast
Enthusiast
Posts: 536
Joined: Mon Feb 16, 2009 10:42 am
Location: sweden
Contact:

Linear Regression / best fit

Post by jesperbrannmark »

For whoever will need it.
Based on this http://www.easycalculation.com/statisti ... ession.php
Used if you want to do something like this
Image
http://en.wikipedia.org/wiki/Simple_linear_regression

Code: Select all

Global Dim p.f(500,2)
Global count.l=0
Procedure.f calc(x)
  For i=0 To count-1
    delta_x.f+p.f(i,0)
    delta_y.f+p.f(i,1)
    delta_xy.f+(p.f(i,0)*p.f(i,1))
    delta_xx.f+(p.f(i,0)*p.f(i,0))
  Next
  slope.f=((count*delta_xy)-(delta_x*delta_y))/((count*delta_xx)-(delta_x*delta_x))
  intercept.d=(delta_y)-(slope.f*delta_x)
  intercept.d/count
  Debug intercept
  regression.f=intercept+(slope*x)
  ProcedureReturn regression
EndProcedure

p.f(0,0)=60
p.f(0,1)=3.1
p.f(1,0)=61
p.f(1,1)=3.6
p.f(2,0)=62
p.f(2,1)=3.8
p.f(3,0)=63
p.f(3,1)=4
p.f(4,0)=65
p.f(4,1)=4.1
count=5
Debug calc(64)