TweetFollow Us on Twitter

Curve Fitting 2
Volume Number:1
Issue Number:12
Column Tag:Forth Forum

Curve Fitting, Part II

By Jörg Langowski, Chemical Engineer, Grenoble, France, MacTutor Editorial Board

After last month's refresher on accessing floating point routines from Forth, this column is now going to show you the main part of the curve fitter program.

Let's state our objective again: We have a series of data points (measurements) yi at certain time points ti. The measured data is supposed to follow some quantitative law, so that we can state a theoretical relationship between time t and data y:

The ai are parameters that determine the exact form of the function f.

Lets assume we have estimated initial values for the parameters ai° somehow so that they are not too far away from the true values. Then we need a method that creates some correction terms ai, which when added to the initial values give new, better estimates of the parameters ai':

As you could read in last month's column, the ai are eventually obtained from the solution of a system of linear equations, and we had defined the word gauss to implement the Gauss algorithm that solves such a system.

This month I am going to show you how one sets up the equations (example given for three parameters),

starting from the data and the initial estimate of the function that one wants to fit to it. The full details of the method are given in an appendix to this column.

At this point it is only important to know that the coefficients cij, as defined in last month's column, look like the following:

where N is the total number of data points and fk/ai is the first derivative of the theoretical function at the time tk with respect to the parameter ai.

The terms on the right hand side of the equations, bi, are:

Ri are the residuals (as defined last month),

the differences between the theoretical and measured values at the time points ti.

Therefore, the curve fitting algorithm will consist of the following major parts:

- one routine that calculates the theoretical function value f(ti), given a set of parameters (this will be the 'model' that you use to fit your data),

- one routine that calculates the derivative of this function with respect to one of the parameters ai,

