Complex Calculation

Complex Calculation

am 31.03.2008 22:00:52 von RoRo

>I'm fairly new to SQL server 2005
> database. The client wants to use weighted parameters(5) or areas
> of a project, rated from 1-5 on a scale of 1 being the highest, and
> the Cost of each project.
> i.e. Project A Param1=1, Param2=4 , Param3=1, Param4=2, Param5=5 , Cost= $50000
>Using the least squares fit they want to be able to calculate a
>projected cost for a project with same weighted parameters.
> With this being said I was wondering what I could use to do this using the information from the SQL Server. >Example:

> Currently this is a formula used for calculating the least square's fit using a matrixs
> Each Row of completed Project Cost for a completed project
> ex. Matrix A = 1, u1, v1, w1, x1 Matrix b = M1
> 1, u2, v2, w2, x2 M2
> 1, u3, v3, w3, x3 M3
> 1, un, vn, wn, xn Mn


> where u,v,x are weighted parameters where M is a a project


> x=(A^T * A)^-1 * (A^T * b) x is the function that defines the least
> squares curve for the data set

Re: Complex Calculation

am 31.03.2008 23:23:07 von Erland Sommarskog

RoRo (RonaldaLajuan@gmail.com) writes:
>>I'm fairly new to SQL server 2005
>> database. The client wants to use weighted parameters(5) or areas of a
>> project, rated from 1-5 on a scale of 1 being the highest, and the Cost
>> of each project. i.e. Project A Param1=1, Param2=4 , Param3=1,
>> Param4=2, Param5=5 , Cost= $50000 Using the least squares fit they want
>> to be able to calculate a projected cost for a project with same
>> weighted parameters. With this being said I was wondering what I could
>> use to do this using the information from the SQL Server.
>> Example:
>
>> Currently this is a formula used for calculating the least square's fit
>> using a matrixs
>> Each Row of completed Project Cost for a
>> completed project ex. Matrix A = 1, u1, v1, w1, x1 Matrix b = M1
>> 1, u2, v2, w2, x2 M2
>> 1, u3, v3, w3, x3 M3
>> 1, un, vn, wn, xn Mn
>>
>>
>> where u,v,x are weighted parameters where M is a a
>> project
>>
>>
>> x=(A^T * A)^-1 * (A^T * b) x is the function that defines the least
>> squares curve for the data set

It was looooong ago I did least squares, so I'm fairly rusty on it. And
I have some difficulties to understand your matrixes.

The usual recommendation is that you post:
o CREATE TABLE statements for your tables.
o INSERT statements with sample data.
o The desired result given the sample.




--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online
for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downlo ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books .mspx

Re: Complex Calculation

am 01.04.2008 01:43:57 von RoRo

Just let me warn you this is very complex and I'm sorry If I don't
explain enough detail but if anyone could help I would greatly
appreciate it.
This is the Table that is created in my database::

The Insert Statement for the data is very complex b/c I'm getting the
data from radio buttons in my webform...
Just know it works and the information is store like this for a
Metric(Project with the ID of 26).


MetricParameters:::

ParamID MetricID ParamValue Description
4 26 1 rstk
2 26 5 sre6dtk
6 26 1 seu
3 26 2 setj
5 26 2
1 26 5 sdth
7 26 4
12 26 2
13 26 4
9 26 1 sedhj
8 26 4
10 26 4
11 26 2
14 26 2
16 26 3
15 26 2
18 26 1 sedrhe
17 26 3

Of course I don't really need the description of the projects but the
ParamValue(Column) is always a weighted number between 1-5, the
ParamID is the ID number given to all of the Parameter for an Item
Type. i.e Item Antenna has 18 weighted Parameters. With a ProjectID or
MetricID of 26.

The cost for the Metric 26 is also put into another table called
MetricCost.

MetricsCost::::
metricID
PrjID ............................................................ ...........
ActualHours(Cost in Hours)

26
15
1589

