MathGroup Archive 2000

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: More troubles coming from indexed variables

I hope it's not too late to suggest an alternative treatment to the 
problem you pose.
I take your least squares example. Sometime ago I was asked a similar 
question, and I proposed the following approach.

I wil use Array[y, n] to denote your y values, and Array[x, n] to denote 
your x

 values. "ones" is a vector of 1's, and "." means Dot (dot product):

 First, assign a value to n:


 n = 10;


 ones = Table[1, {10}]


 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}

 This is where you define the function you want to minimize:


 Q[v_, w_, k_] := (Array[y, k] - v*ones - w*Array[x, k]).(Array[y, k] 
- v*ones -

 w*Array[x, k])

 and now you ask for the solution of the two equations:


 e = Solve[{D[Q[a, b, n], a] == 0, D[Q[a, b, n], b] == 0}, {a, 

 (I omit the output, but you may try it: it works fine, in all 

 and it requires only that n be previously specified, as above)

 The output looks a bit messy, but you can reduce it by using 
ReplaceRepeated on Simplify[e], giving adequate transformation rules for 
each of the forms you obtain, like sums, sums of squares and sums of 
cross products (these will appear clearly in the output Out[5]):

 In[5]:= Simplify[e] //. {Plus @@ Array[y, k] -> Sy, Plus @@ Array[x, 
k] -> Sx,

   Plus @@ (Array[x, k]^2) -> Sx2, Array[x, k].Array[y, k] -> Sxy}


Again, I omit the output, but it certainly gives the right answer for 

 least squares estimators of a and b, in a relatively neat form, which 

 could further reduce if you wished to. And, of course, from this result 

 a particular value of n you might be able to infer the general formula.

 I wonder if this is what you were looking for.

Tomas Garza

Mexico City

Barbara DaVinci [barbara_79_f at] wrote:

> I'm writing an educational notebook

> on least-square fit and in a subsection I must show

> how the well-known

> formula is carried out.


> This provides a symbolic data set :

> datsimx = Table[x[k], {k, 1, 10}]

> datsimy = datsimx /. x -> y


> {x[1], x[2], x[3], x[4], x[5], x[6], x[7], x[8], x[9],

> x[10]}

> {y[1], y[2], y[3], y[4], y[5], y[6], y[7], y[8], y[9],

> y[10]}


> Now I must declare x[1] ... x[10] and y[1] ... y[10]

> to be costants

> (otherwise D[] and Dt[] ...) .


> I tried two ways, both ineffective.



  • Prev by Date: Re: How to keep things real?
  • Next by Date: Re: Re: Simple integral wrong
  • Previous by thread: Re: More troubles coming from indexed variables
  • Next by thread: Re: Functional or rules-based equivalent for procedural program