Re: Solve, D, and summations
- To: mathgroup at smc.vnet.net
- Subject: [mg64055] Re: Solve, D, and summations
- From: Peter Pein <petsie at dordos.net>
- Date: Sun, 29 Jan 2006 23:10:10 -0500 (EST)
- References: <dri7ji$9u4$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
misha schrieb:
> I am (still) a new user of Mathematica and want to solve for the
> (easiest case) least squares estimators.
>
> As usual, I want to choose b_0 and b_1 to minimize S, where S is the sum
> of squared differences. i.e.,
>
> In [1]:= S = sum{1,...,n}[y_i - b_0 - (b_1)*x_i]^2
>
> Out [1]= sum{1,...,n}[y_i - b_0 - (b_1)*x_i]^2
>
> After defining this function as above, which gave me the expression as I
> expected, I then wrote,
>
> In [2]:= Solve[{D[S, b_0]==0, D[S, b_1]==0}, {b_0, b_1}]
>
> But got
>
> Out [2]= {}
>
> I also tried it this way:
>
> In [3]:= S[b_0, b_1] = sum{1,...,n}[y_i - b_0 - (b_1)*x_i]^2
>
> Out [3]= sum{1,...,n}[y_i - b_0 - (b_1)*x_i]^2
>
> In [4]:= Solve[{D[S, b_0]==0, D[S, b_1]==0}, {b_0, b_1}]
>
> But got
>
> Out [4]= {{}}
>
> Is it the 'n' in the summation that is giving me the problem?
>
> I also tried this:
>
> In [5]:= D[S, b_0]
>
> Out [5]= sum{1,...,n}-2*[y_i - b_0 - (b_1)*x_i]
>
> (as expected)
>
> In [6]:= D[S, b_1]
>
> Out [6]= sum{1,...,n}-2*x_i*[y_i - b_0 - (b_1)*x_i]
>
> After which I copied the returned expressions, set them to zero, and
> used Solve as below:
>
> In [7]:= Solve[{sum{1,...,n}-2*[y_i - b_0 - (b_1)*x_i]
> ==0, sum{1,...,n}-2*x_i*[y_i - b_0 - (b_1)*x_i]==0}, {b_0, b_1}]
>
> But, again, I got
>
> Out [7]= {}
>
> Thank you
>
Hi Misha,
the first I'd try is to use Mathematica syntax. The next is not to use an unassigned n, when solving:
In[1]:=
s = Sum[(y[i] - (Subscript[b, 0] + Subscript[b, 1]*x[i]))^2, {i, 0, n}];
eqns = Thread[D[s, {{Subscript[b, 0], Subscript[b, 1]}}] == 0]
Out[2]=
{Sum[-2*(-Subscript[b, 0] - Subscript[b, 1]*x[i] + y[i]), {i, 0, n}] == 0,
Sum[-2*x[i]*(-Subscript[b, 0] - Subscript[b, 1]*x[i] + y[i]), {i, 0, n}] == 0}
In[3]:=
AbsoluteTiming[sol = {Subscript[b, 0], Subscript[b, 1]} /.
First[Solve[eqns /. n -> 20, {Subscript[b, 0], Subscript[b, 1]}]]; ]
Out[3]=
{0.546875*Second, Null}
In[4]:=
SeedRandom[20060129]; (* just to make results reproducable *)
sol /. {x -> Identity, y -> ((E + (2*Random[] - 1)/10)*#1 - Pi + (2*Random[] - 1)/10 & )}
Out[5]=
{-3.147055160870977, 2.6868147584854687}
Peter