So with that I need a table that ends up like this:

Project Name Weighed Parameters Actual
Cost
Sys Para Elect ME I&T Cost in Hours
M1 1 1 1 1 500
M2 1 2 1 1 870
M3 2 2 1 1 1000
M4 2 2 1 2 1750
M5 3 3 2 1 2000
M6 3 3 3 1 2500
M7 4 4 3 2 5000
M8 4 4 3 3 6200
M9 4 5 5 5 15000
M10 5 5 5 5 150050

And with this I take a New Matrix we will say EstimatedMatrix with the
same # parameter and try to calculate the cost for the new Project
based on other like it in the database.

New Project Weights Calculated Cost in Hours
1 1 1 1 494
1 3 1 1 1131
1 1 2 2 4306
2 2 1 2 2336
2 2 2 2 3971
2 3 3 2 5925
3 3 3 4 9626
3 4 3 4 9945
4 4 3 4 9292
4 5 5 5 15057
5 5 5 5 14404

The formula above for this calculation was given to me by the client.
I'm just not sure how to go about tackling this complex
calculation....



Each Row is a Metric for a completed
project Cost for a completed project


A= 1, u1, v12, ...,
x1m b= M1

1, u2, v22, ...,
x2m M2
...
1, un vn2 ...
xnm M3
where M is a project

where u,v, ...x are weighted parameters


x = (A^T*A)^ -1 * A^T*b T==> transpose -1==> inverse matrix

x is function that defines the least squares curve for the data set

Cost of new bid = V^T*x T==>transpose

Weighted Parameters of New Bid

V = (1, ubid, vbid2, ..., xbidm )



I'm not asking for anyone to do this for me, I just have no idea where
to start. Can I write a stored procedure or function? How do I get
this information stored the way I need it to be stored for
calculations. It should also be known that I'm using visual web
developer b/c the client wants this posted on their intranet system.










On Mar 31, 4:23 pm, Erland Sommarskog wrote:
> RoRo (RonaldaLaj...@gmail.com) writes:
> >>I'm fairly new to SQL server 2005
> >> database. The client wants to use weighted parameters(5) or areas of a
> >> project, rated from 1-5 on a scale of 1 being the highest, and the Cost
> >> of each project. i.e. Project A Param1=1, Param2=4 , Param3=1,
> >> Param4=2, Param5=5 , Cost= $50000 Using the least squares fit they want
> >> to be able to calculate a projected cost for a project with same
> >> weighted parameters. With this being said I was wondering what I could
> >> use to do this using the information from the SQL Server.
> >> Example:
>
> >> Currently this is a formula used for calculating the least square's fit
> >> using a matrixs
> >> Each Row of completed Project Cost for a
> >> completed project ex. Matrix A = 1, u1, v1, w1, x1 Matrix b = M1
> >> 1, u2, v2, w2, x2 M2
> >> 1, u3, v3, w3, x3 M3
> >> 1, un, vn, wn, xn Mn
>
> >> where u,v,x are weighted parameters where M is a a
> >> project
>
> >> x=(A^T * A)^-1 * (A^T * b) x is the function that defines the least
> >> squares curve for the data set
>
> It was looooong ago I did least squares, so I'm fairly rusty on it. And
> I have some difficulties to understand your matrixes.
>
> The usual recommendation is that you post:
> o CREATE TABLE statements for your tables.
> o INSERT statements with sample data.
> o The desired result given the sample.
>
> --
> Erland Sommarskog, SQL Server MVP, esq...@sommarskog.se
>
> Books Online
> for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/down loads/books...
> Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/boo ks.mspx- Hide quoted text -
>
> - Show quoted text -

Re: Complex Calculation

am 01.04.2008 01:46:26 von RoRo

On Mar 31, 6:43=A0pm, RoRo wrote:

Sorry google messed up my formating on the tables.

