MathGroup Archive 2006

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

Search the Archive

Re: Solve, D, and summations

  • To: mathgroup at smc.vnet.net
  • Subject: [mg64059] Re: Solve, D, and summations
  • From: Bill Rowe <readnewsciv at earthlink.net>
  • Date: Sun, 29 Jan 2006 23:10:13 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

On 1/29/06 at 5:57 AM, iamisha1 at comcast.net (misha) wrote:

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

Whenever you get this kind of result, i.e., Mathematica returns the same expression you input, it is happening because Mathematica doesn't know how to evaluate your input. The syntax you are using probably resembles something else you have used but is not correct Mathematica syntax. So, the results from:

>I then wrote,

>In [2]:= Solve[{D[S, b_0]==0, D[S, b_1]==0}, {b_0, b_1}]

>But got

>Out [2]= {}

Shouldn't be unexpected.

You are definitely trying to do things the hard way.

The simplest way to do a least squares regression would be to use FindFit and not worry about the details of how the error terms are summed. For example,

First generate some data

In[1]:=
data = Table[{n, 20*n + 5 + Random[]}, {n, 1, 5}];

then use FindFit

In[2]:=
FindFit[data, m*x + b, {m, b}, x]
Out[2]=
{m -> 20.156649576345245, b -> 5.085224815678416}

But if you want to "roll your own" for some reason. the fthe sum of the errors squared can be computed as:

In[18]:=
ssx = Total[(m*First[#1] + b - Last[#1])^2&/@data];

and the desired coefficients can be computed by:

In[19]:=
Minimize[ssx, {b, m}]
Out[19]=
{0.26546327330229563, 
  {b -> 5.085224815678411, m -> 20.15664957634526}}

And there are many other ways to get the same result in Mathematica.
--
To reply via email subtract one hundred and four


  • Prev by Date: Re: Annoying Maximize behaviour
  • Next by Date: Re: Solve, D, and summations
  • Previous by thread: Solve, D, and summations
  • Next by thread: Re: Solve, D, and summations