MathGroup Archive 2006

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

Search the Archive

Re: Solve, D, and summations

  • To: mathgroup at smc.vnet.net
  • Subject: [mg64164] Re: Solve, D, and summations
  • From: Paul Abbott <paul at physics.uwa.edu.au>
  • Date: Fri, 3 Feb 2006 01:03:51 -0500 (EST)
  • Organization: The University of Western Australia
  • References: <dri7ji$9u4$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

In article <dri7ji$9u4$1 at smc.vnet.net>, misha <iamisha1 at comcast.net> 
wrote:

> 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?

Not really. The problem is that Sum does not expand the summand or map 
over its arguments, nor do constants get factored out. Clearly, 
Mathematica can solve for the least squares estimators for arbitrary n, 
given the expanded and simplified equations:

  eqns = {
   a n + b Sum[Subscript[x, i], {i, 1, n}] - 
    Sum[Subscript[y, i], {i, 1, n}] == 0,    
   a Sum[Subscript[x, i], {i, 1, n}] + 
   b Sum[Subscript[x, i]^2, {i, 1, n}] -      
   Sum[Subscript[x, i] Subscript[y, i], {i, 1, n}] == 0
  }

  Solve[eqns, {a, b}]

Note also, that the equations can be put in matrix form using

  <<LinearAlgebra`

  LinearEquationsToMatrices[eqns, {a,b}]

Cheers,
Paul

_______________________________________________________________________
Paul Abbott                                      Phone:  61 8 6488 2734
School of Physics, M013                            Fax: +61 8 6488 1014
The University of Western Australia         (CRICOS Provider No 00126G)    
AUSTRALIA                               http://physics.uwa.edu.au/~paul


  • Prev by Date: Re: t-test question
  • Next by Date: Re: t-test question
  • Previous by thread: Re: General--A Minor Annoyance
  • Next by thread: Re: summing a series in mathematica