> Just let me warn you this is very complex and I'm sorry If I don't
> explain enough detail but if anyone could help I would greatly
> appreciate it.
> This is the Table that is created in my database::
>
> The Insert Statement for the data is very complex b/c I'm getting the
> data from radio buttons in my webform...
> Just know it works and the information is store like this for a
> Metric(Project with the ID of 26).
>
> MetricParameters:::
>
> ParamID =A0 =A0 =A0MetricID =A0 =A0ParamValue =A0 =A0Description
> 4 =A0 =A0 =A0 26 =A0 =A0 =A01 =A0 =A0 =A0 rstk
> 2 =A0 =A0 =A0 26 =A0 =A0 =A05 =A0 =A0 =A0 sre6dtk
> 6 =A0 =A0 =A0 26 =A0 =A0 =A01 =A0 =A0 =A0 seu
> 3 =A0 =A0 =A0 26 =A0 =A0 =A02 =A0 =A0 =A0 setj
> 5 =A0 =A0 =A0 26 =A0 =A0 =A02
> 1 =A0 =A0 =A0 26 =A0 =A0 =A05 =A0 =A0 =A0 sdth
> 7 =A0 =A0 =A0 26 =A0 =A0 =A04
> 12 =A0 =A0 =A026 =A0 =A0 =A02
> 13 =A0 =A0 =A026 =A0 =A0 =A04
> 9 =A0 =A0 =A0 26 =A0 =A0 =A01 =A0 =A0 =A0 sedhj
> 8 =A0 =A0 =A0 26 =A0 =A0 =A04
> 10 =A0 =A0 =A026 =A0 =A0 =A04
> 11 =A0 =A0 =A026 =A0 =A0 =A02
> 14 =A0 =A0 =A026 =A0 =A0 =A02
> 16 =A0 =A0 =A026 =A0 =A0 =A03
> 15 =A0 =A0 =A026 =A0 =A0 =A02
> 18 =A0 =A0 =A026 =A0 =A0 =A01 =A0 =A0 =A0 sedrhe
> 17 =A0 =A0 =A026 =A0 =A0 =A03
>
> Of course I don't really need the description of the projects but the
> ParamValue(Column) is always a weighted number between 1-5, the
> ParamID is the ID number given to all of the Parameter for an Item
> Type. i.e Item Antenna has 18 weighted Parameters. With a ProjectID or
> MetricID of 26.
>
> The cost for the Metric 26 is also put into another table called
> MetricCost.
>
> MetricsCost:::: metricID PrjID =A0 =A0..................................=
................... ActualHours(Cost in Hours)
>
> 26 15 =
1589
>
> So with that I need a table that ends up like this:
>
> Project Name =A0 =A0Weighed Parameters =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
=A0 =A0 =A0 =A0 =A0 =A0Actual
> Cost
> =A0 =A0 =A0 =A0 Sys Para =A0 =A0 =A0 =A0Elect =A0 ME =A0 =A0 =A0I&T =A0 =
=A0 =A0 =A0 Cost in Hours
> M1 =A0 =A0 =A01 =A0 =A0 =A0 1 =A0 =A0 =A0 1 =A0 =A0 =A0 1 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 500
> M2 =A0 =A0 =A01 =A0 =A0 =A0 2 =A0 =A0 =A0 1 =A0 =A0 =A0 1 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 870
> M3 =A0 =A0 =A02 =A0 =A0 =A0 2 =A0 =A0 =A0 1 =A0 =A0 =A0 1 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 1000
> M4 =A0 =A0 =A02 =A0 =A0 =A0 2 =A0 =A0 =A0 1 =A0 =A0 =A0 2 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 1750
> M5 =A0 =A0 =A03 =A0 =A0 =A0 3 =A0 =A0 =A0 2 =A0 =A0 =A0 1 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 2000
> M6 =A0 =A0 =A03 =A0 =A0 =A0 3 =A0 =A0 =A0 3 =A0 =A0 =A0 1 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 2500
> M7 =A0 =A0 =A04 =A0 =A0 =A0 4 =A0 =A0 =A0 3 =A0 =A0 =A0 2 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 5000
> M8 =A0 =A0 =A04 =A0 =A0 =A0 4 =A0 =A0 =A0 3 =A0 =A0 =A0 3 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 6200
> M9 =A0 =A0 =A04 =A0 =A0 =A0 5 =A0 =A0 =A0 5 =A0 =A0 =A0 5 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 15000
> M10 =A0 =A0 5 =A0 =A0 =A0 5 =A0 =A0 =A0 5 =A0 =A0 =A0 5 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 150050
>
> And with this I take a New Matrix we will say EstimatedMatrix with the
> same # parameter and try to calculate the cost for the new Project
> based on other like it in the database.
>
> New Project Weights =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Ca=
lculated Cost in Hours
> 1 =A0 =A0 =A0 1 =A0 =A0 =A0 1 =A0 =A0 =A0 1 =A0 =A0 =A0 494
> 1 =A0 =A0 =A0 3 =A0 =A0 =A0 1 =A0 =A0 =A0 1 =A0 =A0 =A0 1131
> 1 =A0 =A0 =A0 1 =A0 =A0 =A0 2 =A0 =A0 =A0 2 =A0 =A0 =A0 4306
> 2 =A0 =A0 =A0 2 =A0 =A0 =A0 1 =A0 =A0 =A0 2 =A0 =A0 =A0 2336
> 2 =A0 =A0 =A0 2 =A0 =A0 =A0 2 =A0 =A0 =A0 2 =A0 =A0 =A0 3971
> 2 =A0 =A0 =A0 3 =A0 =A0 =A0 3 =A0 =A0 =A0 2 =A0 =A0 =A0 5925
> 3 =A0 =A0 =A0 3 =A0 =A0 =A0 3 =A0 =A0 =A0 4 =A0 =A0 =A0 9626
> 3 =A0 =A0 =A0 4 =A0 =A0 =A0 3 =A0 =A0 =A0 4 =A0 =A0 =A0 9945
> 4 =A0 =A0 =A0 4 =A0 =A0 =A0 3 =A0 =A0 =A0 4 =A0 =A0 =A0 9292
> 4 =A0 =A0 =A0 5 =A0 =A0 =A0 5 =A0 =A0 =A0 5 =A0 =A0 =A0 15057
> 5 =A0 =A0 =A0 5 =A0 =A0 =A0 5 =A0 =A0 =A0 5 =A0 =A0 =A0 14404
>
> The formula above for this calculation was given to me by the client.
> I'm just not sure how to go about tackling this complex
> calculation....
>
> Each Row is a Metric for a completed
> project =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Cost fo=
r a completed project
>
> A=3D 1, u1, v12, =A0..., x1m =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0b=3D =
=A0M1
>
> =A0 =A0 =A01, u2, v22, =A0..., x2m =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
=A0 =A0 =A0M2
> =A0 =A0 =A0 ...
> =A0 =A0 =A01, =A0un =A0vn2 =A0..., xnm =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
=A0 =A0 =A0 =A0 M3
> where M is a project
>
> =A0where u,v, ...x are weighted parameters
>
> x =3D (A^T*A)^ -1 * A^T*b =A0 =A0 =A0 =A0 =A0T==> transpose =A0-1=3D=
=3D> inverse matrix
>
> x is function that defines the least squares curve for the data set
>
> Cost of new bid =3D V^T*x =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0T==>transpose
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0Weighted Parameters of New Bid
>
> =A0 =A0 =A0 =A0 V =3D (1, ubid, vbid2, =A0..., xbidm )
>
> I'm not asking for anyone to do this for me, I just have no idea where
> to start. =A0Can I write a stored procedure or function? How do I get
> this information stored the way I need it to be stored for
> calculations. It should also be known that I'm using visual web
> developer b/c the client wants this posted on their intranet system.
>
> On Mar 31, 4:23 pm, Erland Sommarskog wrote:
>
>
>
> > RoRo (RonaldaLaj...@gmail.com) writes:
> > >>I'm fairly new to =A0SQL server 2005
> > >> database. The client wants to use =A0weighted parameters(5) or areas =
of a
> > >> project, rated from 1-5 on a scale of 1 being the highest, and the Co=
st
> > >> of each project. i.e. Project A =A0 Param1=3D1, Param2=3D4 , Param3=
=3D1,
> > >> Param4=3D2, Param5=3D5 , Cost=3D $50000 Using the least squares fit t=
hey want
> > >> to be able to calculate a projected cost for a project =A0with same
> > >> weighted parameters. With this being said I was wondering what I coul=
d
> > >> use to do this using the information from the SQL Server.
> > >> Example:
>
> > >> Currently this is a formula used for calculating the least square's f=
it
> > >> using a matrixs
> > >> Each Row of completed Project =A0 =A0 =A0 =A0 =A0 =A0 =A0Cost for a
> > >> completed project ex. Matrix A =3D =A01, u1, v1, w1, x1 =A0 =A0 =A0Ma=
trix b =3D M1
> > >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A01, u2, v2, w2, x2 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 M2
> > >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A01, u3, v3, w3, x3 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 M3
> > >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A01, un, vn, wn, xn =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Mn
>
> > >> where u,v,x are weighted parameters =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0wh=
ere M is a a
> > >> project
>
> > >> x=3D(A^T * A)^-1 * (A^T * b) x is the function that defines the least=

