POV-Ray : Newsgroups : povray.off-topic : Linear regression Server Time
4 Sep 2024 05:14:19 EDT (-0400)
  Linear regression (Message 11 to 15 of 15)  
<<< Previous 10 Messages Goto Initial 10 Messages
From: Kevin Wampler
Subject: Re: Linear regression
Date: 8 Jun 2010 15:43:15
Message: <4c0e9d53$1@news.povray.org>
Orchid XP v8 wrote:
>>> Any idea how to get a confidence estimate?
>>
>> You could take a look at the section labeled "Confidence Intervals" on 
>> the page that scott linked.
> 
> Yes. Except, as originally explained, I don't understand it.
> 

Considering that you're only looking for an implementation it doesn't 
really matter that you understand what's going on so long as you can 
just type in the equations.  Everything is pretty straightforward except 
for determining t*_(n-2), for which I suggest either finding an existing 
implementation of the inverse cumulative Student's t-distribution 
function or looking up the appropriate value from the table on 
http://en.wikipedia.org/wiki/Student's_t-distribution.

In case you're still having trouble, I whipped up some Python code to do 
what you want.  It's almost pseudocode, so hopefully it'll be pretty 
easy to follow even if you don't know Python.


------

from math import sqrt
from scipy.stats import t as tFcn


def linRegress(X, Y, conf):
	# maximum likelihood estimates
	n = len(X)
	Sx = sum(X)
	Sy = sum(Y)
	Sxx = sum(x*x for x in X)
	Sxy = sum(x*y for x, y in zip(X,Y))
	Syy = sum(y*y for y in Y)
	b = (n*Sxy - Sx*Sy)/(n*Sxx - Sx*Sx)
	a = Sy/n - b*Sx/n

	# confidence interval stuff
	ve = (n*Syy - Sy*Sy - b*b*(n*Sxx - Sx*Sx))/(n*(n-2))
	vb = n*ve / (n*Sxx - Sx*Sx)
	va = vb*Sxx / n

	tstar = tFcn(n-2).isf(1-conf)
	return (a, tstar*sqrt(va)), (b, tstar*sqrt(vb))


if __name__ == "__main__":
	# example data from the Wikipedia page
	X = [1.47, 1.50, 1.52, 1.55, 1.57, 1.60, 1.63, 1.65, 1.68, 1.70, 1.73, 
1.75, 1.78, 1.80, 1.83]
	Y = [52.21, 53.12, 54.48, 55.84, 57.20, 58.57, 59.93, 61.29, 63.11, 
64.47, 66.28, 68.10, 69.92, 72.19, 74.46]

	a, b = linRegress(X,Y,0.975)
	print "intercept:  mean=%f min=%f max=%f" % (a[0], a[0]-a[1], a[0]+a[1])
	print "slope:  mean=%f min=%f max=%f" % (b[0], b[0]-b[1], b[0]+b[1])


Post a reply to this message

From: Invisible
Subject: Re: Linear regression
Date: 9 Jun 2010 05:12:33
Message: <4c0f5b01$1@news.povray.org>
Gilles Tran wrote:

> Here it is. Have fun figuring that out.

Heh. On line 300 it says

   ' Holy cow!

So I'm guessing that's a code comment then. I've also just spent 20 
minutes sorting out all the indentation so that it's actually possible 
to follow the flow control. We'll see if I can deduce anything further...


Post a reply to this message

From: Invisible
Subject: Re: Code regression
Date: 9 Jun 2010 05:27:32
Message: <4c0f5e84$1@news.povray.org>
Invisible wrote:

> We'll see if I can deduce anything further...

Public Function FACTLN(N)
   Dim A(1 To 100) As Double
   Dim I As Integer
   For I = 1 To 100
     A(I) = -100
   Next I
   If N < 0 Then End
   If N <= 99 Then
     If A(N + 1) < 0 Then A(N + 1) = GAMMALN(N + 1)
     FACTLN = A(N + 1)
   Else
     FACTLN = GAMMALN(N + 1)
   End If
End Function

Uh... anybody want to venture a guess is to how this is different from

Public Function FACTLN(N)
   IF N < 0 Then End
   FACTLN = GAMMALN(N + 1)
End Function

 From what I can tell, it creates an array, initialises it with negative 
values, then checks to see if a given cell is negative (which it will 
be, 100% of the time), and if so, calculates a value, puts it into the 
array, and returns it.

It _looks like_ it's trying to cache previously-computed results. Except 
that the array gets recreated each time the function is called. (Unless 
I've missed something.) And even if it doesn't, each call to the 
function zeros out the whole array anyway! WTF?


Post a reply to this message

From: Gilles Tran
Subject: Re: Code regression
Date: 9 Jun 2010 10:54:02
Message: <4c0fab0a$1@news.povray.org>

4c0f5e84$1@news.povray.org...

> It _looks like_ it's trying to cache previously-computed results. Except 
> that the array gets recreated each time the function is called. (Unless 
> I've missed something.) And even if it doesn't, each call to the function 
> zeros out the whole array anyway! WTF?

The source is here:
http://books.google.com/books?id=gn_4mpdN9WkC&pg=PA208
It seems to make sense in Fortran 77 (possibly due to the way it handles 
arrays?), so I just adapted it to VBA without bothering to streamline it. In 
any case it doesn't make much sense in VBA for sure. Perhaps the FACTRL 
function can be simplified as well.
When I wrote that, the whole Fisher part seemed to run on pixie dust anyway 
and I was in a hurry. It worked, so I didn't change anything for fear of 
breaking stuff.

G.


Post a reply to this message

From: Darren New
Subject: Re: Code regression
Date: 9 Jun 2010 12:05:28
Message: <4c0fbbc8$1@news.povray.org>
Gilles Tran wrote:
> It seems to make sense in Fortran 77 (possibly due to the way it handles 
> arrays?),

Due to the statement that says
   data a


That's the fortran equivalent of "static" in C, sort of.

-- 
Darren New, San Diego CA, USA (PST)
    Eiffel - The language that lets you specify exactly
    that the code does what you think it does, even if
    it doesn't do what you wanted.


Post a reply to this message

<<< Previous 10 Messages Goto Initial 10 Messages

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.