A PASCAL Program For Fitting Non Linear Regression Models On A Microcomputer
A numerical algorithm which calculates a least squares f it of data to general nonlinear models is implemented as A PASCAL procedure. As the use of micro-computers is increasing, the need for small, efficient fitting routines arises.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
364 views3 pages
A PASCAL Program For Fitting Non Linear Regression Models On A Microcomputer
A numerical algorithm which calculates a least squares f it of data to general nonlinear models is implemented as A PASCAL procedure. As the use of micro-computers is increasing, the need for small, efficient fitting routines arises.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3
B UYs/VON G ADOW, A PASCAL program for fitting nonlinear regression models 105
EDV in Medizin und Biologie 18 (4),105-107, ISSN 0300-8282
Eugen Ulmer GmbH & Co., Stuttgart ; Gustav Fischer Verlag KG, Stuttgart A PASCAL program for fitting nonlinear regression models on a micro-computer Johan D. Buys and Klaus van Gadow Summary A numerical algorithm which calculates a least squares f it of data to general nonlinear models is implemented as a PASCAL procedure. 1. Introduction The use of nonlin ear regr ession models is very popular in forestr y research and management. Standard statistical pack- ages are availabl e for fitting data to these model s, for example , the routine BMDPAR or the procedure NU N in SAS. How- ever, as the use of micro-computers is increasing, the need for small, efficient fitting routines arises. There are commercial programs for fitting a non linear mod el on a small computer. However, these programs are inaccessible to the user and cannot be modified to improve the processing performance under specific circumstances. For this reason we present a numerical algorithm and a corresponding procedure in PASCAL. 2. The algorithm We have chosen to impl ement the well-known algorithm of Levenberg and Marquardt (see I. E. DENNIS and R. B. SCHNABEL, 1983) for minimizing the sum of squares of devia- tions " S(a l, .. ., am) = 1: [y; - b( x;; ab " ' , am)]' i : l where the regression mod el is y = f(x; a1> .. . , am) + 10 , the mod el parameters are ab . . ., amand the data points are (x., y;), i = 1, . . . , n. The algori thm is iterative, and starts with initial estimates arl, i = 1, . . . , ID of the desired parameter values. Aft er k steps of the algorithm, estimates ark), i s, 1, ... , ID have been obtained and improved estimates ark+ 1) are then computed as follows: ar k + 1 ) = afk) + 6.ai(kl, i = 1, . .. , m where the corrections Llafk) are obt ained as the solut ion of the set of linear equat ions [1[1, + AkDk] nark) = 1[R k . In the line ar system, 6.a(k) is the vector of corr ections Llafk), R, is the residual vector R, ~ [y; - f(x;; al k ) , .. , a ~ ) ] T , J k is the n x m Jaco bian matrix _ [ iJf l k ) (k) ] Jk - -&aj (Xi , a , ... , a m ) , D, is a diagonal weight matri x which we take as D, = diag(JfJ k ) , and Ak is a parameter which is used to enhance convergence of the algorithm (see below). In our implementation of the algorithm, we compute all derivatives (elements of the matrix Jc) numerically with a finit e difference formul a, but this can easily be changed if analytical derivatives are available. In order to conserve storage space, the matrix J k is not stored. JfJ k is formed step by step as the necessary element s are computed. The system of linear equa- tions is solved by the Cho lesky method for positive definite systems . The strategy for choosing the parameter Akis as follows. The residual sum of squares Sk = S(i t l,-. . " a ~ ) ) is monitored at each step of the algorithm. If Sk+ I < S b it is assumed that the algorithm is converging and Akis decreased by a fixed factor in order to force the algorithm towards a Gauss-Newton step for fast convergence . If however Sk+1 ~ Sb the corrections Llafk) are not accepted; instead Ak is increased to force the algorithm towards a steepest gradi ent step in an effort to enfor ce con- vergence, and the corrections Llafk) are recomputed . The algorithm is stopped when the corrections L.\ afk) are small compared to the parameter estimates afk + 1) . EDV in Medizin und Biologie 4/1987 106 BUYS/VON G ADOW, A PASCAL program for fitting nonlinear regression models {speci fy t he equation} FUNCTION tunc (x : REAL; a : dir:lpar ) : REAL; Bt GIN func :-a ( 1) * ( I -exp( a ( 2]*X) ; {example} END; ( fu nc ) PROCEDURE nl s q (n, ( I nput : Number of par ameter s ) m : I NTEGER; (Input: Number of observat ions ) VAR a : d impar; ( Input and output : vector of parameter values) x, I Input: Vector of independent observat ions} y : d i mdata; ( I nput: Vector of dependent obs ervat i ons ) VAR tail : BOOLEAN (Out put : Er r or i ndi c at or ) ) , VAA i, j, k, i t , rep : INTEGER: a i, er, hi , eu , sq, sqq, SS, u REAL; f : d i mdata; a, dd, s : dimpa r ; h : di ll'lpar 2; s q d ec : BOOLEAN ; CONST r epmax : I NTEGER _ 10 ; i tmax : INTEGER - 50 ; i nc : REAL - J. ; dec : REAL _ 5 . ; e ps : REAL - 1.e- 6; hh : REAL - 1 .e-6 ; LABEL stop; BEGI N t(k): -funC(X(k],d); er : - y [ k)- f [ k] ; . s qq : - s qq+er *er ; END; IF sqq > sq+1 .e- 4*ss THEN BEGI N r e p: -re p+l ; IP rep - repmax THEN fai l: -True ; mu : - mu*i nc : I F r ep> 2 THEN mu: -mu*inc ; Writeln(i t : J ,rep : 3 ,' ' , sq: 15, ' ' , s qq :15 , ' ' ) ;{ s s : 6: J,' ' ,) BEGI N const ants; a ( 1]: - 35 ; a [2]: - - 0.05; ( initial par amet er values) nl sq(2 ,5 , a ,x,y,fa1 1) ; wr iteln; ',fai l, ' ' ) ; FOR 1:-1 TO 2 DO Writ e(a[i J : I 0:7;' '): END. REPEAT i t : - it+l ; FOR i:=l TO n DO BEGIN s [i]: -hh*a [ i ] ; IF s(l ] < 1.e- l0 THEN s [ i]: - hh; dd [ i ]: - o.; FOR j:-i TO n DO h[i,j]: =>o . END; FOR k: ""l TO m DO BEGIN FOR 1 : - 1 TO n DO BEGIN al : - a ( l] ; a (i ] :-a i+s ( l ], h i : =- 4( i }-ai: d [ i ]: - (fun c (:lI: [ k ] , a ) - t( k]) /hi; a[i] : -d; END; FOR i : - l TO n 00 BEGIN dd[!] : -dd [i }+d {i) * (y [k] -t [k ]) ; FOR j : - i TO n DO h ( i ,j J :-h [ ! , j ] +d [ i ] *d [ j J; END END; FOR 1 : - 1 TO n DO s[i ] : =h[i , i] ; r ep: -O ; s qd ec:-f alse; REPEAT u: "mu+1 . ; FOR 1: - 1 TO n DO BEGIN h [i,i ] : -u*s [ 1] ; dei ] : -dd[i]; END; cholsolv e; I F fai l THEN GOTO stop ; s s :=O. : FOR 1: - 1 TO n 00 BEGIN s s: - s s+d ( i }*dd [ i] ; d ( i ] : " a [ i] +d [ i] ; END; sqq:-O. ; FOR k:-l TO m 00 END ELSE s qd ec : -True : UNTI L sqdec OR fai l ; IF t ail THEN GOTO stop; sq: - sqq: ss:-O. : FOR i : -1 TO n DO BEGIN ai : - Abs (d( i) -a{i])/ (1. e- 12+Abs (d [ i] ; I F ai > ss THEN ss : - ai ; a ( i ): -d[i ] ; END; IF rep - 0 THEN mu:-mu/ dec; IF i t - itmax THEN fa i l : =True ; Writeln; Write(it :3,' ' , mu:l 0 , ' ', s q:l 0, ' '); FOR i :-l TO n DO Write (a [ iJ: 9 :5 ,' ' ) ; wr iteI n ; UNTIL (ss < eps ) OR fail ; stop: END; lnlsq) 33 dat a pa i rs ) 10 par ameters I ( llI U :: ot ( llI a x of REAL; PROGRAM Nonlin; TYPE a 1ma ata - ARRAY[1 .. J3 ] OF REAL; aimpar - ARRAY ( l . lO] OF REAL; VARdlmpar2 '"' ARRAY[l. .lO, l. .lO) OF 1 : I NTEGER; fa il : BOOU;AN ; fk : CHAR; x, y : dl mdata ; a : d i mpa r; PROCEDURE cons tants ; b egin {tree heights( Y) and ages(X ) } x [l] : =5 . 0 ; x[ 2) : - 10 . 0; x[ 3] : - 20 . 0 ; X( 4] : - 30. 0; x( 5] : => 40 . 0; y [ 1 ]:=5. 0 ; y[2]: -10 ,6 : y[3] : " 18.2 ; y[4] : -24.5 ; y[ 5 ) :=-27 .9; end ; PROCEDURE chol s ol v e ; VAR i, j , k : I NTEGER; s , ss : REAL; BEGIN fa il : -Fa lse ; FOR j : - l TO n DO BEGIN S: "'h[j,j ] ; FOR 1:-1 TO j -l DO s : o.s - h (j , i ] *h ( j , i J; IF s>-O. THEN BEGI N s : - Sqrt(s ) ; h [ j , 1] : =5 ; FOR 1: -j +1 TO n 00 BEGI N s s :-h( j , i ) ; FOR k:-l TO j - l DO s s : - s s - h[ i , k ) *h ( j , k ) ; h[ i ,j] : -ss/ s: END END ELSE ta il:-True ; END; IF NOT fail THEN BEGI N d el ] :-d[ ll/h ( 1, 1] ; FOR i : "2 TO n DO BEGIN s :-d[ i J; FOR j : -l TO i - I DO s : - s - h(i ,j ]* d [ j ) ; d ei] : as / h ( i,i ]; END ; den] : - d [ n ]/ h [ n, n ] ; FOR 1:-n- 1 DOWNTO 1 DO BEGI N s: -d[ 1] ; FOR j: -i+l TO n DO s : -s-h(j , il *d (j) ; a Ci] :><s /h[ i , i ] ; END END END; {cho Lac Lve ] BEGIN mu : -20 . ; 1t : -o; s q :-O.; FOR )[; :-1 TO :IQ DO BEGIN f [k] : -tunc (x[k] , a ) ; er: -y[k ] -f [ k] ; sq: - sq+er-e r; END; EDV in Mcdizin und Biologic 411987 BUYs/VONGADOW, A PASCAL program for fitting nonlincar regression models 107 3. Numerical examples i = 10,02; P ~ 2, 1826; q = 0,0524. The foll owing para meter values were obtained afte r 28 itera- tions: As a first exa mple , we present the under bark radii (ri, in centime tres) at differ ent height s (hi, in metres) of a Eucalyptus cloezian a tree: . BRINK. C. F. and K. VON GADOW, 1986: On the use of growth and decay functions for modelling stem profiles. EDV in Medizin und Biologie 17(1/2): 20-27. D ENNIs , J. E . ; and R. B. SOINABEL, 1983: Numerical methods for unconstrained optimization and nonlinear equations. Prenti ce-Hall , New Jersey. GARClA, 0 ., 1984: New Class of Growth Models for Even-Aged Stands: Pinus radiat a in Golden Downs Forest. N. Z. Journal of Forestry Science 14(1). A = 27,7; k = -0, 1042; m ~ 0,6305. The residual SS was 0,1736. The mean height/age data set was used again to fit the 4- parameter Chapman Richards model: h ~ A(I - be") U(I - m) (3) using the ini tial values A = 40; b = 1; k = - 0, 2; m = 0,5. The final parameter values were obtained after 22 iterations : A = 28, 1; b = 1,07; k = - 0,09479; m = 0,2341; and the residual SS was 0,00919 (note the improved fit when compared with the 3-parameter mod el) . Given the ini tial paramet er value s: A = 40; k = - 0,2; m = 0,5 ; the followi ng final values were obtained aft er 10 itera- tions : 4, References = unde r bark tree radius (cm), = tree height (m), = breast height (1,35 m), = under bark radius at breast height (6,06 cm), = total tree beight (18,3 m) , = 2,71 .. . = parame ters to be. estimated. r(h) h b rb H e i , p, q whe re h;: 00,6 1,2 1,35 2,4 4,9 7,3 9,8 12,2 15,2 18,3 r;: 7,37 6,736,10 6,06 5,84 5,08 4,57 3,81 3,05 1,52 0 The following taper model (c. F. BRINK and K. VON GADOW, 1986) was fitted to the data: r(h) = i + (rb - i), P(b- b) _ ~ [ eql h -H) _ eq(b-H) + pl b -h)] (I) p +q The err or mean square was 0,028. These values were very close to those obta ined with the commercial routine BMDPAR: i = 10,09; p = 2,2126; q = 0,0520. Th e second example concern s the mean hei ght in a stand of Pinu s patula tree s (hi, metres) at different ages (t j , yea rs): t;: 1,75 4,24 6,62 8,75 12,75 17,6723,6729 ,45 31,0435,38 h.: 1,3 5,4 9,3 12,4 17,0 20,9 24,0 25,3 25,7 26,0 26,7 The 3' parame ter Chapman Richards model used by O. G AR 2IA (1984) was fitt ed to the dat a: h = A(l - e")Um (2) vhere h t e A, k ,m = mean height (m) , = stand age (years) , = 2,71, ... = paramet ers to be est imated . Eingegangen am 22. Juli 1987 Th e aut hors' add resses: J. D. Buys. Department ofMathe matics, K. you Gadow. Department ofFores t Science, Univer sity of Stellenbosch, 7600 Stellenbosch, South Africa EDV in Medizin und Biologi c 4/1987