> > >> squares curve for the data set
>
> > It was looooong ago I did least squares, so I'm fairly rusty on it. And
> > I have some difficulties to understand your matrixes.
>
> > The usual recommendation is that you post:
> > o =A0 CREATE TABLE statements for your tables.
> > o =A0 INSERT statements with sample data.
> > o =A0 The desired result given the sample.
>
> > --
> > Erland Sommarskog, SQL Server MVP, esq...@sommarskog.se
>
> > Books Online
> > for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2=
005/downloads/books...
> > Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo=
/previousversions/books.mspx-Hide quoted text -
>
> > - Show quoted text -- Hide quoted text -
>
> - Show quoted text -

Re: Complex Calculation

am 01.04.2008 23:24:24 von Erland Sommarskog

RoRo (RonaldaLajuan@gmail.com) writes:
> Just let me warn you this is very complex and I'm sorry If I don't
> explain enough detail but if anyone could help I would greatly
> appreciate it.
> This is the Table that is created in my database::
>
> The Insert Statement for the data is very complex b/c I'm getting the
> data from radio buttons in my webform...

Writing the INSERT statements for the few rows you posted is not
very complex. Possibly a little tedious.

Anyway, I am not sure how it could have helped.

> Just know it works and the information is store like this for a
> Metric(Project with the ID of 26).
>
>
> MetricParameters:::
>
> ParamID MetricID ParamValue Description
> 4 26 1 rstk
> 2 26 5 sre6dtk
> 6 26 1 seu
> 3 26 2 setj
>...

> MetricsCost::::
> metricID
> PrjID
............................................................ ............
> ActualHours(Cost in Hours)
>
> 26
> 15
> 1589
>
> So with that I need a table that ends up like this:
>
> Project Name Weighed Parameters Actual
> Cost
> Sys Para Elect ME I&T Cost in Hours
> M1 1 1 1 1 500

As I cannot we how you get from the above to this table. I understand
that ParamValue above are weights, but the weights has to be applied
to something.

> x = (A^T*A)^ -1 * A^T*b T==> transpose -1==> inverse matrix

Hm, transposing matrix is not really something you want to do in
SQL. Rows and columns are quite different things.

Maybe you should read the data into client level and work there? (Or
in a CLR stored procedure.)

But I think I have even less understanding of what is going on, so I
may be completely off-base.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downlo ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books .mspx