| Author |
Topic  |
|
|
Peso
Patron Saint of Lost Yaks
Sweden
27383 Posts |
Posted - 01/09/2007 : 08:01:16
|
For a set of data points (x, y), this algorithm can be used to fit the data to any of the following curves:
1. Straight line (linear regresion); y = A + b*x 2. Exponential curve; y = A*EXP(b*x); nb a > 0 3. Logarithmic curve; y = A + b*LN(x) 4. Power curve; y = A*x^b; nb a > 0
The coefficient of determination is R2 (how well does the curve fit)
-- Prepare test data
CREATE TABLE cf
(
x decimal(38, 10),
y decimal(38, 10)
)
-- Calculate Linear regression
INSERT cf
SELECT 40.5, 104.5 UNION ALL
SELECT 38.6, 102 UNION ALL
SELECT 37.9, 100 UNION ALL
SELECT 36.2, 97.5 UNION ALL
SELECT 35.1, 95.5 UNION ALL
SELECT 34.6, 94
SELECT 'Linear regression' AS Type, A, b, R2
FROM dbo.fnCurveFitting(1)
UNION ALL
SELECT 'Bestfit = ' + CAST(Type AS VARCHAR), A, b, R2
FROM dbo.fnBestFit()
-- Calculate Exponential regression
DELETE
FROM cf
INSERT cf
SELECT .72, 2.16 UNION ALL
SELECT 1.31, 1.61 UNION ALL
SELECT 1.95, 1.16 UNION ALL
SELECT 2.58, .85 UNION ALL
SELECT 3.14, .5
SELECT 'Exponential regression' AS Type, A, b, R2
FROM dbo.fnCurveFitting(1)
UNION ALL
SELECT 'Bestfit = ' + CAST(Type AS VARCHAR), A, b, R2
FROM dbo.fnBestFit()
-- Calculate Logarithmic regression
DELETE
FROM cf
INSERT cf
SELECT 3, 1.5 UNION ALL
SELECT 4, 9.3 UNION ALL
SELECT 6, 23.4 UNION ALL
SELECT 10, 45.8 UNION ALL
SELECT 12, 60.1
SELECT 'Logarithmic regression' AS Type, A, b, R2
FROM dbo.fnCurveFitting(1)
UNION ALL
SELECT 'Bestfit = ' + CAST(Type AS VARCHAR), A, b, R2
FROM dbo.fnBestFit()
-- Calculate Power regression
DELETE
FROM cf
INSERT cf
SELECT 10, .95 UNION ALL
SELECT 12, 1.05 UNION ALL
SELECT 15, 1.25 UNION ALL
SELECT 17, 1.41 UNION ALL
SELECT 20, 1.73 UNION ALL
SELECT 22, 2 UNION ALL
SELECT 25, 2.53 UNION ALL
SELECT 27, 2.98 UNION ALL
SELECT 30, 3.85 UNION ALL
SELECT 32, 4.59 UNION ALL
SELECT 35, 6.02
SELECT 'Power regression' AS Type, A, b, R2
FROM dbo.fnCurveFitting(1)
UNION ALL
SELECT 'Bestfit = ' + CAST(Type AS VARCHAR), A, b, R2
FROM dbo.fnBestFit()
DROP TABLE cf Peter Larsson Helsingborg, Sweden |
Edited by - Peso on 01/10/2007 05:00:31
|
|
|
Peso
Patron Saint of Lost Yaks
Sweden
27383 Posts |
Posted - 01/09/2007 : 08:02:03
|
Here are the functions. When using a type that is not valid, the function defaults to linear regression.CREATE FUNCTION dbo.fnCurveFitting
(
@Type TINYINT
)
RETURNS @p TABLE (A DECIMAL(38, 10), b DECIMAL(38, 10), R2 DECIMAL(38, 10))
AS
/*
Type = 1 Linear y = a + b*x
Type = 2 Exponential y = a*e^(b*x) nb a > 0
Type = 3 Logarithmic y = a + b*ln(x)
Type = 4 Power y = a*x^b nb a > 0
*/
BEGIN
DECLARE @n DECIMAL(38, 10),
@x DECIMAL(38, 10),
@x2 DECIMAL(38, 10),
@y DECIMAL(38, 10),
@xy DECIMAL(38, 10),
@y2 DECIMAL(38, 10),
@d DECIMAL(38, 10),
@a DECIMAL(38, 10),
@b DECIMAL(38, 10),
@r2 DECIMAL(38, 10)
SELECT @n = COUNT(*),
@x = CASE
WHEN @Type = 2 THEN SUM(x)
WHEN @Type = 3 THEN SUM(LOG(x))
WHEN @Type = 4 THEN SUM(LOG(x))
ELSE SUM(x)
END,
@x2 = CASE
WHEN @Type = 2 THEN SUM(x * x)
WHEN @Type = 3 THEN SUM(LOG(x) * LOG(x))
WHEN @Type = 4 THEN SUM(LOG(x) * LOG(x))
ELSE SUM(x * x)
END,
@y = CASE
WHEN @Type = 2 THEN SUM(LOG(y))
WHEN @Type = 3 THEN SUM(y)
WHEN @Type = 4 THEN SUM(LOG(y))
ELSE SUM(y)
END,
@xy = CASE
WHEN @Type = 2 THEN SUM(x * LOG(y))
WHEN @Type = 3 THEN SUM(LOG(x) * y)
WHEN @Type = 4 THEN SUM(LOG(x) * LOG(y))
ELSE SUM(x * y)
END,
@y2 = CASE
WHEN @Type = 2 THEN SUM(LOG(y) * LOG(y))
WHEN @Type = 3 THEN SUM(y * y)
WHEN @Type = 4 THEN SUM(LOG(y) * LOG(y))
ELSE SUM(y * y)
END,
@d = @n * @x2 - @x * @x
FROM cf
IF @d = 0
RETURN
SELECT @a = (@x2 * @y - @x * @xy) / @d,
@b = (@n * @xy - @x * @y) / @d,
@r2 = (@a * @y + @b * @xy - @y * @y / @n) / (@y2 - @y * @y / @n)
INSERT @p
SELECT CASE
WHEN @Type = 2 THEN EXP(@a)
WHEN @Type = 3 THEN @a
WHEN @Type = 4 THEN EXP(@a)
ELSE @a
END,
@b,
@r2
RETURN
END
CREATE FUNCTION dbo.fnBestFit
(
)
RETURNS @p TABLE (Type TINYINT, A DECIMAL(38, 10), b DECIMAL(38, 10), R2 DECIMAL(38, 10))
AS
BEGIN
INSERT @p
SELECT 1,
A,
b,
R2
FROM dbo.fnCurveFitting(1)
INSERT @p
SELECT 2,
A,
b,
R2
FROM dbo.fnCurveFitting(2)
INSERT @p
SELECT 3,
A,
b,
R2
FROM dbo.fnCurveFitting(3)
INSERT @p
SELECT 4,
A,
b,
R2
FROM dbo.fnCurveFitting(4)
DELETE
FROM @p
WHERE R2 <> (SELECT MAX(R2) FROM @p)
RETURN
END
Peter Larsson Helsingborg, Sweden |
Edited by - Peso on 01/09/2007 08:44:09 |
 |
