Re: More troubles coming from indexed variables
- To: mathgroup at smc.vnet.net
- Subject: [mg25092] Re: [mg25000] More troubles coming from indexed variables
- From: Tomas Garza <tgarza01 at prodigy.net.mx>
- Date: Fri, 8 Sep 2000 03:00:40 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
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:
In[1]:=
n = 10;
In[2]:=
ones = Table[1, {10}]
Out[2]=
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
This is where you define the function you want to minimize:
In[3]:=
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:
In[4]:=
e = Solve[{D[Q[a, b, n], a] == 0, D[Q[a, b, n], b] == 0}, {a,
b}]
(I omit the output, but you may try it: it works fine, in all
generality,
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
the
least squares estimators of a and b, in a relatively neat form, which
you
could further reduce if you wished to. And, of course, from this result
for
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 yahoo.it] wrote:
<Snip>
> 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.
>
<Snip>