next up previous index
Next: Fitting, the Lessons Up: Fitting Previous: Fitting in Maple

$\chi ^2$ Fitting in Mathematica

We begin by creating arrays xi, yi and $\sigma _i$: 

\begin{figure}
{\footnotesize
\begin{tex2html_preform}\begin{verbatim}In[1]:= Ar...
... x[299],
> x[300]}In[2]:=\end{verbatim}\end{tex2html_preform}}
\end{figure}

The syntax of the Mathematica command Array is, in this context, identical to the syntax of Maxima's command array. The only difference is that whereas the array xi created by Maxima would have entries from x0 through x300, in Mathematica the numbering goes from x1 through x300. Unfortunately we also get a screenful of gibberish after the array has been successfully allocated. Recall that in Maxima we could terminate a command with a dollar sign instead of a semicolon, and in this way tell Maxima to shut up. In Mathematica we accomplish the same task by terminating Mathematica's command with a semicolon, instead of just a newline. 

And so, we can now safely proceed to allocate arrays yi and $\sigma _i$:

\begin{figure}
{\footnotesize
\begin{tex2html_preform}\begin{verbatim}In[2]:= Ar...
...]:= Array[sigma,300];In[4]:=\end{verbatim}\end{tex2html_preform}}
\end{figure}

Now let us proceed with the definition of $\chi^2 = \sum_{i=1}^N \left(y_i - a - b x_i\right)^2/\sigma_i^2$, and its derivatives with respect to a and b:

\begin{figure}
{\footnotesize
\begin{tex2html_preform}\begin{verbatim}In[4]:= ch...
... := D[chi2[a, b], b]In[10]:=\end{verbatim}\end{tex2html_preform}}
\end{figure}

Now we are in a position, finally, to formulate our equations and request their solutions:
\begin{figure}
{\footnotesize
\begin{tex2html_preform}\begin{verbatim}In[13]:= S...
..., b}]Out[13]= {{}}In[14]:=\end{verbatim}\end{tex2html_preform}}
\end{figure}

$\ldots$ and, much to our disappointment, we get a similar answer to the one we got from Maxima. Mathematica returned an empty list, because it doesn't know how to solve these equations. And the reason for the latter is as follows:
\begin{figure}
{\footnotesize
\begin{tex2html_preform}\begin{verbatim}In[15]:= E...
...{i, 1, n}]
sigma[i]In[17]:=\end{verbatim}\end{tex2html_preform}}
\end{figure}

The derivatives inside the sums remain unexpanded in the output, even though Mathematica knows how to do them. This can be easily seen by using Mathematica's equivalent of Maxima's pickapart. All Mathematica expressions are stored as lists, much as in Lisp. But they are printed on Mathematica's output in a way, to which Mathematica users are more accustomed.  You can ask Mathematica to print an expression the way it is stored by invoking a command FullForm:
\begin{figure}
{\footnotesize
\begin{tex2html_preform}\begin{verbatim}In[22]:= F...
..., b], List[i, 1, n]]In[23]:=\end{verbatim}\end{tex2html_preform}}
\end{figure}

You can get at individual elements of the list as follows: 
\begin{figure}
{\footnotesize
\begin{tex2html_preform}\begin{verbatim}In[27]:= d...
...Out[29]= {i, 1, n}In[30]:=\end{verbatim}\end{tex2html_preform}}
\end{figure}

Observe that the derivative has been automatically evaluated when we have asked Mathematica to print part 1 of dchi2b[a,b], but that part is not stored in an evaluated form. We can easily create a new expression, in which the derivative is fully evaluated by calling the ReplacePart command: 
\begin{figure}
{\footnotesize
\begin{tex2html_preform}\begin{verbatim}In[32]:= R...
... 1, n}]
2
sigma[i]In[33]:=\end{verbatim}\end{tex2html_preform}}
\end{figure}

The command ReplacePart takes an expression as its first argument. The number of the expression part to be replaced is given in the third argument. The second argument contains the replacement. In our case the replacement is the expression, which Mathematica labelled as Out[32]. Recall that we had the same facility available to us in Maxima: all inputs and outputs were automatically labelled, and could be reused while conversing with Maxima.

We can now repeat this procedure for dchi2a[a,b]:

\begin{figure}
{\footnotesize
\begin{tex2html_preform}\begin{verbatim}In[34]:= d...
... 1, n}]
2
sigma[i]In[36]:=\end{verbatim}\end{tex2html_preform}}
\end{figure}

Let us attempt a solution now:
\begin{figure}
{\footnotesize
\begin{tex2html_preform}\begin{verbatim}In[42]:= S...
...----------
2
> }}In[43]:=\end{verbatim}\end{tex2html_preform}}
\end{figure}