|
|
Peso
Patron Saint of Lost Yaks
Sweden
27383 Posts |
Posted - 01/09/2007 : 08:02:51
|
Harsh, it seems that doing this SQL wise would be faster than doing it in front-end, since SQL Server does this set-based.
Peter Larsson Helsingborg, Sweden |
 |
|
|
harsh_athalye
Flowing Fount of Yak Knowledge
India
5467 Posts |
Posted - 01/09/2007 : 08:08:07
|
Is it? Oh wow! That's a wonderful observation then.
I think you are right, if you know the correct way to implement the solution, the language hardly matters.
Harsh Athalye India. "The IMPOSSIBLE is often UNTRIED" |
 |
|
|
spirit1
Cybernetic Yak Master
Slovenia
11645 Posts |
Posted - 01/09/2007 : 08:33:13
|
running those log running queries again, are we peter? Boredom on the horizon? Too much free time? 
  
Go with the flow & have fun! Else fight the flow  blog thingie: http://weblogs.sqlteam.com/mladenp |
 |
|
|
Peso
Patron Saint of Lost Yaks
Sweden
27383 Posts |
Posted - 01/09/2007 : 08:42:45
|
Free time? When writing algorithms here? No no... 
Peter Larsson Helsingborg, Sweden |
 |
|
|
daydreamer82
Starting Member
3 Posts |
Posted - 04/11/2007 : 16:46:38
|
hello,
This algorithm is providential for me. Im a student experimenting path discovering on Internet. I succeeded to put some traceroute data into a mysql database. And now, i tried to find a way to find shortests paths...
Thanks a lot ;)
PS. If you have the mysql version ... ;)
|
 |
|
|
Peso
Patron Saint of Lost Yaks
Sweden
27383 Posts |
|
|
daydreamer82
Starting Member
3 Posts |
Posted - 04/11/2007 : 17:00:36
|
Ho yes, you are right. I lost myself during the registration ;)
Soo sorry. Admin? hem... |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
USA
6298 Posts |
Posted - 04/11/2007 : 17:05:39
|
Peter,
Why stop half way?
Shouldn't your script insert the data into an Excel spreadsheet, and create a graph with the proper trend line too?
CODO ERGO SUM |
 |
|
|
Peso
Patron Saint of Lost Yaks
Sweden
27383 Posts |
Posted - 04/11/2007 : 17:07:53
|
I love you too, Michael 
Peter Larsson Helsingborg, Sweden |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
USA
6298 Posts |
Posted - 04/11/2007 : 17:30:10
|
Sometimes, I just have to let out my inner twit.
CODO ERGO SUM |
 |
|
|
blindman
Flowing Fount of Yak Knowledge
USA
2267 Posts |
Posted - 04/11/2007 : 19:05:52
|
To keep the outer one company?  e4 d5 xd5 Nf6 |
Edited by - blindman on 04/11/2007 19:07:10 |
 |
|
| |
Topic  |
|
|
|