- calculation of all the derivatives fk/ai with k ranging from 0 to (# of data points - 1) and i from 0 to (# of parameters - 1) and saving those derivatives in a matrix,

- computation of the coefficients cij and bi (and setting them up in a matrix),

- solution of the linear equation system thus obtained, giving correction values for the parameters ai,

- changing the parameters by the correction terms and repetition of the whole algorithm if the change is still larger than some predefined (small) number.

The algorithm is implemented in this month's program example (listing 1). The first part contains some additions to the floating point and Gauss algorithm routines that you've already seen last month. One bug had to be corrected in gauss, which left a number on the stack that was supposed to be dropped (added drop in italics in the listing). The floating point output routine has been slightly modified so that real numbers are always printed with one digit in front of the decimal.

In order to be able to check whether the iteration has been completed or not, we will have to compare real numbers, so first we define the

Floating point comparison operator

The SANE package provides a routine for the comparison of two floating point numbers. Four cases are distinguished and the processor status register flags set accordingly:

X N Z V C

x < y 1 1 0 0 1

x = y 0 0 1 0 0

x > y 0 0 0 0 0

unordered0 0 0 1 0

where the address of x is on top of the address of y on the stack.

The operation code for floating point comparison in SANE is 8, so we define

hex : f> 8 fp68k @sr 10 and ;

for comparing two real numbers on the stack. Bit 5 of the result (the X flag) is and-ed out, the reason being that this is the only one that is left unchanged after the SANE trap has been called and the Forth interpreter has taken over again. All the other flags cannot be used, so we won't be able to compare two real numbers for being equal. With 80-bit precision, however, such a test does not make much sense, you will almost never check for equality but rather if a number is less than one other or whether their difference is smaller than some predefined value.

The comparison for 'unorderedness', which sets the V flag, is also trashed by the Forth interpreter. This would only be important if we wanted to test for a NaN (Not a Number), which for instance results from a 0/0 division.

This leaves us with the X flag as the only one usable after the fp68k call. It is set when the real number whose address is on top of the stack is less than the number whose address is below it.

Random number generator - floating point version

For initializing the 'data points' as an input to the curve fitting program, we are going to use simulated data from the same function that we are fitting. We add some 'random noise' to it and need a random number generator for that. SANE contains a floating point random number generator, which computes from an 80-bit floating point input a new random 80-bit floating point number in the range 0 < x < 231-1. We want a number between 0 and 1 (for convenience) so the word ranf scales this number after calling the random number routine. ranf leaves the address of the result on the stack. For initializing the random number generator with an arbitrary seed, ranset is defined.

You will notice one important concept in the definition of ranf and ranset: both words operate on variables (rr, rs and sf) that are axed after the definition is completed. The variables are erased from the vocabulary that way and stay local to the random number routines. This is the way the 'nameless' tokens are created that you read about some issues ago.

The function to be fitted is defined as the word func. 50 bytes are reserved for the array par, so that at maximum 5 parameters (extended precision) can be used in the function. The data points are stored in the single precision arrays xdat and ydat. init_pars initializes the parameters to some arbitrary values, in this case par[1] = 1.0, par[2] = 2.0, and par[3] = -0.1. Calling func with these values, init then creates a set of simulated data (including random noise).

deriv calculates the first derivatives of the function with respect to the parameters from differences,

This definition, too, uses the local variables da1, da2, da3 and da4, which are axed afterwards.

make_derivmat computes all necessary derivative values and sets them up in a single precision matrix. This matrix is then used by the word make_resmat to compute the sums that make up the coefficient matrix. For the right-hand-side coefficients one also needs the residuals, which are calculated and stored into a matrix by residuals.

One iteration of the curve fitting process is done by the word one_iter, which sets up the derivative matrix and computes the residuals by calling the appropriate words, prints the sum of error squares (so you can check the quality of the fit) then sets up the coefficient matrix and calls gauss to solve the linear equation system. The solution is stored in the array delta; these are the correction terms that have to be added to the parameters to get improved estimates. new_pars does this correction and prints out the values of the new parameters, leaving false (zero) on the stack if any parameter has changed by more than one part in 10-5.

The actual curve fitter, nlsqfit, then loops through the iteration until the best fit is obtained.

You can check the fitting process by calling init first and then, for instance, setting par[1] = 2.0, par[2] = 1.0, par[3] = -0.05:

two par x2x  one par 10+ x2x
two par 20 + f/

and start nlsqfit. This will bring you back close to the simulated values, par[1] = 1.0, par[2] = 2.0, and par[3] = -0.1 in about 4 to 5 iterations. The fitted values will not be exactly equal to the simulated ones because of the random noise added to the data.

So far, this is only a skeleton of a curve fitting program because we cannot input floating point numbers manually or from the clipboard (e.g. it would be nice to transfer data from a spreadsheet); also a graphical output will be needed to display the data points and the fitted curve. Next column will deal with those problems.

Listing 1: Non-linear least squares curve fitting routine
( © 1985 J.Langowski by MacTutor )
( Note that this is not stand-alone but needs some definitions from last 
month's example. Only the changed parts are printed here )

hex
: f> 8 fp68k @sr 10 and ;  : fabs f fp68k ;
: lnx 0 elems68k ; : log2x 2 elems68k ;
: ln1x 4 elems68k ; : log21x 6 elems68k ;
: expx 8 elems68k ; : exp2x a elems68k ;
: exp1x c elems68k ; : exp21x e elems68k ;
: x^i 8010 elems68k ; : x^y 8012 elems68k ;
: compoundx c014 elems68k ; 
: annuityx c016 elems68k ;
: sinx 18 elems68k ; : cosx 1a elems68k ;
: tanx 1c elems68k ; : atanx 1e elems68k ;
: randomx 20 elems68k ;   decimal

: dec. ( float\format# -- )
       zzformat ! zzformat swap zzs1 b2d
       zzs1 dup w@ 255 > if ." -" else ."  " then
       dup 4+ count over 1 type ." ."
       swap 1+ swap 1- type ( mantissa )
       2+ w@ ( get exponent )
            1 w* zzformat @ + 1- 
            ." E" . ;

( define constants )
float one  float -one  float zero  float two  float four
1 sp@ one in2x drop  -1 sp@ -one in2x drop 
0 sp@ zero in2x drop
2 sp@ two in2x drop  4 sp@ four in2x drop
( define some floating accumulators)
float fa1   float fa2   float fa3   float fa4

( Gauss algorithm for linear equations)          
float dg    float fk    float ee
variable nv   variable coeff variable solution
( addresses for storing actual parameters)
: gauss ( z\x\n | --)  nv !  8- coeff !  solution !
  nv @ 1- 0 do  ( i-loop)
     i dup coeff @ calc.offset dg s2x ( diag elem)
     nv @ i 1+ do  ( j-loop)
        i j coeff @ calc.offset fk s2x   dg fk f/
        nv @ 1+ j do  ( k-loop)
            k i coeff @ calc.offset fa1 s2x
                      fk fa1 f*  fa1 fneg  ( -fk*x[i,k])
            j i coeff @ calc.offset dup fa1 s+
                      fa1 swap x2s
                  loop
              loop
           loop
nv @ dup 0 do i over coeff @ calc.offset  fa1 s2x
                       fa1 solution @ i 4* + x2s loop drop
1 nv @ 1- do
     i dup coeff @ calc.offset dg s2x
     solution @ i 4* + ee s2x  dg ee f/
     0 i 1- do i j coeff @ calc.offset fa1 s2x
                         ee fa1 f* fa1 fneg
               solution @ i 4* + dup fa1 s+ fa1 swap x2s
            -1 +loop
       -1 +loop
nv @ 0 do  solution @ i 4* +  fa1 s2x
           i dup coeff @ calc.offset  fa1 s/
           fa1 solution @ i 4* + x2s
       loop ;

( declarations for curve fitter )
create ydat 400 allot   create xdat 400 allot
create residues 400 allot
100 10 matrix derivmat    10 11 matrix resmat
     3 constant npars        10 constant npts
create par 50 allot   create delta 20 allot
float eps  float errsum
1 sp@ eps in2x drop  10000 sp@ eps in/ drop
float onehundred  100 sp@ onehundred in2x drop
float ten  10 sp@ ten in2x drop
( define function )                              
: func ( x -- f[x] = par[1] + par[2] * exp[par[3]*x] )
  par 20 + over f* dup expx  
  par 10 + over f*  par over f+ ;
: test 10 0 do i sp@ fa1 in2x . 2 spaces
                          fa1 func 10 dec. cr loop ;
: >fa1  fa1 s2x ;
: init_pars
  one par x2x  two par 10+ x2x
 -one par 20 + x2x  ten par 20 + f/ ;
init_pars

( derivative, matrix of derivs )                 
float da1 float da2 float da3 float da4  ( local vars )
: deriv ( par \ x -- d-func/d-par at x )
  dup da1 x2x da2 x2x  dup da4 x2x  eps da4 f*
  da4 over f+          da2 func  da3 x2x
  da4 over 2dup f- f-  da1 func  da3 f-
  da4 da3 f/   two da3 f/  da4 swap f+  da3  ;
axe da1 axe da2 axe da3 axe da4

: make_derivmat
  npts 0 do  npars 0 do
       xdat j 4* +  >fa1
       par i 10 * +   fa1 deriv  j i derivmat x2s
     loop  loop ;

( calculate residuals )
: residuals
  zero errsum x2x
  npts 0 do
     xdat i 4* + >fa1   fa1 func   ydat i 4* +  swap  s-
     fa1 residues i 4* + x2s  fa1 dup f*  fa1 errsum f+
     loop  ;
: .resid 
   npts 0 do  residues i 4* + >fa1  fa1 7 dec. cr loop ;

( make matrix of residuals )
 make_resmat
  npars 0 do   npars 0 do    zero fa1 x2x
    npts 0 do
      i k derivmat fa2 s2x    i j derivmat fa2 s*
      fa2 fa1 f+  loop
    fa1  i j resmat x2s    fa1  j i resmat x2s
  loop  loop
  npars 0 do  zero fa1 x2x
     npts 0 do
            i j derivmat fa2 s2x  residues i 4* + fa2 s*
            fa2 fa1 f-  loop
          fa1  i npars  resmat x2s loop ;

( calculate correction terms)
: one_iter
  make_derivmat    residuals  
  ." sum of error squares: " errsum 7 dec. cr
  make_resmat      delta 0 0 resmat npars gauss ;

: new_pars 16 ( true if no significant changes )
  npars 0 do par i 10 * +
    delta i 4* +  over  s+
    ." par[" i . ." ] = " dup  7 dec. cr
    delta i 4* + fa1 s2x  fa1 f/
    fa1 fabs  eps fa1 f> and loop ;

( ranf, initialize data matrices ) 
float rr float rs float sf ( local to ranf )
1 31 scale 1 - sp@ sf in2x drop
: ranset rr x2x ;
: ranf rr randomx  rr rs x2x  sf rs f/  rs ;
axe rr axe rs axe sf
12345678 sp@ fa1 in2x drop fa1 ranset
80 ' npts !    
: init npts 0 do i sp@ fa1 in2x 4*
          xdat over + fa1 swap x2s
          ydat over + fa1 func  ranf fa2 x2x
          ten fa2 f/  fa2 over f+  swap x2s
          i .  xdat over + >fa1 fa1 7 dec. 2 spaces
               ydat  +   >fa1   fa1 7 dec. cr    loop ;

( print matrices for debugging )
: .dmat
  npts 0 do
    npars 0 do  j i derivmat >fa1 fa1 5 dec. loop
    cr loop ;
: .rmat
  npars 0 do
    npars 1+ 0 do  j i resmat >fa1 fa1 5 dec. loop
    cr loop ;

( nonlinear fit, core routine) 
: nlsqfit cr   begin  one_iter cr  new_pars cr  until ;

Appendix: Theoretical background of the curve fitting routine

We want to determine the values of the ai in such a way that the differences between the theoretical function and the measured yk values at times tk become a minimum. These differences are called the residuals rk:

rk = f (tk, a1, a2, a3, .... , an) - yk

and one usually tries to minimize the sum of the squared residuals of all data points.

Lets assume ri are the 'true' residuals that one obtains with the exact ai values. If we estimate the parameters by some initial values ai°, then 'computed' residuals

Rk = f (tk, a1°, a2°, a3°, .... , an°) - yk

can be calculated, which are usually larger than the true ones. To get a correction term that brings the ai° closer to the 'true' ai, one now linearly expands the function f around the estimated value:

f(tk,a1,a2,....,an)

f(tk,a1°,a2°,....,an°) + fk/a1(a1-a1°)

+ fk/a2 (a2-a2°)

. . . . .

+ fk/an(an-an°)

The differences, (ai-ai°), are denoted by ai, now we can write

f(tk,a1,a2,....,an) - yk   
 f(tk,a1°,a2°,....,an°) - yk 
 +  fk/a1 a1 +  fk/a2 a2
 . . . . .
 +  fk/an an

which gives us a relationship between the true and the computed residuals

rk   Rk +  fk/a1 a1 +  fk/a2 a2
 . . . . .
 +  fk/an an .

It is the sum of the squares of the true residuals (N being the number of data points)

that has to be minimized with respect to changes in ai, this means all the derivatives Q/(ai) have to be zero simultaneously. When you evaluate the expressions for the Q/(ai) and set them to zero, you arrive at the equation system that was desribed in the main article.

 
AAPL
$99.00
Apple Inc.
+1.33
MSFT
$44.07
Microsoft Corpora
-0.43
GOOG
$589.79
Google Inc.
+0.77

MacTech Search:
Community Search:

Software Updates via MacUpdate

Acorn 4.4 - Bitmap image editor. (Demo)
Acorn is a new image editor built with one goal in mind - simplicity. Fast, easy, and fluid, Acorn provides the options you'll need without any overhead. Acorn feels right, and won't drain your bank... Read more
Bartender 1.2.20 - Organize your menu ba...
Bartender lets you organize your menu bar apps. Features: Lets you tidy your menu bar apps how you want. See your menu bar apps when you want. Hide the apps you need to run, but do not need to... Read more
TotalFinder 1.6.2 - Adds tabs, hotkeys,...
TotalFinder is a universally acclaimed navigational companion for your Mac. Enhance your Mac's Finder with features so smart and convenient, you won't believe you ever lived without them. Tab-based... Read more
Vienna 3.0.0 RC 2 :be5265e: - RSS and At...
Vienna is a freeware and Open-Source RSS/Atom newsreader with article storage and management via a SQLite database, written in Objective-C and Cocoa, for the OS X operating system. It provides... Read more
VLC Media Player 2.1.5 - Popular multime...
VLC Media Player is a highly portable multimedia player for various audio and video formats (MPEG-1, MPEG-2, MPEG-4, DivX, MP3, OGG, ...) as well as DVDs, VCDs, and various streaming protocols. It... Read more
Default Folder X 4.6.7 - Enhances Open a...
Default Folder X attaches a toolbar to the right side of the Open and Save dialogs in any OS X-native application. The toolbar gives you fast access to various folders and commands. You just click... Read more
TinkerTool 5.3 - Expanded preference set...
TinkerTool is an application that gives you access to additional preference settings Apple has built into Mac OS X. This allows to activate hidden features in the operating system and in some of the... Read more
Audio Hijack Pro 2.11.0 - Record and enh...
Audio Hijack Pro drastically changes the way you use audio on your computer, giving you the freedom to listen to audio when you want and how you want. Record and enhance any audio with Audio Hijack... Read more
Intermission 1.1.1 - Pause and rewind li...
Intermission allows you to pause and rewind live audio from any application on your Mac. Intermission will buffer up to 3 hours of audio, allowing users to skip through any assortment of audio... Read more
Autopano Giga 3.6 - Stitch multiple imag...
Autopano Giga allows you to stitch 2, 20, or 2,000 images. Version 3.0 integrates impressive new features that will definitely make you adopt Autopano Pro or Autopano Giga: Choose between 9... Read more

Latest Forum Discussions

See All

Traps n’ Gemstones Review
Traps n’ Gemstones Review By Campbell Bird on July 28th, 2014 Our Rating: :: CASTLEVANIA JONESUniversal App - Designed for iPhone and iPad Fight mummies, dig tunnels, and ride a runaway minecart to discover ancient secrets in this... | Read more »
The Phantom PI Mission Apparition Review
The Phantom PI Mission Apparition Review By Jordan Minor on July 28th, 2014 Our Rating: :: GHOSTS BUSTEDUniversal App - Designed for iPhone and iPad The Phantom PI is an exceedingly clever and well-crafted adventure game.   | Read more »
More Stubies Are Coming Your Way in a Ne...
More Stubies Are Coming Your Way in a New Update Posted by Jessica Fisher on July 28th, 2014 [ permalink ] Universal App - Designed for iPhone and iPad | Read more »
The Great Prank War Review
The Great Prank War Review By Nadia Oxford on July 28th, 2014 Our Rating: :: PRANKING IS SERIOUS BUSINESSUniversal App - Designed for iPhone and iPad Though short, The Great Prank War offers an interesting and fun mix of action and... | Read more »
Marvel Contest of Champions Announced at...
Marvel Contest of Champions Announced at Comic-Con Posted by Jennifer Allen on July 28th, 2014 [ permalink ] Announced over the weekend at San Diego Comic-Con was the fairly exciting looking Marvel Contest of Champions. | Read more »
Teenage Mutant Ninja Turtles Review
Teenage Mutant Ninja Turtles Review By Jennifer Allen on July 28th, 2014 Our Rating: :: DULL SWIPINGUniversal App - Designed for iPhone and iPad The pizza power is weak when it comes to this Teenage Mutant Ninja Turtles game.   | Read more »
Exploration Focused Puzzle Game Beatbudd...
Exploration Focused Puzzle Game Beatbuddy Set to Make Transition from PC to iOS this September Posted by Jennifer Allen on July 28th, 2014 [ permalink ] | Read more »
PlanetHD
PlanetHD By Nadia Oxford on July 28th, 2014 Our Rating: :: SPACE MADNESSUniversal App - Designed for iPhone and iPad PlanetHD will keep players busy for a while, though its unpredictable physics are a handful to deal with.   | Read more »
This Week at 148Apps: July 21-25, 2014
Another Week of Expert App Reviews   At 148Apps, we help you sort through the great ocean of apps to find the ones we think you’ll like and the ones you’ll need. Our top picks become Editor’s Choice, our stamp of approval for apps with that little... | Read more »
Reddme for iPhone - The Reddit Client (...
Reddme for iPhone - The Reddit Client 1.0 Device: iOS iPhone Category: News Price: $.99, Version: 1.0 (iTunes) Description: Reddme for iPhone is an iOS 7-optimized Reddit client that offers a refreshing new way to experience Reddit... | Read more »

Price Scanner via MacPrices.net

13-inch 2.5GHz MacBook Pro on sale for $1099,...
Best Buy has the 13″ 2.5GHz MacBook Pro available for $1099.99 on their online store. Choose free shipping or free instant local store pickup (if available). Their price is $100 off MSRP. Price is... Read more
Roundup of Apple refurbished MacBook Pros, th...
The Apple Store has Apple Certified Refurbished 13″ and 15″ MacBook Pros available for up to $400 off the cost of new models. Apple’s one-year warranty is standard, and shipping is free. Their prices... Read more
Record Mac Shipments In Q2/14 Confound Analys...
A Seeking Alpha Trefis commentary notes that Apple’s fiscal Q3 2014 results released July 22, beat market predictions on earnings, although revenues were slightly lower than anticipated. Apple’s Mac’... Read more
Intel To Launch Core M Silicon For Use In Not...
Digitimes’ Monica Chen and Joseph Tsai, report that Intel will launch 14nm-based Core M series processors specifically for use in fanless notebook/tablet 2-in-1 models in Q4 2014, with many models to... Read more
Apple’s 2014 Back to School promotion: $100 g...
 Apple’s 2014 Back to School promotion includes a free $100 App Store Gift Card with the purchase of any new Mac (Mac mini excluded), or a $50 Gift Card with the purchase of an iPad or iPhone,... Read more
iMacs on sale for $150 off MSRP, $250 off for...
Best Buy has iMacs on sale for up to $160 off MSRP for a limited time. Choose free home shipping or free instant local store pickup (if available). Prices are valid for online orders only, in-store... Read more
Mac minis on sale for $100 off MSRP, starting...
Best Buy has Mac minis on sale for $100 off MSRP. Choose free shipping or free instant local store pickup. Prices are for online orders only, in-store prices may vary: 2.5GHz Mac mini: $499.99 2.3GHz... Read more
Global Tablet Market Grows 11% in Q2/14 Notwi...
Worldwide tablet sales grew 11.0 percent year over year in the second quarter of 2014, with shipments reaching 49.3 million units according to preliminary data from the International Data Corporation... Read more
New iPhone 6 Models to Have Staggered Release...
Digitimes’ Cage Chao and Steve Shen report that according to unnamed sources in Apple’s upstream iPhone supply chain, the new 5.5-inch iPhone will be released several months later than the new 4.7-... Read more
New iOS App Helps People Feel Good About thei...
Mobile shoppers looking for big savings at their favorite stores can turn to the Goodshop app, a new iOS app with the latest coupons and deals at more than 5,000 online stores. In addition to being a... Read more

Jobs Board

*Apple* Solutions Consultant (ASC) - Apple (...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
Sr. Product Leader, *Apple* Store Apps - Ap...
**Job Summary** Imagine what you could do here. At Apple , great ideas have a way of becoming great products, services, and customer experiences very quickly. Bring Read more
*Apple* Solutions Consultant (ASC) - Apple (...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
*Apple* Solutions Consultant (ASC) - Apple (...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
WW Sales Program Manager, *Apple* Online St...
**Job Summary** Imagine what you could do here. At Apple , great ideas have a way of becoming great products, services, and customer experiences very quickly. Bring Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.