Well, we've moved forward just a little, but Mathematica's Solve still can't cope with the equations. As in Maxima we need to disentangle a and b explicitly from the sum, so that Mathematica's Solve would notice that the resulting equations are really quite simple and linear in a and b.

Let us begin by expanding the fractions in the sums:

\begin{figure}
{\footnotesize
\begin{tex2html_preform}\begin{verbatim}In[54]:= O...
...i] sigma[i] sigma[i]In[58]:=\end{verbatim}\end{tex2html_preform}}
\end{figure}

The next step is to distribute  the sums over the expressions as follows:
\begin{figure}
{\footnotesize
\begin{tex2html_preform}\begin{verbatim}In[59]:= D...
... 1, n}]
2
sigma[i]In[61]:=\end{verbatim}\end{tex2html_preform}}
\end{figure}

These equations are still not tractable by Solve. We must factor out a and b. We do that again by manipulating the Sums manually, since tricks such as:
\begin{figure}
{\footnotesize
\begin{tex2html_preform}\begin{verbatim}In[83]:= O...
...-----------------
aIn[85]:=\end{verbatim}\end{tex2html_preform}}
\end{figure}

accomplish nothing. Only when a specific numerical substitution is made, we can eventually get somewhere:
\begin{figure}
{\footnotesize
\begin{tex2html_preform}\begin{verbatim}In[85]:= %...
...1] sigma[2] sigma[3]In[87]:=\end{verbatim}\end{tex2html_preform}}
\end{figure}

This is too little and too late.

So here is how we rewrite these expressions manually:

\begin{figure}
{\footnotesize
\begin{tex2html_preform}\begin{verbatim}2
2 a x[i...
...-----, {i, 1, n}]
2
sigma[i]\end{verbatim}\end{tex2html_preform}}
\end{figure}

$\ldots$ and similarly for the second equation:
\begin{figure}
{\footnotesize
\begin{tex2html_preform}\begin{verbatim}Out[53]= S...
... 1, n}]
2
sigma[i]In[56]:=\end{verbatim}\end{tex2html_preform}}
\end{figure}

Make no mistake, this is a butchering job that would not stand up to a scrutiny of any artificial reasoning system , such as HOL  or Isabelle . We are basically creating completely new formulas, by manually selecting parts from the old ones - and such manipulations are prone to mistakes. We had to do considerably more work in Mathematica than even in Maxima. The reason for this is that Mathematica knows even less about manipulating sums than Maxima does. Function Sum in Mathematica is designed specifically for working with power series rather than with arrays.

On the other hand, you must have noticed that Mathematica's vocabulary is rich enough to let us design more sophisticated rewriting rules for sums and derivatives, and we will explore this possibility further down the road.

Now, at long last, we can solve the equations:

\begin{figure}
{\footnotesize
\begin{tex2html_preform}\begin{verbatim}In[58]:= S...
...2
sigma[i] sigma[i]In[59]:=\end{verbatim}\end{tex2html_preform}}
\end{figure}

We make final assignments to a and b by extracting appropriate expressions from the list returned by Solve:
\begin{figure}
{\footnotesize
\begin{tex2html_preform}\begin{verbatim}In[67]:= a...
...2
sigma[i] sigma[i]In[69]:=\end{verbatim}\end{tex2html_preform}}
\end{figure}

As was the case in Maxima and in Maple, Mathematica evaluates $\partial a / \partial y_k$ and $\partial b/\partial y_k$ incorrectly too, returning zero in both cases:

\begin{figure}
{\footnotesize
\begin{tex2html_preform}\begin{verbatim}In[69]:= D...
...b, y[k]]Out[70]= 0In[71]:=\end{verbatim}\end{tex2html_preform}}
\end{figure}

In order to perform this differentiation, we must make specific substitutions both for N and for k, e.g.,
\begin{figure}
{\footnotesize
\begin{tex2html_preform}\begin{verbatim}In[75]:= D...
...3] sigma[4] sigma[5]In[76]:=\end{verbatim}\end{tex2html_preform}}
\end{figure}

and similarly for $\partial b/\partial y_k$:
\begin{figure}
{\footnotesize
\begin{tex2html_preform}\begin{verbatim}In[79]:= D...
...3] sigma[4] sigma[5]In[80]:=\end{verbatim}\end{tex2html_preform}}
\end{figure}

Compare these results with Section 2.1.8.


next up previous index
Next: Fitting, the Lessons Up: Fitting Previous: Fitting in Maple
Zdzislaw Meglicki
2001-02-26