aboutsummaryrefslogtreecommitdiff
path: root/math/nlfit/nlfit.gx
blob: 50dc0b2149e1bb4c39b625d5ff71d348fd57f878 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
include <mach.h>
include <math/nlfit.h>
$if (datatype == r)
include "nlfitdefr.h"
$else
include "nlfitdefd.h"
$endif

# NLFIT -- Routine to perform a non-linear least squares fit. At least MINITER
# iterations must be performed before the fit is complete.

procedure nlfit$t (nl, x, z, w, npts, nvars, wtflag, stat)

pointer	nl		# pointer to nlfit structure
PIXEL	x[ARB]		# independent variables (npts * nvars)
PIXEL	z[ARB]		# function values (npts)
PIXEL	w[ARB]		# weights (npts)
int	npts		# number of points
int	nvars		# number of independent variables
int	wtflag		# weighting type
int	stat		# error code

int	i, miniter, ier
PIXEL	scatter, dscatter
PIXEL	nlscatter$t()

begin
	# Initialize.
	NL_ITER(nl) = 0
	NL_LAMBDA(nl) = PIXEL (.001)
	NL_REFSQ(nl) = PIXEL (0.0)
	NL_SCATTER(nl) = PIXEL(0.0)

	# Initialize the weights.
	switch (wtflag) {
	case WTS_UNIFORM:
	    do i = 1, npts
		w[i] = PIXEL (1.0)
	case WTS_SCATTER:
	    ;
	case WTS_USER:
	    ;
	case WTS_CHISQ:
	    do i = 1, npts {
		if (z[i] > PIXEL (0.0))
		    w[i] = PIXEL (1.0) / z[i]
		else if (z[i] < PIXEL (0.0))
		    w[i] = PIXEL (-1.0) / z[i]
	        else
	            w[i] = PIXEL (0.0)
	    }
	default:
	    do i = 1, npts
		w[i] = PIXEL (1.0)
	}

	# Initialize.
	scatter = PIXEL(0.0)
	if (wtflag == WTS_SCATTER)
	    miniter = MINITER + 1
	else
	    miniter = MINITER

	repeat {

	    # Perform a single iteration.
	    call nliter$t (nl, x, z, w, npts, nvars, ier)
	    NL_ITER(nl) = NL_ITER(nl) + 1
	    #call eprintf ("niter=%d refsq=%g oldsq=%g sumsq=%g\n")
		#call pargi (NL_ITER(nl))
		#call parg$t (NL_REFSQ(nl))
		#call parg$t (NL_OLDSQ(nl))
		#call parg$t (NL_SUMSQ(nl))
	    stat = ier
	    if (stat == NO_DEG_FREEDOM)
	        break

	    # Make the convergence checks.
	    if (NL_ITER(nl) < miniter)
		stat = NOT_DONE
	    else if (NL_SUMSQ(nl) <= PIXEL (10.0) * EPSILON$T)
		stat = DONE
	    else if (NL_REFSQ(nl) < NL_SUMSQ(nl))
		stat = NOT_DONE
	    else if (((NL_REFSQ(nl) - NL_SUMSQ(nl)) / NL_SUMSQ(nl)) <
	        NL_TOL(nl))
		stat = DONE
	    else
		stat = NOT_DONE

	    # Check for a singular solution.
	    if (stat == DONE) {
		if (ier == SINGULAR)
		    stat = ier
		break
	    }

	    # Quit if the lambda parameter goes to zero.
	    if (NL_LAMBDA(nl) <= 0.0)
		break

	    # Check the number of iterations.
	    if ((NL_ITER(nl) >= miniter) && (NL_ITER(nl) >= NL_ITMAX(nl)))
		break

	    # Adjust the weights if necessary.
	    switch (wtflag) {
	    case WTS_SCATTER:
	        dscatter = nlscatter$t (nl, x, z, w, npts, nvars)
		if ((NL_ITER(nl) >= MINITER) && (dscatter >= PIXEL (10.0) *
		    EPSILON$T)) {
		    do i = 1, npts {
			if (w[i] <= PIXEL(0.0))
			    w[i] = PIXEL(0.0)
			else {
		            w[i] = PIXEL(1.0) / (PIXEL(1.0) / w[i] + dscatter)
			}
		    }
		    scatter = scatter + dscatter
		}
	    default:
		;
	    }

	    # Get ready for next iteration.
	    NL_REFSQ(nl) = min (NL_OLDSQ(nl), NL_SUMSQ(nl))
	}

	NL_SCATTER(nl) = NL_SCATTER(nl) + scatter
end


# NLSCATTER -- Routine to estimate the original scatter in the fit.

PIXEL procedure nlscatter$t (nl, x, z, w, npts, nvars)

pointer	nl		# Pointer to nl fitting structure
PIXEL	x[ARB]		# independent variables (npts * nvars)
PIXEL	z[ARB]		# function values (npts)
PIXEL	w[ARB]		# weights (npts)
int	npts		# number of points
int	nvars		# number of independent variables

pointer	sp, zfit, errors
PIXEL	scatter, variance, chisqr
int	nlstati()

begin
	# Allocate working memory.
	call smark (sp)
	call salloc (zfit, npts, TY_PIXEL)
	call salloc (errors, nlstati (nl, NLNPARAMS), TY_PIXEL)

	# Initialize
	scatter = PIXEL (0.0)

	# Compute the fit and the errors.
	call nlvector$t (nl, x, Mem$t[zfit], npts, nvars)
	call nlerrors$t (nl, z, Mem$t[zfit], w, npts, variance, chisqr,
	    Mem$t[errors])

	# Estimate the scatter.
	if (chisqr <= PIXEL(0.0) || variance <= PIXEL(0.0))
	    scatter = PIXEL (0.0)
	else
	    scatter = PIXEL(0.5) * variance * (chisqr - PIXEL(1.0)) / chisqr

	call sfree (sp)

	return (scatter)
end